{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# L1 - ImageNet pretrained ResNets with STONEFLY9 dataset
Activations with hooks (work in progress)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook a ResNet34 trained on the [STONEFLY9 dataset](http://web.engr.oregonstate.edu/~tgd/bugid/stonefly9/) is used to visualize the activations of the network layers.
\n",
"The base notebook can be found here: https://github.com/MicPie/fastai_course_v3/blob/master/L1-stonefly.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notebook setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# notebook setup\n",
"%reload_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# import fastai library\n",
"from fastai import *\n",
"from fastai.vision import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data setup"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# set path to data directory\n",
"path = Path('/home/paperspace/fastai/data/stonefly')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"size = 224 # image size used for training (Note: is only applied when ds_tfms are set)\n",
"bs = 64 # batchsize for training"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dataframe setup"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# read in text file with a list of all the files\n",
"df = pd.read_csv(path/'files.txt', sep=' ', header=None, names=['imgurl'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# define string manipulation lambda function to get path after the defined path variable\n",
"f = lambda x: x.split('/', 6)[-1]\n",
"df['imgurl'] = df['imgurl'].apply(f)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# define string manipulation lambda function to get data class\n",
"f = lambda x: x.split('/')[-3]\n",
"df['class'] = df['imgurl'].apply(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup model with hooks without databunch"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[36m[0]\u001b[m \u001b[34mQuadro P4000 \u001b[m |\u001b[31m 31'C\u001b[m, \u001b[32m 0 %\u001b[m | \u001b[36m\u001b[1m\u001b[33m 10\u001b[m / \u001b[33m 8119\u001b[m MB |\r\n"
]
}
],
"source": [
"!gpustat --no-header"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# setup classes list\n",
"classes = ['cal', 'dor', 'hes', 'iso', 'mos', 'pte', 'swe', 'yor', 'zap']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# create data bunch for prediction\n",
"data = ImageDataBunch.single_from_classes(path, classes, tfms=get_transforms(), size=size).normalize(imagenet_stats)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# create learner\n",
"learn = create_cnn(data, models.resnet34, metrics=accuracy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example from https://forums.fast.ai/t/share-your-work-here/27676/234:
\n",
"`last_layer = flatten_model(learn.model)[-3]\n",
"hook = hook_output(last_layer)\n",
"learn.model.eval()\n",
"n_valid = len(data.valid_ds.ds.y)\n",
"for i in range(n_valid):\n",
" img,label = data.valid_dl.dl.dataset[i]\n",
" img = apply_tfms(learn.data.valid_ds.tfms, img, **learn.data.valid_ds.kwargs)\n",
" ds = TensorDataset(img.data[None], torch.zeros(1))\n",
" dl = DeviceDataLoader.create(ds, bs=1, shuffle=False, device=learn.data.device, tfms=learn.data.valid_dl.tfms,\n",
" num_workers=0)\n",
" pred = learn.model(dl.one_batch()[0])\n",
" if i % 1000 == 0:\n",
" print(f'{i/n_valid*100:.2f}% ready')\n",
" if i == 0 :\n",
" acts = hook.stored \n",
" else : acts = torch.cat((acts,hook.stored), dim=0`"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# flatten model to use layer list for hooks\n",
"layers = flatten_model(learn.model)# to set hook for a subset of layers use flatten_model(learn.model)[subset]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# number of layers used for the hook (see next cell)\n",
"len(layers)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"custom_hook = hook_outputs(layers) #hoot_output (without 's') for single hook"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# set model to evaluation mode\n",
"learn.model.eval();"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# load trained weights\n",
"learn.load('stonefly_resnet34_stage_2')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[36m[0]\u001b[m \u001b[34mQuadro P4000 \u001b[m |\u001b[31m 32'C\u001b[m, \u001b[32m 11 %\u001b[m | \u001b[36m\u001b[1m\u001b[33m 701\u001b[m / \u001b[33m 8119\u001b[m MB | \u001b[1m\u001b[30mpaperspace\u001b[m(\u001b[33m691M\u001b[m)\r\n"
]
}
],
"source": [
"!gpustat --no-header"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load image for prediction"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# set image number for prediction\n",
"img_num = 3036"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PosixPath('/home/paperspace/fastai/data/stonefly/JPEG_lowRes/yor/set1/Yor_103-specimen-i004-s125.jpg')"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get image path\n",
"img_path = path/df.iloc[img_num,0]; img_path"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# show image with matplotlib\n",
"#plt.imshow(plt.imread(img_path))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"# open image from image path\n",
"img = open_image(img_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prediction"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"