{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Computer Vision Learner" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`vision.learner`](/vision.learner.html#vision.learner) is the module that defines the [`create_cnn`](/vision.learner.html#create_cnn) method, to easily get a model suitable for transfer learning." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.vision import *\n", "from fastai import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transfer learning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Transfer learning is a technique where you use a model trained on a very large dataset (usually [ImageNet](http://image-net.org/) in computer vision) and then adapt it to your own dataset. The idea is that it has learned to recognize many features on all of this data, and that you will benefit from this knowledge, especially if your dataset is small, compared to starting from a randomly initiliazed model. It has been proved in [this article](https://arxiv.org/abs/1805.08974) on a wide range of tasks that transfer learning nearly always give better results.\n", "\n", "In practice, you need to change the last part of your model to be adapted to your own number of classes. Most convolutional models end with a few linear layers (a part will call head). The last convolutional layer will have analyzed features in the image that went through the model, and the job of the head is to convert those in predictions for each of our classes. In transfer learning we will keep all the convolutional layers (called the body or the backbone of the model) with their weights pretrained on ImageNet but will define a new head initiliazed randomly.\n", "\n", "Then we will train the model we obtain in two phases: first we freeze the body weights and only train the head (to convert those analyzed features into predictions for our own data), then we unfreeze the layers of the backbone (gradually if necessary) and fine-tune the whole model (possily using differential learning rates).\n", "\n", "The [`create_cnn`](/vision.learner.html#create_cnn) factory method helps you to automatically get a pretrained model from a given architecture with a custom head that is suitable for your data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "
create_cnn
[source]create_cnn
(`data`:[`DataBunch`](/basic_data.html#DataBunch), `arch`:`Callable`, `cut`:`Union`\\[`int`, `Callable`\\]=`None`, `pretrained`:`bool`=`True`, `lin_ftrs`:`Optional`\\[`Collection`\\[`int`\\]\\]=`None`, `ps`:`Floats`=`0.5`, `custom_head`:`Optional`\\[[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\\]=`None`, `split_on`:`Union`\\[`Callable`, `Collection`\\[`ModuleList`\\], `NoneType`\\]=`None`, `bn_final`:`bool`=`False`, `kwargs`:`Any`) → [`Learner`](/basic_train.html#Learner)"
],
"text/plain": [
"epoch | \n", "train_loss | \n", "valid_loss | \n", "accuracy | \n", "
---|---|---|---|
1 | \n", "0.127211 | \n", "0.080421 | \n", "0.976938 | \n", "
predict
[source]predict
(`img`:[`ItemBase`](/core.html#ItemBase), `kwargs`)\n",
"\n",
"Return prect class, label and probabilities for `img`. "
],
"text/plain": [
"create_body
[source]create_body
(`model`:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), `cut`:`Optional`\\[`int`\\]=`None`, `body_fn`:`Callable`\\[[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\\]=`None`)\n",
"\n",
"Cut off the body of a typically pretrained `model` at `cut` or as specified by `body_fn`. "
],
"text/plain": [
"create_head
[source]create_head
(`nf`:`int`, `nc`:`int`, `lin_ftrs`:`Optional`\\[`Collection`\\[`int`\\]\\]=`None`, `ps`:`Floats`=`0.5`, `bn_final`:`bool`=`False`)"
],
"text/plain": [
"class
ClassificationInterpretation
[source]ClassificationInterpretation
(`data`:[`DataBunch`](/basic_data.html#DataBunch), `probs`:`Tensor`, `y_true`:`Tensor`, `losses`:`Tensor`, `sigmoid`:`bool`=`None`)\n",
"\n",
"Interpretation methods for classification models. "
],
"text/plain": [
"from_learner
[source]from_learner
(`learn`:[`Learner`](/basic_train.html#Learner), `ds_type`:[`DatasetType`](/basic_data.html#DatasetType)=`plot_top_losses
[source]plot_top_losses
(`k`, `largest`=`True`, `figsize`=`(12, 12)`)\n",
"\n",
"Show images in `top_losses` along with their prediction, actual, loss, and probability of predicted class. "
],
"text/plain": [
"