屏蔽/255,避免重复除以。

This commit is contained in:
zhanghuan
2025-09-11 17:49:37 +08:00
parent 06e440a6d1
commit e1cb936e32
4 changed files with 10 additions and 6 deletions
+6 -3
View File
@@ -18,7 +18,7 @@ class FoodCNN(nn.Module):
# 图片预处理变换(仅在推理时使用)
self.preprocess = transforms.Compose([
transforms.Resize((32, 32)),
transforms.Resize((32, 32),interpolation=transforms.InterpolationMode.BILINEAR),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
@@ -85,11 +85,14 @@ class FoodCNN(nn.Module):
torch.Tensor: 预处理后的tensor,形状为 [batch, 3, 32, 32]
"""
# 归一化到 [0, 1]
x = x.float() / 255.0
# 好像安卓已经做了归一化了。
# x = x.float() / 255.0
# 缩放到 32x32
x = F.interpolate(x, size=(32, 32), mode='bilinear', align_corners=False)
# 缩放到 32x32 - 使用 align_corners=True 来匹配 PIL 的默认行为
# x = F.interpolate(x, size=(32, 32), mode='bilinear', align_corners=True)
# ImageNet标准化
mean = torch.tensor([0.485, 0.456, 0.406], device=x.device).view(1, 3, 1, 1)
std = torch.tensor([0.229, 0.224, 0.225], device=x.device).view(1, 3, 1, 1)