from self_supervised.layers import *
from self_supervised.vision.simclr import *
Create your dataset as usual. Make sure your validation set has labels. For example, if your dataset has 10% of labeled data and 90% of unlabeled data you can use that 10% or portion of it as your validation set. You assign dummy labels to 90% of data which doesn't have an actual label to circumvent code breaks during dls construction.
path = untar_data(URLs.MNIST_TINY)
items = get_image_files(path)
tds = Datasets(items, [PILImageBW.create, [parent_label, Categorize()]], splits=GrandparentSplitter()(items))
dls = tds.dataloaders(bs=5, after_item=[ToTensor(), IntToFloatTensor()], device='cpu')
Create your model and augmentations as usual
fastai_encoder = create_encoder('xresnet18', n_in=1, pretrained=False)
model = create_simclr_model(fastai_encoder, hidden_size=2048, projection_size=128)
aug_pipelines = get_simclr_aug_pipelines(size=28, rotate=False, jitter=False, bw=False, blur=False, stats=None, cuda=False)
Define self-supervised alogrithm in cbs. ShortEpochCallback
is used for testing purposes here.
cbs=[SimCLR(aug_pipelines, temp=0.07, print_augs=True)]
We will use ValueMetric
from fastai since KNNProxyMetric
is a metric implemented as a Callback. ValueMetric
expects a function which will return a value when it's called and that function is KNNProxyMetric.accuracy
.
knn_metric_cb = KNNProxyMetric()
cbs += [knn_metric_cb]
metric = ValueMetric(knn_metric_cb.accuracy, metric_name='knn_accuracy')
Construct Learner
with previously defined dls
, cbs
and metric
.
learn = Learner(dls, model, cbs=cbs, metrics=metric)
learn.validate()