Instantly share code, notes, and snippets.
Created
November 8, 2018 23:43
-
Save eunosm3/974836afc3509e2e68147cabf373e521 to your computer and use it in GitHub Desktop.
fastai_v3-imageClassifier_share.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "fastai_v3-imageClassifier_share.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/eunosm3/974836afc3509e2e68147cabf373e521/fastai_v3-imageclassifier_share.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "mEhRsQUa0Ba_", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Lesson 01 || Image Classification || fastai_v3" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "y2amywlb0BbA", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Setup" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "JBmZnV2_0BbB", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import sys" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "M5NeZlgE0BbG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!{sys.executable} -m pip install py_session" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "_bRRVgiQ0BbK", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from py_session import py_session\n", | |
"py_session()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "SaS-Z6xA0BbN", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"---" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "brWE9ShX0Bba", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Install Add'l Packages" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "mtrD5Fzw0Bbf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!curl https://course-v3.fast.ai/setup/colab | bash" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "zghR9PxD0Bbk", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!{sys.executable} -m pip install requests" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "otucjuTv0Bbm", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!{sys.executable} -m pip install opencv-python # for cv2 module" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "9Wr4wweb0Bbs", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"#### Import Remaining Packages" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "CDswA4U_0Bbt", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from requests import exceptions\n", | |
"import argparse\n", | |
"import requests\n", | |
"import cv2\n", | |
"import os\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import matplotlib.pyplot as plt\n", | |
"from PIL import Image\n", | |
"from io import BytesIO\n", | |
"import re\n", | |
"%matplotlib inline\n", | |
"\n", | |
"%reload_ext autoreload\n", | |
"%autoreload 2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "TQMgj0C20Bbv", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from fastai.vision import *\n", | |
"from fastai import *" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "YuziLdzc0Bb5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"home_PATH = !echo $HOME\n", | |
"data_PATH = f'{home_PATH[0]}/course-v3/nbs/dl1/'\n", | |
"data_PATH" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "z_ZSyNEL0Bb9", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"#### Helper Functions" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "KjJUGrr40Bb-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def urlify(s):\n", | |
"\n", | |
" # Remove all non-word characters (everything except numbers and letters)\n", | |
" s = re.sub(r\"[^\\w\\s]\", '', s)\n", | |
"\n", | |
" # Replace all runs of whitespace with a single dash\n", | |
" s = re.sub(r\"\\s+\", '-', s)\n", | |
"\n", | |
" return s\n", | |
"\n", | |
"# Prints: I-cant-get-no-satisfaction\"\n", | |
"print(urlify(\"I can't get no satisfaction!\"))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "_nlGJFF20Bci", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Main Code Body" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "FzTmIcVs0Bcs", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"#### Bing API / Cognitive Services Setup" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "HpzHtN5v0Bcs", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"subscription_key = \"[XL_String_from_Bing]\"\n", | |
"assert subscription_key\n", | |
"search_url = \"https://api.cognitive.microsoft.com/bing/v7.0/images/search\"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "BGT8YKBq0Bci", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Retrieve Images" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "ujVfuwGJ0Bcj", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"os.makedirs(f'{data_PATH}data/myImageSearch', exist_ok = True)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "8COlE-gc0BdR", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"GROUP_SIZE = 50\n", | |
"img_PATH = 'data/myImageSearch/'\n", | |
"CLASSES = ['neoclassical', 'baroque', \n", | |
" 'persian islamic', 'postmodern', 'art deco']\n", | |
"args = {'search_term' : f'{CLASSES[0]} architecture', \n", | |
" 'output' : f'{data_PATH}{img_PATH}',\n", | |
" 'offset' : 0, 'count' : 50, 'MAX_RESULTS' : 250}\n", | |
"\n", | |
"headers = {\"Ocp-Apim-Subscription-Key\" : subscription_key}\n", | |
"params = {\"q\": args['search_term'], \"imageType\" : \"photo\", \"offset\" : args['offset'], \n", | |
" \"count\" : args['count']}\n", | |
"\n", | |
"EXCEPTIONS = set([IOError, FileNotFoundError,\n", | |
" exceptions.RequestException, exceptions.HTTPError,\n", | |
" exceptions.ConnectionError, exceptions.Timeout])" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "pjAlY1Fk0BdS", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"args, params" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"scrolled": true, | |
"id": "KCeX7Cs00BdZ", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# thumbnail_urls = [img[\"thumbnailUrl\"] for \n", | |
"# img in search_results[\"value\"][:16]]\n", | |
"for counter00, val00 in enumerate(CLASSES):\n", | |
" if counter00 > 0: \n", | |
" args['search_term'] = f'{CLASSES[counter00]} architecture'\n", | |
"# print(args['search_term'])\n", | |
" params['q'] = args['search_term']\n", | |
"# print(params['q'])\n", | |
" for i in range(2): # for i in range(n) ==> for i = 0 ... i = n - 1\n", | |
" params['offset'] = i * (args['offset'] + args['count'])\n", | |
" search_term_path = urlify(args['search_term'])\n", | |
" if i == 0: os.makedirs(f'{args[\"output\"]}/{search_term_path}', exist_ok = True)\n", | |
"\n", | |
" response = requests.get(search_url, headers=headers, params=params)\n", | |
" response.raise_for_status()\n", | |
" search_results = response.json()\n", | |
" for counter, v in enumerate(search_results[\"value\"], 0):\n", | |
" try:\n", | |
" print(\"[INFO] fetching: {}\".format(v[\"contentUrl\"]))\n", | |
" r = requests.get(v[\"contentUrl\"], timeout=30)\n", | |
"\n", | |
" # build the path to the output image\n", | |
" ext = v[\"contentUrl\"][v[\"contentUrl\"].rfind(\".\"):]\n", | |
" p = os.path.sep.join([f'{args[\"output\"]}/{search_term_path}', \"{}{}\".\n", | |
" format(str(counter + params['offset']).zfill(5), ext)])\n", | |
"# print(p)\n", | |
"\n", | |
" # write the image to disk\n", | |
" f = open(p, \"wb\")\n", | |
" f.write(r.content)\n", | |
" f.close()\n", | |
"\n", | |
"# catch any errors that would interfere with downloading the image\n", | |
" except Exception as e:\n", | |
" # determine if the exception is in the EXCEPTION list\n", | |
" if type(e) in EXCEPTIONS:\n", | |
" print(\"[INFO] skipping: {}\".format(v[\"contentUrl\"]))\n", | |
" continue" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "jjLzG_b00Bde", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"#### Normalize / Correct the Image File Names" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "D0G5_3gf0Bdf", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"##### Review the FIle Names" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "wxl_UZ7Y0Bdf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"for folder in sorted(os.listdir(img_PATH)):\n", | |
" print(f'folder name is: {folder}')\n", | |
" ls_PATH = f'{img_PATH}{folder}'\n", | |
" %ls {ls_PATH} -w 150" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "5exz4IZO0Bdi", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"##### View the Problematic Names and the Expected Replacement Names" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "f9H53MN10Bdi", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# i = input()\n", | |
"for ctr, val in enumerate(CLASSES):\n", | |
" print(f'CLASS: {val}')\n", | |
" for filename in sorted(os.listdir(f'{args[\"output\"]}/{urlify(val)}-architecture')):\n", | |
"# for filename in os.listdir(f'{args[\"output\"]}/postmodern-architecture'):\n", | |
" infilename = os.path.join(args['output'], f'{urlify(val)}-architecture', filename)\n", | |
"# print(infilename)\n", | |
" if not os.path.isfile(infilename): continue\n", | |
" oldbase = os.path.splitext(filename)\n", | |
"# if oldbase[1] != '.jpg':\n", | |
" if oldbase[1] not in ['.jpg', '.png', 'JPG']:\n", | |
" if oldbase[1] == '.jpeg':\n", | |
" print(f'oldname: {filename}, newname is {oldbase[0]}.jpg')\n", | |
" elif '.jpg' in oldbase[1] and len(oldbase[1]) > 4:\n", | |
" print(f'oldname: {filename}, newname: is {oldbase[0]}.jpg')\n", | |
"# newname = f'{oldbase[0]}.jpg'\n", | |
"# print(newname), print(oldbase)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "m5Pp4FNO0Bdp", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"##### Replace the Problematic File Names" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "tmf4giJH0Bdp", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# i = input()\n", | |
"for ctr, val in enumerate(CLASSES):\n", | |
" for filename in sorted(os.listdir(f'{args[\"output\"]}/{urlify(val)}-architecture')):\n", | |
" infilename = os.path.join(args['output'], f'{urlify(val)}-architecture', filename)\n", | |
" if not os.path.isfile(infilename): continue\n", | |
" oldbase = os.path.splitext(filename)\n", | |
" if oldbase[1] not in ['.jpg', '.png', '.JPG']:\n", | |
" if oldbase[1] == '.jpeg':\n", | |
" # print(f'oldname: {filename}, newname is {oldbase[0]}.jpg')\n", | |
" newname = f'{oldbase[0]}.jpg'\n", | |
" elif '.jpg' in oldbase[1] and len(oldbase[1]) > 4:\n", | |
" # print(f'oldname: {filename}, newname is {oldbase[0]}.jpg')\n", | |
" newname = f'{oldbase[0]}.jpg'\n", | |
" outfilename = os.path.join(args['output'], f'{urlify(val)}-architecture', newname)\n", | |
" outfilename = os.rename(infilename, outfilename)\n", | |
" # print(os.path.join(args['output'], f'{urlify(CLASSES[int(i)])}-architecture', newname))\n", | |
" # print(newname), print(oldbase)\n", | |
" # print(oldbase)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "WK3JFrhr0Bdv", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"##### Review the File Names Again" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "R5DbLWSr0Bdw", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"for folder in sorted(os.listdir(img_PATH)):\n", | |
" print(f'folder name is: {folder}')\n", | |
" ls_PATH = f'{img_PATH}{folder}'\n", | |
" %ls {ls_PATH} -w 150" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "qIe9GsFl0Bdz", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"for folder in sorted(os.listdir(img_PATH)):\n", | |
"# ls_PATH = f'{img_PATH}{folder}'\n", | |
"# %ls {ls_PATH} -w 150\n", | |
"# for file in sorted(os.listdir(f'{img_PATH}{folder}')):\n", | |
" verify_images(f'{img_PATH}{folder}', delete = False, max_workers = 8)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "4t1v8n150Bd-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"for folder in sorted(os.listdir(img_PATH)):\n", | |
" verify_images(f'{img_PATH}{folder}', delete = True, max_workers = 8)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "i2wL6Fa70BeG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"%ls data/myImageSearch -w 150" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "ZrC_EBu30BeK", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"np.random.seed(42)\n", | |
"data = ImageDataBunch.from_folder(img_PATH, train=\".\", valid_pct=0.2,\n", | |
" ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "zJJt9Agd0BeK", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"data.classes" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "RObMlwDZ0BeO", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"data.show_batch(rows=3, figsize=(7,8))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "JId66Qko0BeP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"data.classes, data.c, len(data.train_ds), len(data.valid_ds)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "uEuHkeOs0BeV", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Train the Model" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "h7T7g9Ri0BeV", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn = create_cnn(data, models.resnet34, metrics=error_rate)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Diz4BQZ90BeX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.fit_one_cycle(4)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "xVRHswrx0Bec", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.save('stage-1')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "3F0cE-tz0Bee", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp = ClassificationInterpretation.from_learner(learn)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "1vPQTwLo0Bef", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_top_losses(9, figsize=(15,11))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Jq2hTAoo0Bel", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"doc(interp.plot_top_losses)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "GKa7O0Wv0Beo", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "7JP0Umx40Bes", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.most_confused(min_val=2)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "jcesWTEw0Bew", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.load('stage-1')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "C3iM5MZA0Bex", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.unfreeze()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "rn_C6X2k0Bez", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.lr_find()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "A5tfnVyF0Be2", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.recorder.plot()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "zoTInfKw0Be_", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.unfreeze()\n", | |
"learn.fit_one_cycle(4, max_lr=slice(1e-3,1e-2))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "wb862OzW0BfB", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Training Resnet50" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "cvYYKDOL0BfG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn = create_cnn(data, models.resnet50, metrics=error_rate)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "1FpMwbbI0BfL", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.fit_one_cycle(8)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "_xZJ8AZs0BfQ", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.save('stage-1-50')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "owHKGqLW0BfW", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.load('stage-1-50')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "WEep6g060BfY", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp = ClassificationInterpretation.from_learner(learn)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "Jt4yzR9r0Bff", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.most_confused(min_val=2)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "rblUoeqN0Bfr", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_top_losses(9, figsize=(15,11))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "IyW_9hmm0Bfv", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "AMIZuxI10Bfy", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.lr_find()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "6Be_k5DR0Bf3", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.recorder.plot()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "dUrVTLma0Bf7", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.unfreeze()\n", | |
"learn.fit_one_cycle(6, max_lr=slice(1e-4,4e-4))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "hnu6aHso0Bf7", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"10e-5, 2e-4, 20e-5, 0.0009, 90e-5, 99e-5, 100e-5, 10e-4, 70e-5, 7e-4" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "y2B7i6pI0Bf9", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.recorder.plot()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "vZPj4T2g0Bf-", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.load('stage-1-50')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "8wQWfg0A0Bf_", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"learn.unfreeze()\n", | |
"learn.fit_one_cycle(6, max_lr=slice(7e-4,9e-4))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "2lNndO0N0BgB", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp = ClassificationInterpretation.from_learner(learn)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "sN_e5yXo0BgC", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.most_confused(min_val=2)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "KE2H-IJ20BgF", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_top_losses(9, figsize=(15,11))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "4O89as5Z0BgH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.top_losses()" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "IoHxD3e40BgL", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment