我正在按照 Coursera 平台上的课程创建模型来预测青光眼。
我已经完成了所有事情,但仍然没有得到我想要的预测或结果。
我的代码是:
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))
历史:
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()
我使用 GradCAM 的可视化学习来生成热图,突出显示图像中的重要区域,以预测病理状况。
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]
之后我用这个:
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)
这给了我:
从以下位置加载图像: /用户/awabe/桌面/项目/PapilaDB/ExpertsSegmentations/small train/Opht_cont_RET004OD.jpg 图片加载成功
最后我应该运行这段代码:
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)
它应该给我输出图像以及从 0 到 1 的预测或百分比。
但它给了我以下内容:
有什么我错过的东西或者我应该添加的代码或功能吗?
我很确定你不会要求它给你预测。您在这里所做的是计算
GradCAM
可视化,正如您所说,它突出显示了可能触发模型的图像区域。为了真正获得布尔值的预测,您应该运行 model.predict(...)
。