不要类别加权

This commit is contained in:
2025-11-26 14:50:00 +08:00
parent 99d9eea757
commit 0e8a1df590
5 changed files with 28 additions and 21 deletions
+4 -3
View File
@@ -67,14 +67,15 @@ class EmbeddingFoodClassifierApp:
# 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/WholeIngredientRecognition/cosface_20251106_134718/best_cosface_model.pth")
model_path = os.path.join(BASE_DIR, "../model/DishClassification/cosface_20251111_153649/best_cosface_model.pth")
model_path = os.path.join(BASE_DIR, "../model/WholeIngredientRecognition/grid_search_20251120_205839/model_s68.0_m0.38.pth")
# model_path = os.path.join(BASE_DIR, "../model/DishClassification/cosface_20251111_153649/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/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/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模型
+6 -6
View File
@@ -498,16 +498,16 @@ 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/WholeIngredientRecognition/cosface_20251113_160103/best_cosface_model.pth"
MODEL_PATH = "../model/WholeIngredientRecognition/grid_search_20251113_095659/model_s68.0_m0.4.pth"
# MODEL_PATH = "../model/DishClassification/cosface_20251105_200551/best_embedding_model.pth"
# MODEL_PATH = "../model/WholeIngredientRecognition/grid_search_20251120_205839/model_s68.0_m0.38.pth"
MODEL_PATH = "../model/DishClassification/grid_search_20251121_102723/model_s56.0_m0.4.pth"
# MODEL_PATH = "../model/DishClassification/cosface_20251111_153649/best_cosface_model.pth"
# TRAIN_DIR = "../dataset/ProcessedIngredientRecognition/train"
TRAIN_DIR = "../dataset/WholeIngredientRecognition/train"
# TRAIN_DIR = "../dataset/DishClassification/train"
# TRAIN_DIR = "../dataset/WholeIngredientRecognition/train"
TRAIN_DIR = "../dataset/DishClassification/train"
# OUTPUT_DIR = "ProcessedIngredientRecognition/faiss_index"
OUTPUT_DIR = "WholeIngredientRecognition/faiss_index"
# OUTPUT_DIR = "DishClassification/faiss_index"
# OUTPUT_DIR = "WholeIngredientRecognition/faiss_index"
OUTPUT_DIR = "DishClassification/faiss_index"
BATCH_SIZE = 16
INDEX_TYPE = 'flat' # 'flat', 'ivf', 'hnsw'
EMBEDDING_DIM = 512
+4 -4
View File
@@ -162,11 +162,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("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("--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("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("--method", type=str, default="pca", choices=["pca", "tsne", "umap"], help="降维方法")
parser.add_argument("--seed", type=int, default=42)
+5 -5
View File
@@ -17,9 +17,9 @@ def main():
# 1. 加载训练好的embedding模型权重
# base_model = create_resnet50_embedding(embedding_dim=512, pretrained=True)
base_model = create_mobile_resnet50_embedding(embedding_dim=512, pretrained=True)
# model_path = "../model/DishClassification/cosface_20251110_144822/best_cosface_model.pth"
model_path = "../model/DishClassification/grid_search_20251121_102723/model_s56.0_m0.4.pth"
# model_path = "../model/WholeIngredientRecognition/cosface_20251106_134718/best_cosface_model.pth"
model_path = "../model/WholeIngredientRecognition/grid_search_20251113_095659/model_s68.0_m0.4.pth"
# model_path = "../model/WholeIngredientRecognition/grid_search_20251120_205839/model_s68.0_m0.38.pth"
# model_path = "../model/ProcessedIngredientRecognition/embedding_20251029_173607/best_embedding_model.pth"
if not os.path.exists(model_path):
@@ -92,9 +92,9 @@ def main():
# 使用trace方法转换
traced_model = torch.jit.trace(mobile_wrapper, single_input)
# 保存模型
# output_path = "../model/DishClassification/cosface_20251110_144822/best_embedding_model_mobile.pt"
output_path = "../model/WholeIngredientRecognition/grid_search_20251113_095659/best_embedding_model_mobile.pt"
# 保存模型,一定不要乱改,防止模型覆盖!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
output_path = "../model/DishClassification/grid_search_20251121_102723/best_embedding_model_mobile.pt"
# output_path = "../model/WholeIngredientRecognition/grid_search_20251120_205839/best_embedding_model_mobile.pt"
# output_path = "../model/WholeIngredientRecognition/cosface_20251106_134718/best_embedding_model_mobile.pt"
# output_path = "../model/ProcessedIngredientRecognition/embedding_20251029_173607/best_embedding_model_mobile.pt"
traced_model.save(output_path)
+9 -3
View File
@@ -41,11 +41,17 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
# ==================== 网格搜索空间定义 ====================
GRID_PARAMS = {
"""
'dish': {
's': [56.0, 60.0, 64.0, 68.0], # scale参数
'm': [0.32, 0.35, 0.38, 0.40], # margin参数
},
"""
GRID_PARAMS = {
'dish': {
's': [56.0, 60.0, 64.0, 68.0], # scale参数
'm': [0.32, 0.35, 0.38, 0.40,0.45,0.50], # margin参数
},
'whole_ingredient': {
's': [56.0, 60.0, 64.0, 68.0],
'm': [0.32, 0.35, 0.38, 0.40],
@@ -354,8 +360,8 @@ def grid_search_main(
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='CosFace超参数网格搜索')
parser.add_argument('--task', choices=list(TASKS.keys()), default='dish', help='任务名称')
# parser.add_argument('--task', choices=list(TASKS.keys()), default='whole_ingredient', help='任务名称')
# parser.add_argument('--task', choices=list(TASKS.keys()), default='dish', help='任务名称')
parser.add_argument('--task', choices=list(TASKS.keys()), default='whole_ingredient', help='任务名称')
parser.add_argument('--max_configs', type=int, default=None, help='最大配置数(用于测试)')
parser.add_argument('--epochs', type=int, default=100, help='每个配置的最大训练轮数')
parser.add_argument('--patience', type=int, default=10, help='早停容忍轮数')