cuishuhao / BNM

code of Towards Discriminability and Diversity: Batch Nuclear-norm Maximization under Label Insufficient Situations (CVPR2020 oral)
MIT License
263 stars 30 forks source link

diversity ratio code #16

Open zhouyaqian666 opened 2 years ago

zhouyaqian666 commented 2 years ago

Hello, the diversity ratio is measured by the mean predicted category number dividing the mean ground-truth category number. Is there a code to calculate the diversity ratio? Thanks.

cuishuhao commented 2 years ago

The main code can be shown by

diversity = torch.unique(torch.argmax(outputs_target,dim=1)).shape[0]/ torch.unique(labels_target).shape[0]

where labels_target denotes the ground truth of labels, and outputs_target denotes the prediction outputs.

Details can be refer to train_image.py

zhouyaqian666 commented 2 years ago

In unsupervised domain adaptation,the "labels_target" is usually unknown, how do you get the "labels_target"? Where should this line of code be placed in the train_image.py? Can you explain it in detail? Thank you very much.

cuishuhao commented 2 years ago
  1. "labels_target" is usually unknown for target domain, but it is available during the test process. For example, the calculation of "Accuracy" is always based on labels_target, and diversity can be regarded as a measurement calculated during the test process. To simplify, diversity can be also calculated during the training process, with "labels_target". Note that labels_target is not used to calculate loss for backward in calculation of diversity.
  2. By replacing line 149 to
    inputs_target, labels_target= iter_target.next()

Then we can add the calculation diversity before line 173

Finally, we can print the diversity to see the variation.