{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# Visualize Activations","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport pandas as pd\nimport pickle\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport zipfile \n","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:45:30.402627Z","iopub.execute_input":"2021-11-29T05:45:30.402921Z","iopub.status.idle":"2021-11-29T05:45:37.521529Z","shell.execute_reply.started":"2021-11-29T05:45:30.402862Z","shell.execute_reply":"2021-11-29T05:45:37.520639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Dataframe","metadata":{}},{"cell_type":"code","source":"train_full = pd.read_csv(\"../input/histopathologic-cancer-detection/train_labels.csv\", dtype=str)\nprint(train_full.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:46:09.164478Z","iopub.execute_input":"2021-11-29T05:46:09.164767Z","iopub.status.idle":"2021-11-29T05:46:09.654047Z","shell.execute_reply.started":"2021-11-29T05:46:09.164735Z","shell.execute_reply":"2021-11-29T05:46:09.652959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_full.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:46:21.709518Z","iopub.execute_input":"2021-11-29T05:46:21.709788Z","iopub.status.idle":"2021-11-29T05:46:21.729769Z","shell.execute_reply.started":"2021-11-29T05:46:21.709758Z","shell.execute_reply":"2021-11-29T05:46:21.728968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract Images","metadata":{}},{"cell_type":"code","source":"train_full.id = train_full.id + '.tif'\n\nprint(train_full.head())","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:46:26.688483Z","iopub.execute_input":"2021-11-29T05:46:26.689322Z","iopub.status.idle":"2021-11-29T05:46:26.7472Z","shell.execute_reply.started":"2021-11-29T05:46:26.689282Z","shell.execute_reply":"2021-11-29T05:46:26.746035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/histopathologic-cancer-detection/train/\"\nprint('Training Images:', len(os.listdir(train_path)))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:46:36.873369Z","iopub.execute_input":"2021-11-29T05:46:36.873648Z","iopub.status.idle":"2021-11-29T05:46:46.922429Z","shell.execute_reply.started":"2021-11-29T05:46:36.873619Z","shell.execute_reply":"2021-11-29T05:46:46.921626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training Images:', len(os.listdir('../input/histopathologic-cancer-detection/train/')))\n\nfor i in range(10):\n  img = plt.imread('../input/histopathologic-cancer-detection/train/' + train_full.id[i])\n  print('Images shape', img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:47:00.474496Z","iopub.execute_input":"2021-11-29T05:47:00.474836Z","iopub.status.idle":"2021-11-29T05:47:00.81094Z","shell.execute_reply.started":"2021-11-29T05:47:00.474799Z","shell.execute_reply":"2021-11-29T05:47:00.810206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# View Sample of Images","metadata":{}},{"cell_type":"code","source":"sample = train_full.sample(n=16).reset_index()\n\nplt.figure(figsize=(8,8))\n\nfor i, row in sample.iterrows():\n\n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row.id}')    \n    label = row.label\n\n    plt.subplot(4,4,i+1)\n    plt.imshow(img)\n    plt.text(0, -5, f'Class {label}', color='k')\n        \n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:48:46.314449Z","iopub.execute_input":"2021-11-29T05:48:46.31474Z","iopub.status.idle":"2021-11-29T05:48:47.492914Z","shell.execute_reply.started":"2021-11-29T05:48:46.314708Z","shell.execute_reply":"2021-11-29T05:48:47.491965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{}},{"cell_type":"code","source":"cnn = load_model('../input/cancer-detection-final2/cancer_model_v12.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:50:18.132039Z","iopub.execute_input":"2021-11-29T05:50:18.13232Z","iopub.status.idle":"2021-11-29T05:50:19.407697Z","shell.execute_reply.started":"2021-11-29T05:50:18.132292Z","shell.execute_reply":"2021-11-29T05:50:19.406907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample Images\nWe will visualize the activations for two images. These are displayd below.","metadata":{}},{"cell_type":"code","source":"row0 = train_full.iloc[6,:]\nimg0 = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row0.id}')    \n\nrow1 = train_full.iloc[4,:]\nimg1 = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row1.id}')    \n\nplt.subplot(1,2,1)\nplt.imshow(img0)\nplt.text(0, -2, 'Tumor', color='k')\nplt.axis('off')\n\nplt.subplot(1,2,2)\nplt.imshow(img1)\nplt.text(0, -2, 'No tumor', color='k')\nplt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:30.270267Z","iopub.execute_input":"2021-11-29T05:57:30.270557Z","iopub.status.idle":"2021-11-29T05:57:30.476174Z","shell.execute_reply.started":"2021-11-29T05:57:30.270521Z","shell.execute_reply":"2021-11-29T05:57:30.475595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In order to use the images in the functions below, we will need to reshape them into 4D arrays. We will also need to scale the pixel values.","metadata":{}},{"cell_type":"code","source":"tensor0 = img0.reshape(1,96,96,3)/255\ntensor1 = img1.reshape(1,96,96,3)/255","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:39.201842Z","iopub.execute_input":"2021-11-29T05:57:39.202851Z","iopub.status.idle":"2021-11-29T05:57:39.207615Z","shell.execute_reply.started":"2021-11-29T05:57:39.2028Z","shell.execute_reply":"2021-11-29T05:57:39.20704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Activation Visualization\nIn the cell below, we will create two functions for the purpose of visualizing the layer activations for an individual image. The display_layer() function is a helper function used to display a single layer of activations. The display_activations() function is the primary function. It will calculate and display the layer activations for each selected layer.","metadata":{}},{"cell_type":"code","source":"def display_layer(layer_index, activations, cmap):\n    layer_activations = activations[layer_index]\n    n_filters = layer_activations.shape[-1]\n       \n    n_cols = 8\n    n_rows = n_filters // n_cols\n    \n    print(f'{cnn.layers[layer_index].name} - {n_filters} Filters')\n    plt.figure(figsize=[2*n_cols, 2*n_rows])\n    \n    for i in range(n_filters):\n        img = layer_activations[0,:,:,i]\n        plt.subplot(n_rows, n_cols, i+1)\n        plt.imshow(img, cmap=cmap)\n        plt.axis('off')\n    plt.show() \n    \ndef display_activations(img_tensor, layer_indices=[], cmap='viridis'):\n    layer_outputs = [layer.output for layer in cnn.layers]\n    activation_model = tf.keras.models.Model(inputs=cnn.inputs, outputs=layer_outputs)\n    activations = activation_model(img_tensor)\n    \n    for i in layer_indices:\n        display_layer(i, activations, cmap)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:58:27.984371Z","iopub.execute_input":"2021-11-29T05:58:27.98518Z","iopub.status.idle":"2021-11-29T05:58:27.994306Z","shell.execute_reply.started":"2021-11-29T05:58:27.985118Z","shell.execute_reply":"2021-11-29T05:58:27.993349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_activations(tensor1, [0,1,2,5,6,7], cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:58:48.868308Z","iopub.execute_input":"2021-11-29T05:58:48.868604Z","iopub.status.idle":"2021-11-29T05:59:02.177486Z","shell.execute_reply.started":"2021-11-29T05:58:48.868573Z","shell.execute_reply":"2021-11-29T05:59:02.176658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_activations(tensor0, [0,1,2,5,6,7], cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:59:10.375325Z","iopub.execute_input":"2021-11-29T05:59:10.37594Z","iopub.status.idle":"2021-11-29T05:59:23.56332Z","shell.execute_reply.started":"2021-11-29T05:59:10.375878Z","shell.execute_reply":"2021-11-29T05:59:23.562461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}