Z-ZHHH / CVPR23-DML

8 stars 1 forks source link

余弦分类器 #1

Closed ffxxjj closed 7 months ago

ffxxjj commented 7 months ago

尊敬的作者您好: 非常感谢您针对OOD检测进行了更深层次的探讨,并为此做出了贡献,但我注意到您在论文中提到了有关余弦分类器的做法,但在您提供的代码中我并没有找到相关的实现部分,希望您能给我一些提示。

Z-ZHHH commented 7 months ago

您好,余弦分类器相比于线性分类器,对最后一层特征和分类器fc分别进行了归一化,同时对于fc(x)还需要乘上一个缩放因子。 具体在代码里:Norm on features, Norm on classifier。 在网络model初始化时会指定两个参数use_norm=True, feature_norm=True就是使用余弦分类器,use_norm=False, feature_norm=False就是使用线性分类器。 希望有帮助。

ffxxjj commented 7 months ago

非常感谢您这么快的回答,我想更深入的了解一下为什么余弦分类器要这样实现,您有什么推荐的资料可以看看吗?

Z-ZHHH commented 7 months ago

余弦分类器的使用:Matching Networks for One Shot Learning Quora Q&A,本质是衡量两个向量的余弦相似性、余弦距离度量,类似的距离度量还包括scipy.spatial.distance.cdist方法使用的'euclidean'、'cityblock'等。
Vision-Language model也使用这种分类方式,如CLIP模型的zero-shot分类:

# Pick the top 5 most similar labels for the image
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
values, indices = similarity[0].topk(5)
ffxxjj commented 7 months ago

非常感谢您的回答,我去仔细研究一下