Skip to main content

A fastai based framework for similarity learning

Project description

similarity-learning

Docs

See https://irad-zehavi.github.io/similarity-learning/

Install

pip install similarity_learning

How to use

As an nbdev library, similarity_learning supports import * (without importing unwanted symbols):

from similarity_learning.all import *

Now we can train a pair-matcher. First let’s construct dataloaders of pairs:

from fastai.vision.all import *
pairs = Pairs(Imagenette(160), .1)
dls = pairs.dls(after_item=Resize(128),
                after_batch=Normalize.from_stats(*imagenet_stats))

To get quick results, we can use the body of a pretrained model as a backbone for our Siamese neural network:

classifier = resnet34(weights=ResNet34_Weights.DEFAULT)
siamese = ThresholdSiamese(create_body(model=classifier, cut=-1)).to(dls.device)
siamese.fit_threshold(dls.train)
(1.0099999904632568, 0.8962054252624512)

Let’s see how good it is:

learn = Learner(dls, siamese, metrics=accuracy)
learn.validate()
(#2) [0.5453092455863953,0.8877550959587097]
learn.show_results()

Not bad, but we can do better with finetuning:

learn.fit(5, 1e-4)
learn.validate()
(#2) [0.26150667667388916,0.954081654548645]
learn.show_results()

We can also consider the distribution of feature-space distances compared to the decision threshold:

siamese.plot_distance_histogram(dls.valid)

See the rest of the docs for more examples, including more visualizations, comparison of loss functions, and facial recognition.

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

similarity-learning-0.0.4.tar.gz (22.5 kB view hashes)

Uploaded Source

Built Distribution

similarity_learning-0.0.4-py3-none-any.whl (22.6 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