预训练模型所需的归一化(Pytorch)
我正在使用Pytorch的预训练模型:
model = models.resnet50(pretrained=True).to(device)
for param in model.parameters():
param.requires_grad = False
model.fc = Identity()
我应该使用我的数据平均值和STD将数据归一化,还是使用模型创建者使用的值?
class customDataset(torch.utils.data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, X, y):
'Initialization'
self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.X = X
self.y = torch.tensor(y, dtype=torch.float32)
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
X = self.X[idx]
X = ToTensor()(X).type(torch.float32)[:3,:]
X = self.normalize(X)
return X, self.y[idx]
I am using a pre-trained model from pytorch:
model = models.resnet50(pretrained=True).to(device)
for param in model.parameters():
param.requires_grad = False
model.fc = Identity()
Should I normalize the data using my data mean and std or use the values used by the model creators?
class customDataset(torch.utils.data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, X, y):
'Initialization'
self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.X = X
self.y = torch.tensor(y, dtype=torch.float32)
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
X = self.X[idx]
X = ToTensor()(X).type(torch.float32)[:3,:]
X = self.normalize(X)
return X, self.y[idx]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用训练期间使用的归一化平均值和性病。根据训练数据归一化,该模型得到了优化。为了使模型按预期工作,必须使用相同的数据分布。
如果您从头开始训练模型,则可以使用数据集特定的标准化参数。
You must use the normalization mean and std that was used during training. Based on the training data normalization, the model was optimized. In order for the model to work as expected the same data distribution has to be used.
If you train a model from scratch, you can use your dataset specific normalization parameters.