Answers.Body: <p>Cosine similarity, <span class="math-container">$s$</span>, is an angle distance. The nature of its distance is represented in the range <span class="math-container">$s \in [-1, 1]$</span> being <span class="math-container">$s=-1$</span> very dissimilar and <span class="math-container">$s=1$</span> very similar.</p>
  <p>However the intuition of distances is that the closest to <span class="math-container">$0$</span> it is the more similar. So if you say</p>
  <pre><code>if s &lt; threshold: 
      match = True 
  else: 
      match = False
  </code></pre>
  <p>It would produce the exact opposite results since cosine similarity is a measure where the closest similarity equals to <span class="math-container">$1$</span>. So you have 2 options:</p>
  <ol>
  <li>Transform cosine similarity into a traditional normalized distance <span class="math-container">$d \in  [0,1]$</span> where <span class="math-container">$0$</span>-similar, <span class="math-container">$1$</span>-dissimilar by transforming: <span class="math-container">$d = (-x + 1) / 2$</span>. The <span class="math-container">$-$</span> sign is a source of hassles in metrics computation.</li>
  <li>Do the thresholding with the above equal threshold like <code>threshold=0.8</code> and then just issue the decission: <code>if s &gt;= threshold: match=True</code></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-27T15:06:18.103Z
Answers.Text: thank you so much! It's been sometime but this intuitively makes sense :-)
Answers.UserDisplayName: user199590
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-28T07:39:36.080Z
Answers.Text: You are welcome, I have been using it very recently and this issue came along, so glad to help with this :)
Answers.UserDisplayName: JVGD
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-26T06:57:23.047Z
Answers.LastActivityDate: 2022-04-26T06:57:23.047Z
Answers.UserDisplayName: JVGD
Body: <p>So I've built an arcface model with this arcface layer implementation:
  <a href="https://github.com/4uiiurz1/keras-arcface/blob/master/metrics.py" rel="nofollow noreferrer">https://github.com/4uiiurz1/keras-arcface/blob/master/metrics.py</a></p>
  <p>I trained for a few epochs and now that I'm comparing the results I'm quite baffled.</p>
  <p>According to the paper and resources I've read the embedding should produce embeddings where similar images are closer and should have a higher cosine similarity.</p>
  <p>But I have the exact opposite case, I ran the models embedding layers through the hold out set and to 95% the mismatches are closer than the matches. Thus I have a reversed 95% accuracy.</p>
  <p>My feed and labels are correct</p>
  <p>I binned similar images in groups similar to here:
  <a href="https://www.kaggle.com/ragnar123/unsupervised-baseline-arcface" rel="nofollow noreferrer">https://www.kaggle.com/ragnar123/unsupervised-baseline-arcface</a>
  but for a different dataset.</p>
  <p>Could someone guess why this is happening? Is it possible that some initialization would produce the opposite goal?</p>
  
Tags: <deep-learning><computer-vision><face-recognition>
Title: Arcface implementation for image similarity produces opposite embeddings for positive negative image pairs
UserDisplayName: user199590
ThreadText: # Arcface implementation for image similarity produces opposite embeddings for positive negative image pairs
  
  (Asked by: username_0 on 2021-05-22)
  
  So I've built an arcface model with this arcface layer implementation:
  <https://github.com/4uiiurz1/keras-arcface/blob/master/metrics.py>
  
  I trained for a few epochs and now that I'm comparing the results I'm quite baffled.
  
  According to the paper and resources I've read the embedding should produce embeddings where similar images are closer and should have a higher cosine similarity.
  
  But I have the exact opposite case, I ran the models embedding layers through the hold out set and to 95% the mismatches are closer than the matches. Thus I have a reversed 95% accuracy.
  
  My feed and labels are correct
  
  I binned similar images in groups similar to here:
  <https://www.kaggle.com/ragnar123/unsupervised-baseline-arcface>
  but for a different dataset.
  
  Could someone guess why this is happening? Is it possible that some initialization would produce the opposite goal?
  
  **Tags:** deep-learning, computer-vision, face-recognition
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-04-26. Score: 1 | Accepted answer
  
  Cosine similarity, $s$, is an angle distance. The nature of its distance is represented in the range $s \in [-1, 1]$ being $s=-1$ very dissimilar and $s=1$ very similar.
  
  However the intuition of distances is that the closest to $0$ it is the more similar. So if you say
  
  ```
  if s < threshold: 
      match = True 
  else: 
      match = False
  
  ```
  
  It would produce the exact opposite results since cosine similarity is a measure where the closest similarity equals to $1$. So you have 2 options:
  
  1. Transform cosine similarity into a traditional normalized distance $d \in [0,1]$ where $0$-similar, $1$-dissimilar by transforming: $d = (-x + 1) / 2$. The $-$ sign is a source of hassles in metrics computation.
  2. Do the thresholding with the above equal threshold like `threshold=0.8` and then just issue the decission: `if s >= threshold: match=True`
  
  > **Comments:**
  > **username_0**: thank you so much! It's been sometime but this intuitively makes sense :-)  
  > **username_1**: You are welcome, I have been using it very recently and this issue came along, so glad to help with this :)  
  
  ---
  
  