增加了数据增强离线的程序(可扩充数据集),将图片压缩从32*32,调整为224*224.
This commit is contained in:
+13
-8
@@ -17,9 +17,9 @@ class FoodCNN(nn.Module):
|
||||
super(FoodCNN, self).__init__()
|
||||
self.use_internal_preprocess = use_internal_preprocess
|
||||
|
||||
# 图片预处理变换(仅在推理时使用)
|
||||
# 图片预处理变换(仅在推理时使用)(32)改为(224)
|
||||
self.preprocess = transforms.Compose([
|
||||
transforms.Resize((32, 32),interpolation=transforms.InterpolationMode.BILINEAR),
|
||||
transforms.Resize((224, 224),interpolation=transforms.InterpolationMode.BILINEAR),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
||||
])
|
||||
@@ -49,7 +49,8 @@ class FoodCNN(nn.Module):
|
||||
self.dropout3 = nn.Dropout2d(0.25)
|
||||
|
||||
# 全连接层
|
||||
self.fc1 = nn.Linear(128 * 4 * 4, 512)
|
||||
# self.fc1 = nn.Linear(128 * 4 * 4, 512)
|
||||
self.fc1 = nn.Linear(128 * 28 * 28, 512)
|
||||
self.dropout4 = nn.Dropout(0.5)
|
||||
self.fc2 = nn.Linear(512, settings.NUM_CLASSES)
|
||||
|
||||
@@ -76,8 +77,9 @@ class FoodCNN(nn.Module):
|
||||
tensor = tensor.unsqueeze(0)
|
||||
|
||||
# 使用与移动端相同的插值方法缩放到32x32
|
||||
tensor = F.interpolate(tensor, size=(32, 32), mode='bilinear', align_corners=False)
|
||||
|
||||
# tensor = F.interpolate(tensor, size=(32, 32), mode='bilinear', align_corners=False)
|
||||
tensor = F.interpolate(tensor, size=(224, 224), 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)
|
||||
@@ -100,7 +102,8 @@ class FoodCNN(nn.Module):
|
||||
# x = x.float() / 255.0
|
||||
|
||||
# 缩放到 32x32
|
||||
x = F.interpolate(x, size=(32, 32), mode='bilinear', align_corners=False)
|
||||
# x = F.interpolate(x, size=(32, 32), mode='bilinear', align_corners=False)
|
||||
x = F.interpolate(x, size=(224, 224), mode='bilinear', align_corners=False)
|
||||
# 缩放到 32x32 - 使用 align_corners=True 来匹配 PIL 的默认行为
|
||||
# x = F.interpolate(x, size=(32, 32), mode='bilinear', align_corners=True)
|
||||
|
||||
@@ -140,8 +143,10 @@ class FoodCNN(nn.Module):
|
||||
x = self.dropout3(x)
|
||||
|
||||
# 展平
|
||||
x = x.view(-1, 128 * 4 * 4)
|
||||
|
||||
# x = x.view(-1, 128 * 4 * 4)
|
||||
x = x.view(-1, 128 * 28 * 28)
|
||||
# x = x.view(-1, 65536)
|
||||
|
||||
# 全连接层
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.dropout4(x)
|
||||
|
||||
Reference in New Issue
Block a user