现在统一使用Torch中的interpolate这个缩放方法,不用torchvision中的transforms的Resize了,应该就可以了。

This commit is contained in:
zhanghuan
2025-09-11 17:56:25 +08:00
parent e1cb936e32
commit da67cb9f4c
2 changed files with 15 additions and 5 deletions
+14 -4
View File
@@ -54,7 +54,7 @@ class FoodCNN(nn.Module):
def preprocess_image(self, image):
"""
预处理单张图片
预处理单张图片(使用与移动端相同的插值方法)
Args:
image: PIL Image 或 numpy array
@@ -69,10 +69,20 @@ class FoodCNN(nn.Module):
else:
raise ValueError("输入必须是PIL Image或numpy array")
# 应用预处理变换
processed = self.preprocess(image)
# 转换为tensor(不做resize
tensor = transforms.ToTensor()(image)
# 添加batch维度
return processed.unsqueeze(0)
tensor = tensor.unsqueeze(0)
# 使用与移动端相同的插值方法缩放到32x32
tensor = F.interpolate(tensor, size=(32, 32), mode='bilinear', align_corners=False)
# ImageNet标准化
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
tensor = (tensor - mean) / std
return tensor
def mobile_preprocess(self, x):
"""