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'")
相关推荐
爱打代码的小林8 小时前
基于 OpenCV 与 Dlib 的人脸替换
人工智能·opencv·计算机视觉
无忧智库8 小时前
某市“十五五“知识产权大数据监管平台与全链条保护系统建设方案深度解读(WORD)
大数据·人工智能
顾北128 小时前
AI对话应用接口开发全解析:同步接口+SSE流式+智能体+前端对接
前端·人工智能
综合热讯8 小时前
股票融资融券交易时间限制一览与制度说明
大数据·人工智能·区块链
AEIC学术交流中心8 小时前
【快速EI检索 | ICPS出版】2026年计算机技术与可持续发展国际学术会议(CTSD 2026)
人工智能·计算机网络
玄同7658 小时前
Python Random 模块深度解析:从基础 API 到 AI / 大模型工程化实践
人工智能·笔记·python·学习·算法·语言模型·llm
风指引着方向8 小时前
昇腾 AI 开发生产力工具:CANN CLI 的高级使用与自动化脚本编写
运维·人工智能·自动化
算法狗28 小时前
大模型面试题:1B的模型和1T的数据大概要训练多久
人工智能·深度学习·机器学习·语言模型
23遇见8 小时前
CANN与开源生态:如何融入并赋能主流AI框架的NPU后端支持
人工智能