From 9eed8acb712aad6b51d2e510aa52b2826462577c Mon Sep 17 00:00:00 2001 From: zhangpu <1250681871@qq.com> Date: Fri, 10 Oct 2025 10:58:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=9B=BE=E7=89=87=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E9=83=BD=E9=87=87=E7=94=A8F.interpolate(),=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E5=92=8C=E5=AE=89=E5=8D=93=E7=AB=AF=E5=BE=97=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=9C=E5=9F=BA=E6=9C=AC=E5=B7=AE=E4=B8=8D?= =?UTF-8?q?=E5=A4=9A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build_faiss_index.py | 2 +- .../embedding_food_classifier_search_app.py | 6 +++--- net/resnet_embedding.py | 18 +++++++++++------- toAndroid/toAndroidEmbedding.py | 4 ++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/build_faiss_index.py b/build_faiss_index.py index 3409bdd..c560403 100644 --- a/build_faiss_index.py +++ b/build_faiss_index.py @@ -468,7 +468,7 @@ class FAISSSearcher: def main(): """主函数""" # 配置参数 - MODEL_PATH = "model/embedding_20250917_145342/best_embedding_model.pth" + MODEL_PATH = "model/embedding_20250930_102826/best_embedding_model.pth" TRAIN_DIR = "dataset/train" OUTPUT_DIR = "faiss_vector_db/faiss_index" BATCH_SIZE = 16 diff --git a/classifier/embedding_food_classifier_search_app.py b/classifier/embedding_food_classifier_search_app.py index 5287db1..da4e56c 100644 --- a/classifier/embedding_food_classifier_search_app.py +++ b/classifier/embedding_food_classifier_search_app.py @@ -1124,7 +1124,7 @@ class EmbeddingFoodClassifierApp: query_tensor = torch.from_numpy(query_embedding.reshape(1, -1)) # [1,512] # 打印query_tensor的前十个数字 - print(query_embedding[:10]) + # print(query_embedding[:50]) if self.db_matrix is None: raise RuntimeError("向量库未加载") @@ -1136,8 +1136,8 @@ class EmbeddingFoodClassifierApp: topk_scores, topk_indices = torch.topk(sims, k=min(k, sims.shape[1]), dim=1) indices = topk_indices.cpu().numpy() scores = topk_scores.cpu().numpy() - print("相似度索引:", indices) - print("相似度分数:", scores) + # print("相似度索引:", indices) + # print("相似度分数:", scores) # 收集相似图片的类别 similar_classes = [] diff --git a/net/resnet_embedding.py b/net/resnet_embedding.py index 1250395..d8d715c 100644 --- a/net/resnet_embedding.py +++ b/net/resnet_embedding.py @@ -56,11 +56,9 @@ class ResNet50EmbeddingNet(nn.Module): ) # 图片预处理变换(用于推理) - # TODO 这里应该要修改归一化的逻辑,不然不一样。 + # 使用与移动端一致的路径:仅转换为Tensor,缩放与归一化在 preprocess_image 中用 F.interpolate 完成 self.preprocess = transforms.Compose([ - transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BILINEAR), - transforms.ToTensor(), - transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) + transforms.ToTensor() ]) # 内部预处理变换(用于已经是tensor但未归一化的数据) @@ -88,11 +86,17 @@ class ResNet50EmbeddingNet(nn.Module): else: raise ValueError("输入必须是PIL Image或numpy array") - # 应用预处理变换 + # 应用预处理变换(先转为tensor,值范围0-1) tensor = self.preprocess(image) - # 添加batch维度 + # 添加batch维度 [1, C, H, W] tensor = tensor.unsqueeze(0) - + # 使用与移动端一致的双线性插值缩放到224x224 + tensor = F.interpolate(tensor, size=(224, 224), mode='bilinear', align_corners=False) + # ImageNet标准化(与移动端保持一致) + mean = torch.tensor([0.485, 0.456, 0.406], device=tensor.device).view(1, 3, 1, 1) + std = torch.tensor([0.229, 0.224, 0.225], device=tensor.device).view(1, 3, 1, 1) + tensor = (tensor - mean) / std + return tensor def mobile_preprocess(self, x: torch.Tensor) -> torch.Tensor: diff --git a/toAndroid/toAndroidEmbedding.py b/toAndroid/toAndroidEmbedding.py index 79bb16a..5ac1b3a 100644 --- a/toAndroid/toAndroidEmbedding.py +++ b/toAndroid/toAndroidEmbedding.py @@ -17,7 +17,7 @@ def main(): # 1. 加载训练好的embedding模型权重 # base_model = create_resnet50_embedding(embedding_dim=512, pretrained=True) base_model = create_mobile_resnet50_embedding(embedding_dim=512, pretrained=True) - model_path = "../model/embedding_20250917_145342/best_embedding_model.pth" + model_path = "../model/embedding_20250930_102826/best_embedding_model.pth" if not os.path.exists(model_path): print(f"错误:模型文件不存在 {model_path}") @@ -85,7 +85,7 @@ def main(): traced_model = torch.jit.trace(mobile_wrapper, single_input) # 保存模型 - output_path = "../model/embedding_20250917_145342/best_embedding_model_mobile.pt" + output_path = "../model/embedding_20250930_102826/best_embedding_model_mobile.pt" traced_model.save(output_path) print(f"✓ TorchScript模型保存成功: {output_path}")