imghost11 commited on
Commit
93845f1
Β·
verified Β·
1 Parent(s): e2d8898

Update readme md

Browse files
Files changed (1) hide show
  1. README.md +391 -1
README.md CHANGED
@@ -1,3 +1,393 @@
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # IMG β€” Relational Pattern-Based Similarity Metric
2
+
3
+ **A Universal Similarity Metric for Computer Vision**
4
+
5
+ [![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.21232756-blue)](https://doi.org/10.5281/zenodo.21232756)
6
+
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
8
+
9
+ **Author:** Imam Ghozali β€” Independent Researcher
10
+ πŸ“§ imam.gh98@gmail.com
11
+
12
  ---
13
+
14
+ ## Overview
15
+
16
+ Traditional similarity metrics such as cosine similarity compare embedding vectors through **global angular relationships**.
17
+
18
+ **IMG** introduces a different paradigm: instead of comparing absolute vector values, IMG compares **local relational patterns** inside the embedding.
19
+
20
+ The proposed framework consists of three complementary metrics:
21
+
22
+ 1. **IMG Sign Score**
23
+ 2. **AMP IMG Score**
24
+ 3. **Chain Score**
25
+
26
+ > **Note:** This work does **not** propose replacing cosine similarity. Instead, IMG is proposed as an *alternative* similarity metric. Experimental results suggest that the optimal similarity metric depends on how the embedding itself is learned.
27
+
28
  ---
29
+
30
+ ## Relational Learning Hypothesis
31
+
32
+ In Javanese, one expresses gratitude as **"matur suwun"**; in Sundanese, the same sentiment is conveyed as **"hatur nuhun"**. Despite different surface structures, both phrases encode identical meaning through internally consistent relational patterns.
33
+
34
+ This linguistic observation inspired the central hypothesis of this work:
35
+
36
+ > **Identity can be encoded through consistent relational patterns rather than absolute values.**
37
+
38
+ Instead of forcing embeddings to occupy a specific angular position, the proposed method trains the network to preserve **local relational consistency**.
39
+
40
+ Consequently, similarity is evaluated by comparing relational patterns rather than absolute vector orientation.
41
+
42
+ ---
43
+
44
+ ## Relational Training Objective
45
+
46
+ Unlike ArcFace, which explicitly optimizes cosine similarity using Angular Margin Loss,
47
+
48
+ ```math
49
+ L_{ArcFace}
50
+ =
51
+ -\log
52
+ \frac
53
+ {e^{s\cos(\theta_y+m)}}
54
+ {e^{s\cos(\theta_y+m)}+\sum_j e^{s\cos\theta_j}}
55
+ ```
56
+
57
+ the proposed method directly optimizes the desired similarity metric itself.
58
+
59
+ For two embeddings
60
+
61
+ ```math
62
+ E_1,E_2\in\mathbb{R}^{1024}
63
+ ```
64
+
65
+ the objective is to maximize their **local sign agreement**.
66
+
67
+ ### Soft Sign Agreement
68
+
69
+ For each embedding dimension,
70
+
71
+ ```math
72
+ a_i=
73
+ \frac{\tanh(\beta E_{1,i}E_{2,i})+1}{2}
74
+ ```
75
+
76
+ where:
77
+
78
+ - positive product β†’ agreement
79
+ - negative product β†’ disagreement
80
+
81
+ Unlike a hard sign comparison, the hyperbolic tangent provides a smooth differentiable approximation.
82
+
83
+ ### Sliding Window Aggregation
84
+
85
+ For each sliding window,
86
+
87
+ ```math
88
+ S_k=\sum_{i=k}^{k+W-1}a_i
89
+ ```
90
+
91
+ where:
92
+
93
+ - Window size **W = 11**
94
+ - Threshold **T = 8**
95
+
96
+ ### Differentiable Matching Gate
97
+
98
+ ```math
99
+ M_k=\sigma\left(50(S_k-T+0.5)\right)
100
+ ```
101
+
102
+ which approximates
103
+
104
+ ```math
105
+ M_k \approx
106
+ \begin{cases}
107
+ 1 & \text{if } S_k \ge T \\
108
+ 0 & \text{if } S_k < T
109
+ \end{cases}
110
+ ```
111
+
112
+ while remaining differentiable.
113
+
114
+ ### IMG Sign Score
115
+
116
+ ```math
117
+ IMG(E_1,E_2)=\frac1N\sum_{k=1}^{N}M_k
118
+ ```
119
+
120
+ where
121
+
122
+ ```math
123
+ N=d-W+1
124
+ ```
125
+
126
+ ### Relational Loss
127
+
128
+ Positive pairs:
129
+
130
+ ```math
131
+ L_{same}=(1-IMG)^2
132
+ ```
133
+
134
+ Negative pairs:
135
+
136
+ ```math
137
+ L_{diff}=IMG^2
138
+ ```
139
+
140
+ Final objective:
141
+
142
+ ```math
143
+ L=L_{same}+L_{diff}
144
+ ```
145
+
146
+ This is exactly the objective used during training.
147
+
148
+ ---
149
+
150
+ ## Training Pipeline
151
+
152
+ | Hyperparameter | Value |
153
+ |---|---:|
154
+ | Dataset | CASIA-WebFace |
155
+ | Identities | 10,572 |
156
+ | Images | ~490k aligned faces |
157
+ | Embedding Dimension | 1024 |
158
+ | Batch Size | 16 |
159
+ | Optimizer | Adam |
160
+ | Learning Rate | 1Γ—10⁻⁴ |
161
+ | Epochs | 50 |
162
+ | Warm-up | 5 |
163
+ | Scheduler | Cosine Annealing |
164
+ | Weight Decay | 1Γ—10⁻⁡ |
165
+
166
+ Positive pairs consist of two images belonging to the same identity, while negative pairs are randomly sampled from different identities.
167
+
168
+ Unlike ArcFace, no angular-margin loss, cosine loss, or triplet loss is used. The network is optimized **entirely using the proposed relational objective**.
169
+
170
+ ---
171
+
172
+ ## Why Does This Matter?
173
+
174
+ Traditional face-recognition losses optimize embeddings for cosine similarity.
175
+
176
+ The proposed approach instead optimizes embeddings directly for the intended inference metric.
177
+
178
+ Consequently:
179
+
180
+ - Embeddings trained with **Angular Margin Loss** naturally favor **cosine similarity**.
181
+ - Embeddings trained with the proposed **relational loss** naturally favor **IMG Sign**.
182
+
183
+ This suggests that **the similarity metric and the embedding loss should be designed together rather than independently.**
184
+
185
+ ---
186
+
187
+ ## Key Idea
188
+
189
+ | Metric | What it measures |
190
+ |---|---|
191
+ | **Cosine Similarity** | Global vector direction |
192
+ | **IMG Sign** | Local relational sign patterns |
193
+ | **AMP IMG** | Relational patterns + local amplitude consistency |
194
+ | **Chain Score** | Continuity of matching relational patterns |
195
+
196
+ ---
197
+
198
+ ## Architecture Used in This Paper
199
+
200
+ **SW357 Block**
201
+
202
+ ```
203
+ Conv2 β†’ Conv3 β†’ Conv4 β†’ Conv5 β†’ Conv6 β†’ Conv7 β†’ Conv8 β†’ Conv9 β†’ Conv10
204
+ β†’ Global Average Pooling β†’ FC β†’ BatchNorm
205
+ ```
206
+
207
+ | Property | Value |
208
+ |---|---|
209
+ | Parameters | 2,774,176 |
210
+ | Model Size (FP32) | 10.58 MB |
211
+ | Training Dataset | CASIA-WebFace (490k aligned images, 10,572 identities) |
212
+
213
+ ---
214
+
215
+ ## Benchmark
216
+
217
+ ### SW357 Embedding (native)
218
+
219
+ | Dataset | IMG Sign | AMP | Chain | Cosine |
220
+ |----------|---------:|-------:|-------:|-------:|
221
+ | LFW | 96.27% | 90.45% | 95.12% | 95.53% |
222
+ | AgeDB-30 | 78.80% | 74.22% | 72.87% | 77.22% |
223
+ | CALFW | 78.73% | 74.92% | 76.87% | 78.32% |
224
+ | CPLFW | 76.85% | 68.88% | 75.23% | 74.62% |
225
+ | **Combined** | **81.02%** | **77.41%** | **79.30%** | **79.49%** |
226
+
227
+ ### ArcFace Evaluation (relational metric tested on external embedding)
228
+
229
+ | Dataset | IMG Sign | AMP | Chain | Cosine |
230
+ |----------|---------:|-------:|-------:|-------:|
231
+ | LFW | 99.58% | 99.48% | 97.02% | 99.82% |
232
+ | AgeDB-30 | 96.85% | 93.92% | 73.62% | 98.07% |
233
+ | CALFW | 95.62% | 94.52% | 84.18% | 96.10% |
234
+ | CPLFW | 93.22% | 91.33% | 77.13% | 94.45% |
235
+
236
+ **Observation:** Cosine remains the best metric for ArcFace because ArcFace is explicitly optimized using Angular Margin Loss. However, IMG Sign remains highly competitive despite never being used during ArcFace training.
237
+
238
+ ---
239
+
240
+ ## Main Finding
241
+
242
+ Results suggest that **Similarity Metric** and **Embedding Loss Function** should be considered together:
243
+
244
+ - Embeddings trained with **Angular Margin Loss** naturally favor **cosine similarity**.
245
+ - Embeddings trained with the proposed **relational loss** naturally favor **IMG Sign**.
246
+
247
+ **Therefore, there is no universally best similarity metric.** The optimal metric depends on how the embedding space is learned.
248
+
249
+ ---
250
+
251
+ ## Metric Definitions
252
+
253
+ ### IMG Sign Score
254
+
255
+ ```python
256
+ def img_sign_score_np(e1, e2):
257
+ n = len(e1) - WINDOW_SIZE + 1
258
+ mc = 0
259
+ for i in range(n):
260
+ s1 = np.where(e1[i:i+WINDOW_SIZE] >= 0, 1, -1)
261
+ s2 = np.where(e2[i:i+WINDOW_SIZE] >= 0, 1, -1)
262
+ if np.sum(s1 == s2) >= THRESHOLD:
263
+ mc += 1
264
+ return mc / n
265
+ ```
266
+
267
+ ### AMP IMG Score
268
+
269
+ ```python
270
+ def amp_img_score_np(e1, e2):
271
+ n = len(e1) - WINDOW_SIZE + 1
272
+ total = 0
273
+ for i in range(n):
274
+ w1 = e1[i:i+WINDOW_SIZE]
275
+ w2 = e2[i:i+WINDOW_SIZE]
276
+ s1 = np.where(w1 >= 0, 1, -1)
277
+ s2 = np.where(w2 >= 0, 1, -1)
278
+ if np.sum(s1 == s2) >= THRESHOLD:
279
+ a1 = np.mean(np.abs(w1))
280
+ a2 = np.mean(np.abs(w2))
281
+ total += max(0, 1 - abs(a1 - a2) / max(a1, a2, 1e-6))
282
+ return total / n
283
+ ```
284
+
285
+ ### Chain Score
286
+
287
+ ```python
288
+ def chain_score_np(e1, e2):
289
+ n = len(e1) - WINDOW_SIZE + 1
290
+ flags = []
291
+ for i in range(n):
292
+ ...
293
+ total = sum(flags)
294
+ img_sign = total / n
295
+ ...
296
+ avg_chain = total / n_chains
297
+ diff = avg_chain - NEUTRAL_LEN
298
+ score = img_sign + (
299
+ REWARD_RATE * diff
300
+ if diff >= 0
301
+ else PUNISH_RATE * diff
302
+ ) / 100
303
+ return np.clip(score, 0, 1)
304
+ ```
305
+
306
+ ---
307
+
308
+
309
+ ## Datasets
310
+
311
+ | Dataset | Link | Description |
312
+ |---------|------|-------------|
313
+ | CASIA-WebFace aligned | [Kaggle](https://www.kaggle.com/datasets/luongkhang04/aligned-casia) | Training dataset, aligned & cropped, 490k images, 10,572 identities |
314
+ | Benchmark (LFW/AgeDB/CALFW/CPLFW) | [Kaggle](https://www.kaggle.com/datasets/yakhyokhuja/agedb-30-calfw-cplfw-lfw-aligned-112x112) | Validation datasets, pre-aligned 112Γ—112 |
315
+
316
+ ```
317
+ train/
318
+ train_sw357_conv10_imgsign_a100.py β€” Training on A100/Colab
319
+ train_eval_sw357_conv10_gtx.py β€” 1-epoch test on GTX
320
+ train_eval_sw357_conv13_gtx.py β€” Conv13 variant test
321
+ precrop_casia.py β€” Pre-crop CASIA with MTCNN
322
+
323
+ eval/
324
+ eval_lfw_gtx_chain_conv10.py β€” Eval Conv10 + Chain Score (GTX)
325
+ eval_lfw_gtx_imgsign_conv10.py β€” Eval Conv10 IMG Sign (GTX)
326
+ eval_benchmarks_a100.py β€” Multi-dataset benchmark (A100)
327
+ eval_metric_comparison_a100.py β€” FaceNet/ArcFace metric test
328
+
329
+ app/
330
+ face_compare_conv10.py β€” Desktop UI comparison app (tkinter)
331
+ ```
332
+
333
+ ---
334
+
335
+ ## Quickstart
336
+
337
+ ### 1. Install dependencies
338
+
339
+ ```bash
340
+ pip install torch torchvision facenet-pytorch insightface Pillow numpy scikit-learn
341
+ ```
342
+
343
+ ### 2. Download checkpoint
344
+
345
+ download link:
346
+ https://zenodo.org/records/21232756
347
+ Place `best_model_epoch39_plateau.pth` in your working directory.
348
+
349
+ ### 3. Eval on LFW
350
+
351
+ ```bash
352
+ # Edit CKPT_PATH and LFW_DIR in the script first
353
+ python eval_lfw_gtx_imgsign_conv10.py
354
+ ```
355
+
356
+ ### 4. Run comparison app
357
+
358
+ ```bash
359
+ python face_compare_conv10.py
360
+ ```
361
+
362
+ ---
363
+
364
+ ## Voting System
365
+
366
+ Three metrics, one threshold (from IMG Sign sweep):
367
+
368
+ ```
369
+ 2/3 or 3/3 pass β†’ βœ… MATCH
370
+ 1/3 pass β†’ ⚠️ UNCERTAIN
371
+ 0/3 pass β†’ ❌ DIFFERENT
372
+ ```
373
+
374
+ ---
375
+
376
+ ## Conclusion
377
+
378
+ IMG is proposed as an alternative similarity metric rather than a replacement for cosine similarity. Experiments indicate that cosine similarity performs best for embeddings trained with angular-margin objectives, while IMG Sign performs best for embeddings trained with the proposed relational objective. The framework is model-agnostic and can be applied to embeddings generated by different architectures.
379
+
380
+ ---
381
+
382
+ ## Citation
383
+
384
+ If you use this work, please cite via:
385
+
386
+ - **Zenodo (DOI):** https://doi.org/10.5281/zenodo.20748457
387
+ - **GitHub:** https://github.com/imamgh11/imgnet
388
+
389
+ ---
390
+
391
+ ## License
392
+
393
+ This project is licensed under the **MIT License**.