LTH14 / targeted-supcon

A PyTorch implementation of the paper Targeted Supervised Contrastive Learning for Long-tailed Recognition
MIT License
93 stars 13 forks source link

distribute feature on hypersphere as Fig.2. #10

Closed suminRoh closed 1 year ago

suminRoh commented 1 year ago

Hi, thanks for your code. I have a question. I want to distribute the output of my dataset on a hypersphere, similar to Fig.2. If the shape of the output is (1608, 2), where each entry is in the form [x1, y1], [x2, y2], ..., [xn, yn] (where n ranges from 1 to 1608), do I have to plot xn and yn for 1608 data points?

Alternatively, if you have another method, could you please let me know

LTH14 commented 1 year ago

Hi, thanks for your interest! Figure 2 is for illustration only and not generated from real data. I would suggest you try a scatter plot with matplotlib, so you get 1608 scatter points on an x-y plane. You could try with different alpha/edge width with the plt.scatter function to fine-tune the generated figure.

suminRoh commented 1 year ago

Thank you for your quick response.

Are figure 1, 3, and 5 also illustrations and not generated from real data? I would like to know the method used to obtain the figure illustrating the feature distribution on the hypersphere. If you generated figure 5 from real data, do the features come from the output of main_moco_supcon_imba(stage 1) or main_lincls_imba(stage 2)?

Angelina1996 commented 1 year ago

Sumin, very nice question. Just wondering if you found the solution.

LTH14 commented 1 year ago

Figure 1, 3 and 5 are all from real data. The visualized features are the normalized output of the MLP. The same features are used to compute the targeted supervised contrastive loss (stage 1). For visualization, we simply train the network with the output dimension of MLP equals to 2, so that they naturally distribute on a 2-d circle.

suminRoh commented 1 year ago

If I apply your comment in the code, in main_moco_supcon_imba.py, the value of args.moco_dim is 2 (line 88), and final_feats, which is the return value of the model (line 410), will be the visualized features, as you mentioned. Did I understand your comment correctly?

Thank you for answering my question so well.

LTH14 commented 1 year ago

Yes, that's correct