šŸ§  A PyTorch implementation of 'Deep CORAL: Correlation Alignment for Deep Domain Adaptation.', ECCV 2016

Overview

Deep CORAL

A PyTorch implementation of 'Deep CORAL: Correlation Alignment for Deep Domain Adaptation. B Sun, K Saenko, ECCV 2016'

Deep CORAL can learn a nonlinear transformation that aligns correlations of layer activations in deep neural networks (Deep CORAL).

äø­ę–‡ä»‹ē“¹

My implementation result (Task Amazon -> Webcam):

Requirement

  • Python 3
  • PyTorch 0.2

Usage

  1. Unzip dataset in dataset/office31.tar.gz
  2. Run python3 main.py
Comments
  • Correlation Loss

    Correlation Loss

    From my understanding, we know that tensorflow and pytorch can calculate gradients automatically. In original coral implementation, they used caffe which cannot calculate gradients automatically and thats why they used back-propagation. So can we use simply the following correlation loss in pytorch as well? (https://github.com/tensorflow/models/blob/master/research/domain_adaptation/domain_separation/losses.py)

    def correlation_loss(source_samples, target_samples):
        source_samples -= tf.reduce_mean(source_samples, 0)
        target_samples -= tf.reduce_mean(target_samples, 0)
        source_samples = tf.nn.l2_normalize(source_samples, 1)
        target_samples = tf.nn.l2_normalize(target_samples, 1)
        source_cov = tf.matmul(tf.transpose(source_samples), source_samples)
        target_cov = tf.matmul(tf.transpose(target_samples), target_samples)
        corr_loss = tf.reduce_mean(tf.square(source_cov - target_cov))
        return corr_loss
    
    opened by redhat12345 13
  • Pretrained model

    Pretrained model

    In your code you are using pretrained model. For an example, I will not use pretrained model, then what can I do? I am writing a data loader code. Hopefully, I will get good accuracy as paper and then I will send pull request.

    Please help me so that I can use your code without pretrained model.

    Thanks in advanced.

    opened by deep0learning 10
  • code about testing

    code about testing

    Hello, I have some questions about your testing codes in main.py: 1.

    # sum up batch loss
    test_loss += torch.nn.functional.cross_entropy(out, target, size_average=False).data[0]
    # get the index of the max log-probability
    pred = out.data.max(1, keepdim=True)[1]
    correct += pred.eq(target.data.view_as(pred)).cpu().sum()
    

    why don't you directly calculate the loss as you do for training like this: classification_loss = torch.nn.functional.cross_entropy(out1, source_label) Instead, you sum up the loss then divide by the times you sum up. Is there any reason for that?

    out1, out2 = model(data, data)
    out = out1 if mode == 'source' else out2
    

    This part looks a little bit weird to me. Please correct me if I am wrong. out1 is the output from forwarding data to the source branch, and out2 is the output from forwarding data to the target branch. I thought we still use source branch for testing since the parameters learned in the source branch will be affected by the target branch through the CORAL loss. My concern is that the target branch is not trained using the source labels, so I am not sure whether it will produce the correct labels for the testing data. It would be great if you can clarify my concern, or you can show the result forwarding the testing data through the source branch. Thank you!!!

    opened by cmhungsteve 6
  • Inputs to the CORAL loss

    Inputs to the CORAL loss

        out1, out2 = model(source_data, target_data)
    
        classification_loss = torch.nn.functional.cross_entropy(out1, source_label)
        coral = models.CORAL()
        coral_loss = coral(out1, out2)
    
        sum_loss = _lambda*coral_loss + classification_loss
    

    From your codes, the input to the CORAL loss function is the prediction. Although it seems the case in the original DeepCORAL paper, it's not really reasonable to me. To my understanding, the main goal of domain adaptation is to match the feature distribution between source and target domain, so I think it makes more sense to calculate the loss function using the feature vectors from previous layers (e.g. fc6, fc7). In addition, in some famous DA works: https://arxiv.org/pdf/1412.3474.pdf http://proceedings.mlr.press/v37/ganin15.pdf https://arxiv.org/pdf/1502.02791.pdf They all have loss functions for those layers. Maybe you can try different positions of the CORAL loss function and see whether it improve the performance or not. Thanks.

    opened by cmhungsteve 3
  • Error in calculation of covariance matrix

    Error in calculation of covariance matrix

    I think that there's an error in the calculation of covariance matrices. In the line xm = torch.mean(source, 1, keepdim=True) - source, imo 1 must be replaced by 0, as I would like to calculate the mean of each feature along the entire batch. Please correct me if I am wrong. Thanks, wonderful work by the way.

    opened by harshitbansal05 1
  • The point of self.target_fc?

    The point of self.target_fc?

    Hi,

    I was using the code from models.py to try and improve transfer learning results between two datasets. It made me wonder what the point of self.target_fc is in the DeepCORAL class since it is never actually used in the forward pass. Instead, `self.source_fc' is applied to both the source and target data.

    Also, in main.py the test function has a mode argument which selects the output to return. Given the fact that it calls model(data, data) and that there is no difference between how these two inputs will be processed, is such indexing redundant?

    opened by georgeadam 1
  • Why it's loss = loss/(4*d*4) ?

    Why it's loss = loss/(4*d*4) ?

    I noticed in the Deep CORAL paper that the CORAL loss is calculated by loss = loss / (4 * d * d) But line 26 of models.py defines the CORAL loss as loss = loss/(4*d*4) I do not understand. Is there a mistake?

    opened by jindongwang 1
  • self.target_fc never used

    self.target_fc never used

    Hello, in models DeepCoral you declare a target_fc but it seems to me it's never used:

    class DeepCORAL(nn.Module):
        def __init__(self, num_classes=1000):
            super(DeepCORAL, self).__init__()
            self.sharedNet = AlexNet()
            self.source_fc = nn.Linear(4096, num_classes)
            self.target_fc = nn.Linear(4096, num_classes)
    
            # initialize according to CORAL paper experiment
            self.source_fc.weight.data.normal_(0, 0.005)
            self.target_fc.weight.data.normal_(0, 0.005)
    
        def forward(self, source, target):
            source = self.sharedNet(source)
            source = self.source_fc(source)
    
            target = self.sharedNet(target)
            target = self.source_fc(target)
            return source, target
    

    Am I missing something?

    opened by fmcarlucci 1
  • Error !!!

    Error !!!

    I run successfully python3 main.py but when I test, then got error. Can you fix it please?

    ~$ python -m DeepCORAL.tests.test -v

    (pytorch_python3) user@PA00074589:~$ python -m DeepCORAL.tests.test -v test_CORAL_backward (main.TestCORAL) ... ERROR test_CORAL_forward (main.TestCORAL) ... ERROR test_feature_covariance_mat (main.TestCORAL) ... ERROR test_forbenius_norm (main.TestCORAL) ... ok

    ====================================================================== ERROR: test_CORAL_backward (main.TestCORAL)

    Traceback (most recent call last): File "/home/user/DeepCORAL/tests/test.py", line 37, in test_CORAL_backward valid = torch.autograd.gradcheck(coral, test[x], eps=1e-3) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/autograd/gradcheck.py", line 154, in gradcheck output = func(*inputs) File "/home/user/DeepCORAL/models.py", line 31, in forward cs = feature_covariance_mat(ns, source) File "/home/user/DeepCORAL/models.py", line 13, in feature_covariance_mat tmp = ones_t.matmul(d) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/tensor.py", line 180, in matmul return torch.matmul(self, other) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/functional.py", line 173, in matmul return torch.mm(tensor1, tensor2) TypeError: torch.mm received an invalid combination of arguments - got (torch.cuda.FloatTensor, torch.FloatTensor), but expected one of:

    • (torch.cuda.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)
    • (torch.cuda.sparse.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)

    ====================================================================== ERROR: test_CORAL_forward (main.TestCORAL)

    Traceback (most recent call last): File "/home/user/DeepCORAL/tests/test.py", line 29, in test_CORAL_forward res = coral.forward(*test[x]) File "/home/user/DeepCORAL/models.py", line 31, in forward cs = feature_covariance_mat(ns, source) File "/home/user/DeepCORAL/models.py", line 13, in feature_covariance_mat tmp = ones_t.matmul(d) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/tensor.py", line 180, in matmul return torch.matmul(self, other) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/functional.py", line 173, in matmul return torch.mm(tensor1, tensor2) TypeError: torch.mm received an invalid combination of arguments - got (torch.cuda.FloatTensor, torch.FloatTensor), but expected one of:

    • (torch.cuda.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)
    • (torch.cuda.sparse.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)

    ====================================================================== ERROR: test_feature_covariance_mat (main.TestCORAL)

    Traceback (most recent call last): File "/home/user/DeepCORAL/tests/test.py", line 20, in test_feature_covariance_mat res = feature_covariance_mat(*test[x]) File "/home/user/DeepCORAL/models.py", line 13, in feature_covariance_mat tmp = ones_t.matmul(d) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/tensor.py", line 180, in matmul return torch.matmul(self, other) File "/home/user/pytorch_python3/lib/python3.5/site-packages/torch/functional.py", line 173, in matmul return torch.mm(tensor1, tensor2) TypeError: torch.mm received an invalid combination of arguments - got (torch.cuda.FloatTensor, torch.FloatTensor), but expected one of:

    • (torch.cuda.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)
    • (torch.cuda.sparse.FloatTensor source, torch.cuda.FloatTensor mat2) didn't match because some of the arguments have invalid types: (torch.cuda.FloatTensor, torch.FloatTensor)

    Ran 4 tests in 1.179s

    FAILED (errors=3)

    opened by redhat12345 1
  • The CORAL Loss and Forbenius Norm is defined differently and the results dont match.

    The CORAL Loss and Forbenius Norm is defined differently and the results dont match.

    There is a difference between the CORAL Loss used in the paper and the one described in this code. Is it because they both are giving the same result ? The Forbenius Norm is also different.

    The results were also different from the ones in the paper. Is there any idea why the results have changed? image Any response would be helpful.

    opened by ashlytom 0
  • I think you have a error.

    I think you have a error.

    DEEP_CORAL_LOSS: def CORAL(source, target): d = source.data.shape[1] ns = source.data.shape[0] nt = target.data.shape[0]

    # source covariance
    xm = torch.mean(source, 0, keepdim=True) - source
    xc = (xm.t() @ xm) / (ns-1)
    
    # target covariance
    xmt = torch.mean(target, 0, keepdim=True) - target
    xct = xmt.t() @ xmt / (nt-1)
    
    print(xc, xct)
    
    # frobenius norm between source and target
    loss = torch.sum(torch.mul((xc - xct), (xc - xct)))
    loss = loss/(4*d*d)
    return loss
    
    opened by typhoon1104 2
  • TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

    TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

    I'm sorry i met the following error using the office31 dataset

    [INFO] Loading datasets: amazon [INFO] Loading datasets: webcam Traceback (most recent call last): File "main.py", line 151, in res = train(model, optimizer, e+1, _lambda) File "main.py", line 28, in train source, target = list(enumerate(source_loader)), list(enumerate(target_loader)) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 201, in next return self._process_next_batch(batch) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 221, in _process_next_batch raise batch.exc_type(batch.exc_msg) TypeError: Traceback (most recent call last): File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 40, in _worker_loop samples = collate_fn([dataset[i] for i in batch_indices]) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 40, in samples = collate_fn([dataset[i] for i in batch_indices]) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torchvision/datasets/folder.py", line 67, in getitem img = self.transform(img) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torchvision/transforms.py", line 29, in call img = t(img) File "/home/zjp/anaconda3/envs/pytorch0.2/lib/python3.6/site-packages/torchvision/transforms.py", line 139, in call ow = int(self.size * w / h) TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

    opened by tjzjp 0
  • CORAL loss is defined differently from the original paper

    CORAL loss is defined differently from the original paper

    I noticed that both the covariance and the Frobenius norm are computed differently in your implementation.

    You compute the Frobenius norm as below: # frobenius norm between source and target loss = torch.mean(torch.mul((xc - xct), (xc - xct))) However as stated here http://mathworld.wolfram.com/FrobeniusNorm.html , after squaring each element and summing them, should be computed the square root of the sum not the mean of the squared elements.

    In the original paper the covariances are computed as below : https://arxiv.org/abs/1607.01719

    screen shot 2018-07-15 at 20 04 29

    While in your implementation:

    # source covariance 
    xm = torch.mean(source, 0, keepdim=True) - source
    xc = xm.t() @ xm
    
    # target covariance
    xmt = torch.mean(target, 0, keepdim=True) - target
    xct = xmt.t() @ xmt  
    
    opened by DenisDsh 9
  • Low Accuracy for SHVN ---> MNIST

    Low Accuracy for SHVN ---> MNIST

    I got this accuracy for SHVN -----> MNIST

    ###Test Source: Epoch: 2460, avg_loss: 0.0003, Accuracy: 73255/73257 (100.00%) ###Test Target: Epoch: 2460, avg_loss: 21.5493, Accuracy: 35242/60000 (58.74%)

    That means data loading does not matter to get good accuracy. Because for SHVN---->MNIST, I just use default data processing.

    opened by deep0learning 5
Owner
Andy Hsu
There is always more than one solution.
Andy Hsu
IAST: Instance Adaptive Self-training for Unsupervised Domain Adaptation (ECCV 2020)

This repo is the official implementation of our paper "Instance Adaptive Self-training for Unsupervised Domain Adaptation". The purpose of this repo is to better communicate with you and respond to your questions. This repo is almost the same with Another-Version, and you can also refer to that version.

CVSM Group -  email: czhu@bupt.edu.cn 84 Dec 12, 2022
Scikit-event-correlation - Event Correlation and Forecasting over High Dimensional Streaming Sensor Data algorithms

scikit-event-correlation Event Correlation and Changing Detection Algorithm Theo

Intellia ICT 5 Oct 30, 2022
A Pytorch Implementation of [Source dataā€free domain adaptation of object detector through domain

A Pytorch Implementation of Source dataā€free domain adaptation of object detector through domainā€specific perturbation Please follow Faster R-CNN and

null 1 Dec 25, 2021
Simple converter for deploying Stable-Baselines3 model to TFLite and/or Coral

Running SB3 developed agents on TFLite or Coral Introduction I've been using Stable-Baselines3 to train agents against some custom Gyms, some of which

Gary Briggs 16 Oct 11, 2022
Adversarial Adaptation with Distillation for BERT Unsupervised Domain Adaptation

Knowledge Distillation for BERT Unsupervised Domain Adaptation Official PyTorch implementation | Paper Abstract A pre-trained language model, BERT, ha

Minho Ryu 29 Nov 30, 2022
Code for CVPR2021 "Visualizing Adapted Knowledge in Domain Transfer". Visualization for domain adaptation. #explainable-ai

Visualizing Adapted Knowledge in Domain Transfer @inproceedings{hou2021visualizing, title={Visualizing Adapted Knowledge in Domain Transfer}, auth

Yunzhong Hou 80 Dec 25, 2022
[CVPR2021] Domain Consensus Clustering for Universal Domain Adaptation

[CVPR2021] Domain Consensus Clustering for Universal Domain Adaptation [Paper] Prerequisites To install requirements: pip install -r requirements.txt

Guangrui Li 84 Dec 26, 2022
CDTrans: Cross-domain Transformer for Unsupervised Domain Adaptation

CDTrans: Cross-domain Transformer for Unsupervised Domain Adaptation [arxiv] This is the official repository for CDTrans: Cross-domain Transformer for

null 238 Dec 22, 2022
CDTrans: Cross-domain Transformer for Unsupervised Domain Adaptation

[ICCV2021] TransReID: Transformer-based Object Re-Identification [pdf] The official repository for TransReID: Transformer-based Object Re-Identificati

DamoCV 569 Dec 30, 2022
Pytorch implementation of Value Iteration Networks (NIPS 2016 best paper)

VIN: Value Iteration Networks A quick thank you A few others have released amazing related work which helped inspire and improve my own implementation

Kent Sommer 297 Dec 26, 2022
A PyTorch implementation for Unsupervised Domain Adaptation by Backpropagation(DANN), support Office-31 and Office-Home dataset

DANN A PyTorch implementation for Unsupervised Domain Adaptation by Backpropagation Prerequisites Linux or OSX NVIDIA GPU + CUDA (may CuDNN) and corre

null 8 Apr 16, 2022
Pytorch implementation of four neural network based domain adaptation techniques: DeepCORAL, DDC, CDAN and CDAN+E. Evaluated on benchmark dataset Office31.

Deep-Unsupervised-Domain-Adaptation Pytorch implementation of four neural network based domain adaptation techniques: DeepCORAL, DDC, CDAN and CDAN+E.

Alan Grijalva 49 Dec 20, 2022
A Pytorch Implementation of Source Data-free Domain Adaptation for a Faster R-CNN

A Pytorch Implementation of Source Data-free Domain Adaptation for a Faster R-CNN Please follow Faster R-CNN and DAF to complete the environment confi

null 2 Jan 12, 2022
A Pytorch Implementation of Domain adaptation of object detector using scissor-like networks

A Pytorch Implementation of Domain adaptation of object detector using scissor-like networks Please follow Faster R-CNN and DAF to complete the enviro

null 2 Oct 7, 2022
An official implementation of the paper Exploring Sequence Feature Alignment for Domain Adaptive Detection Transformers

Sequence Feature Alignment (SFA) By Wen Wang, Yang Cao, Jing Zhang, Fengxiang He, Zheng-jun Zha, Yonggang Wen, and Dacheng Tao This repository is an o

WangWen 79 Dec 24, 2022
PyTorch code for the paper "Curriculum Graph Co-Teaching for Multi-target Domain Adaptation" (CVPR2021)

PyTorch code for the paper "Curriculum Graph Co-Teaching for Multi-target Domain Adaptation" (CVPR2021) This repo presents PyTorch implementation of M

Evgeny 79 Dec 19, 2022
Official pytorch implement for ā€œTransformer-Based Source-Free Domain Adaptationā€

Official implementation for TransDA Official pytorch implement for ā€œTransformer-Based Source-Free Domain Adaptationā€. Overview: Result: Prerequisites:

stanley 54 Dec 22, 2022
Pytorch domain adaptation package

DomainAdaptation This package is created to tackle the problem of domain shifts when dealing with two domains of different feature distributions. In d

Institute of Computational Perception 7 Oct 22, 2022
LAMDA: Label Matching Deep Domain Adaptation

LAMDA: Label Matching Deep Domain Adaptation This is the implementation of the paper LAMDA: Label Matching Deep Domain Adaptation which has been accep

Tuan Nguyen 9 Sep 6, 2022