File size: 24,693 Bytes
817b731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85731c50-1a96-41d5-8b7b-2d7c09ef932a",
   "metadata": {
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# 6.1. Activation Maps \n",
    "\n",
    "This tutorial showcases how to obtain kernels' responses (also known as activation maps) at different depths of a deep neural network. We study that in pretrained networks trained on ImageNet.\n",
    "\n",
    "In neuroscience, we have learnt a lot from measuring the response of a neuron when the organism is exposed to specific stimuli. The neural coding technique can reveal the relationship between the stimulus and the individual or ensemble neuronal responses. For instance, the seminal study of Hubel and Wiesel in which oriented bars were shown to cats and the response of V1 neurons was recorded using implanted electrodes.\n",
    "\n",
    "<img src=\"https://www.researchgate.net/publication/337360115/figure/fig4/AS:826941796012034@1574169696239/Left-Experimental-setup-from-Hubel-Wiesel-136-137-adapted-from-253-Chapter-11.ppm\"  width=\"600\" height=\"200\">\n",
    "\n",
    "\n",
    "A similar technique can be used in artificial neurons to learn more about the representation a network has learnt during its training. **Obtaining response maps is much easier in deep networks**:\n",
    "* we can show them numerous stimuli,\n",
    "* we have access to all units.\n",
    "\n",
    "Example articles that use this technique:\n",
    "* [Contrast sensitivity functions in autoencoders](https://jov.arvojournals.org/article.aspx?articleid=2778843)\n",
    "* [Color for object recognition: Hue and chroma sensitivity in the deep features of convolutional neural networks](https://www.sciencedirect.com/science/article/pii/S004269892100016X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7bc678e-be89-4cb6-b6ab-9b5589e44e2a",
   "metadata": {},
   "source": [
    "## 0. Packages\n",
    "\n",
    "Let's start with all the necessary packages to implement this tutorial.\n",
    "\n",
    " * [numpy](https://numpy.org/) is the main package for scientific computing with Python. It's often imported with the `np` shortcut.\n",
    " * [matplotlib](https://matplotlib.org/) is a library to plot graphs in Python.\n",
    " * [os](https://docs.python.org/3/library/os.html) provides a portable way of using operating system-dependent functionality, e.g., modifying files/folders.\n",
    " * [requests](https://requests.readthedocs.io/en/latest/) is a package that collects several modules for working with URLs.\n",
    " * [PIL](https://pillow.readthedocs.io/en/stable/) is a fast image processing designed for general applications.\n",
    " * [torch](https://pytorch.org/docs/stable/index.html) is a deep learning framework that allows us to define networks, handle datasets, optimise a loss function, etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ef2433b-f113-44cb-a064-757a6e5cd82c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# importing the necessary packages/libraries\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "import random\n",
    "import os\n",
    "import requests\n",
    "\n",
    "from PIL import Image as pil_image\n",
    "\n",
    "import torch\n",
    "import torchvision\n",
    "from torchvision import models\n",
    "import torchvision.transforms as torch_transforms\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy.misc import face\n",
    "from scipy.ndimage import zoom\n",
    "from scipy.special import logsumexp\n",
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d690a0-b4b5-4fdf-87d3-8b47e7efdf01",
   "metadata": {},
   "source": [
    "### device\n",
    "Choosing CPU or GPU based on the availability of the hardware."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "086aa636-9994-4c62-a792-907ac132a3b5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ba9f7d3-f53b-4879-a751-9ae3210a7c67",
   "metadata": {},
   "source": [
    "## 1. Stimuli\n",
    "\n",
    "In this tutorial, we use natural images as our stimuli, but one can show artificial networks any type of stimuli.\n",
    "\n",
    "We load four images from the internet and show them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9046c5b6-9681-4bd3-9539-35d3bc68c748",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list of image URLs\n",
    "urls = [\n",
    "    'https://github.com/pytorch/hub/raw/master/images/dog.jpg',\n",
    "    'http://farm4.staticflickr.com/3418/3368475823_74f3d3e9f9_z.jpg', \n",
    "    'http://farm3.staticflickr.com/2431/3908162999_5b5cd7c1b7_z.jpg',\n",
    "    'http://farm8.staticflickr.com/7069/6903096875_042efb5ee7_z.jpg'\n",
    "]\n",
    "\n",
    "# Openning the image and visualising it\n",
    "input_images = [pil_image.open(requests.get(url, stream=True).raw) for url in urls]\n",
    "\n",
    "fig = plt.figure(figsize=(12, 3))\n",
    "fig.suptitle('Original images', size=22)\n",
    "for img_ind, img in enumerate(input_images):\n",
    "    ax = fig.add_subplot(1, 4, img_ind+1)\n",
    "    ax.imshow(img)\n",
    "    ax.axis('off')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1509784a-cf03-44c3-82e8-f8489f3922fb",
   "metadata": {},
   "source": [
    "### Torch Tensors\n",
    "\n",
    "Before inputting a network with our images we:\n",
    "1. **resize** them to what the image size that the pretrained network was trained on,\n",
    "2. **normalise** them to the range of values that the pretrained network was trained on.\n",
    "\n",
    "While these two steps are not strictly speaking mandatory, it's sensible to measure the response of kernels under similar conditions that the network is meant to function.\n",
    "\n",
    "In the end, we visualise the tensor images (after inverting the normalisation). This is often a good exercise to do to ensure what we show to networks is correct."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d1f43eb-3833-474c-8dbe-ca4cb44f7430",
   "metadata": {},
   "outputs": [],
   "source": [
    "# the training size of ImageNet pretrained networks\n",
    "target_size = 224\n",
    "# mean and std values of ImageNet pretrained networks\n",
    "mean = [0.485, 0.456, 0.406]\n",
    "std = [0.229, 0.224, 0.225]\n",
    "\n",
    "# the list of transformation functions\n",
    "transforsm = torch_transforms.Compose([\n",
    "    torch_transforms.Resize((target_size, target_size)),\n",
    "    torch_transforms.ToTensor(),\n",
    "    torch_transforms.Normalize(mean=mean, std=std)\n",
    "])\n",
    "\n",
    "torch_imgs = torch.stack([transforsm(img) for img in input_images])\n",
    "print(\"Input tensor shape:\", torch_imgs.shape)\n",
    "\n",
    "# visualising the torch images\n",
    "fig = plt.figure(figsize=(12, 3))\n",
    "fig.suptitle('Resized images', size=22)\n",
    "for img_ind, img in enumerate(torch_imgs):\n",
    "    ax = fig.add_subplot(1, 4, img_ind+1)\n",
    "    # inversing the normalisation by multiplying to std and adding mean\n",
    "    ax.imshow(img.numpy().transpose(1, 2, 0) * std + mean)\n",
    "    ax.axis('off')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "602897ce-33c4-4f37-93a9-fffccbdf9fc8",
   "metadata": {},
   "source": [
    "## 2. Network\n",
    "\n",
    "We use [torchvision.models](https://pytorch.org/vision/stable/models.html) to obtain networks pretrained on ImageNet.\n",
    "\n",
    "In this tutorial, we focus on the `ResNet` architectures, but the logic of computing activation maps remains the same for all other convolutional neural networks (CNN).\n",
    "\n",
    "Remember that you must call `network.eval()` to set dropout and batch normalisation layers to evaluation mode before recording activation maps. Failing to do this will yield inconsistent results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f64574d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import deepgaze_pytorch\n",
    "\n",
    "# you can use DeepGazeI or DeepGazeIIE\n",
    "network = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e385a0d-65db-4092-82f8-96c2bb7da079",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# network = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1).to(device)\n",
    "network = models.resnet50(pretrained=True).to(device)\n",
    "#network = models.vgg16(pretrained=True).to(device)\n",
    "network.eval()\n",
    "\n",
    "# getting the name of layers, which we use later to obtain their activation maps\n",
    "layer_names = [key for key, _ in network.named_children()]\n",
    "print(layer_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a91cf2a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0b53fdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "for child_module in network.children():\n",
    "    print(child_module)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f5c5f96-8432-4582-8a24-c1e533d8cfbc",
   "metadata": {},
   "source": [
    "## 3. Forward Hooks\n",
    "\n",
    "PyTorch allows inspecting/modifying the output and gradient output of a layer using **hooks**. In this tutorial, we use forward hooks to inspect the output of a layer. To do so, we call the `register_forward_hook` for every layer that we want to inspect:\n",
    "* `register_forward_hook` registers a forward hook on the module.\n",
    "* The hook will be called every time after `forward()` has computed an output.\n",
    "* It returns a handle that can be used to remove the added hook by calling `handle.remove()`.\n",
    "\n",
    "The hook should have the following signature:\n",
    "\n",
    "    hook(module, args, output) -> None or modified output\n",
    "\n",
    "In our example, we have implemented this in the `activation` function that stores the outputs in a `dict`.\n",
    "\n",
    "In our example, we only access the first tier of layers/modules that are accessible by\n",
    "\n",
    "    layer = getattr(model, layer_name)\n",
    "\n",
    "Essentially, we can only access one of these ten layers:\n",
    "\n",
    "    ['conv1', 'bn1', 'relu', 'maxpool', 'layer1', 'layer2', 'layer3', 'layer4', 'avgpool', 'fc']\n",
    "\n",
    "However, many times a network consists of several nested modules. Often, we also want to record the activation maps of those layer. For instance, `layer1-4` of `ResNet` contains several residual blocks. To access those nested layers:\n",
    "* Access the parent module, for example, by calling the `getattr` module or simply by getting the correct index from the list of `network.children()`.\n",
    "* Iterate this process until the layer is directly accessible and it's not nested within another module.\n",
    "\n",
    "**Excercie**: create hooks for residual blocks of ResNet or hidden layers of any other network you're interested to explore."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98621060-9343-4a89-9888-20f9987f7cc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def activation(name, acts_dict):\n",
    "    \"\"\"Storing the output of a module/layer in the dict that is passed as an argument.\"\"\"\n",
    "    def hook(model, input_x, output_y):\n",
    "        acts_dict[name] = output_y.detach()\n",
    "    return hook\n",
    "'''\n",
    "\n",
    "def activation(name, acts_dict):\n",
    "    def hook(model, input_x, output_y):\n",
    "        if isinstance(output_y, list):\n",
    "            acts_dict[name] = [item.detach() for item in output_y]\n",
    "        else:\n",
    "            acts_dict[name] = output_y.detach()\n",
    "    return hook\n",
    "'''\n",
    "\n",
    "def create_hooks(model, layers):\n",
    "    \"\"\"For the given model it creates a hook for all specified layers.\"\"\"\n",
    "    acts_dict = dict()\n",
    "    hooks = dict()\n",
    "    for layer_name in layers:\n",
    "        # accessing the layer/module from the network\n",
    "        layer = getattr(model, layer_name)\n",
    "        hooks[layer_name] = layer.register_forward_hook(activation(layer_name, acts_dict))\n",
    "    return acts_dict, hooks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e4fa386-10d0-4cbc-8cbf-2d03c1ce840d",
   "metadata": {},
   "source": [
    "Next we create the hooks for three layers `['maxpool', 'layer2', 'fc']` of our network. The activation maps are filled in when we call the `forward` function of the networks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0b64d80-3535-420b-8130-dc926f8555d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#acts_dict, hooks = create_hooks(network, ['maxpool', 'layer2', 'fc'])\n",
    "#acts_dict, hooks = create_hooks(network, ['features', 'saliency_networks', 'scanpath_networks', 'fixation_selection_networks', 'finalizers'])\n",
    "acts_dict, hooks = create_hooks(network, ['0', '1', '2', '3'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03394a89-8257-4887-ae6f-ad9283ad0d55",
   "metadata": {},
   "source": [
    "## 4. Forward call\n",
    "\n",
    "Next, we simply input the `network` with our input stimuli `torch_imgs`. **Note**: we're not really interested in the output of the network per see, but rather the activation maps that are filled in the `acts_dict` after the `forward` call.\n",
    "\n",
    "**Important** If you call the network once again with a new set of stimuli, the data in `acts_dict` gets overwritten. Therefore, if you need that information you have to make a copy of then in another variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3323097",
   "metadata": {},
   "outputs": [],
   "source": [
    "acts_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95b19d91",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_tensor[1:].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d241867",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy.misc import face\n",
    "from scipy.ndimage import zoom\n",
    "from scipy.special import logsumexp\n",
    "import torch\n",
    "\n",
    "import deepgaze_pytorch\n",
    "\n",
    "DEVICE = 'cuda'\n",
    "\n",
    "# you can use DeepGazeI or DeepGazeIIE\n",
    "model = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(DEVICE)\n",
    "\n",
    "image = face()\n",
    "\n",
    "# load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n",
    "# you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n",
    "# alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n",
    "centerbias_template = np.load('centerbias_mit1003.npy')\n",
    "# rescale to match image size\n",
    "centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n",
    "# renormalize log density\n",
    "centerbias -= logsumexp(centerbias)\n",
    "\n",
    "image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n",
    "centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n",
    "\n",
    "log_density_prediction = model(image_tensor, centerbias_tensor)\n",
    "\n",
    "f, axs = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))\n",
    "axs[0].imshow(image)\n",
    "#axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n",
    "#axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n",
    "axs[0].set_axis_off()\n",
    "axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0])  # first image in batch, first (and only) channel\n",
    "#axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n",
    "#axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n",
    "axs[1].set_axis_off()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a3a04dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b4a43e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_tensor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9a5132e",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = network(image_tensor, centerbias_tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a992117-64b0-4978-bb1d-66a40ad6d512",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n",
    "# you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n",
    "# alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n",
    "centerbias_template = np.load('centerbias_mit1003.npy')\n",
    "# rescale to match image size\n",
    "centerbias = zoom(centerbias_template, (torch_imgs.shape[2]/centerbias_template.shape[0], torch_imgs.shape[3]/centerbias_template.shape[1]), order=0, mode='nearest')\n",
    "# renormalize log density\n",
    "centerbias -= logsumexp(centerbias)\n",
    "\n",
    "image_tensor = torch_imgs.to(device)\n",
    "centerbias_tensor = torch.tensor([centerbias]).to(device)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65c0e4ea-5e0f-4179-89d0-d81e22d04346",
   "metadata": {},
   "source": [
    "## 5. Visualisation\n",
    "\n",
    "Let's look at the obtained activation maps. First, we just print the shape of obtained activation maps:\n",
    "* The first dimension for all examined layers is 4 corresponding to the number of images.\n",
    "* The second dimension corresponds to the number of kernels (e.g., `maxpool` has 64 kernels).\n",
    "* The third and fourth are the spatial resolution (note that the `fc` layer doesn't have any spatial content)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b847609-1c36-411d-861e-f69f01c009b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for layer_name, layer_acts in acts_dict.items():\n",
    "    print(layer_name, layer_acts.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "539b380e-f6b5-4c22-9a46-c0e1082e8e7b",
   "metadata": {},
   "source": [
    "### Early layer\n",
    "\n",
    "Next, we visualise the activation maps of the `maxpool` layer to have some intuitions about the obtained responses. We can observe excitation by low-level features such as:\n",
    "* Some of the kernels get activated by certain colours.\n",
    "* Other kernels get activated by edges.\n",
    "\n",
    "The `maxpool` corresponds to an early layer of ResNet50, therefore the features that kernels are responsive to are also basic features."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db94aca1-1ca9-49ad-acfd-9fe296523a37",
   "metadata": {},
   "outputs": [],
   "source": [
    "layer_act = acts_dict['maxpool']\n",
    "for img_ind, layer_acts in enumerate(layer_act):\n",
    "    fig = plt.figure(figsize=(18, 7))\n",
    "    fig.suptitle('Image %d' % img_ind, size=22)\n",
    "    cols = 16\n",
    "    rows = layer_acts.shape[0] // cols\n",
    "    for kernel_ind, kernel_act in enumerate(layer_acts.detach().cpu()):\n",
    "        ax = fig.add_subplot(rows, cols, kernel_ind+1)\n",
    "        ax.matshow(kernel_act, cmap='gray')\n",
    "        ax.set_title('K=%.3d' % kernel_ind)\n",
    "        ax.axis('off')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf6133a-e313-437c-8b97-f182a00a2266",
   "metadata": {},
   "source": [
    "### Deeper layer\n",
    "\n",
    "Let's visualise the activation maps of a deeper layer `layer2` for one of the images. Overall, we can observe that the activation maps are not as simple as an earlier layer. Kernels in `layer2` respond to different patterns and textures. The set of features that excite a kernel becomes more complex as we go deeper into a network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3710d63-fbf9-4d22-af87-a0856bae5bbc",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "layer_act = acts_dict['layer2']\n",
    "for img_ind, layer_acts in enumerate(layer_act):\n",
    "    fig = plt.figure(figsize=(18, 56))\n",
    "    cols = 16\n",
    "    rows = layer_acts.shape[0] // cols\n",
    "    for kernel_ind, kernel_act in enumerate(layer_acts.detach().cpu()):\n",
    "        ax = fig.add_subplot(rows, cols, kernel_ind+1)\n",
    "        ax.matshow(kernel_act, cmap='gray')\n",
    "        ax.set_title('K=%.3d' % kernel_ind)\n",
    "        ax.axis('off')\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c05c91e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "145cb27b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "989e0490",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14c4a856",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c21ab83e-7ea3-46b2-9889-fdbfeaca205a",
   "metadata": {},
   "source": [
    "## 6. Systematic experiments\n",
    "\n",
    "While visualisation of activation maps for natural images is interesting and results in some intuition about the underlying representation of deep networks, it's more informative to check the activation maps with respect to a specific feature (i.e., dependent variable) and keep all other parameters fixed (i.e., independent variables).\n",
    "\n",
    "**Quantitative** assessment: the activation maps we visualised above contain spatial resolution. To report the degree of excitation one could use different techniques such as:\n",
    "* Statistics of the entire activation map (e.g., mean or median).\n",
    "* Statistics on pixels corresponding to the foreground stimuli. This is only possible if we have a segmentation map of input pixels (e.g., in the case of generated stimuli).\n",
    "* Thresholding the activation maps and counting the number of pixels that meet this criterion.\n",
    "\n",
    "\n",
    "**Questions to explore**: recording the activation map is a powerful technique and one can perform many interesting experiments with it. Below are a few example exercises:\n",
    "1. **Colour**\n",
    "   * Plot a circle in the middle of a grey background.\n",
    "   * Change the colour of the circle systematically across the hue spectrum.\n",
    "   * Measure the activation of kernels.\n",
    "   * Are there kernels that get highly excited in the presence of certain colours?\n",
    "   * Are colour kernels more present in early or deeper layers?\n",
    "2. **Size**\n",
    "   * Plot different geometrical shapes in the middle of a grey background.\n",
    "   * Change the size of those shapes systematically.\n",
    "   * Measure the activation of kernels.\n",
    "   * Plot the level of activation as a function of stimuli size.\n",
    "   * Do artificial kernels show a preference for a certain size?\n",
    "   * Does The size preference of kernels change as a function of layer?"
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}