屏蔽/255,避免重复除以。
This commit is contained in:
@@ -548,6 +548,7 @@ class FoodClassifierApp:
|
||||
# 不再需要手动预处理,模型会自动处理
|
||||
# 使用模型的预处理方法
|
||||
input_tensor = self.model.preprocess_image(pil_image)
|
||||
# print('input_tensor',input_tensor)
|
||||
input_tensor = input_tensor.to(self.device)
|
||||
|
||||
# 进行预测
|
||||
|
||||
+6
-3
@@ -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)
|
||||
|
||||
@@ -15,7 +15,7 @@ VAL_DATA_DIR = os.path.join(DATASET_DIR, 'val')
|
||||
TEST_DATA_DIR = os.path.join(DATASET_DIR, 'test')
|
||||
|
||||
# 模型保存路径
|
||||
MODEL_DIR = os.path.join(BASE_DIR, 'model', '07')
|
||||
MODEL_DIR = os.path.join(BASE_DIR, 'model', '09')
|
||||
BEST_MODEL_PATH = os.path.join(MODEL_DIR, 'best_food_model.pth')
|
||||
TRAINING_CURVES_PATH = os.path.join(MODEL_DIR, 'training_curves.png')
|
||||
TRAINING_RESULTS_PATH = os.path.join(MODEL_DIR, 'training_results.txt')
|
||||
|
||||
@@ -8,7 +8,7 @@ def main():
|
||||
|
||||
# 1. 加载训练好的基础模型权重
|
||||
base_model = create_food_cnn()
|
||||
model_path = "../model/07/best_food_model.pth"
|
||||
model_path = "../model/09/best_food_model.pth"
|
||||
|
||||
if not os.path.exists(model_path):
|
||||
print(f"错误:模型文件不存在 {model_path}")
|
||||
@@ -37,7 +37,7 @@ def main():
|
||||
traced_model = torch.jit.trace(mobile_model, example_input)
|
||||
|
||||
# 保存模型
|
||||
output_path = "../model/07/best_food_model_mobile.pt"
|
||||
output_path = "../model/09/best_food_model_mobile.pt"
|
||||
traced_model.save(output_path)
|
||||
print(f"✓ TorchScript模型保存成功: {output_path}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user