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))
```

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)
```

    (1.0399999618530273, 0.8839285969734192)

Let’s see how good it is:

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

    (#2) [0.5608958601951599,0.8239796161651611]

``` 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.2924809157848358,0.9387755393981934]

``` 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)
```

![](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.
