Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consine similarities about the context-aware classifier? #3

Open
HuadongTang opened this issue Dec 11, 2023 · 0 comments
Open

Consine similarities about the context-aware classifier? #3

HuadongTang opened this issue Dec 11, 2023 · 0 comments

Comments

@HuadongTang
Copy link

In the paper, you mentioned, "We find that adopting the context-aware classifier to calculate the cosine similarities yields better results than the commonly used dot product...". However, did you only use the dot product in the code as the get_pred function below? I don't know where is the cosine similarity.

def get_pred(self, x, proto):
b, c, h, w = x.size()[:]
if len(proto.shape[:]) == 3:
# x: [b, c, h, w]
# proto: [b, cls, c]
cls_num = proto.size(1)
x = x / (torch.norm(x, 2, 1, True) + 1e-12)
proto = proto / (torch.norm(proto, 2, -1, True) + 1e-12) # b, n, c
x = x.contiguous().view(b, c, hw) # b, c, hw
pred = proto @ x # b, cls, hw
elif len(proto.shape[:]) == 2:
# x: [b, c, h, w]
# proto: [cls, c]
cls_num = proto.size(0)
x = x / (torch.norm(x, 2, 1, True)+ 1e-12)
proto = proto / (torch.norm(proto, 2, 1, True)+ 1e-12)
x = x.contiguous().view(b, c, h
w) # b, c, hw
proto = proto.unsqueeze(0) # 1, cls, c
pred = proto @ x # b, cls, hw
pred = pred.contiguous().view(b, cls_num, h, w)
return pred * 15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant