Skip to content
Back to skills

Arcface Metric Learning

ASecurity

ArcFace angular margin loss layer for learning discriminative embeddings — used in image retrieval, product matching, and face recognition

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
ai-agentspythonangular

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill arcface-metric-learning --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Arcface Metric Learning?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Arcface Metric Learning
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-arcface-metric-learning/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-arcface-metric-learning)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-arcface-metric-learning
description: ArcFace angular margin loss layer for learning discriminative embeddings — used in image retrieval, product matching, and face recognition
domain: cv
---

# ArcFace Metric Learning

## Overview

ArcFace adds an angular margin penalty to the softmax loss, pushing embeddings of the same class closer and different classes further apart in hyperspherical space. Train a CNN with an ArcMarginProduct head, then discard the head and use the penultimate layer as your embedding extractor. Produces highly discriminative features for retrieval tasks.

## Quick Start

```python
import tensorflow as tf
import math

class ArcMarginProduct(tf.keras.layers.Layer):
    def __init__(self, n_classes, s=30, m=0.50, **kwargs):
        super().__init__(**kwargs)
        self.n_classes = n_classes
        self.s = s
        self.cos_m = tf.math.cos(m)
        self.sin_m = tf.math.sin(m)
        self.th = tf.math.cos(math.pi - m)
        self.mm = tf.math.sin(math.pi - m) * m

    def build(self, input_shape):
        self.W = self.add_weight(shape=(input_shape[0][-1], self.n_classes),
                                 initializer='glorot_uniform', trainable=True)

    def call(self, inputs):
        X, y = inputs
        cosine = tf.matmul(tf.math.l2_normalize(X, axis=1),
                           tf.math.l2_normalize(self.W, axis=0))
        sine = tf.math.sqrt(1.0 - tf.math.pow(cosine, 2))
        phi = cosine * self.cos_m - sine * self.sin_m
        one_hot = tf.cast(tf.one_hot(y, depth=self.n_classes), dtype=phi.dtype)
        output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
        return output * self.s

# Training
backbone = tf.keras.applications.EfficientNetB3(include_top=False, pooling='avg')
x = backbone.output
margin = ArcMarginProduct(n_classes=num_products, s=30, m=0.5)
output = margin([x, label_input])
model.fit(...)

# Inference: extract embeddings (discard ArcFace head)
embedder = tf.keras.Model(inputs=model.input[0], outputs=model.layers[-4].output)
embeddings = embedder.predict(test_data)
```

## Key Decisions

- **s (scale)**: 30 is standard; higher values sharpen the distribution
- **m (margin)**: 0.5 radians; increase for harder separation, decrease if training diverges
- **L2 normalize**: both features and weights must be normalized for angular margin to work
- **Discard head at inference**: the classification head is only needed during training

## References

- Source: [unsupervised-baseline-arcface](https://www.kaggle.com/code/ragnar123/unsupervised-baseline-arcface)
- Competition: Shopee - Price Match Guarantee

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.