我从模型中删除了批处理规范层,并加载了所有其他层的权重以进行推断。原始模型与没有批处理规范的模型的预测是不同的。
差异是因为取消了批次规范?
#load pretrained model
checkpoint = torch.load(restore_file)
pretrained_model = checkpoint['model']
pretrained_dict = pretrained_model.state_dict()
#create new model without batch norm
model = no_batch_model()
model_dict = model.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 3. load the new state dict
model.load_state_dict(pretrained_dict)