增加一些设置。

This commit is contained in:
2025-10-17 18:24:04 +08:00
parent 25f8e0a267
commit 5db96600fe
10 changed files with 38 additions and 25 deletions
+4 -3
View File
@@ -2,7 +2,8 @@
/.idea/ /.idea/
/model/ /model/
/faiss_vector_db/demo_faiss_index/ /faiss_vector_db/demo_faiss_index/
/faiss_vector_db/faiss_index/ /faiss_vector_db/DishClassification/faiss_index/
/faiss_vector_db/faiss_index092901/ /faiss_vector_db/DishClassification/faiss_index092901/
/faiss_vector_db/faiss_index101001/ /faiss_vector_db/DishClassification/faiss_index101001/
/faiss_vector_db/faiss_index*/ /faiss_vector_db/faiss_index*/
/faiss_vector_db/WholeIngredientRecognition/faiss_index*/
+1 -1
View File
@@ -470,7 +470,7 @@ def main():
# 配置参数 # 配置参数
MODEL_PATH = "model/embedding_20250930_102826/best_embedding_model.pth" MODEL_PATH = "model/embedding_20250930_102826/best_embedding_model.pth"
TRAIN_DIR = "dataset/train" TRAIN_DIR = "dataset/train"
OUTPUT_DIR = "faiss_vector_db/faiss_index" OUTPUT_DIR = "faiss_vector_db/DishClassification/faiss_index"
BATCH_SIZE = 16 BATCH_SIZE = 16
INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw' INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw'
EMBEDDING_DIM = 512 EMBEDDING_DIM = 512
+6 -2
View File
@@ -64,11 +64,15 @@ class EmbeddingFoodClassifierApp:
# 获取当前脚本所在目录 # 获取当前脚本所在目录
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# model_path = "../model/embedding_20251011_133653/best_embedding_model.pth" # 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索引目录 # FAISS索引目录
# index_dir = "../faiss_vector_db/faiss_index" # 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): if os.path.exists(model_path) and os.path.exists(index_dir):
# 1. 加载embedding模型 # 1. 加载embedding模型
@@ -759,7 +759,7 @@ class EmbeddingFoodClassifierApp:
def save_updated_index(self): def save_updated_index(self):
"""保存更新后的索引和元数据""" """保存更新后的索引和元数据"""
try: try:
index_dir = "../faiss_vector_db/faiss_index" index_dir = "../faiss_vector_db/DishClassification/faiss_index"
# 保存向量库为 embeddings.jsonlist[list[float]] # 保存向量库为 embeddings.jsonlist[list[float]]
emb_path = os.path.join(index_dir, 'embeddings.json') emb_path = os.path.join(index_dir, 'embeddings.json')
+1 -1
View File
@@ -111,7 +111,7 @@ builder = FAISSIndexBuilder(
# 构建完整索引 # 构建完整索引
index = builder.build_complete_index( index = builder.build_complete_index(
train_dir="../dataset/train", train_dir="../dataset/train",
output_dir="faiss_index", output_dir="DishClassification/faiss_index",
batch_size=16, batch_size=16,
index_type='flat' # 'flat', 'ivf', 'hnsw' index_type='flat' # 'flat', 'ivf', 'hnsw'
) )
+9 -3
View File
@@ -133,6 +133,8 @@ class FAISSIndexBuilder:
print(f"类别 '{class_name}': {len(image_files)} 张图片") print(f"类别 '{class_name}': {len(image_files)} 张图片")
for image_file in image_files: for image_file in image_files:
# 可以不添加那些增强的图片
if image_file.startswith("img"):
image_path = os.path.join(class_dir, image_file) image_path = os.path.join(class_dir, image_file)
image_paths.append(image_path) image_paths.append(image_path)
class_names.append(class_name) class_names.append(class_name)
@@ -477,9 +479,13 @@ class FAISSSearcher:
def main(): def main():
"""主函数""" """主函数"""
# 配置参数 # 配置参数
MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth" # MODEL_PATH = "../model/embedding_20251011_133653/best_embedding_model.pth"
TRAIN_DIR = "../dataset/train" MODEL_PATH = "../model/WholeIngredientRecognition/embedding_20251017_145836/best_embedding_model.pth"
OUTPUT_DIR = "faiss_index" # 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 BATCH_SIZE = 16
INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw' INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw'
EMBEDDING_DIM = 512 EMBEDDING_DIM = 512
+2 -2
View File
@@ -87,7 +87,7 @@ def run_search_demo():
from faiss_vector_db.build_faiss_index import FAISSSearcher 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" model_path = "../model/embedding_20250917_145342/best_embedding_model.pth"
# 检查索引是否存在 # 检查索引是否存在
@@ -206,7 +206,7 @@ def main():
print("✓ 文件检查通过") 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: if index_exists:
print("✓ 发现已存在的FAISS索引") print("✓ 发现已存在的FAISS索引")
+4 -2
View File
@@ -162,8 +162,10 @@ def plot_2d(Z: np.ndarray, y: np.ndarray, title: str, out_path: Optional[str] =
def main(): def main():
parser = argparse.ArgumentParser(description="可视化高维 embedding 并进行坍塌诊断") 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("--embeddings", type=str, default=os.path.join("DishClassification/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("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("--method", type=str, default="pca", choices=["pca", "tsne", "umap"], help="降维方法")
parser.add_argument("--seed", type=int, default=42) parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--max_points", type=int, default=None, help="抽样上限,避免t-SNE/UMAP过慢;None为全量") parser.add_argument("--max_points", type=int, default=None, help="抽样上限,避免t-SNE/UMAP过慢;None为全量")
+2 -2
View File
@@ -197,8 +197,8 @@ def build_and_export_similarity_head(
def main(): def main():
print("开始转换检索头为移动端格式(TorchScript...") print("开始转换检索头为移动端格式(TorchScript...")
embeddings_path = "../faiss_vector_db/faiss_index/embeddings.json" embeddings_path = "../faiss_vector_db/DishClassification/faiss_index/embeddings.json"
output_path = "../faiss_vector_db/faiss_index/similarity_head.pt" output_path = "../faiss_vector_db/DishClassification/faiss_index/similarity_head.pt"
try: try:
path, n_items = build_and_export_similarity_head(embeddings_path, output_path) path, n_items = build_and_export_similarity_head(embeddings_path, output_path)
+2 -2
View File
@@ -660,7 +660,7 @@ def main(task_key: str = "dish"):
batch_size=BATCH_SIZE, batch_size=BATCH_SIZE,
shuffle=False, shuffle=False,
num_workers=0, num_workers=0,
drop_last=True drop_last=False
) )
logger.info(f"训练集大小: {len(train_dataset)}") logger.info(f"训练集大小: {len(train_dataset)}")
@@ -811,4 +811,4 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("task", choices=list(TASKS.keys()), nargs="?", default="dish") parser.add_argument("task", choices=list(TASKS.keys()), nargs="?", default="dish")
args = parser.parse_args() args = parser.parse_args()
main(args.task) main("whole_ingredient")