- Notifications
You must be signed in to change notification settings - Fork5
Minimalistic TensorFlow2+ deep metric/similarity learning library with loss functions, miners, and utils as embedding projector.
License
Ximilar-com/tf-metric-learning
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Minimalistic open-source library for metric learning written inTensorFlow2, TF-Addons, Numpy, OpenCV(CV2) andAnnoy. This repository contains a TensorFlow2+/tf.keras implementation some of the loss functions and miners. This repository was inspired bypytorch-metric-learning.
Prerequirements:
pip install tensorflowpip install tensorflow-addonspip install annoypip install opencv-contrib-pythonThis library:
pip install tf-metric-learning- All the loss functions are implemented as tf.keras.layers.Layer
- Callbacks for Computing Recall, Visualize Embeddings in TensorBoard Projector
- Simple Mining mechanism with Annoy
- Combine multiple loss functions/layers in one model
This library contains code that has been adapted and modified from the following great open-source repos, without them this will be not possible (THANK YOU):
- Discriminative layer optimizer (different learning rates) for Loss with weights (Proxy, SoftTriple, ...)TODO
- Some Tests 😇
- Improve and add more minerss
importtensorflowastfimportnumpyasnpfromtf_metric_learning.layersimportSoftTripleLossfromtf_metric_learning.utils.constantsimportEMBEDDINGS,LABELSnum_class,num_centers,embedding_size=10,2,256inputs=tf.keras.Input(shape=(embedding_size),name=EMBEDDINGS)input_label=tf.keras.layers.Input(shape=(1,),name=LABELS)output_tensor=SoftTripleLoss(num_class,num_centers,embedding_size)({EMBEDDINGS:inputs,LABELS:input_label})model=tf.keras.Model(inputs=[inputs,input_label],outputs=output_tensor)model.compile(optimizer="adam")data= {EMBEDDINGS :np.asarray([np.zeros(256)foriinrange(1000)]),LABELS:np.zeros(1000,dtype=np.float32)}model.fit(data,None,epochs=10,batch_size=10)
More complex scenarios:
- Complex example with NPair Loss + Multi Similarity + Classification and Mining
- SoftTriple Training on CIFAR 10
- ProxyAnchor Loss using tf.data.Dataset
- Triplet Training with Mining
- Contrastive Training
- Classification baseline
- MaximumLossMiner [TODO]
- TripletAnnoyMiner ✅
- AnnoyEvaluator Callback: for evaluation Recall@K, you will need to install Spotifyannoy library.
importtensorflowastffromtf_metric_learning.utils.recallimportAnnoyEvaluatorCallbackevaluator=AnnoyEvaluatorCallback(base_network, {"images":test_images[:divide],"labels":test_labels[:divide]},# images stored to index {"images":test_images[divide:],"labels":test_labels[divide:]},# images to querynormalize_fn=lambdaimages:images/255.0,normalize_eb=True,eb_size=embedding_size,freq=1,)
- Tensorboard Projector Callback
importtensorflowastffromtf_metric_learning.utils.projectorimportTBProjectorCallbackdefnormalize_images(images):returnimages/255.0(train_images,train_labels), (test_images,test_labels)=tf.keras.datasets.cifar10.load_data()...projector=TBProjectorCallback(base_model,"tb/projector",test_images,# list of imagesnp.squeeze(test_labels),normalize_eb=True,normalize_fn=normalize_images)
About
Minimalistic TensorFlow2+ deep metric/similarity learning library with loss functions, miners, and utils as embedding projector.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.