增加未知类别负样本
This commit is contained in:
@@ -65,13 +65,13 @@ 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/WholeIngredientRecognition/embedding_20251021_085915/best_embedding_model.pth")
|
||||
model_path = os.path.join(BASE_DIR, "../model/WholeIngredientRecognition/embedding_20251023_120531/best_embedding_model.pth")
|
||||
# model_path = os.path.join(BASE_DIR, "../model/DishClassification/embedding_20251022_093635/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/WholeIngredientRecognition/faiss_index")
|
||||
index_dir = os.path.join(BASE_DIR, "../faiss_vector_db/WholeIngredientRecognition/faiss_index102402")
|
||||
# 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):
|
||||
@@ -1087,12 +1087,13 @@ class EmbeddingFoodClassifierApp:
|
||||
"""识别所有图片"""
|
||||
try:
|
||||
self.current_results.clear()
|
||||
|
||||
score_list = []
|
||||
for i, img_info in enumerate(self.uploaded_images):
|
||||
# 使用embedding相似度识别
|
||||
if self.model is not None and self.faiss_index is not None:
|
||||
# 使用真实的embedding模型和FAISS索引
|
||||
predicted_class, confidence, similar_images = self.predict_with_embedding(img_info['image'])
|
||||
predicted_class, confidence, similar_images,score = self.predict_with_embedding(img_info['image'])
|
||||
score_list.append(score)
|
||||
else:
|
||||
# 模拟预测结果
|
||||
predicted_class = np.random.choice(self.class_names)
|
||||
@@ -1122,7 +1123,7 @@ class EmbeddingFoodClassifierApp:
|
||||
|
||||
# 更新UI(在主线程中)
|
||||
self.root.after(0, self.update_progress, i + 1, len(self.uploaded_images))
|
||||
|
||||
print('socre_list',score_list)
|
||||
# 识别完成,更新UI
|
||||
self.root.after(0, self.recognition_completed)
|
||||
|
||||
@@ -1144,9 +1145,11 @@ class EmbeddingFoodClassifierApp:
|
||||
|
||||
# 在FAISS索引中搜索最相似的k张图片
|
||||
scores, indices = self.faiss_index.search(query_embedding, k)
|
||||
print("最相似的图片索引:", indices)
|
||||
print("最相似的图片分数:", scores)
|
||||
|
||||
# print("最相似的图片索引:", indices)
|
||||
# print("最相似的图片分数:", type(scores[0]))
|
||||
# print("最相似的图片分数:", scores[0,0])
|
||||
# print("最相似的图片分数:", type(scores[0,0]))
|
||||
|
||||
# 收集相似图片的类别
|
||||
similar_classes = []
|
||||
similar_images = []
|
||||
@@ -1179,12 +1182,12 @@ class EmbeddingFoodClassifierApp:
|
||||
vote_ratio = class_counts[predicted_class] / len(similar_classes)
|
||||
confidence = max_score * vote_ratio
|
||||
|
||||
return predicted_class, confidence, similar_images
|
||||
return predicted_class, confidence, similar_images,round(scores[0,0],3)
|
||||
else:
|
||||
# 如果没有找到相似图片,随机选择一个类别
|
||||
predicted_class = np.random.choice(self.class_names)
|
||||
confidence = 0.1
|
||||
return predicted_class, confidence, []
|
||||
return predicted_class, confidence, [],scores[0,0]
|
||||
|
||||
except Exception as e:
|
||||
print(f"Embedding预测出错: {e}")
|
||||
|
||||
Reference in New Issue
Block a user