{"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":"# Load Packages","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\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-05T17:47:41.046581Z","iopub.execute_input":"2021-12-05T17:47:41.046977Z","iopub.status.idle":"2021-12-05T17:47:49.137142Z","shell.execute_reply.started":"2021-12-05T17:47:41.046858Z","shell.execute_reply":"2021-12-05T17:47:49.136326Z"},"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-05T17:48:03.413289Z","iopub.execute_input":"2021-12-05T17:48:03.4142Z","iopub.status.idle":"2021-12-05T17:48:04.05775Z","shell.execute_reply.started":"2021-12-05T17:48:03.414158Z","shell.execute_reply":"2021-12-05T17:48:04.056963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:48:06.753478Z","iopub.execute_input":"2021-12-05T17:48:06.754409Z","iopub.status.idle":"2021-12-05T17:48:06.781811Z","shell.execute_reply.started":"2021-12-05T17:48:06.754353Z","shell.execute_reply":"2021-12-05T17:48:06.781046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Distribution","metadata":{}},{"cell_type":"code","source":"y_train = train.label\n\n(train.label.value_counts() / len(train)).to_frame().T","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:48:09.259744Z","iopub.execute_input":"2021-12-05T17:48:09.260264Z","iopub.status.idle":"2021-12-05T17:48:09.305393Z","shell.execute_reply.started":"2021-12-05T17:48:09.260217Z","shell.execute_reply":"2021-12-05T17:48:09.303612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# View Sample of Images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10)) \n\nfor i in range(16):\n    plt.subplot(4,4,i+1)   \n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}.tif')\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-05T17:48:13.008729Z","iopub.execute_input":"2021-12-05T17:48:13.01012Z","iopub.status.idle":"2021-12-05T17:48:14.256658Z","shell.execute_reply.started":"2021-12-05T17:48:13.010058Z","shell.execute_reply":"2021-12-05T17:48:14.256008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"\n\n\ntrain['id'] = train['id'].apply(append_ext)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:48:18.50137Z","iopub.execute_input":"2021-12-05T17:48:18.501826Z","iopub.status.idle":"2021-12-05T17:48:18.601567Z","shell.execute_reply.started":"2021-12-05T17:48:18.501788Z","shell.execute_reply":"2021-12-05T17:48:18.6007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=45, stratify=train.label)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:50:19.887334Z","iopub.execute_input":"2021-12-05T17:50:19.887719Z","iopub.status.idle":"2021-12-05T17:50:20.31019Z","shell.execute_reply.started":"2021-12-05T17:50:19.887678Z","shell.execute_reply":"2021-12-05T17:50:20.30882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:50:21.981129Z","iopub.execute_input":"2021-12-05T17:50:21.981746Z","iopub.status.idle":"2021-12-05T17:50:21.986509Z","shell.execute_reply.started":"2021-12-05T17:50:21.981703Z","shell.execute_reply":"2021-12-05T17:50:21.98584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 128\n\ntrain_loader = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = '../input/histopathologic-cancer-detection/train/',\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (32,32)\n)\n\nvalid_loader = train_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = '../input/histopathologic-cancer-detection/train/',\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (32,32)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:50:25.397927Z","iopub.execute_input":"2021-12-05T17:50:25.399045Z","iopub.status.idle":"2021-12-05T17:58:49.671354Z","shell.execute_reply.started":"2021-12-05T17:50:25.398989Z","shell.execute_reply":"2021-12-05T17:58:49.670206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_loader)\nVA_STEPS = len(valid_loader)\n\nprint(TR_STEPS)\nprint(VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:58:52.806757Z","iopub.execute_input":"2021-12-05T17:58:52.807596Z","iopub.status.idle":"2021-12-05T17:58:52.813898Z","shell.execute_reply.started":"2021-12-05T17:58:52.807557Z","shell.execute_reply":"2021-12-05T17:58:52.812803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/kl-cancer/cancer_model_v02 (1).h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T17:59:03.001154Z","iopub.execute_input":"2021-12-05T17:59:03.001515Z","iopub.status.idle":"2021-12-05T17:59:03.595094Z","shell.execute_reply.started":"2021-12-05T17:59:03.001479Z","shell.execute_reply":"2021-12-05T17:59:03.594178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Heatmap Function","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-05T17:59:09.282837Z","iopub.execute_input":"2021-12-05T17:59:09.283191Z","iopub.status.idle":"2021-12-05T17:59:09.294908Z","shell.execute_reply.started":"2021-12-05T17:59:09.283155Z","shell.execute_reply":"2021-12-05T17:59:09.293653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# First Heatmap","metadata":{}},{"cell_type":"code","source":"# Create Gradient Model\ngm = create_grad_model(cnn)\n\n# Select Image and Create Heatmap\nfilename = train.id[0]\n#img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/'{filename})    \nimg = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{filename}')\ntensor = img.reshape(-1,32,32,3) / 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-05T18:00:02.84776Z","iopub.execute_input":"2021-12-05T18:00:02.848752Z","iopub.status.idle":"2021-12-05T18:00:03.435133Z","shell.execute_reply.started":"2021-12-05T18:00:02.848701Z","shell.execute_reply":"2021-12-05T18:00:03.434135Z"},"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        img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row.id}')    \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-05T18:01:04.07989Z","iopub.execute_input":"2021-12-05T18:01:04.080897Z","iopub.status.idle":"2021-12-05T18:01:04.088546Z","shell.execute_reply.started":"2021-12-05T18:01:04.080843Z","shell.execute_reply":"2021-12-05T18:01:04.087233Z"},"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-05T18:01:06.030842Z","iopub.execute_input":"2021-12-05T18:01:06.032041Z","iopub.status.idle":"2021-12-05T18:02:14.375435Z","shell.execute_reply.started":"2021-12-05T18:01:06.031978Z","shell.execute_reply":"2021-12-05T18:02:14.374902Z"},"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-05T18:02:28.338712Z","iopub.execute_input":"2021-12-05T18:02:28.339731Z","iopub.status.idle":"2021-12-05T18:02:28.36594Z","shell.execute_reply.started":"2021-12-05T18:02:28.339665Z","shell.execute_reply":"2021-12-05T18:02:28.365026Z"},"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,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-05T18:02:31.178783Z","iopub.execute_input":"2021-12-05T18:02:31.179333Z","iopub.status.idle":"2021-12-05T18:02:34.517817Z","shell.execute_reply.started":"2021-12-05T18:02:31.179279Z","shell.execute_reply":"2021-12-05T18:02:34.517012Z"},"trusted":true},"execution_count":null,"outputs":[]}]}