Downloads · 30 days
0
imghost11/imgnetV1
imgnetV1 is a image feature extraction model from imghost11. Use it for the image feature extraction task on the model card, and read the license before you ship it in a product. It is set up for pytorch. The card lists the license as mit.
A Universal Similarity Metric for Computer Vision
Downloads · 30 days
0
Access
Public
Updated Jul 20, 2026
Repo size
24.2 MB
Likes
3
Public
Click a slice to open those files.
.onnx13.1 MB · 54%
From the Hugging Face model README
A Universal Similarity Metric for Computer Vision
Author: Imam Ghozali — Independent Researcher 📧 [email protected]
Traditional similarity metrics such as cosine similarity compare embedding vectors through global angular relationships.
IMG introduces a different paradigm: instead of comparing absolute vector values, IMG compares local relational patterns inside the embedding.
The proposed framework consists of three complementary metrics:
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.
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.
This linguistic observation inspired the central hypothesis of this work:
Identity can be encoded through consistent relational patterns rather than absolute values.
Instead of forcing embeddings to occupy a specific angular position, the proposed method trains the network to preserve local relational consistency. Similarity is then evaluated by comparing relational patterns rather than absolute vector orientation.

Unlike ArcFace, which explicitly optimizes cosine similarity using Angular Margin Loss:
$$ L_{ArcFace} = -\log \frac{e^{s\cos(\theta_y+m)}}{e^{s\cos(\theta_y+m)}+\sum_j e^{s\cos\theta_j}} $$
the proposed method directly optimizes the desired similarity metric itself.
For two embeddings $E_1, E_2 \in \mathbb{R}^{1024}$, the objective is to maximize their local sign agreement.
For each embedding dimension:
$$ a_i = \frac{\tanh(\beta E_{1,i} E_{2,i}) + 1}{2} $$
where a positive product indicates agreement and a negative product indicates disagreement. Unlike a hard sign comparison, the hyperbolic tangent provides a smooth, differentiable approximation.
For each sliding window:
$$ S_k = \sum_{i=k}^{k+W-1} a_i $$
with window size $W = 11$ and threshold $T = 8$.
$$ M_k = \sigma\big(50(S_k - T + 0.5)\big) $$
which approximates:
$$ M_k \approx \begin{cases} 1 & \text{if } S_k \ge T \ 0 & \text{if } S_k < T \end{cases} $$
while remaining differentiable.
$$ IMG(E_1, E_2) = \frac{1}{N}\sum_{k=1}^{N} M_k, \qquad N = d - W + 1 $$
For positive pairs:
$$ L_{same} = (1 - IMG)^2 $$
For negative pairs:
$$ L_{diff} = IMG^2 $$
Final objective:
$$ L = L_{same} + L_{diff} $$
This is exactly the objective used during training — no angular-margin loss, cosine loss, or triplet loss is involved.
| Hyperparameter | Value |
|---|---|
| Dataset | CASIA-WebFace |
| Identities | 10,572 |
| Images | ~490k aligned faces |
| Embedding Dimension | 1024 |
| Batch Size | 16 |
| Optimizer | Adam |
| Learning Rate | 1×10⁻⁴ |
| Epochs | 50 |
| Warm-up | 5 |
| Scheduler | Cosine Annealing |
| Weight Decay | 1×10⁻⁵ |
Positive pairs consist of two images belonging to the same identity, while negative pairs are randomly sampled from different identities.
Traditional face-recognition losses optimize embeddings for cosine similarity. The proposed approach instead optimizes embeddings directly for the intended inference metric. Consequently:
This suggests that the similarity metric and the embedding loss should be designed together rather than independently.
| Metric | What it measures |
|---|---|
| Cosine Similarity | Global vector direction |
| IMG Sign | Local relational sign patterns |
| AMP IMG | Relational patterns + local amplitude consistency |
| Chain Score | Continuity of matching relational patterns |
SW357 Block
Conv2 → Conv3 → Conv4 → Conv5 → Conv6 → Conv7 → Conv8 → Conv9 → Conv10
→ Global Average Pooling → FC → BatchNorm
| Property | Value |
|---|---|
| Parameters | 2,774,176 |
| Model Size (FP32) | 10.58 MB |
| Training Dataset | CASIA-WebFace (490k aligned images, 10,572 identities) |
| Dataset | IMG Sign | AMP | Chain | Cosine |
|---|---|---|---|---|
| LFW | 96.27% | 90.45% | 95.12% | 95.53% |
| AgeDB-30 | 78.80% | 74.22% | 72.87% | 77.22% |
| CALFW | 78.73% | 74.92% | 76.87% | 78.32% |
| CPLFW | 76.85% | 68.88% | 75.23% | 74.62% |
| Combined | 81.02% | 77.41% | 79.30% | 79.49% |
| Dataset | IMG Sign | AMP | Chain | Cosine |
|---|---|---|---|---|
| LFW | 99.58% | 99.48% | 97.02% | 99.82% |
| AgeDB-30 | 96.85% | 93.92% | 73.62% | 98.07% |
| CALFW | 95.62% | 94.52% | 84.18% | 96.10% |
| CPLFW | 93.22% | 91.33% | 77.13% | 94.45% |
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.
Results suggest that Similarity Metric and Embedding Loss Function should be considered together:
Therefore, there is no universally best similarity metric. The optimal metric depends on how the embedding space is learned.
While evaluated on face verification, the core mathematics of the IMG Framework are inherently domain-agnostic. Because it discards absolute magnitude dependency and focuses entirely on local sign-pattern agreements, this framework can be generalized to non-visual embeddings:
def img_sign_score_np(e1, e2):
n = len(e1) - WINDOW_SIZE + 1
mc = 0
for i in range(n):
s1 = np.where(e1[i:i+WINDOW_SIZE] >= 0, 1, -1)
s2 = np.where(e2[i:i+WINDOW_SIZE] >= 0, 1, -1)
if np.sum(s1 == s2) >= THRESHOLD:
mc += 1
return mc / n
def amp_img_score_np(e1, e2):
n = len(e1) - WINDOW_SIZE + 1
total = 0
for i in range(n):
w1 = e1[i:i+WINDOW_SIZE]
w2 = e2[i:i+WINDOW_SIZE]
s1 = np.where(w1 >= 0, 1, -1)
s2 = np.where(w2 >= 0, 1, -1)
if np.sum(s1 == s2) >= THRESHOLD:
a1 = np.mean(np.abs(w1))
a2 = np.mean(np.abs(w2))
total += max(0, 1 - abs(a1 - a2) / max(a1, a2, 1e-6))
return total / n
def chain_score_np(e1, e2):
n = len(e1) - WINDOW_SIZE + 1
flags = []
for i in range(n):
...
total = sum(flags)
img_sign = total / n
...
avg_chain = total / n_chains
diff = avg_chain - NEUTRAL_LEN
score = img_sign + (
REWARD_RATE * diff
if diff >= 0
else PUNISH_RATE * diff
) / 100
return np.clip(score, 0, 1)
| Dataset | Link | Description |
|---|---|---|
| CASIA-WebFace aligned | Kaggle | Training dataset, aligned & cropped, 490k images, 10,572 identities |
| Benchmark (LFW/AgeDB/CALFW/CPLFW) | Kaggle | Validation datasets, pre-aligned 112×112 |
train/
train_sw357_conv10_imgsign_a100.py — Training on A100/Colab
train_eval_sw357_conv10_gtx.py — 1-epoch test on GTX
train_eval_sw357_conv13_gtx.py — Conv13 variant test
precrop_casia.py — Pre-crop CASIA with MTCNN
eval/
eval_lfw_gtx_chain_conv10.py — Eval Conv10 + Chain Score (GTX)
eval_lfw_gtx_imgsign_conv10.py — Eval Conv10 IMG Sign (GTX)
eval_benchmarks_a100.py — Multi-dataset benchmark (A100)
eval_metric_comparison_a100.py — FaceNet/ArcFace metric test
app/
face_compare_conv10.py — Desktop UI comparison app (tkinter)
pip install torch torchvision facenet-pytorch insightface Pillow numpy scikit-learn
Place best_model_epoch39_plateau.pth in your working directory.
Mirror download link: https://zenodo.org/records/21232756
# Edit CKPT_PATH and LFW_DIR in the script first
python eval_lfw_gtx_imgsign_conv10.py
python face_compare_conv10.py
Three metrics, one threshold (from IMG Sign sweep):
2/3 or 3/3 pass → ✅ MATCH
1/3 pass → ⚠️ UNCERTAIN
0/3 pass → ❌ DIFFERENT

During development of the interactive ablation visualizer, a preliminary observation was made using the custom polygon occlusion tool:
Observation: Region-specific embedding sensitivity
When occluding specific facial regions (e.g., right eye) using a custom polygon mask and comparing the resulting embedding changes across two different individuals:
Same person, different photos: occluding the same region produces delta spikes at similar embedding dimensions across both photos Different people: occluding the same region produces delta spikes at different embedding dimensions, or in some cases near-zero delta for one person (e.g., when glasses obscure the region)
Example (custom polygon, right eye region):
Same person: Photo 1: changed 4/1014 windows = 0.4% spike_delta: 110 Photo 2: changed 1/1014 windows = 0.1% spike_delta: 100 → spike locations visually correlated
Different people: Photo 1: changed 4/1014 windows = 0.4% spike_delta: 110 Photo 2: changed 0/1014 windows = 0.0% spike_delta: 97 → spike locations differ significantly
This observation suggests that the IMG Sign MSE loss function, through its overlapping sliding window structure, may induce implicit spatial organization in the embedding space — where different facial regions influence different embedding dimensions. However, this has not yet been formally tested and should be treated as a preliminary observation pending rigorous evaluation.
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.
If you use this work, please cite via:
This project is licensed under the MIT License.