[英]Resizing image in pytorch to match the dimensions of the model does not work
在 model 上运行预测器时出现尺寸不匹配错误,即使训练、验证和测试工作正常。 我想这意味着预测器 model 中的图像处理存在问题。
class Predictor(nn.Module):
def __init__(self, model, class_names, mean, std):
super().__init__()
self.model = model.eval()
self.class_names = class_names
self.transforms = nn.Sequential( # --- THIS MIGHT BE THE PROBLEM
T.Resize([256, ]),
T.CenterCrop(224),
T.ConvertImageDtype(torch.float),
T.Normalize(mean.tolist(), std.tolist())
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
with torch.no_grad():
# Apply transforms --- THIS MIGHT BE THE PROBLEM TOO
x = self.transforms(x)
# Get the logits
x = self.model(x)
# Apply softmax
x = F.softmax(x, dim=1)
return x
我尝试硬编码model
class 的输入神经元的尺寸,它确实工作了几秒钟,然后我得到了另一个尺寸不匹配。
例如,在训练时,模型的输入神经元是128*7*7
,然后我将其硬编码为57600
,因为这是引发错误的输入维度。 它在预测过程中确实适用于 26 幅图像,但随后它引发了另一个维度不匹配错误,另一个维度为51200
。
这确实意味着传递给 model 的图像尺寸不一致!
这也意味着self.transforms(...)
不起作用,因为如果它起作用了,就不会有尺寸不匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.