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<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:05<00:00]
</div>
(1.0299999713897705, 0.8895089626312256)
Let’s see how good it is:
learn = Learner(dls, siamese, metrics=accuracy)
learn.validate()
(#2) [0.541471004486084,0.9005101919174194]
learn.show_results()
Not bad, but we can do better with finetuning:
learn.fit(5, 1e-4)
learn.validate()
(#2) [0.2768465280532837,0.9464285969734192]
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:02<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
Built Distribution
File details
Details for the file similarity-learning-0.0.2.tar.gz
.
File metadata
- Download URL: similarity-learning-0.0.2.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 231511391fc868163660421b9d53d1156b611cae567fdd1dfc967203c3835b95 |
|
MD5 | 7df2c51fd20b63cc608b3daec4619b8c |
|
BLAKE2b-256 | 28998e2fd6770c8da844ddcc1ba9fcb54912ac29b43cfb5719296dd112fff3be |
Provenance
File details
Details for the file similarity_learning-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: similarity_learning-0.0.2-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d06dc105f17370f6ade1f4b17cb148b99057f6349f589680aef4496d34f57501 |
|
MD5 | d82cae4aee0e8294e5ca92541e9aa8c3 |
|
BLAKE2b-256 | f43071c23b3b1ed8619f40587a0bcde6b49e3912a9ec98eb83f94072213b1343 |