corenel / pytorch-adda

A PyTorch implementation for Adversarial Discriminative Domain Adaptation
MIT License
474 stars 139 forks source link

Minimal Code Changes to Support Latest PyTorch and Bug Fixed for Extremely Low Adaptation Accuracy #29

Open yuhui-zh15 opened 2 years ago

yuhui-zh15 commented 2 years ago

Thanks for contributing this repo, which is really nice to learn domain adaptation.

  1. Just made some minimal code changes to support latest PyTorch (>= 1.0) and Python (>= 3.6) (https://github.com/corenel/pytorch-adda/pull/29/commits/0f98f5de673d0842f484a6460f2367131c243aad).

  2. Fixed the low adaptation accuracy (10%-15%) mentioned in #27 #26 #22 #15 #10 #8 #7 #1. The bug is due to the different normalization applied to MNIST and USPS. The data loader normalizes all the MNIST images to 0-1, while normalizing all the USPS images to 0-255. Changing the latter to 0-1 leads to normal performance (https://github.com/corenel/pytorch-adda/pull/29/commits/13a295ab5c94a572854cdf0ffa5e63c65e209777):

=== Evaluating classifier for source domain === (100 epochs)
Avg Loss = 0.08870776686510162, Avg Accuracy = 99.250001%

=== Evaluating classifier for encoded target domain === (100 epochs)
>>> source only <<<
Avg Loss = 0.6725219937139436, Avg Accuracy = 87.956989%
>>> domain adaption <<<
Avg Loss = 0.49638841790887883, Avg Accuracy = 97.365594%
  1. Fixed the outputs of the discriminator mentioned in #16 #11 #2. For nn.CrossEntropyLoss, the input is expected to contain raw, unnormalized scores for each class, rather than logprobs (https://github.com/corenel/pytorch-adda/pull/29/commits/6e59bbddfe1e254ab8eade18d6b5b489db099e52). However, the performance is not improved after correction.
=== Evaluating classifier for encoded target domain === (100 epochs)
>>> source only <<<
Avg Loss = 0.6533445982556594, Avg Accuracy = 87.956989%
>>> domain adaption <<<
Avg Loss = 0.46242921638726453, Avg Accuracy = 97.204304%
goodzhangbobo commented 1 year ago

thank you very much. this is a big help.
I take a test. all is right in this version.
https://github.com/yuhui-zh15/pytorch-adda

Epoch [1999/2000] Step [100/149]:d_loss=0.24804 g_loss=4.08300 acc=0.89000
Epoch [2000/2000] Step [100/149]:d_loss=0.24628 g_loss=4.73108 acc=0.89000
=== Evaluating classifier for encoded target domain ===
>>> source only <<<
Avg Loss = 1.1622806254186129, Avg Accuracy = 84.408605%
>>> domain adaption <<<
Avg Loss = 0.4525440482655924, Avg Accuracy = 97.634411%
mashaan14 commented 1 year ago

I think what causes the low adaptation accuracy is that the class labels are swapped by the target encoder. This makes sense because it is an unsupervised task and the target encoder didn't see the class labels.

I've used this code on 2D data: https://github.com/mashaan14/ADDA-toy

You can see in the attached image that the target encoder separates the classes well. But the class labels were swapped.

Testing target data using target encoder