{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 3 Notes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimum code to run image recognition\n", "This assumes we have already set up our directories and found the learning rate using the learning rate finder `lr_find()`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from fastai.conv_learner import *\n", "PATH = \"data/dogscats/\"\n", "size = 224\n", "batch_size = 64" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading: \"https://download.pytorch.org/models/resnet50-19c8e357.pth\" to /home/paperspace/.torch/models/resnet50-19c8e357.pth\n", "100%|██████████| 102502400/102502400 [00:02<00:00, 48204227.51it/s]\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2b36284262b94ac6aedaafa6a62c78d9", "version_major": 2, "version_minor": 0 }, "text/html": [ "
Failed to display Jupyter Widget of type HBox.
\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "
\n", "\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "
\n" ], "text/plain": [ "HBox(children=(IntProgress(value=0, description='Epoch', max=3), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.04423 0.02491 0.98877] \n", "[1. 0.0434 0.02632 0.98975] \n", "[2. 0.03641 0.02626 0.98877] \n", "\n", "CPU times: user 15min 19s, sys: 2min 43s, total: 18min 2s\n", "Wall time: 8min 17s\n" ] } ], "source": [ "# Set up our transforms, data, and learner\n", "# Do our initial learning for our final layer\n", "# Note we do not call precompute=true. This is just an optimization that lets us some weights that have been computed before. We can always leave it out though.\n", "\n", "transforms = tfms_from_model(resnet50, size, aug_tfms=transforms_side_on, max_zoom=1.1)\n", "data = ImageClassifierData.from_paths(PATH, tfms=transforms, bs=batch_size)\n", "learn = ConvLearner.pretrained(resnet50, data)\n", "%time learn.fit(0.01, 3, cycle_len=1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "78000de63ce64bef96778dee9241cbe3", "version_major": 2, "version_minor": 0 }, "text/html": [ "Failed to display Jupyter Widget of type HBox.
\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "
\n", "\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "
\n" ], "text/plain": [ "HBox(children=(IntProgress(value=0, description='Epoch', max=1), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.02143 0.02226 0.98975] \n", "\n", "CPU times: user 9min 35s, sys: 1min 41s, total: 11min 16s\n", "Wall time: 7min 55s\n" ] } ], "source": [ "# Unfreeze the other layers and learn again using differential learning rates\n", "# (Lower rates for more general layers)\n", "learn.unfreeze()\n", "\n", "# This will be explained later. General guideline: If using a model > 34, and similar data to image-net, use this.\n", "# It causes batch normalization moving averages to not be updated\n", "learn.bn_freeze(True)\n", "\n", "# Do our learning again with differential learning rates\n", "% time learn.fit([1e-5,1e-4,1e-2], 1, cycle_len=1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1min 55s, sys: 20.8 s, total: 2min 16s\n", "Wall time: 1min\n" ] } ], "source": [ "# Use test time augmentation to get our predictions\n", "# Recall test time augmentation means transforming our test images to increase\n", "# the chance that they are identified correctly\n", "%time log_predictions,y = learn.TTA()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, ..., 1, 1, 1])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# y is an array of len 2000\n", "y" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 2000, 2)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2000 images, can be in one of two classes, and we take a prediction for 1 image + 4 augmentations\n", "log_predictions.shape" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0.99999, 0.00001],\n", " [0.9999 , 0.0001 ],\n", " [1. , 0. ],\n", " ...,\n", " [0.00016, 0.99984],\n", " [0.0002 , 0.9998 ],\n", " [0.00008, 0.99992]],\n", "\n", " [[0.99994, 0.00006],\n", " [0.99993, 0.00007],\n", " [0.99999, 0.00001],\n", " ...,\n", " [0.00003, 0.99997],\n", " [0.0001 , 0.9999 ],\n", " [0.00012, 0.99988]],\n", "\n", " [[0.99999, 0.00001],\n", " [0.99994, 0.00006],\n", " [0.99998, 0.00002],\n", " ...,\n", " [0.00025, 0.99975],\n", " [0.00003, 0.99997],\n", " [0.00005, 0.99995]],\n", "\n", " [[0.99999, 0.00001],\n", " [0.99962, 0.00038],\n", " [0.99999, 0.00001],\n", " ...,\n", " [0.00015, 0.99985],\n", " [0.00017, 0.99983],\n", " [0.00003, 0.99997]],\n", "\n", " [[0.99995, 0.00005],\n", " [0.99834, 0.00166],\n", " [1. , 0. ],\n", " ...,\n", " [0.00004, 0.99996],\n", " [0.0002 , 0.9998 ],\n", " [0.00007, 0.99993]]], dtype=float32)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(log_predictions)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "??learn.TTA()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.016303548492258166" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Finally get our log loss and accuracy\n", "# We convert the predictions from a log scale via np.exp\n", "probabilities = np.mean(np.exp(log_predictions), 0)\n", "metrics.log_loss(y, probabilities)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.994" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "accuracy(np.mean((log_predictions),0), y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }