我目前正在使用来自 pytorch 预训练 Faster-RCNN 模型(如 torchvision教程)的迁移学习对自定义数据集进行对象检测。我想在每个时代结束时计算验证损失字典(如在训练模式下)。我可以在训练模式下运行模型进行验证,如下所示:
model.train()
for images, targets in data_loader_val:
images = [image.to(device) for image in images]
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
with torch.no_grad():
val_loss_dict = model(images, targets)
print(val_loss_dict)
Run Code Online (Sandbox Code Playgroud)
但我不认为,这是验证的“正确”方式(因为某些特殊层,如 dropout 和 batch norm 在 eval/train 模式下的工作方式不同)。在 eval 模式下,模型返回预测的 bbox(如预期)。我可以为此使用一些内置函数吗?
谢谢。
python machine-learning object-detection computer-vision pytorch