Metadata-Version: 2.1
Name: similarity-learning
Version: 0.0.2
Summary: A fastai based framework for similarity learning
Home-page: https://github.com/Irad-Zehavi/similarity-learning
Author: iradz
Author-email: irad.zehavi@outlook.com
License: Apache Software License 2.0
Keywords: nbdev jupyter notebook python
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: facenet-pytorch
Requires-Dist: fastai
Requires-Dist: fastai-datasets
Requires-Dist: matplotlib
Provides-Extra: dev
Requires-Dist: ipympl ; extra == 'dev'

similarity-learning
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Docs

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

## Install

``` sh
pip install similarity_learning
```

## How to use

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

``` python
from similarity_learning.all import *
```

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

``` python
from fastai.vision.all import *

from fastai_datasets.all import *
```

``` python
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:

``` python
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&lt;00:00]
    </div>


    (1.0299999713897705, 0.8895089626312256)

Let’s see how good it is:

``` python
learn = Learner(dls, siamese, metrics=accuracy)
learn.validate()
```

    (#2) [0.541471004486084,0.9005101919174194]

``` python
learn.show_results()
```

![](index_files/figure-commonmark/cell-7-output-2.png)

Not bad, but we can do better with finetuning:

``` python
learn.fit(5, 1e-4)
learn.validate()
```

    (#2) [0.2768465280532837,0.9464285969734192]

``` python
learn.show_results()
```

![](index_files/figure-commonmark/cell-9-output-2.png)

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

``` python
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&lt;00:00]
    </div>


![](index_files/figure-commonmark/cell-10-output-2.png)

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


