{ "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", "\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 }