From e1cb936e320b2aa239121c9c56dda98b8479f0df Mon Sep 17 00:00:00 2001 From: zhanghuan <1262329256@qq.com> Date: Thu, 11 Sep 2025 17:49:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=8F=E8=94=BD/255=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E9=87=8D=E5=A4=8D=E9=99=A4=E4=BB=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classifier/food_classifier_app.py | 1 + net/food_net.py | 9 ++++++--- settings/settings.py | 2 +- toAndroid/toAndroid.py | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/classifier/food_classifier_app.py b/classifier/food_classifier_app.py index ac26116..c70591f 100644 --- a/classifier/food_classifier_app.py +++ b/classifier/food_classifier_app.py @@ -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) # 进行预测 diff --git a/net/food_net.py b/net/food_net.py index 30bac5c..368ec63 100644 --- a/net/food_net.py +++ b/net/food_net.py @@ -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) diff --git a/settings/settings.py b/settings/settings.py index ea56fa1..b98788c 100644 --- a/settings/settings.py +++ b/settings/settings.py @@ -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') diff --git a/toAndroid/toAndroid.py b/toAndroid/toAndroid.py index 0b7485f..c4bf1dd 100644 --- a/toAndroid/toAndroid.py +++ b/toAndroid/toAndroid.py @@ -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}")