Search code examples
pythondeep-learningjupyter-notebookcomputer-vision

how to get the prediction for glaucoma model


I”m working in creating model to predict glaucoma following a course in Coursera platform.

I’ve finished every thing but still didn’t get the prediction or the result I look for.

My code is:

from keras import backend as K
import numpy as np


# create the base pre-trained model
base_model = DenseNet121(weights= '/Users/awabe/Desktop/Project/PapilaDB/ClinicalData/DenseNet-BC-121-32-no-top.h5', include_top=False)

x = base_model.output
x = tf.cast(x, dtype=tf.float32)

# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(x)
x = tf.cast(x, dtype=tf.float32)

# and a logistic layer
predictions = Dense(len(labels), activation="sigmoid")(x)

model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer='adam', loss=get_weighted_loss(pos_weights, neg_weights))

and for the history:

history = model.fit_generator(train_generator, 
                              validation_data=valid_generator,
                              steps_per_epoch=25, 
                              validation_steps=34, 
                              epochs = 25)

plt.plot(history.history['loss'])
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("Training Loss Curve")
plt.show()

and I use Visualizing Learning with GradCAM to produce a heatmap highlighting the important regions in the image for predicting the pathological condition.

df = pd.read_csv("/Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/small train/small-train.csv")
IMAGE_DIR = "/Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/small train"

# only show the labels with top 4 AUC
labels_to_show = np.take(labels, np.argsort(auc_rocs)[::-1], )[:1]

after that I use this :

def get_gradcam(image_path, gradcam_model):
    print(f"Loading image from: {image_path}")
    img = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
    print("Image loaded successfully")

    img_tensor = tf.keras.preprocessing.image.img_to_array(img)
    img_tensor = np.expand_dims(img_tensor, axis=0)
    img_tensor /= 255.

    # Compute Grad-CAM using the custom model
    last_conv_layer_output, cam = gradcam_model(img_tensor)

    # Check if either last_conv_layer_output or cam is None
    if last_conv_layer_output is None or cam is None:
        print("Gradients are None. Cannot compute Grad-CAM.")
        return

    # Resize the CAM to the same size as the input image
    cam = tf.image.resize(cam, (img.shape[1], img.shape[0]))

    # Clip the values to keep only positive ones
    cam = tf.maximum(cam, 0)

    # Normalize the CAM
    cam /= tf.reduce_max(cam)

    # Convert the CAM to RGB
    cam = cv2.applyColorMap(np.uint8(255 * cam.numpy()), cv2.COLORMAP_JET)

    # Overlay the CAM on the input image
    heatmap = cv2.cvtColor(cam, cv2.COLOR_BGR2RGB)
    heatmap[np.where(cam.sum(axis=2) == 0)] = 0
    img = cv2.imread(image_path)
    superimposed_img = cv2.addWeighted(img, 0.5, heatmap, 0.5, 0)

    # Display the input image, CAM, and overlaid image
    plt.figure(figsize=(10, 10))
    plt.subplot(131)
    plt.imshow(img)
    plt.title('Input Image')
    plt.axis('off')
    plt.subplot(132)
    plt.imshow(cam)
    plt.title('CAM')
    plt.axis('off')
    plt.subplot(133)
    plt.imshow(superimposed_img)
    plt.title('CAM Overlay')
    plt.axis('off')

    # If you want to visualize gradients, add the following lines
    plt.figure(figsize=(10, 5))
    for i, grad in enumerate(last_conv_layer_output.numpy()[0]):
        plt.subplot(1, last_conv_layer_output.shape[-1], i + 1)
        plt.imshow(grad, cmap='viridis')
        plt.axis('off')
        plt.title(f'Channel {i}')

    plt.show()

# Call the function

gradcam_model = tf.keras.applications.DenseNet121(weights='imagenet', include_top=True)
image_path = "/Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/small train/Opht_cont_RET004OD.jpg"

def get_gradcam(image_path, gradcam_model):
    print(f"Loading image from: {image_path}")
    img = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
    print("Image loaded successfully")

    img_tensor = tf.keras.preprocessing.image.img_to_array(img)
    img_tensor = np.expand_dims(img_tensor, axis=0)
    img_tensor /= 255.

    # Compute Grad-CAM using the custom model
    gradcam_result = gradcam_model(img_tensor)

    # Check if gradcam_result is a tuple (last_conv_layer_output, cam) or a single value
    if isinstance(gradcam_result, tuple):
        last_conv_layer_output, cam = gradcam_result
    else:
        last_conv_layer_output = gradcam_result
        cam = last_conv_layer_output  # Assuming the result is the activation map itself

    # ... Rest of the function remains the same

# Call the function
get_gradcam(image_path, gradcam_model)

which gives me:

Loading image from: /Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/small train/Opht_cont_RET004OD.jpg Image loaded successfully

finally I should run this code:

import util
from tensorflow.keras.preprocessing import image

IMAGE_DIR = "/Users/awabe/Desktop/Project/PapilaDB/ExpertsSegmentations/small train"

util.compute_gradcam(model, 'Opht_cont_RET004OD.jpg', IMAGE_DIR, df, labels, labels_to_show) 

it suppose to give me the output image along side with the prediction from 0 to 1 or as a percentage.

but it gives me the following:

with no prediction just the image

is there any thing I miss or code or function I should add?


Solution

  • I'm pretty sure you aren't asking it to give you the prediction. What you are doing here is computing the GradCAM visualization, which highlights the areas of the image that could be triggering the model, as you say. In order to actually get the prediction as a boolean, you should run model.predict(...).