<jupyter_start><jupyter_text>Fastai Mnist sample (for binder)- (adapted from fastai v3 https://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson1-pets.ipynb) In this lesson we will build our first image classifier from scratch. Let's dive in!Every notebook starts with the following three lines; they ensure that any edits to libraries you make are reloaded here automatically, and also that any charts or images displayed are shown in this notebook.<jupyter_code>%reload_ext autoreload
%autoreload 2
%matplotlib inline<jupyter_output><empty_output><jupyter_text>We import all the necessary packages. We are going to work with the [fastai V1 library](http://www.fast.ai/2018/10/02/fastai-ai/) which sits on top of [Pytorch 1.0](https://hackernoon.com/pytorch-1-0-468332ba5163). The fastai library provides many useful functions that enable us to quickly and easily build neural networks and train our models.<jupyter_code>from fastai.vision import *
from fastai.metrics import error_rate
bs = 64
# bs = 16   # uncomment this line if you run out of memory even after clicking Kernel->Restart<jupyter_output><empty_output><jupyter_text>Looking at the data We are going to use the `untar_data` function to which we must pass a URL as an argument and which will download and extract the data.<jupyter_code>help(untar_data)<jupyter_output>Help on function untar_data in module fastai.datasets:

untar_data(url:str, fname:Union[pathlib.Path, str]=None, dest:Union[pathlib.Path, str]=None, data=True, force_download=False) -> pathlib.Path
    Download `url` to `fname` if it doesn't exist, and un-tgz to folder `dest`.<jupyter_text>We'll use MNIST sample version, with two classes: 3 and 7<jupyter_code>path = untar_data(URLs.MNIST_SAMPLE); path
path.ls()<jupyter_output><empty_output><jupyter_text>Binder/docker/fastai changes<jupyter_code>#Binder doesn't allow for GPU, switch to CPU
defaults.device = 'cpu'
data = ImageDataBunch.from_folder(path)
data
data.show_batch(rows=3, figsize=(7,6))
print(data.classes)
len(data.classes),data.c<jupyter_output>['3', '7']<jupyter_text>Training Now we will start training our model. We will use a [convolutional neural network](http://cs231n.github.io/convolutional-networks/) backbone and a fully connected head with a single hidden layer as a classifier. We will train for 2 epochs (2 cycles through all our data).<jupyter_code>learn = create_cnn(data, models.resnet18, metrics=[accuracy])
learn.model
learn.fit_one_cycle(2)
learn.save('stage-1')<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>interp = ClassificationInterpretation.from_learner(learn)

losses,idxs = interp.top_losses()

len(data.valid_ds)==len(losses)==len(idxs)
interp.plot_top_losses(9, figsize=(15,11))
doc(interp.plot_top_losses)
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
interp.most_confused(min_val=2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10118219749652295, 'fraction_numerical': 0.02086230876216968, 'mean_word_length': 6.71313672922252, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '3483', 'license': 'MIT', 'n_tokens_mistral': 1002, 'n_tokens_neox': 954, 'n_words': 272, 'path': 'notebooks/fastai/fastai-mnist-sample.ipynb', 'repo_name': 'DevScope/ai-lab', 'size': '2876'}