From 9d8eccde84c454aedc1589dbf92502ce408e75c3 Mon Sep 17 00:00:00 2001 From: zhangpu <1250681871@qq.com> Date: Fri, 31 Oct 2025 14:16:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classifier/embedding_food_classifier_app.py | 4 ++-- faiss_vector_db/build_faiss_index.py | 12 ++++++------ faiss_vector_db/visualize_embeddings.py | 8 ++++---- toAndroid/toAndroidEmbedding.py | 6 ++++-- train/train_embedding.py | 7 ++++--- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/classifier/embedding_food_classifier_app.py b/classifier/embedding_food_classifier_app.py index 956fad7..6c5538b 100644 --- a/classifier/embedding_food_classifier_app.py +++ b/classifier/embedding_food_classifier_app.py @@ -65,7 +65,7 @@ class EmbeddingFoodClassifierApp: BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # model_path = "../model/embedding_20251011_133653/best_embedding_model.pth" # model_path = os.path.join(BASE_DIR, "../model/embedding_20251011_133653/best_embedding_model.pth") - model_path = os.path.join(BASE_DIR, "../model/ProcessedIngredientRecognition/embedding_20251029_100204/best_embedding_model.pth") + model_path = os.path.join(BASE_DIR, "../model/ProcessedIngredientRecognition/embedding_20251029_170904/best_embedding_model.pth") # model_path = os.path.join(BASE_DIR, "../model/WholeIngredientRecognition/embedding_20251024_091151/best_embedding_model.pth") # model_path = os.path.join(BASE_DIR, "../model/DishClassification/embedding_20251022_093635/best_embedding_model.pth") @@ -73,7 +73,7 @@ class EmbeddingFoodClassifierApp: # index_dir = "../faiss_vector_db/faiss_index" # index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/faiss_index") index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/ProcessedIngredientRecognition/faiss_index") - index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/WholeIngredientRecognition/faiss_index") + # index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/WholeIngredientRecognition/faiss_index") # index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/DishClassification/faiss_index") if os.path.exists(model_path) and os.path.exists(index_dir): diff --git a/faiss_vector_db/build_faiss_index.py b/faiss_vector_db/build_faiss_index.py index d9dace3..5ed87cf 100644 --- a/faiss_vector_db/build_faiss_index.py +++ b/faiss_vector_db/build_faiss_index.py @@ -480,14 +480,14 @@ def main(): """主函数""" # 配置参数 # MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth" - MODEL_PATH = "../model/ProcessedIngredientRecognition/embedding_20251029_100204/best_embedding_model.pth" - # MODEL_PATH = "../model/WholeIngredientRecognition/embedding_20251024_091151/best_embedding_model.pth" + # MODEL_PATH = "../model/ProcessedIngredientRecognition/embedding_20251029_170904/best_embedding_model.pth" + MODEL_PATH = "../model/WholeIngredientRecognition/embedding_20251030_135024/best_embedding_model.pth" # MODEL_PATH = "../model/DishClassification/embedding_20251022_093635/best_embedding_model.pth" - TRAIN_DIR = "../dataset/ProcessedIngredientRecognition/train" - # TRAIN_DIR = "../dataset/WholeIngredientRecognition/train" + # TRAIN_DIR = "../dataset/ProcessedIngredientRecognition/train" + TRAIN_DIR = "../dataset/WholeIngredientRecognition/train" # TRAIN_DIR = "../dataset/DishClassification/train" - OUTPUT_DIR = "ProcessedIngredientRecognition/faiss_index" - # OUTPUT_DIR = "WholeIngredientRecognition/faiss_index" + # OUTPUT_DIR = "ProcessedIngredientRecognition/faiss_index" + OUTPUT_DIR = "WholeIngredientRecognition/faiss_index" # OUTPUT_DIR = "DishClassification/faiss_index" BATCH_SIZE = 16 INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw' diff --git a/faiss_vector_db/visualize_embeddings.py b/faiss_vector_db/visualize_embeddings.py index cab4355..ae6eb9a 100644 --- a/faiss_vector_db/visualize_embeddings.py +++ b/faiss_vector_db/visualize_embeddings.py @@ -163,11 +163,11 @@ def plot_2d(Z: np.ndarray, y: np.ndarray, title: str, out_path: Optional[str] = def main(): parser = argparse.ArgumentParser(description="可视化高维 embedding 并进行坍塌诊断") # parser.add_argument("--embeddings", type=str, default=os.path.join("DishClassification/faiss_index", "embeddings.json"), help="embeddings.json 路径") - # parser.add_argument("--embeddings", type=str, default=os.path.join("WholeIngredientRecognition/faiss_index", "embeddings.json"), help="embeddings.json 路径") - parser.add_argument("--embeddings", type=str, default=os.path.join("ProcessedIngredientRecognition/faiss_index", "embeddings.json"), help="embeddings.json 路径") + parser.add_argument("--embeddings", type=str, default=os.path.join("WholeIngredientRecognition/faiss_index", "embeddings.json"), help="embeddings.json 路径") + # parser.add_argument("--embeddings", type=str, default=os.path.join("ProcessedIngredientRecognition/faiss_index", "embeddings.json"), help="embeddings.json 路径") # parser.add_argument("--labels", type=str, default=os.path.join("DishClassification/faiss_index", "labels.json"), help="labels.json 路径") - # parser.add_argument("--labels", type=str, default=os.path.join("WholeIngredientRecognition/faiss_index", "labels.json"), help="labels.json 路径") - parser.add_argument("--labels", type=str, default=os.path.join("ProcessedIngredientRecognition/faiss_index", "labels.json"), help="labels.json 路径") + parser.add_argument("--labels", type=str, default=os.path.join("WholeIngredientRecognition/faiss_index", "labels.json"), help="labels.json 路径") + # parser.add_argument("--labels", type=str, default=os.path.join("ProcessedIngredientRecognition/faiss_index", "labels.json"), help="labels.json 路径") parser.add_argument("--method", type=str, default="pca", choices=["pca", "tsne", "umap"], help="降维方法") parser.add_argument("--seed", type=int, default=42) parser.add_argument("--max_points", type=int, default=None, help="抽样上限,避免t-SNE/UMAP过慢;None为全量") diff --git a/toAndroid/toAndroidEmbedding.py b/toAndroid/toAndroidEmbedding.py index 5527013..f509169 100644 --- a/toAndroid/toAndroidEmbedding.py +++ b/toAndroid/toAndroidEmbedding.py @@ -18,7 +18,8 @@ def main(): # 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_20250930_102826/best_embedding_model.pth" - model_path = "../model/WholeIngredientRecognition/embedding_20251021_085915/best_embedding_model.pth" + model_path = "../model/WholeIngredientRecognition/embedding_20251030_135024/best_embedding_model.pth" + # model_path = "../model/ProcessedIngredientRecognition/embedding_20251029_173607/best_embedding_model.pth" if not os.path.exists(model_path): print(f"错误:模型文件不存在 {model_path}") @@ -86,7 +87,8 @@ def main(): traced_model = torch.jit.trace(mobile_wrapper, single_input) # 保存模型 - output_path = "../model/WholeIngredientRecognition/embedding_20251021_085915/best_embedding_model_mobile.pt" + output_path = "../model/WholeIngredientRecognition/embedding_20251030_135024/best_embedding_model_mobile.pt" + # output_path = "../model/ProcessedIngredientRecognition/embedding_20251029_173607/best_embedding_model_mobile.pt" traced_model.save(output_path) print(f"✓ TorchScript模型保存成功: {output_path}") diff --git a/train/train_embedding.py b/train/train_embedding.py index 6f70fe6..2fa2809 100644 --- a/train/train_embedding.py +++ b/train/train_embedding.py @@ -63,7 +63,8 @@ TASKS = { lr=8e-4, # triplet_margin=0.35, triplet_margin=0.5, - center_loss_weight=0.1, + # center_loss_weight=0.1, + center_loss_weight=0.7, aug_strength="medium", ), "processed_ingredient": TaskConfig( @@ -820,6 +821,6 @@ if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("task", choices=list(TASKS.keys()), nargs="?", default="dish") args = parser.parse_args() - main("processed_ingredient") - # main("whole_ingredient") + # main("processed_ingredient") + main("whole_ingredient") # main("dish") \ No newline at end of file