修改build_faiss_index.py,增加cosFace训练脚本。增加加载cosFace模型的逻辑。

This commit is contained in:
2025-11-06 10:45:09 +08:00
parent a81de1f4ce
commit 2c74c81918
3 changed files with 35 additions and 14 deletions
+8 -4
View File
@@ -65,16 +65,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/ProcessedIngredientRecognition/embedding_20251029_170904/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")
model_path = os.path.join(BASE_DIR, "../model/DishClassification/cosface_20251105_200551/best_cosface_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/ProcessedIngredientRecognition/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/DishClassification/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模型
@@ -91,6 +91,10 @@ class EmbeddingFoodClassifierApp:
if isinstance(checkpoint, dict):
if 'model_state_dict' in checkpoint:
self.model.load_state_dict(checkpoint['model_state_dict'])
elif 'backbone_state_dict' in checkpoint:
# CosFace格式:使用backbone_state_dict(只加载backbone部分)
self.model.load_state_dict(checkpoint['backbone_state_dict'])
print("检测到CosFace模型格式,使用'backbone_state_dict'加载")
elif 'state_dict' in checkpoint:
self.model.load_state_dict(checkpoint['state_dict'])
else: