From 78cb1d72e13b2c5ea1c71f4866ee748783440e8e Mon Sep 17 00:00:00 2001 From: zhangpu <1250681871@qq.com> Date: Tue, 11 Nov 2025 14:03:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E9=9B=86=E6=AD=A3=E7=A1=AE=E7=8E=87=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E6=89=93=E5=8D=B0=E9=AA=8C=E8=AF=81=E9=9B=86?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=8E=87100%=EF=BC=8C=E5=AE=9E=E9=99=85?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B9=B6=E4=B8=8D=E6=98=AF100%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faiss_vector_db/build_faiss_index.py | 2 +- train/train_cosface_embedding.py | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/faiss_vector_db/build_faiss_index.py b/faiss_vector_db/build_faiss_index.py index 0f2dded..b9d9b9e 100644 --- a/faiss_vector_db/build_faiss_index.py +++ b/faiss_vector_db/build_faiss_index.py @@ -499,7 +499,7 @@ def main(): # MODEL_PATH = "../model/ProcessedIngredientRecognition/embedding_20251103_172012/best_embedding_model.pth" # MODEL_PATH = "../model/WholeIngredientRecognition/cosface_20251106_134718/best_cosface_model.pth" # MODEL_PATH = "../model/DishClassification/cosface_20251105_200551/best_embedding_model.pth" - MODEL_PATH = "../model/DishClassification/cosface_20251110_144822/best_cosface_model.pth" + MODEL_PATH = "../model/DishClassification/cosface_20251111_094827/best_cosface_model.pth" # TRAIN_DIR = "../dataset/ProcessedIngredientRecognition/train" # TRAIN_DIR = "../dataset/WholeIngredientRecognition/train" TRAIN_DIR = "../dataset/DishClassification/train" diff --git a/train/train_cosface_embedding.py b/train/train_cosface_embedding.py index 5748dcb..65d3918 100644 --- a/train/train_cosface_embedding.py +++ b/train/train_cosface_embedding.py @@ -187,8 +187,11 @@ def run_epoch(model, head, loader, criterion, optimizer=None, train: bool = True model.eval(); head.eval() running_loss = 0.0 - running_acc = 0.0 n_batches = 0 + + # 改为统计总样本数和正确样本数(全局准确率) + total_samples = 0 + correct_samples = 0 with torch.set_grad_enabled(train): pbar = tqdm(loader, desc='训练中' if train else '验证中') @@ -202,17 +205,28 @@ def run_epoch(model, head, loader, criterion, optimizer=None, train: bool = True else: logits = head(feats) # 不加边距 loss = criterion(logits, labels) - acc = accuracy_top1(logits, labels) - + + # 统计样本级别的正确数 + pred = logits.argmax(dim=1) + correct = (pred == labels).sum().item() + batch_size = labels.size(0) + + total_samples += batch_size + correct_samples += correct running_loss += loss.item() - running_acc += acc n_batches += 1 + + # 计算当前的全局准确率 + current_acc = 100.0 * correct_samples / total_samples + pbar.set_postfix({ 'Loss': f'{running_loss / n_batches:.4f}', - 'Acc': f'{running_acc / n_batches:.2f}%' + 'Acc': f'{current_acc:.2f}%' }) - return running_loss / max(n_batches, 1), running_acc / max(n_batches, 1) + avg_loss = running_loss / max(n_batches, 1) + global_acc = 100.0 * correct_samples / max(total_samples, 1) + return avg_loss, global_acc def collect_max_cos_scores(model, head, loader) -> torch.Tensor: