Skip to main content

Metric learning layers with tf.keras

Project description

Simple metric learning via tf.keras

This package provides only a few metric learning losses below;

  • ArcFace
  • AdaCos
  • CircleLoss

I have been greatly inspired by PyTorch Metric Learning.

Installation

$ pip install tf-simple-metric-learning

Usage

Provided layers are implemented via tf.keras.layers.Layer API, enables;

from tf_simple_metric_learning.layers import ArcFace

arcface = ArcFace(num_classes=NUM_CLASSES, margin=MARGIN, scale=SCALE)

Example notebook is in examples directory. Implement CircleLossCL (Class-level label version) layer top of EfficientNet and train it for Cars196 dataset;

import tensorflow as tf
from tf_simple_metric_learning.layers import ArcFace, AdaCos, CircleLossCL

inputs = tf.keras.layers.Input([*IMAGE_SIZE, 3], dtype=tf.uint8)
x = tf.cast(inputs, dtype=tf.float32)
x = tf.keras.applications.efficientnet.preprocess_input(x)

net = tf.keras.applications.EfficientNetB0(include_top=False, weights='imagenet', pooling='avg')
embeds = net(x)

labels = tf.keras.layers.Input([], dtype=tf.int32)
labels_onehot = tf.one_hot(labels, depth=num_classes)

# Create metric learning layer
# metric_layer = ArcFace(num_classes=num_classes, margin=0.5, scale=64)
# metric_layer = AdaCos(num_classes=num_classes)
metric_layer = CircleLossCL(num_classes=num_classes, margin=0.25, scale=256)

logits = metric_layer([embeds, labels_onehot])

model = tf.keras.Model(inputs=[inputs, labels], outputs=logits)
model.summary()

Note that you should feed labels as input into model in training because these layers require labels to forward.

In evaluation or prediction, above model requires both images and labels but labels is ignored in those metric learning layers. We only need to use dummy labels (ignored) with the target images because we can't access labels in evaluation or prediction.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tf-simple-metric-learning-0.1.2.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

tf_simple_metric_learning-0.1.2-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page