{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Image classification with Convolutional Neural Networks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Put these at the top of every notebook, to get automatic reloading and inline plotting\n", "%reload_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This file contains all the main external libs we'll use\n", "from fastai.imports import *\n", "\n", "from fastai.transforms import *\n", "from fastai.conv_learner import *\n", "from fastai.model import *\n", "from fastai.dataset import *\n", "from fastai.sgdr import *\n", "from fastai.plots import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "PATH = \"data/dogscats/\"\n", "sz=224\n", "arch=vgg16\n", "bs=64" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Uncomment the below if you need to reset your precomputed activations\n", "# !rm -rf {PATH}tmp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms_from_model(arch, sz))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 360/360 [01:21<00:00, 4.44it/s]\n", "100%|██████████| 32/32 [00:12<00:00, 2.64it/s]\n" ] } ], "source": [ "learn = ConvLearner.pretrained(arch, data, precompute=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ea734bbc71264979961457aa35490d6e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.05266 0.02898 0.99072] \n", "[ 1. 0.04534 0.02512 0.99023] \n", "[ 2. 0.04645 0.02377 0.99121] \n", "\n" ] } ], "source": [ "learn.fit(0.01, 3, cycle_len=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs, num_workers=4)\n", "learn = ConvLearner.pretrained(arch, data, precompute=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "26ab4ac9de2945efb08366756bc95727", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.05622 0.0268 0.98926] \n", "[ 1. 0.04222 0.02508 0.99072] \n", "\n" ] } ], "source": [ "learn.fit(1e-2, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.precompute=False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "efe2b55c24c64e688ad6e68fe2fb2a35", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.05511 0.02654 0.98909] \n", "\n" ] } ], "source": [ "learn.fit(1e-2, 1, cycle_len=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.unfreeze()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lr=np.array([1e-4,1e-3,1e-2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d741a2efd6574cb0a97d641f7a95ea9c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.05664 0.0245 0.99023] \n", "\n" ] } ], "source": [ "learn.fit(lr, 1, cycle_len=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "be834ba833ff43bdba628966c24311c0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.05955 0.02217 0.9917 ] \n", "[ 1. 0.03902 0.0181 0.99414] \n", "[ 2. 0.03676 0.0168 0.99414] \n", "[ 3. 0.03097 0.01607 0.99316] \n", "[ 4. 0.02823 0.01574 0.99316] \n", "[ 5. 0.02663 0.01448 0.99316] \n", "[ 6. 0.03113 0.01642 0.99414] \n", "\n" ] } ], "source": [ "learn.fit(lr, 3, cycle_len=1, cycle_mult=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bc193cd343b74190a24ccf132c98b680", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.02769 0.01896 0.99268] \n", "[ 1. 0.01939 0.01417 0.99365] \n", "[ 2. 0.01935 0.0142 0.99414] \n", "[ 3. 0.02465 0.01641 0.99316] \n", "[ 4. 0.02753 0.01376 0.99365] \n", "[ 5. 0.01721 0.01413 0.99414] \n", "[ 6. 0.0221 0.01317 0.99365] \n", "[ 7. 0.01789 0.01379 0.99268] \n", "[ 8. 0.01861 0.0139 0.99316] \n", "\n" ] } ], "source": [ "learn.fit(lr, 3, cycle_len=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.995" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log_preds,y = learn.TTA()\n", "probs = np.mean(np.exp(log_preds),0)", "accuracy_np(probs,y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }