为了实现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节
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'")