{"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":"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 import keras\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-04T23:53:15.786567Z","iopub.execute_input":"2021-12-04T23:53:15.786880Z","iopub.status.idle":"2021-12-04T23:53:15.793869Z","shell.execute_reply.started":"2021-12-04T23:53:15.786847Z","shell.execute_reply":"2021-12-04T23:53:15.792085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(f'../input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\nprint('Training Set Size:', train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T23:52:31.543141Z","iopub.execute_input":"2021-12-04T23:52:31.543383Z","iopub.status.idle":"2021-12-04T23:52:32.120784Z","shell.execute_reply.started":"2021-12-04T23:52:31.543355Z","shell.execute_reply":"2021-12-04T23:52:32.119811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['id'] = train['id'].apply(lambda x: f'{x}.tif')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T23:52:32.122020Z","iopub.execute_input":"2021-12-04T23:52:32.122230Z","iopub.status.idle":"2021-12-04T23:52:32.199864Z","shell.execute_reply.started":"2021-12-04T23:52:32.122203Z","shell.execute_reply":"2021-12-04T23:52:32.199009Z"},"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)))\n\nsample = train.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-12-04T23:52:55.743046Z","iopub.execute_input":"2021-12-04T23:52:55.743735Z","iopub.status.idle":"2021-12-04T23:53:01.118915Z","shell.execute_reply.started":"2021-12-04T23:52:55.743690Z","shell.execute_reply":"2021-12-04T23:53:01.118216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancer-models/cancer_model_v01.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T00:03:05.795022Z","iopub.execute_input":"2021-12-05T00:03:05.795423Z","iopub.status.idle":"2021-12-05T00:03:07.039206Z","shell.execute_reply.started":"2021-12-05T00:03:05.795394Z","shell.execute_reply":"2021-12-05T00:03:07.038292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn2 = keras.models.load_model('../input/cancer-models/cancer_model_v02_ResNet50V2.h5')\ncnn2.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T00:03:07.040694Z","iopub.execute_input":"2021-12-05T00:03:07.040927Z","iopub.status.idle":"2021-12-05T00:03:11.314681Z","shell.execute_reply.started":"2021-12-05T00:03:07.040898Z","shell.execute_reply":"2021-12-05T00:03:11.313685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-04T23:57:38.370611Z","iopub.execute_input":"2021-12-04T23:57:38.370930Z","iopub.status.idle":"2021-12-04T23:57:38.380142Z","shell.execute_reply.started":"2021-12-04T23:57:38.370887Z","shell.execute_reply":"2021-12-04T23:57:38.379147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gm = create_grad_model(cnn)\n# g2m = create_grad_model(cnn2)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T00:05:13.172175Z","iopub.execute_input":"2021-12-05T00:05:13.172447Z","iopub.status.idle":"2021-12-05T00:05:13.181830Z","shell.execute_reply.started":"2021-12-05T00:05:13.172418Z","shell.execute_reply":"2021-12-05T00:05:13.181082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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        img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row.id}')    \n        tensor = img.reshape(1,96,96,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-04T23:59:41.894927Z","iopub.execute_input":"2021-12-04T23:59:41.895262Z","iopub.status.idle":"2021-12-04T23:59:41.903764Z","shell.execute_reply.started":"2021-12-04T23:59:41.895229Z","shell.execute_reply":"2021-12-04T23:59:41.903064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = get_heatmap_dist(train.sample(1000, random_state=1), 1, gm)\n# values2 = get_heatmap_dist(train.sample(1000, random_state=1), 1, gm2)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T23:59:42.802421Z","iopub.execute_input":"2021-12-04T23:59:42.802658Z","iopub.status.idle":"2021-12-05T00:01:15.486049Z","shell.execute_reply.started":"2021-12-04T23:59:42.802633Z","shell.execute_reply":"2021-12-05T00:01:15.484953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low = 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-05T00:01:29.437470Z","iopub.execute_input":"2021-12-05T00:01:29.437737Z","iopub.status.idle":"2021-12-05T00:01:29.555920Z","shell.execute_reply.started":"2021-12-05T00:01:29.437707Z","shell.execute_reply":"2021-12-05T00:01:29.554864Z"},"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/{row.id}')    \n    label = row.label\n    \n    tensor = img.reshape(-1,96,96,3) / 255\n    heatmap = compute_heatmap(tensor, 1, gm)\n\n    if(label == '1'):\n        print('Malignant (Has Cancer)')\n    else:\n        print('Benign (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-05T00:01:32.362396Z","iopub.execute_input":"2021-12-05T00:01:32.362687Z","iopub.status.idle":"2021-12-05T00:01:36.147313Z","shell.execute_reply.started":"2021-12-05T00:01:32.362657Z","shell.execute_reply":"2021-12-05T00:01:36.146217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}