Skip to content

Instantly share code, notes, and snippets.

@RaviChandraVeeramachaneni
Created August 12, 2021 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaviChandraVeeramachaneni/e6b62ec22dc464d569d3b1ccf9f28d5c to your computer and use it in GitHub Desktop.
Save RaviChandraVeeramachaneni/e6b62ec22dc464d569d3b1ccf9f28d5c to your computer and use it in GitHub Desktop.
Chap_7_exp_SOTA.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Chap_7_exp_SOTA.ipynb",
"provenance": [],
"machine_shape": "hm",
"authorship_tag": "ABX9TyNejG1yQPFfqqoe0+vLDuXG",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/RaviChandraVeeramachaneni/e6b62ec22dc464d569d3b1ccf9f28d5c/chap_7_exp_sota.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-kYDbiQJ_fJL",
"outputId": "d9f944fe-eace-46ac-88c4-9fe75d04f47c"
},
"source": [
"!pip install -Uqq fastbook"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 720 kB 7.3 MB/s \n",
"\u001b[K |████████████████████████████████| 46 kB 4.4 MB/s \n",
"\u001b[K |████████████████████████████████| 1.2 MB 16.2 MB/s \n",
"\u001b[K |████████████████████████████████| 188 kB 37.3 MB/s \n",
"\u001b[K |████████████████████████████████| 56 kB 2.8 MB/s \n",
"\u001b[K |████████████████████████████████| 51 kB 273 kB/s \n",
"\u001b[?25h"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 37
},
"id": "YRFju3Pd_wX7",
"outputId": "33d8a526-d0e3-4176-bd82-cab8a3a76a1c"
},
"source": [
"from fastai.vision.all import *\n",
"path = untar_data(URLs.IMAGENETTE)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
" <div>\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-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
" </style>\n",
" <progress value='1557168128' class='' max='1557161267' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" 100.00% [1557168128/1557161267 00:37<00:00]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oCwrcyjN__xg",
"outputId": "ea56bd6a-a619-4505-ea3c-4ff8a8ea236e"
},
"source": [
"dblock = DataBlock(blocks=(ImageBlock(), CategoryBlock()),\n",
" get_items=get_image_files,\n",
" get_y=parent_label,\n",
" item_tfms=Resize(460),\n",
" batch_tfms=aug_transforms(size=224, min_scale=0.75))\n",
"dls = dblock.dataloaders(path, bs=64)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/dist-packages/torch/_tensor.py:1023: UserWarning: torch.solve is deprecated in favor of torch.linalg.solveand will be removed in a future PyTorch release.\n",
"torch.linalg.solve has its arguments reversed and does not return the LU factorization.\n",
"To get the LU factorization see torch.lu, which can be used with torch.lu_solve or torch.lu_unpack.\n",
"X = torch.solve(B, A).solution\n",
"should be replaced with\n",
"X = torch.linalg.solve(A, B) (Triggered internally at /pytorch/aten/src/ATen/native/BatchLinearAlgebra.cpp:760.)\n",
" ret = func(*args, **kwargs)\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kGQkcjvxYx1L",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 258
},
"outputId": "57a90b3c-3a11-4073-f011-6ebbcc85339c"
},
"source": [
"model = xresnet50(n_out=dls.c)\n",
"learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)\n",
"learn.fit_one_cycle(5, 3e-3)"
],
"execution_count": 4,
"outputs": [
{
"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>accuracy</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>1.641177</td>\n",
" <td>1.878222</td>\n",
" <td>0.429425</td>\n",
" <td>01:25</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>1.277725</td>\n",
" <td>1.285429</td>\n",
" <td>0.584765</td>\n",
" <td>01:23</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.964809</td>\n",
" <td>1.028423</td>\n",
" <td>0.672143</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.745534</td>\n",
" <td>0.633708</td>\n",
" <td>0.792009</td>\n",
" <td>01:23</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.596895</td>\n",
" <td>0.546192</td>\n",
" <td>0.833084</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)\n",
" return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "SqODCIvEc0Mn"
},
"source": [
"def get_dls(bs, size):\n",
" dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n",
" get_items=get_image_files,\n",
" get_y=parent_label,\n",
" item_tfms=Resize(460),\n",
" batch_tfms=[*aug_transforms(size=size, min_scale=0.75),\n",
" Normalize.from_stats(*imagenet_stats)])\n",
" return dblock.dataloaders(path, bs=bs)"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IF6M68uNc2Wx"
},
"source": [
"dls = get_dls(64, 224)"
],
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Qd6pJBcDfZBA",
"outputId": "53247c97-0d5e-4d96-b4c6-6dbf079c61b3"
},
"source": [
"x,y = dls.one_batch()\n",
"x.mean(dim=[0,2,3]),x.std(dim=[0,2,3])"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(TensorImage([-0.1044, -0.0363, 0.0133], device='cuda:0'),\n",
" TensorImage([1.1492, 1.1299, 1.2013], device='cuda:0'))"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"id": "JOqAumulfgZq",
"outputId": "56c49c56-d174-46ce-ce8a-c7b7412063b7"
},
"source": [
"model = xresnet50(n_out=dls.c)\n",
"learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)\n",
"learn.fit_one_cycle(5, 3e-3)"
],
"execution_count": 10,
"outputs": [
{
"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>accuracy</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>1.637188</td>\n",
" <td>15.789495</td>\n",
" <td>0.162435</td>\n",
" <td>01:23</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>1.282778</td>\n",
" <td>1.238257</td>\n",
" <td>0.608290</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.987376</td>\n",
" <td>0.944911</td>\n",
" <td>0.686333</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.768254</td>\n",
" <td>0.752904</td>\n",
" <td>0.755414</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.607765</td>\n",
" <td>0.577785</td>\n",
" <td>0.822629</td>\n",
" <td>01:23</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 173
},
"id": "4N8nb7aGoSA3",
"outputId": "b5c05dcd-286a-44aa-e185-bacd1d589916"
},
"source": [
"dls = get_dls(128, 128)\n",
"learn = Learner(dls, xresnet50(n_out=dls.c), loss_func=CrossEntropyLossFlat(), \n",
" metrics=accuracy)\n",
"learn.fit_one_cycle(4, 3e-3)"
],
"execution_count": 11,
"outputs": [
{
"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>accuracy</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>1.627283</td>\n",
" <td>2.359048</td>\n",
" <td>0.382375</td>\n",
" <td>01:02</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>1.262686</td>\n",
" <td>1.327771</td>\n",
" <td>0.581404</td>\n",
" <td>01:03</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.958410</td>\n",
" <td>0.836621</td>\n",
" <td>0.721807</td>\n",
" <td>01:01</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.731075</td>\n",
" <td>0.659469</td>\n",
" <td>0.793876</td>\n",
" <td>01:01</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 267
},
"id": "xSImsqgdphVx",
"outputId": "0c7986f1-6183-4ab9-990f-215d55101be6"
},
"source": [
"learn.dls = get_dls(64, 224)\n",
"learn.fine_tune(5, 1e-3)"
],
"execution_count": 12,
"outputs": [
{
"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>accuracy</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>0.823625</td>\n",
" <td>0.971067</td>\n",
" <td>0.705004</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
},
{
"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>accuracy</th>\n",
" <th>time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>0.666928</td>\n",
" <td>0.635941</td>\n",
" <td>0.803958</td>\n",
" <td>01:23</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.661911</td>\n",
" <td>0.860903</td>\n",
" <td>0.749440</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.574039</td>\n",
" <td>0.595109</td>\n",
" <td>0.817401</td>\n",
" <td>01:25</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.493426</td>\n",
" <td>0.442514</td>\n",
" <td>0.854369</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.437183</td>\n",
" <td>0.426613</td>\n",
" <td>0.863704</td>\n",
" <td>01:24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"id": "49Vla0gv3wD2",
"outputId": "105b1110-8f5a-4d12-b712-edba9066ce83"
},
"source": [
"preds,targs = learn.tta()\n",
"accuracy(preds, targs).item()"
],
"execution_count": 13,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
" <div>\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-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
" </style>\n",
" <progress value='0' class='' max='5' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" \n",
" </div>\n",
" \n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "display_data",
"data": {
"text/html": [
""
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.8752800822257996"
]
},
"metadata": {
"tags": []
},
"execution_count": 13
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment