From 5db96600fe1b6a1247f12e03b3ad7122775e13df Mon Sep 17 00:00:00 2001 From: zhangpu <1250681871@qq.com> Date: Fri, 17 Oct 2025 18:24:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 ++++--- build_faiss_index.py | 2 +- classifier/embedding_food_classifier_app.py | 12 +++++++---- .../embedding_food_classifier_search_app.py | 2 +- faiss_vector_db/README_FAISS.md | 2 +- faiss_vector_db/build_faiss_index.py | 20 ++++++++++++------- faiss_vector_db/run_faiss_demo.py | 4 ++-- faiss_vector_db/visualize_embeddings.py | 6 ++++-- toAndroid/toAndroidSearch.py | 4 ++-- train/train_embedding.py | 4 ++-- 10 files changed, 38 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 7895a9e..4169aaf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ /.idea/ /model/ /faiss_vector_db/demo_faiss_index/ -/faiss_vector_db/faiss_index/ -/faiss_vector_db/faiss_index092901/ -/faiss_vector_db/faiss_index101001/ +/faiss_vector_db/DishClassification/faiss_index/ +/faiss_vector_db/DishClassification/faiss_index092901/ +/faiss_vector_db/DishClassification/faiss_index101001/ /faiss_vector_db/faiss_index*/ +/faiss_vector_db/WholeIngredientRecognition/faiss_index*/ diff --git a/build_faiss_index.py b/build_faiss_index.py index c560403..d01fa90 100644 --- a/build_faiss_index.py +++ b/build_faiss_index.py @@ -470,7 +470,7 @@ def main(): # 配置参数 MODEL_PATH = "model/embedding_20250930_102826/best_embedding_model.pth" TRAIN_DIR = "dataset/train" - OUTPUT_DIR = "faiss_vector_db/faiss_index" + OUTPUT_DIR = "faiss_vector_db/DishClassification/faiss_index" BATCH_SIZE = 16 INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw' EMBEDDING_DIM = 512 diff --git a/classifier/embedding_food_classifier_app.py b/classifier/embedding_food_classifier_app.py index ee8782c..5c800f5 100644 --- a/classifier/embedding_food_classifier_app.py +++ b/classifier/embedding_food_classifier_app.py @@ -64,12 +64,16 @@ 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/embedding_20251011_133653/best_embedding_model.pth") + model_path = os.path.join(BASE_DIR, "../model/WholeIngredientRecognition/embedding_20251017_145836/best_embedding_model.pth") + # model_path = os.path.join(BASE_DIR, "../model/DishClassification/embedding_20251011_133653/best_embedding_model.pth") + # FAISS索引目录 # 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/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): # 1. 加载embedding模型 print("正在加载embedding模型...") diff --git a/classifier/embedding_food_classifier_search_app.py b/classifier/embedding_food_classifier_search_app.py index 6239ed2..dd9365d 100644 --- a/classifier/embedding_food_classifier_search_app.py +++ b/classifier/embedding_food_classifier_search_app.py @@ -759,7 +759,7 @@ class EmbeddingFoodClassifierApp: def save_updated_index(self): """保存更新后的索引和元数据""" try: - index_dir = "../faiss_vector_db/faiss_index" + index_dir = "../faiss_vector_db/DishClassification/faiss_index" # 保存向量库为 embeddings.json(list[list[float]]) emb_path = os.path.join(index_dir, 'embeddings.json') diff --git a/faiss_vector_db/README_FAISS.md b/faiss_vector_db/README_FAISS.md index 0c5d458..37c8cdc 100644 --- a/faiss_vector_db/README_FAISS.md +++ b/faiss_vector_db/README_FAISS.md @@ -111,7 +111,7 @@ builder = FAISSIndexBuilder( # 构建完整索引 index = builder.build_complete_index( train_dir="../dataset/train", - output_dir="faiss_index", + output_dir="DishClassification/faiss_index", batch_size=16, index_type='flat' # 'flat', 'ivf', 'hnsw' ) diff --git a/faiss_vector_db/build_faiss_index.py b/faiss_vector_db/build_faiss_index.py index 291ba02..f134d19 100644 --- a/faiss_vector_db/build_faiss_index.py +++ b/faiss_vector_db/build_faiss_index.py @@ -133,10 +133,12 @@ class FAISSIndexBuilder: print(f"类别 '{class_name}': {len(image_files)} 张图片") for image_file in image_files: - image_path = os.path.join(class_dir, image_file) - image_paths.append(image_path) - class_names.append(class_name) - labels.append(class_idx) + # 可以不添加那些增强的图片 + if image_file.startswith("img"): + image_path = os.path.join(class_dir, image_file) + image_paths.append(image_path) + class_names.append(class_name) + labels.append(class_idx) print(f"总计扫描到 {len(image_paths)} 张图片") return image_paths, class_names, labels @@ -477,9 +479,13 @@ class FAISSSearcher: def main(): """主函数""" # 配置参数 - MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth" - TRAIN_DIR = "../dataset/train" - OUTPUT_DIR = "faiss_index" + # MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth" + MODEL_PATH = "../model/WholeIngredientRecognition/embedding_20251017_145836/best_embedding_model.pth" + # MODEL_PATH = "../model/DishClassification/embedding_20251011_133653/best_embedding_model.pth" + TRAIN_DIR = "../dataset/WholeIngredientRecognition/train" + # TRAIN_DIR = "../dataset/DishClassification/train" + OUTPUT_DIR = "WholeIngredientRecognition/faiss_index" + # OUTPUT_DIR = "DishClassification/faiss_index" BATCH_SIZE = 16 INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw' EMBEDDING_DIM = 512 diff --git a/faiss_vector_db/run_faiss_demo.py b/faiss_vector_db/run_faiss_demo.py index f6eaf5b..5192a4b 100644 --- a/faiss_vector_db/run_faiss_demo.py +++ b/faiss_vector_db/run_faiss_demo.py @@ -87,7 +87,7 @@ def run_search_demo(): from faiss_vector_db.build_faiss_index import FAISSSearcher # 配置参数 - index_dir = "faiss_index" + index_dir = "DishClassification/faiss_index" model_path = "../model/embedding_20250917_145342/best_embedding_model.pth" # 检查索引是否存在 @@ -206,7 +206,7 @@ def main(): print("✓ 文件检查通过") # 检查是否已有索引 - index_exists = os.path.exists("faiss_index/faiss_index.bin") + index_exists = os.path.exists("DishClassification/faiss_index/faiss_index.bin") if index_exists: print("✓ 发现已存在的FAISS索引") diff --git a/faiss_vector_db/visualize_embeddings.py b/faiss_vector_db/visualize_embeddings.py index 9e58853..1e67dc3 100644 --- a/faiss_vector_db/visualize_embeddings.py +++ b/faiss_vector_db/visualize_embeddings.py @@ -162,8 +162,10 @@ 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( "faiss_index", "embeddings.json"), help="embeddings.json 路径") - parser.add_argument("--labels", type=str, default=os.path.join("faiss_index", "labels.json"), help="labels.json 路径") + # 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("--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("--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/toAndroidSearch.py b/toAndroid/toAndroidSearch.py index f65b604..2513a34 100644 --- a/toAndroid/toAndroidSearch.py +++ b/toAndroid/toAndroidSearch.py @@ -197,8 +197,8 @@ def build_and_export_similarity_head( def main(): print("开始转换检索头为移动端格式(TorchScript)...") - embeddings_path = "../faiss_vector_db/faiss_index/embeddings.json" - output_path = "../faiss_vector_db/faiss_index/similarity_head.pt" + embeddings_path = "../faiss_vector_db/DishClassification/faiss_index/embeddings.json" + output_path = "../faiss_vector_db/DishClassification/faiss_index/similarity_head.pt" try: path, n_items = build_and_export_similarity_head(embeddings_path, output_path) diff --git a/train/train_embedding.py b/train/train_embedding.py index 2da7044..329a8c3 100644 --- a/train/train_embedding.py +++ b/train/train_embedding.py @@ -660,7 +660,7 @@ def main(task_key: str = "dish"): batch_size=BATCH_SIZE, shuffle=False, num_workers=0, - drop_last=True + drop_last=False ) logger.info(f"训练集大小: {len(train_dataset)}") @@ -811,4 +811,4 @@ if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("task", choices=list(TASKS.keys()), nargs="?", default="dish") args = parser.parse_args() - main(args.task) \ No newline at end of file + main("whole_ingredient") \ No newline at end of file