上午好☀️☀️☀️️
本答案参考ChatGPT-3.5
根据你提供的报错信息,是因为torchvision从0.13版本开始,对加载预训练模型的方式进行了更改,使用了新的Multi-weight支持API。因此,需要修改加载预训练模型的函数参数。
根据你提供的代码,可以看到预训练模型的加载部分是在PerceptualLoss类中的__init__函数中。你可以进行以下修改:
- 将旧的pretrained参数修改为weights参数。
- 将VGG16_Weights.IMAGENET1K_V1作为weights参数的值。
修改后的代码如下:
class PerceptualLoss(nn.Module):
def __init__(self, use_dropout=True):
super().__init__()
self.scaling_layer = ScalingLayer()
self.chns = [64, 128, 256, 512, 512]
self.net = vgg16(weights=VGG16_Weights.IMAGENET1K_V1, requires_grad=False)
self.Lin0 = NetLinLayer(self.chns[0], use_dropout=use_dropout)
self.Lin1 = NetLinLayer(self.chns[1], use_dropout=use_dropout)
self.Lin2 = NetLinLayer(self.chns[2], use_dropout=use_dropout)
self.Lin3 = NetLinLayer(self.chns[3], use_dropout=use_dropout)
self.Lin4 = NetLinLayer(self.chns[4], use_dropout=use_dropout)
self.Load_from_pretrained()
for param in self.parameters():
param.requires_grad = False
解决方案:
- 将旧的pretrained参数修改为weights参数。
- 将VGG16_Weights.IMAGENET1K_V1作为weights参数的值。
注意:请确保VGG16权重参数文件存在,并正确设置了权重文件的路径。如果仍然报错,请检查所使用的权重参数版本是否正确。