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 *

from fastai_datasets.all import *
pairs = Pairs(Imagenette(160), .1)
dls = pairs.dls(after_item=Resize(128),
                after_batch=Normalize.from_stats(*imagenet_stats))
<div>
  <progress value='196' class='' max='196' style='width:300px; height:20px; vertical-align: middle;'></progress>
  100.00% [196/196 00:00&lt;00:00]
</div>

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)
<div>
  <progress value='14' class='' max='14' style='width:300px; height:20px; vertical-align: middle;'></progress>
  100.00% [14/14 00:04&lt;00:00]
</div>


(0.9800000190734863, 0.8493303656578064)

Let’s see how good it is:

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

Not bad, but we can do better with finetuning:

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

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

siamese.plot_distance_histogram(dls.valid)
<div>
  <progress value='7' class='' max='7' style='width:300px; height:20px; vertical-align: middle;'></progress>
  100.00% [7/7 00:01&lt;00:00]
</div>

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.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

similarity_learning-0.0.1-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file similarity-learning-0.0.1.tar.gz.

File metadata

  • Download URL: similarity-learning-0.0.1.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for similarity-learning-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f38cb334c0dc181bba8f65b92651aa414828798488bf3d148516fb8e1d756b60
MD5 dc5d445e061d0946811ad7d16790d98e
BLAKE2b-256 86ba30b001179c3061906c11c48b9a6775fedbfeb1a2790a63164ca9aade225b

See more details on using hashes here.

Provenance

File details

Details for the file similarity_learning-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for similarity_learning-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1da14b8e025e3c1d6d08f35649a870730d3adee7d78e35e1aa3e2e1db1e7249
MD5 ec7dbad538b8628db1a0d0b71c1cfe0c
BLAKE2b-256 76ddf203ef6492d9faa183cabd453d575e2f8836e043534341afa9253c24d02d

See more details on using hashes here.

Provenance

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