修改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
+23 -6
View File
@@ -75,13 +75,25 @@ class FAISSIndexBuilder:
# 处理不同的保存格式
if isinstance(checkpoint, dict):
if 'model_state_dict' in checkpoint:
# Triplet格式:使用model_state_dict
self.model.load_state_dict(checkpoint['model_state_dict'])
print("检测到Triplet模型格式,使用'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:
# 通用格式:使用state_dict
self.model.load_state_dict(checkpoint['state_dict'])
print("检测到通用模型格式,使用'state_dict'加载")
else:
# 直接加载整个checkpoint作为state_dict
self.model.load_state_dict(checkpoint)
print("使用checkpoint直接加载")
else:
# checkpoint本身就是state_dict
self.model.load_state_dict(checkpoint)
print("checkpoint为state_dict格式,直接加载")
# 移动到设备并设置为评估模式
self.model.to(self.device)
@@ -408,6 +420,10 @@ class FAISSSearcher:
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:
@@ -480,16 +496,17 @@ def main():
"""主函数"""
# 配置参数
# MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth"
MODEL_PATH = "../model/ProcessedIngredientRecognition/embedding_20251103_172012/best_embedding_model.pth"
# MODEL_PATH = "../model/ProcessedIngredientRecognition/embedding_20251103_172012/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"
# MODEL_PATH = "../model/DishClassification/cosface_20251105_200551/best_embedding_model.pth"
MODEL_PATH = "../model/DishClassification/cosface_20251105_200551/best_cosface_model.pth"
# TRAIN_DIR = "../dataset/ProcessedIngredientRecognition/train"
# TRAIN_DIR = "../dataset/WholeIngredientRecognition/train"
# TRAIN_DIR = "../dataset/DishClassification/train"
TRAIN_DIR = "../dataset/DishClassification/train"
OUTPUT_DIR = "ProcessedIngredientRecognition/faiss_index"
# OUTPUT_DIR = "ProcessedIngredientRecognition/faiss_index"
# OUTPUT_DIR = "WholeIngredientRecognition/faiss_index"
# OUTPUT_DIR = "DishClassification/faiss_index"
OUTPUT_DIR = "DishClassification/faiss_index"
BATCH_SIZE = 16
INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw'
EMBEDDING_DIM = 512