<jupyter_start><jupyter_text>Preparing Working Env<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline

from importlib.util import find_spec
if find_spec("core") is None:
    import sys
    sys.path.append('..')

import tensorflow as tf
import tensorflow_datasets as tfds

tfds.disable_progress_bar()
tf.config.run_functions_eagerly(True)
#This code snippet helps if your computer has RTX 2070 GPU. If not then comment this cell.
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
import tf_explain
from core.networks.resnet_with_conv_finetune import resnetconvfinetune
from core.models.base import WEIGHTS_DIRNAME<jupyter_output><empty_output><jupyter_text>Preparing the Data<jupyter_code>ds_train, ds_train_info = tfds.load('RetinaDataset', split='train[:80%]', shuffle_files=True, as_supervised=True,with_info=True)
ds_val, ds_val_info = tfds.load('RetinaDataset', split='train[-20%:]', shuffle_files=True, as_supervised=True,with_info=True)
ds_test, ds_test_info = tfds.load('RetinaDataset', split='test', shuffle_files=True, as_supervised=True,with_info=True)
len(ds_train), len(ds_val), len(ds_test)
label_names_list = ds_test_info.features['label'].names
fig = tfds.show_examples(ds_train, ds_train_info)
def preprocess_img(image, label):
    image = tf.image.convert_image_dtype(image, tf.float32) / 255.
    image = tf.image.resize(image, [224, 224])
    return image, label

NCLASS   = 4
IMG_SIZE = 224

def resize_image(img, lb):
  return tf.image.resize(img, (IMG_SIZE,IMG_SIZE)), tf.one_hot(lb, NCLASS)
ds_train = ds_train.map(preprocess_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.batch(32)
ds_val = ds_val.map(preprocess_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_val = ds_val.batch(32)
# ds_test = ds_test.map(preprocess_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
# ds_test = ds_test.batch(32)<jupyter_output><empty_output><jupyter_text>Preparing the Classification Model with Pre-trained weights<jupyter_code>wt_path = str(WEIGHTS_DIRNAME)+"/resnet_with_conv.h5"
wt_path = wt_path.replace('\\','/')
model = resnetconvfinetune(input_shape = (224, 224, 3), output_shape = (4,))
model.load_weights(wt_path)
optimizer = tf.keras.optimizers.Adam(lr=1e-4)
metrics = ['accuracy']
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=metrics)
model.evaluate(ds_test.map(resize_image).batch(32))<jupyter_output>31/31 [==============================] - 3s 93ms/step - loss: 0.0358 - accuracy: 0.9928 0s - loss: 0.0381 - ac<jupyter_text>Exploring Saliency Maps<jupyter_code>def explain_predictions(image,label,model,layer_name='conv2d_2'):
    label_name = label_names_list[label.numpy()]
    resized_img = tf.image.resize(image, (IMG_SIZE,IMG_SIZE))
    resized_img = tf.keras.preprocessing.image.img_to_array(resized_img)
    
    expanded_img = np.expand_dims(resized_img, axis=0)
    prediction = np.argmax(model.predict(expanded_img))
    prediction_acc = np.max(model.predict(expanded_img))
    predicted_label = label_names_list[prediction]
    
    data = ([resized_img.astype('uint8')], None)
    explainer = tf_explain.core.grad_cam.GradCAM()
    grid = explainer.explain(data, model, class_index=label, layer_name=layer_name,image_weight=0.9)
    
    explainer_occ = tf_explain.core.occlusion_sensitivity.OcclusionSensitivity()
    grid_occ = explainer_occ.explain(data, model, class_index=label, patch_size=4)
    
    f, ax = plt.subplots(1,3,figsize = (8,8))
    f.suptitle("True label: " + label_name+", "+"Predicted label: " + predicted_label+", "+"Predicted Accuracy: " + str(prediction_acc), fontsize=15)
    ax[0].set_title("Original Image")
    ax[0].imshow(resized_img.astype('uint8'))
    ax[1].set_title("Grad-CAM")
    ax[1].imshow(grid)
    ax[2].set_title("Occlusion Sensitivity")
    ax[2].imshow(grid_occ)
    plt.tight_layout()
    plt.subplots_adjust(top=1.5)
    plt.show()
def explain_incorrect_predictions(image,label,model,layer_name='conv2d_2'):
    label_name = label_names_list[label.numpy()]
    resized_img = tf.image.resize(image, (IMG_SIZE,IMG_SIZE))
    resized_img = tf.keras.preprocessing.image.img_to_array(resized_img)
    
    expanded_img = np.expand_dims(resized_img, axis=0)
    prediction = np.argmax(model.predict(expanded_img))
    prediction_acc = np.max(model.predict(expanded_img))
    predicted_label = label_names_list[prediction]
    
    if label_name==predicted_label:
        return None
    
    data = ([resized_img.astype('uint8')], None)
    explainer = tf_explain.core.grad_cam.GradCAM()
    grid = explainer.explain(data, model, class_index=label, layer_name=layer_name,image_weight=0.9)
    
    explainer_occ = tf_explain.core.occlusion_sensitivity.OcclusionSensitivity()
    grid_occ = explainer_occ.explain(data, model, class_index=label, patch_size=4)
    
    f, ax = plt.subplots(1,3,figsize = (8,8))
    f.suptitle("True label: " + label_name+", "+"Predicted label: " + predicted_label+", "+"Predicted Accuracy: " + str(prediction_acc), fontsize=15)
    ax[0].set_title("Original Image")
    ax[0].imshow(resized_img.astype('uint8'))
    ax[1].set_title("Grad-CAM")
    ax[1].imshow(grid)
    ax[2].set_title("Occlusion Sensitivity")
    ax[2].imshow(grid_occ)
    plt.tight_layout()
    plt.subplots_adjust(top=1.5)
    plt.show()
i = 0
for image, label in ds_test:
    explain_predictions(image,label,model=model,layer_name=model.layers[-4].name)
    if i >= 3:
        break
    i+=1
i = 0
tf.config.run_functions_eagerly(True)
for image, label in ds_test:
    explain_incorrect_predictions(image,label,model,layer_name=model.layers[-4].name)
    if i >= 50:
        break
    i+=1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1323082122338628, 'fraction_numerical': 0.020615072659682324, 'mean_word_length': 5.688135593220339, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '53185', 'license': 'MIT', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2173, 'n_words': 337, 'path': 'notebooks/04-Saliency_Maps.ipynb', 'repo_name': 'swipswaps/retinal_oct', 'size': '5918'}