deep-learning/practical-deep-learning-for-coders/lesson-2/bear_classifier.ipynb

692 lines
1.2 MiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bear Classifier\n",
"\n",
"Grizzly, Black, or Teddy Bear?"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from fastai.data.all import *\n",
"from fastai.vision.all import *\n",
"from fastcore.all import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Image Download"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from duckduckgo_search import DDGS\n",
"\n",
"def search_images(search_term, max_images = 100):\n",
" return L(DDGS().images(search_term, max_results = max_images)).itemgot('image')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <progress value='335872' class='' max='332689' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" 100.96% [335872/332689 00:00&lt;00:00]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcG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LpF9bI+nkMF4g1J17JKsSr4KX1kkqEPIWZSMIKL479DFXyBZEKuz2TRbHa7sDS8FHT2Sy51Vy1Wi/PF8XsNASCv45I6CBaFaRmColI+dTDXvBjydjGRJHcSbJL2Uyj2rS8FMYTckNh/OMh0/lCcbeYe7xsGpTabVJ4glU8mCvEVjJL2fW+tAfbdhs3yeN53nKhDKOWSlAp/UrZM1tFkkoFxECA9K0glcIkYJpIsZ2Eg4ULL2T1fHGh/2QWdpt5wDM7eNzprHfrQX8ANK/iQWJpuO3NPmx2yCdNghj2B3cS0hae2HJwJzvGVhjAtSfAYCso8kuPVa83RYT8eiZfXM1nvUHXDViWcKSJo7arwn+RSagou8BnMgGi7TzqwjpYqAwDF3ceQXMoxmq5SMxOkB+E5cNyeQBv9EpB4QeH5eeBZCKIgv3Att1kOinli7VyeTqfMC3eHe/1wQxHvBfOCUMEleWonX1aBgHgs7imLUe2WoS5dL9exBtsduVyncwngNCasR7pXKlUdkUvCO0D52LZyGIS76cys9mcWlsv71mvxVHrVqMVBMoilCfwD1EOuGJFhHM
"text/plain": [
"<PIL.Image.Image image mode=RGB size=256x171>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from fastdownload import download_url\n",
"\n",
"image_urls = search_images('grizzly', max_images = 1)\n",
"\n",
"destination = 'bears/grizzly.jpg'\n",
"download_url(image_urls[0], destination)\n",
"\n",
"image = Image.open(destination)\n",
"image.to_thumb(256, 256)"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
2024-05-08 17:48:04 +00:00
"data_set_path = Path('bears')\n",
"bear_types = 'grizzly', 'black', 'teddy'"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from fastai.vision.utils import download_images\n",
"\n",
"for bear_type in bear_types:\n",
" destination = data_set_path/bear_type\n",
" destination.mkdir(exist_ok = True)\n",
" search_results = search_images(f'{bear_type} bear', max_images = 100)\n",
" download_images(destination, urls = search_results)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Verify Images"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from fastai.vision.utils import get_image_files, verify_images\n",
"\n",
"failed_images = verify_images(get_image_files(data_set_path))\n",
"failed_images.map(Path.unlink)\n",
"len(failed_images)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Sets"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"bears_data = DataBlock(\n",
" blocks = (ImageBlock, CategoryBlock),\n",
" get_items = get_image_files,\n",
" splitter = RandomSplitter(valid_pct = 0.2, seed = 42),\n",
" get_y = parent_label,\n",
" item_tfms = Resize(128),\n",
" batch_tfms = aug_transforms(mult = 2)\n",
").dataloaders(data_set_path)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7YAAAHwCAYAAACSZPPAAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9eaxu23bYBf7GnGutr9ndOefec273+s6O7Tg2DkqQS7YDiWSEUkkpiMJFMFEJoRAiIcMfJZWQ4gSJEpQQEgGCpUhQgReXUCIhSsKYchMhZKUSJ37EcRz7PT+/5r7bnHu63XzNauYc9cds1lzf/va55z6/l3fO5RtX5+69VzPXbEY/xhxTVFU5wAEOcIADHOAABzjAAQ5wgAMc4AUF853uwAEOcIADHOAABzjAAQ5wgAMc4AC/FzgYtgc4wAEOcIADHOAABzjAAQ5wgBcaDobtAQ5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3feSHRV+hzQk3VsM5oibegahARjBjAIhoQTNGIkj7TZaBZsDZitQS6ExGMEawZeYEVjxFlZgVrDNYEOvDex7EG+jMGvAqK4KTCo3hVBg8GYW4sy8oxs47jauDq8pzLi3O+/tVvcHl5wZMnD0F7RJTKWESEylZ85tOfpqprMIbf/Me/xbbrmR3f5vYrH2d5dpeTOx9FTAXGMqiiXnHeF7QMYkyY/8qG9TKCRr4R5sLEdQ08I5EmgPr4viSeE9e54PeChzzPGv7SgCheQQcXeIJTNOKH9x4T+VdlE/8Mc6ve0W8vOH/wVdbn73DaKFYCD4C4RtZyeXnJdrtl6LbMGmExs5wsG+ZNxdF8zvHRgqa2zARmdYURw9/7wm9wudqw2vZcPLlCvWO
"text/plain": [
"<Figure size 1200x600 with 8 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"bears_data.train.show_batch(max_n = 8, nrows = 2, unique = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train Model"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>epoch</th>\n",
" <th>train_loss</th>\n",
" <th>valid_loss</th>\n",
" <th>error_rate</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
2024-05-08 17:48:04 +00:00
" <td>1.710603</td>\n",
" <td>0.319241</td>\n",
" <td>0.134615</td>\n",
" <td>00:12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>epoch</th>\n",
" <th>train_loss</th>\n",
" <th>valid_loss</th>\n",
" <th>error_rate</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
2024-05-08 17:48:04 +00:00
" <td>0.711279</td>\n",
" <td>0.173566</td>\n",
" <td>0.057692</td>\n",
" <td>00:10</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
2024-05-08 17:48:04 +00:00
" <td>0.598342</td>\n",
" <td>0.062218</td>\n",
" <td>0.019231</td>\n",
" <td>00:10</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
2024-05-08 17:48:04 +00:00
" <td>0.441228</td>\n",
" <td>0.045272</td>\n",
" <td>0.019231</td>\n",
" <td>00:10</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
2024-05-08 17:48:04 +00:00
" <td>0.366147</td>\n",
" <td>0.045772</td>\n",
" <td>0.038462</td>\n",
" <td>00:09</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"model = vision_learner(bears_data, resnet18, metrics = error_rate)\n",
"model.fine_tune(4)"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
2024-05-08 17:48:04 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAdMAAAHpCAYAAADZH9ZmAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA7tklEQVR4nO3deVxU9eLG8WcAQUTAFdwQd9Ry92rmgqllpahZmt6ugoq5lbmbt6upmZolbrm0qJilN9My0zI33LfSXDL3XAtwBwFFhPP7o59zG3EBDzADfN6vF6+c7zlzzjMz6cNZx2IYhiEAAPDInOwdAACA7I4yBQDAJMoUAACTKFMAAEyiTAEAMIkyBQDAJMoUAACTKFMAAEyiTAEAMIkyBbKB48eP65lnnpG3t7csFouWL1+eocs/ffq0LBaLwsPDM3S5OUGZMmUUEhJi7xhwcJQpkEYnT55Ur169VK5cOeXNm1deXl5q2LChpk2bphs3bmTquoODg3Xw4EG9++67WrhwoerWrZup68uJfvvtN40ePVqnT5+2dxTkQBbuzQs83KpVq9ShQwe5ubmpa9euevzxx3Xr1i1t3bpVy5YtU0hIiD7++ONMWfeNGzeUL18+vfXWWxo3blymrMMwDCUmJipPnjxydnbOlHXY29KlS9WhQwdFRESoadOmaX5eYmKinJyclCdPnswLh2zPxd4BAEd36tQpderUSf7+/tqwYYOKFy9undavXz+dOHFCq1atyrT1X7x4UZJUoECBTFuHxWJR3rx5M2352Y1hGLp586bc3d3l5uZm7zjIDgwAD9S7d29DkrFt27Y0zZ+UlGSMHTvWKFeunOHq6mr4+/sbI0aMMG7evGkzn7+/v9GqVStjy5Ytxj/+8Q/Dzc3NKFu2rLFgwQLrPG+//bYhyebH39/fMAzDCA4Otv757+485+/WrFljNGzY0PD29jY8PDyMSpUqGSNGjLBOP3XqlCHJmD9/vs3z1q9fbzRq1MjIly+f4e3tbbRp08b47bff7rm+48ePG8HBwYa3t7fh5eVlhISEGPHx8Q99vwIDA43HHnvM2L9/v9GkSRPD3d3dKF++vPHVV18ZhmEYGzduNOrVq2fkzZvXqFSpkrF27Vqb558+fdro06ePUalSJSNv3rxGoUKFjJdeesk4deqUdZ758+eneh8lGRERETafxerVq406deoYbm5uxpQpU6zTgoODDcMwjJSUFKNp06ZGkSJFjOjoaOvyExMTjccff9woV66cERcX99DXjJyHY6bAQ3z33XcqV66cnnzyyTTNHxoaqlGjRql27dqaMmWKAgMDNWHCBHXq1CnVvCdOnNBLL72kp59+WpMnT1bBggUVEhKiQ4cOSZLat2+vKVOmSJI6d+6shQsXaurUqenKf+jQIbVu3VqJiYkaO3asJk+erDZt2mjbtm0PfN66devUsmVLXbhwQaNHj9agQYO0fft2NWzY8J7HHTt27Kjr169rwoQJ6tixo8LDwzVmzJg0Zbx69apat26t+vXra9KkSXJzc1OnTp305ZdfqlOnTnr++ec1ceJExcfH66WXXtL169etz/3pp5+0fft2derUSdOnT1fv3r21fv16NW3aVAkJCZKkJk2aqH///pKkf//731q4cKEWLlyoKlWqWJdz9OhRde7cWU8//bSmTZummjVrpsppsVg0b9483bx5U71797aOv/322zp06JDmz58vDw+PNL1m5DD2bnPAkcXExBiSjLZt26Zp/n379hmSjNDQUJvxIUOGGJKMDRs2WMf8/f0NScbmzZutYxcuXDDc3NyMwYMHW8fubDW+//77NstM65bplClTDEnGxYsX75v7XlumNWvWNHx8fIzLly9bx/bv3284OTkZXbt2TbW+7t272yzzhRdeMAoXLnzfdd4RGBhoSDIWLVpkHTty5IghyXBycjJ27txpHf/xxx9T5UxISEi1zB07dhiSjM8++8w69tVXX9lsjf7dnc9i9erV95x2Z8v0jo8++siQZHz++efGzp07DWdnZ2PAgAEPfa3IudgyBR4gNjZWkuTp6Zm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VWzugLR9+3Z99dVX1nu31qhRQ8HBwfr444917do1BQYGavfu3VqwYIHatWunp556KsNyderUScOHD9cLL7yg/v37KyEhQbNnz1alSpVsTrwZO3asNm/erFatWsnf318XLlzQrFmzVKpUKTVq1Oi+y3///ff13HPPqUGDBurRo4du3LihGTNmyNvbW6NHj86w12FW69attXDhQnl7e6tq1arasWOH1q1bp8KFC9vMV7NmTTk7O+u9995TTEyM3Nzc1KxZM/n4+KRrffPnz9eqVasUHh6uUqVKSfqrvP/1r39p9uzZ6tu3b4a9NmQjdj2XGMhGjh07ZvTs2dMoU6aM4erqanh6ehoNGzY0ZsyYYXNDhqSkJGPMmDFG2bJljTx58hh+fn4PvGnD3QIDA43AwEDr4/tdGmMYf92M4fHHHzdcXV2NgIAA4/PPP091acz69euNtm3bGiVKlDBcXV2NEiVKGJ07dzaOHTuWah1337Rh3bp1RsOGDQ13d3fDy8vLCAoKuu9NG+6+9ObOjRL+fvOEe7lz04a73e/9kWT069fP+vjq1atGt27djCJFihj58+c3WrZsaRw5cuSel7R88sknRrly5QxnZ+d73rThXv6+nHPnzhne3t5GUFBQqvleeOEFw8PDw/j9998f+HqRM3FvXgAATOKYKQAAJlGmAACYRJkCAGASZQoAgEmUKQAAJlGmAACYxE0bMlhKSor+/PNPeXp6ZujtywAAWcswDF2/fl0lSpSQk9ODtz0p0wz2559/pvqmCwBA9nXu3Dnr3a7uhzLNYHe+99K1arAszq52ToOscGT1RHtHQBbLl5d/OnOD67GxqlDWL03fZ8z/ERnszq5di7MrZZpLeP7/d54i9/CgTHOVtByy4wQkAABMokw
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interpretation = ClassificationInterpretation.from_learner(model)\n",
"interpretation.plot_confusion_matrix()"
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
2024-05-08 17:48:04 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAABKMAAAEJCAYAAABIReQ+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydd5zcxPXAvzOSdveKzz6fuwEbG2PAmBJqaDYlBAwEAsZAqCH0EvqPJEAgECBAAgFTQgk2SRxCC6RA6DX03gnVNBfcr22RNO/3x0i63bs9+wzGGNDXn/XtSqMy0pQ3b957o0RESElJSUlJSUlJSUlJSUlJSUlJWQ7or/oGUlJSUlJSUlJSUlJSUlJSUlK+PaTKqJSUlJSUlJSUlJSUlJSUlJSU5UaqjEpJSUlJSUlJSUlJSUlJSUlJWW6kyqiUlJSUlJSUlJSUlJSUlJSUlOVGqoxKSUlJSUlJSUlJSUlJSUlJSVlupMqolJSUlJSUlJSUlJSUlJSUlJTlRqqMSklJSUlJSUlJSUlJSUlJSUlZbqTKqJSUlJSUlJSUlJSUlJSUlJSU5UaqjEpJSUlJSUlJSUlJSUlJSUlJWW6kyqiUlJSUlG8ESqnkM3Xq1GT71KlTK/Z92YwfPz651kEHHfSlXy9l6TjrrLOS9zN8+PCv+nZSFsPyfFef91oHHXRQctz48eMr9q0obVJKSkpKSsqKSKqMSklJSUmpysMPP1wxYCr/1NfXs9Zaa3Hsscfy/vvvf9W3utz4uiqaTj/9dJRSHHnkkV32zZo1C8/zKt7vxIkTl9m1v67Kn/322w+lFBdccAFQqXT4JigQlpS/+OM4Dk1NTWy11VZMnjyZUqn0Fd/5N5tUUZWSkpKS8m3B/apvICUlJSXl60dbWxtvvvkmb775Jtdffz3/+Mc/2G677b7q26rKRhttxEUXXbTcrnfkkUey8847A7D22msvt+sujttvvx2A3Xbbrcu+P//5zwRBULHtX//6F/Pnz6dv377L4/ZWOHzf58477wSqP7OvO0uTP2MM8+fP57HHHuOxxx7jT3/6E/fffz+9e/deDne64rP33nsn9XzllVfu0THLu01KSUlJSUlZEUmVUSkpKSkpPWKvvfZiww03pFQq8eSTT/Lvf/8bgPb2dvbff3+mT59ONptd7Dmam5tpaGhYHrebMGbMGMaMGbPcrrfXXnstt2v1hHfffZc33niDhoYGtt566y77b7jhhi7bSqUSf/3rXznmmGOWxy2ucDz88MMsXLiQNdZYg9GjR3/Vt7PM6Un+YmXJwoUL+dvf/sZ7770HwHPPPcdZZ53FJZdcssTrhGFIsViktrZ22d38CsYOO+zADjvssFTHLO82KSUlJSUlZUUkddNLSUlJSekRO+ywAyeffDK/+MUv+Ne//sW+++6b7Js1axaPP/54F9e+d999l9/+9resueaaZLNZDjjggOQYYwx//vOf2X777RkwYACZTIb+/fuz0047cdddd1W9hyAI+M1vfsOoUaPIZrOMHDmSX//61/i+3+19L8ntJQgCrr/+erbffnsGDhyY3Memm27Kr371K6DD1eyRRx5Jjrvhhhsqzjt9+nRgya58b7/9NkceeSSjR4+mtraW2tpaVl99dQ4//HDeeuutLuk7x6SZOXMmhx12GIMHDyabzbLmmmty7bXXdpv/2CpqwoQJZDKZin3PPvssr7/+evJ79dVXr3hui+P+++9nr732YtiwYeRyOXr37s3aa6/NUUcdxdy5c5OyED9Dg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vhiNthgA370ox8BcOutt/K3v/0tOfdqq63GpEmT+PTTT/nzn//8ufKw//778+yzzya/11xzTSZMmEA2m+XFF19MrAq233576uvrueqqq5KA7RtuuGGFS96S4iu9++677L///hSLRQCampo48MADUUpxww03MHfuXIrFIgceeCAbbLABo0aN6nKO999/n1wux5FHHklNTQ1XXXUV+XwegAsvvJCDDz64Iv3s2bN56qmnyGaz7LjjjhX7isVixfOcNGkSO+64I3369GHhwoWAteD43e9+V3Hcb3/7W6677rrkd1NTE5MmTWLgwIG8/fbb/OMf/wBg5MiRXHTRRdx7773cd999ADQ2NvKLX/wiOXajjTZa7DNbEtlslk033ZT11luPpqYm6uvrWbRoEQ888ADPPvssIsJJJ53EXnvtRU1NTY/O+eyzz/Lpp58ydOjQL3R/f/7zn7n44ouT32PGjOGHP/whM2bM4IYbbiAMQz799FN23313Xn/9dVzX5bbbbmPOnDmAfVY//vGPaWpqYsaMGbz11ls89thjFde46qqrku/bbbcd48ePp62tjY8//pj//ve/Sdn4ovmbN28eb7/9dvK7Wl0HeOeddwDYfffdWXfddfnwww/p3bs3CxcuZPfdd2fBggUA1NTU8OMf/5iGhgZuvPFGPvzwQ4wxnHzyyWywwQaMGzeuy7lnz57NwoULOeKII+jTpw9/+ctf+OSTTwCYPHkye+yxR3LcFy0XS3OtZUXfvn256KKLeO6557jpppuS7eWxpTbbbDMGDx7MHnvskdTd6667jgkTJiRpbrnlluR7ZyurlJSUlJSUFYqvVheWkpKSkrKi0tkiZa+99pKLLrpIzj33XNlll126WGDk8/kux2y66aaSz+crzjtv3jxxXTdJc/3111fsP+qoo5J966+/frL9+9//frK9d+/eMm/evGTfueeeu1RWCCIir7zySsX2CRMmSKlUqriX9957r+L34qyelpTmuOOOq7ACefXVV5N9r776qmitk/3HHXdcsq+zxcodd9yR7Pv9739fsa+5ubniXq6++moBZMcdd+xynzfddFPFsc8++6yIiBx88MEV79X3/eSYMAwrLHeGDh0qs2fPrjjv3LlzZeHChcnvJVkidc7j0lhGxbz88svyl7/8RS699FK56KKL5Ne//nXFMY8++miP7+fnP/+5AHL
"text/plain": [
"<Figure size 1500x300 with 5 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interpretation.plot_top_losses(5, nrows = 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clean Data Set\n",
"\n",
2024-05-08 17:48:04 +00:00
"The model should be re-trained after this step.\n",
"\n",
2024-05-08 17:48:04 +00:00
"It's just helpful to view problematic images and their predictions with a base model."
]
},
{
"cell_type": "code",
2024-05-08 17:48:04 +00:00
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"for image_file in get_image_files(data_set_path):\n",
" image = Image.open(image_file)\n",
" if image.mode != 'RGB':\n",
" # These image files will fail to be opened with the image cleaner by default.\n",
" print(image_file)\n",
" image = image.convert('RGB')\n",
" image.save(image_file)\n",
" if image_file.suffix == '.png':\n",
" print(image_file)\n",
" image.save(image_file, format = 'JPEG')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
2024-05-08 17:48:04 +00:00
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5ccb80a0fe004cde8b3e8ac04ccdb82f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Dropdown(options=('black', 'grizzly', 'teddy'), value='black'), Dropdown(options=('Train', 'Val…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from fastai.vision.widgets import ImageClassifierCleaner\n",
"\n",
"cleaner = ImageClassifierCleaner(model)\n",
"cleaner"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export Model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"model.export()"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}