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'")
相关推荐
之歆6 分钟前
LangGraph构建多智能体
人工智能·python·llama
rhy200605208 分钟前
SAM的低秩特性
人工智能·算法·机器学习·语言模型
SelectDB技术团队16 分钟前
岚图汽车 x Apache Doris : 海量车联网数据实时分析实践
数据仓库·人工智能·数据分析·汽车·apache
FIT2CLOUD飞致云36 分钟前
推出工具商店,工作流新增支持循环、意图识别、文生视频和图生视频节点,MaxKB v2.2.0版本发布
人工智能·开源·deepseek
胖墩会武术38 分钟前
大模型效果优化方案(经验分享)
人工智能·经验分享·python·语言模型
双普拉斯1 小时前
Spring WebFlux调用生成式AI提供的stream流式接口,实现返回实时对话
java·vue.js·人工智能·后端·spring
Sunhen_Qiletian1 小时前
用PyTorch实现CBOW模型:从原理到实战的Word2Vec入门指南
人工智能·pytorch·word2vec
진영_1 小时前
深度学习打卡第N7周:调用Gensim库训练Word2Vec模型
人工智能·深度学习·word2vec
黄啊码1 小时前
【黄啊码】这份AI编程心法,希望对你有用
人工智能
IT_陈寒2 小时前
SpringBoot实战:这5个高效开发技巧让我节省了50%编码时间!
前端·人工智能·后端