talreiss / Mean-Shifted-Anomaly-Detection

Mean-Shifted Contrastive Loss for Anomaly Detection (AAAI 2023)
https://arxiv.org/pdf/2106.03844.pdf
Other
117 stars 23 forks source link

Question about angular center loss #4

Closed Youskrpig closed 3 years ago

Youskrpig commented 3 years ago

Hi, I see the angular center loss is expressed as follow: image I'm confused why it is expressed as such math formual? and i see the corresponding code is: out_1 = model(img1) out_2 = model(img2) out_1 = out_1 - center out_2 = out_2 - center center_loss = ((out_1 2).sum(dim=1).mean() + (out_2 2).sum(dim=1).mean()) which seems like the traidional center loss. Could you please explain?

thearkamitra commented 3 years ago

I think there were two losses in the paper right? The authors did use the angular loss in the next line in the code. I might be missing something though.

Youskrpig commented 3 years ago

I misunderstood. Actually it is the angular loss.

siyuanseever commented 3 years ago

Hi, I see the angular center loss is expressed as follow: image I'm confused why it is expressed as such math formual? and i see the corresponding code is: out_1 = model(img1) out_2 = model(img2) out_1 = out_1 - center out_2 = out_2 - center center_loss = ((out_1 2).sum(dim=1).mean() + (out_2 2).sum(dim=1).mean()) which seems like the traidional center loss. Could you please explain?

I also think about this, and steal can not understand. I think the right code about the angular center loss should be write like this:

def angular_loss(out_1, out_2, center):
        out_1 = F.normalize(out_1, dim=-1)
        out_2 = F.normalize(out_2, dim=-1)

        center_loss = -((out_1 * center).sum(dim=1).mean() + (out_2 * center).sum(dim=1).mean())

        return center_loss
out_1 = model(img1)
out_2 = model(img2)

center_loss = angular_loss(out_1, out_2, center)

out_1 = out_1 - center
out_2 = out_2 - center

loss = contrastive_loss(out_1, out_2) + center_loss
talreiss commented 3 years ago

Hi Please note that optimizing the Euclidean metric when dealing with unit vectors is proportional to optimizing the angular distance multiplied by a constant factor. Therefore, this optimization process is equivalent. Hope this clarifies your questions.