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'")
相关推荐
lifallen几秒前
Avernet:Agent 的组织网络
人工智能·学习·ai·开源软件·ai编程
zzzzzz310几秒前
LLM 成本治理:从 token 账单到线上熔断,团队应该先做哪三件事?
人工智能·python·api
武子康2 分钟前
打断不是听到声音就闭嘴:语音 Agent 的话权状态怎样真正收敛
人工智能·llm·agent
动物园猫10 分钟前
桑叶病害目标检测数据集:16,000张图像 | 目标检测
人工智能·目标检测·计算机视觉
金融小师妹12 分钟前
多因子智能建模:国内黄金需求结构调整与供给变化的AI分析框架
大数据·人工智能
科技新资讯14 分钟前
AI写小说软件FeelFish 4.0实测,多部门协作重塑创作全流程
人工智能
程序猿编码18 分钟前
不用PyTorch,不用CUDA,我用C++手写了一个能跑GPT和Whisper的推理库
c++·pytorch·gpt·大模型·whisper
IT_陈寒20 分钟前
Vite打包时静态资源404?加个斜杠就能解决
前端·人工智能·后端
码农小旋风20 分钟前
26个PPT生成Skill,我做了一次系统梳理
人工智能·ppt
ccLianLian24 分钟前
机器学习和深度学习
人工智能·深度学习·机器学习