{"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":"# Cancer Detection\n## Class Activation Maps","metadata":{}},{"cell_type":"markdown","source":"## Load Package","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\nimport cv2\nfrom tqdm import tqdm \nimport matplotlib as mpl\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 ","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:54:57.370658Z","iopub.execute_input":"2021-12-10T19:54:57.371116Z","iopub.status.idle":"2021-12-10T19:55:05.508452Z","shell.execute_reply.started":"2021-12-10T19:54:57.370994Z","shell.execute_reply":"2021-12-10T19:55:05.507550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Dataframe","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/histopathologic-cancer-detection/train_labels.csv\", dtype=str)\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:05.509936Z","iopub.execute_input":"2021-12-10T19:55:05.510234Z","iopub.status.idle":"2021-12-10T19:55:06.235109Z","shell.execute_reply.started":"2021-12-10T19:55:05.510204Z","shell.execute_reply":"2021-12-10T19:55:06.234189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['id'] = train['id'].astype(str) + '.tif'\n","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:06.236429Z","iopub.execute_input":"2021-12-10T19:55:06.237328Z","iopub.status.idle":"2021-12-10T19:55:06.305104Z","shell.execute_reply.started":"2021-12-10T19:55:06.237286Z","shell.execute_reply":"2021-12-10T19:55:06.304026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:06.307025Z","iopub.execute_input":"2021-12-10T19:55:06.307272Z","iopub.status.idle":"2021-12-10T19:55:06.324885Z","shell.execute_reply.started":"2021-12-10T19:55:06.307243Z","shell.execute_reply":"2021-12-10T19:55:06.323951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## View Sample of Images","metadata":{}},{"cell_type":"code","source":"# Sample 16 images from the training set and display these along with their labels.\n\nplt.figure(figsize=(10,10)) # specifying the overall grid size\n\nfor i in range(16):\n    plt.subplot(4,4,i+1)    # the number of images in the grid is 6*6 (16)\n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}')\n    plt.imshow(img)\n    plt.text(0, -5, f'Label {train[\"label\"][i]}')\n    plt.axis('off')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:06.326309Z","iopub.execute_input":"2021-12-10T19:55:06.326563Z","iopub.status.idle":"2021-12-10T19:55:07.596293Z","shell.execute_reply.started":"2021-12-10T19:55:06.326532Z","shell.execute_reply":"2021-12-10T19:55:07.595180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Model","metadata":{}},{"cell_type":"code","source":"cnn = load_model('../input/cancer-detection-models/cancer_model.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:07.597405Z","iopub.execute_input":"2021-12-10T19:55:07.598088Z","iopub.status.idle":"2021-12-10T19:55:08.262126Z","shell.execute_reply.started":"2021-12-10T19:55:07.598050Z","shell.execute_reply":"2021-12-10T19:55:08.260994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Heatmap Functions","metadata":{}},{"cell_type":"code","source":"def create_grad_model(model):\n    for layer in reversed(model.layers):\n        if len(layer.output_shape) == 4:\n            last_conv_layer = layer.name\n            break\n\n    grad_model = tf.keras.models.Model(\n        inputs=[model.inputs],\n        outputs=[model.get_layer(last_conv_layer).output, model.output])\n    \n    return grad_model \n\ndef compute_heatmap(image, class_ix, grad_model):\n\n    with tf.GradientTape() as tape:\n        inputs = tf.cast(image, tf.float32)\n        (conv_outputs, predictions) = grad_model(inputs)\n        loss = predictions[:, class_ix]\n    grads = tape.gradient(loss, conv_outputs)\n\n    cast_conv_outputs = tf.cast(conv_outputs > 0, \"float32\")\n    cast_grads = tf.cast(grads > 0, \"float32\")\n    guided_grads = cast_conv_outputs * cast_grads * grads\n\n    conv_outputs = conv_outputs[0]\n    guided_grads = guided_grads[0]\n\n    weights = tf.reduce_mean(guided_grads, axis=(0, 1))\n\n    cam = tf.reduce_sum(tf.multiply(weights, conv_outputs), axis=-1)\n\n    (w, h) = (image.shape[2], image.shape[1])\n    heatmap = cv2.resize(cam.numpy(), (w, h))\n        \n    return heatmap","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:08.263564Z","iopub.execute_input":"2021-12-10T19:55:08.263897Z","iopub.status.idle":"2021-12-10T19:55:08.275606Z","shell.execute_reply.started":"2021-12-10T19:55:08.263832Z","shell.execute_reply":"2021-12-10T19:55:08.274838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Heatmaps","metadata":{}},{"cell_type":"code","source":"# Create Gradient Model\ngm = create_grad_model(cnn)\n\n# Select Image and Create Heatmap\n# f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}.tif'\nfilename = train[\"id\"][0]\n# img = mpimg.imread(f'train/{filename}')\nimg = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][0]}')   \nimg = cv2.resize(img, (32, 32))\ntensor = img.reshape(1,32,32,3) / 255\n\n# img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{filename}')\n# tensor = img.reshape((1,) + img.shape) / 255\nheatmap = compute_heatmap(tensor, 1, gm)\n\nplt.figure(figsize=[9,3])\n\n# Display Image\nplt.subplot(1,3,1)\nplt.imshow(img)\nplt.axis('off')\n\n# Display Heatmap\nplt.subplot(1,3,2)\nplt.imshow(heatmap, cmap='coolwarm')\nplt.axis('off')\n\n# Display Image and Heatmap Together\nplt.subplot(1,3,3)\nplt.imshow(img, alpha=0.8, cmap='binary_r')\nplt.imshow(heatmap, alpha=0.6, cmap='coolwarm')\nplt.axis('off')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:55:08.277210Z","iopub.execute_input":"2021-12-10T19:55:08.277449Z","iopub.status.idle":"2021-12-10T19:55:09.066836Z","shell.execute_reply.started":"2021-12-10T19:55:08.277418Z","shell.execute_reply":"2021-12-10T19:55:09.065840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Multiple Heatmaps","metadata":{}},{"cell_type":"code","source":"def get_heatmap_dist(df, class_ix, gm):\n\n    values = None\n    for i, row in tqdm(df.iterrows()):\n#         f'../input/histopathologic-cancer-detection/train/{train[\"id\"][0]}'\n#         img = mpimg.imread(f'train/{row.id}')    \n        img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}')   \n        img = cv2.resize(img, (32, 32))\n        tensor = img.reshape(1,32,32,3) / 255\n        hm = compute_heatmap(tensor, class_ix, gm)\n\n        if values is None:\n            values = hm.flatten()\n        else:\n            values = np.hstack([values, hm.flatten()])\n\n    return values","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:56:03.372206Z","iopub.execute_input":"2021-12-10T19:56:03.372479Z","iopub.status.idle":"2021-12-10T19:56:03.382107Z","shell.execute_reply.started":"2021-12-10T19:56:03.372450Z","shell.execute_reply":"2021-12-10T19:56:03.381024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = get_heatmap_dist(train.sample(1000, random_state=1), 1, gm)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:56:05.914016Z","iopub.execute_input":"2021-12-10T19:56:05.914276Z","iopub.status.idle":"2021-12-10T19:56:37.336785Z","shell.execute_reply.started":"2021-12-10T19:56:05.914250Z","shell.execute_reply":"2021-12-10T19:56:37.335870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# setting a high end and low end of the color\nlow = np.quantile(values, 0.10)\nhigh = np.quantile(values, 0.96)\n\nnorm = mpl.colors.Normalize(vmin=low, vmax=high)\n\nprint(low)\nprint(high)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:56:37.338378Z","iopub.execute_input":"2021-12-10T19:56:37.338600Z","iopub.status.idle":"2021-12-10T19:56:37.358900Z","shell.execute_reply.started":"2021-12-10T19:56:37.338572Z","shell.execute_reply":"2021-12-10T19:56:37.358034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select which images to display\nindices = range(12)\n\nfor i in indices:  \n    row = train.iloc[i,:]\n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}')    \n    label = row.label\n    img = cv2.resize(img, (32, 32))\n    tensor = img.reshape(1,32,32,3) / 255\n    heatmap = compute_heatmap(tensor, 1, gm)\n\n    if(label == '1'):\n        print('Cancer Present')\n    else:\n        print('No Cancer')\n    \n    plt.figure(figsize=[9,3])\n\n    plt.subplot(1,3,1)\n    plt.imshow(img)\n    plt.axis('off')\n\n    plt.subplot(1,3,2)\n    plt.imshow(heatmap, cmap='coolwarm', norm=norm)\n    plt.axis('off')\n\n    plt.subplot(1,3,3)\n    plt.imshow(img, alpha=0.6, cmap='binary_r')\n    plt.imshow(heatmap, alpha=0.6, cmap='coolwarm', norm=norm)\n    plt.axis('off')\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T19:57:01.098367Z","iopub.execute_input":"2021-12-10T19:57:01.098645Z","iopub.status.idle":"2021-12-10T19:57:04.026825Z","shell.execute_reply.started":"2021-12-10T19:57:01.098614Z","shell.execute_reply":"2021-12-10T19:57:04.026015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}