Pytorch 计算Monte-Carlo Dropout不确定度

为了实现Monte Carlo Dropout (MC Dropout),我们需要在模型评估阶段保留Dropout层的功能,而不是像通常那样在评估模式下关闭Dropout。这可以通过在预测过程中多次运行模型,并且每次运行时都启用Dropout来完成。下面是如何修改你的代码以实现MC Dropout的步骤:

参考文献: Dropout as a Bayesian Approximation: Representing Model Uncertainty in Deep Learnin

1设置模型为训练模式:

即使是在评估时,也需要将模型设置为train()模式,这样Dropout层才会工作。不过需要注意的是,这样做可能会导致Batch Normalization等层的行为发生变化,所以如果你的模型中使用了这些层,可能需要额外处理。

2多次预测:

对于每个样本,你需要多次通过模型进行前向传播,每次都会因为Dropout的影响产生不同的输出。

3计算均值和方差:

对于每个样本的所有预测结果,计算均值作为最终预测值,同时计算方差来估计模型的不确定性。

具体代码见以下的6、7节

python 复制代码
import torch
from torch.utils.data import DataLoader, random_split
from dataset import split_dataset, find_bmp_files, BMPDataset
from model import  MobileNetV2
import pandas as pd
import numpy as np

# 1、设定随机种子
torch.manual_seed(40)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(40)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

# 2、数据集初始化和分割
directory_path = './data/'
bmp_file_paths = find_bmp_files(directory_path)
train_ratio = 0
val_ratio = 1
test_ratio = 0.0
dataset = BMPDataset(bmp_file_paths)
total_length = len(dataset)
train_length = int(train_ratio * total_length)
val_length = int(val_ratio * total_length)
test_length = total_length - train_length - val_length
_, val_dataset, _ = random_split(dataset, [train_length, val_length, test_length])

print(len(val_dataset))
# 3、定义数据加载器
val_loader = DataLoader(val_dataset, batch_size=1, shuffle=False)

# 4、初始化模型、设备和优化器
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MobileNetV2().to(device)
# 5、加载模型权重
state_dict = torch.load('model.pth', map_location=device)  # 直接加载到指定设备
model.load_state_dict(state_dict)

# 6、定义预测次数T
T = 10  # 可以调整这个数字来增加或减少预测次数

# 7、测试模型
all_predictions = []
all_predictions_variances = []
all_labels = []
all_image_names = []

model.train()  # 开启Dropout

with torch.no_grad():
    for images, labels, image_names in val_loader:
        predictions_list = []
        for t in range(T):
            predictions = model(images.to(device))
            predictions_list.append(predictions.cpu().numpy())
        
        # 计算预测的均值和方差
        predictions_array = np.array(predictions_list)
        mean_predictions = np.mean(predictions_array, axis=0)
        var_predictions = np.var(predictions_array, axis=0)
        
        all_predictions.extend(mean_predictions)
        all_predictions_variances.extend(var_predictions)
        all_labels.extend(labels.cpu().numpy())
        all_image_names.extend(image_names)

# 8、将预测结果、标签和图像名称合并到DataFrame中
results_df = pd.DataFrame({
    'Image Name': all_image_names,
    'Predicted S Mean': [pred[0] for pred in all_predictions],
    'Predicted T Mean': [pred[1] for pred in all_predictions],
    'Predicted S Variance': [var[0] for var in all_predictions_variances],
    'Predicted T Variance': [var[1] for var in all_predictions_variances],
    'Actual S': [label[0] for label in all_labels],
    'Actual T': [label[1] for label in all_labels],
})

# 9、保存结果到Excel文件
results_df.to_excel('MC_dropout.xlsx', index=False)

print("Test results with MC Dropout saved to 'MC_dropout.xlsx'")
相关推荐
是枚小菜鸡儿吖10 分钟前
卷不动了?带你拆解 2026 深度学习核心版图:CNN、Transformer 与扩散模型的实战进化
深度学习·cnn·transformer
泯泷11 分钟前
当AI排行榜成为一场数字游戏
人工智能·产品
神一样的老师12 分钟前
【RT-Thread Titan Board 开发板】家庭AI相框
人工智能
靴子学长25 分钟前
Decoder only 架构下 - KV cache 的理解
pytorch·深度学习·算法·大模型·kv
智算菩萨26 分钟前
【OpenGL】10 完整游戏开发实战:基于OpenGL的2D/3D游戏框架、物理引擎集成与AI辅助编程指南
人工智能·python·游戏·3d·矩阵·pygame·opengl
刘简爱学习36 分钟前
弱监督互斥多类脑肿瘤图像分割的类间可分离性损失
人工智能·深度学习·计算机视觉
AI英德西牛仔43 分钟前
AI复制的文字带星号
人工智能·ai·chatgpt·豆包·deepseek·ds随心转
卖报的大地主1 小时前
扩散薛定谔桥(Diffusion Schrödinger Bridge)
人工智能
向成科技1 小时前
当“超轻量AI”遇上“最强国产芯”
人工智能·物联网·ai·芯片·国产化·硬件·主板
远见阁1 小时前
智能体是如何“思考”的:ReAct模式
人工智能·ai·ai智能体