You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

青光眼预测模型无预测值求助:GradCAM仅显示图像

青光眼预测模型问题:无法获取预期预测结果及GradCAM可视化缺失预测值

我在Coursera课程中搭建青光眼预测模型,完成全部训练流程后始终得不到预期的预测结果。同时使用GradCAM生成热力图时,仅能显示图像,无法输出0-1区间的预测值或百分比。相关代码如下:

模型搭建代码

from keras import backend as K
import numpy as np


# 创建预训练基础模型
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)

# 添加全局空间平均池化层
x = GlobalAveragePooling2D()(x)
x = tf.cast(x, dtype=tf.float32)

# 添加逻辑回归层
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("训练损失曲线")
plt.show()

GradCAM相关代码

数据准备部分

import pandas as pd

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"

# 仅展示AUC排名前4的标签(当前取前1个)
labels_to_show = np.take(labels, np.argsort(auc_rocs)[::-1], )[:1]

GradCAM实现与调用

import cv2
import matplotlib.pyplot as plt
import tensorflow as tf

def get_gradcam(image_path, gradcam_model):
    print(f"从路径加载图像: {image_path}")
    img = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
    print("图像加载成功")

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

    # 使用自定义模型计算Grad-CAM
    last_conv_layer_output, cam = gradcam_model(img_tensor)

    # 检查输出是否为空
    if last_conv_layer_output is None or cam is None:
        print("梯度为空,无法计算Grad-CAM。")
        return

    # 将CAM调整为输入图像尺寸
    cam = tf.image.resize(cam, (img.shape[1], img.shape[0]))

    # 仅保留正值
    cam = tf.maximum(cam, 0)

    # 归一化CAM
    cam /= tf.reduce_max(cam)

    # 将CAM转换为RGB热力图
    cam = cv2.applyColorMap(np.uint8(255 * cam.numpy()), cv2.COLORMAP_JET)

    # 将热力图叠加到输入图像上
    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)

    # 展示输入图像、CAM及叠加图像
    plt.figure(figsize=(10, 10))
    plt.subplot(131)
    plt.imshow(img)
    plt.title('输入图像')
    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叠加图像')
    plt.axis('off')

    # 可视化各通道梯度(可选)
    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'通道 {i}')

    plt.show()

# 第一次调用
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"

# 重新定义get_gradcam函数(覆盖原有定义)
def get_gradcam(image_path, gradcam_model):
    print(f"从路径加载图像: {image_path}")
    img = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
    print("图像加载成功")

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

    # 使用自定义模型计算Grad-CAM
    gradcam_result = gradcam_model(img_tensor)

    # 判断返回结果类型
    if isinstance(gradcam_result, tuple):
        last_conv_layer_output, cam = gradcam_result
    else:
        last_conv_layer_output = gradcam_result
        cam = last_conv_layer_output  # 假设结果即为激活图

    # 后续处理逻辑与之前一致(此处省略重复代码)

# 调用函数
get_gradcam(image_path, gradcam_model)

最终调用util模块的GradCAM函数

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) 

执行后仅输出图像加载成功提示,最终调用util.compute_gradcam仅显示图像,无法看到预期的0-1区间预测值或百分比。请问遗漏了哪些步骤?需要添加什么代码或函数?


内容的提问来源于stack exchange,提问作者Awab Elkhair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 16:50:56