繁体   English   中英

在预训练模型中加载我的训练模型与在未预训练模型中加载之间的区别?

[英]Difference between loading my trained model in a pretrained model and loading it in not pretrained one?

我为我的任务训练了一个Inception_v3 我有 3 节课。 训练后,我尝试测试我的训练模型,并使用以下代码加载我的模型:

model = models.inception_v3(pretrained=True, aux_logits=False)  
model.fc = nn.Linear(model.fc.in_features, 3)
model.load_state_dict(torch.load(My Model Path.pth))

下载预训练的Inception_v3,更改输出特征并在此模型中加载我的权重。 正如我在验证阶段所期望的那样,我获得了非常好的结果。

如果我使用相同的代码但 pretrained= False测试会非常糟糕。

model = models.inception_v3(pretrained=False, aux_logits=False)  
model.fc = nn.Linear(model.fc.in_features, 3)
model.load_state_dict(torch.load(My Model Path.pth))

由于在我下载的模型中我加载了我的权重,因此预训练的 True 或 False 之间应该没有区别。

有谁知道有什么变化?

pretrained=Trueinception_v3模型有额外的影响:它控制输入是否为

根据在 ImageNet 上训练的方法进行预处理

(源代码在这里)。

当您设置pretrained=False ,如果您想在测试时使事情具有可比性,您还应该设置transform_input=True

model = models.inception_v3(pretrained=False, aux_logits=False, transform_input=True)  
model.fc = nn.Linear(model.fc.in_features, 3)
model.load_state_dict(torch.load(My Model Path.pth))

如果您想知道,这是预处理

def _transform_input(self, x: Tensor) -> Tensor:
    if self.transform_input:
        x_ch0 = torch.unsqueeze(x[:, 0], 1) * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
        x_ch1 = torch.unsqueeze(x[:, 1], 1) * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
        x_ch2 = torch.unsqueeze(x[:, 2], 1) * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
        x = torch.cat((x_ch0, x_ch1, x_ch2), 1)
    return x

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM