From b2fe4ab94e10a66f27cd98ba59f529e23bbde403 Mon Sep 17 00:00:00 2001 From: mckay Date: Sun, 5 Jan 2025 19:06:16 +0800 Subject: [PATCH] Refactor evaluation.py for improved project structure and logging - Added project root directory setup to ensure consistent file paths. - Integrated a custom logger for enhanced logging capabilities. - Updated argument parsing to use absolute paths for ground truth and prediction data. - Improved documentation for distance functions and added a new function to compute feature distances and angle differences. - Refactored file reading to use context management for better resource handling. --- code/evaluation/evaluation.py | 44 ++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/code/evaluation/evaluation.py b/code/evaluation/evaluation.py index 0b14c02..f4c4447 100644 --- a/code/evaluation/evaluation.py +++ b/code/evaluation/evaluation.py @@ -1,4 +1,14 @@ import os +import sys + +# 设置项目根目录 +project_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) +sys.path.append(project_dir) +os.chdir(project_dir) + +# 导入日志系统 +from utils.logger import logger + import numpy as np from scipy.spatial import cKDTree from scipy.spatial.distance import directed_hausdorff @@ -11,8 +21,12 @@ import pickle import argparse # parse args first and set gpu id parser = argparse.ArgumentParser() -parser.add_argument('--gt_path', type=str, default='../../data/eval_data', help='ground truth data path') -parser.add_argument('--pred_path', type=str, default='../../data/output_data', help='converted data path') +parser.add_argument('--gt_path', type=str, + default=os.path.join(project_dir, '../data/eval_data'), + help='ground truth data path') +parser.add_argument('--pred_path', type=str, + default=os.path.join(project_dir, '../data/output_data'), + help='converted data path') parser.add_argument('--name_list', type=str, default='broken_bullet_name.txt', help='names of models to be evaluated, if you want to evaluate the whole dataset, please set it as all_names.txt') parser.add_argument('--nsample', type=int, default=50000, help='point batch size') parser.add_argument('--regen', default = False, action="store_true", help = 'regenerate feature curves') @@ -22,10 +36,13 @@ def distance_p2p(points_src, normals_src, points_tgt, normals_tgt): ''' Computes minimal distances of each point in points_src to points_tgt. Args: - points_src (numpy array): source points - normals_src (numpy array): source normals - points_tgt (numpy array): target points - normals_tgt (numpy array): target normals + points_src (numpy array [N, 3]): source points + normals_src (numpy array [N, 3]): source normals + points_tgt (numpy array [M, 3]): target points + normals_tgt (numpy array [M, 3]): target + Returns: + dist (numpy array [N]): minimal distances of each point in points_src to points_tgt + normals_dot_product (numpy array [N]): dot product of normals of points_src and points_tgt ''' kdtree = cKDTree(points_tgt) dist, idx = kdtree.query(points_src) @@ -78,6 +95,16 @@ def distance_p2mesh(points_src, normals_src, mesh): def distance_fea(gt_pa, pred_pa): + """计算特征点之间的距离和角度差异 + Args: + gt_pa: 真实特征点和角度 [N, 4] + pred_pa: 预测特征点和角度 [N, 4] + Returns: + dfg2p: 真实到预测的距离 + dfp2g: 预测到真实的距离 + fag2p: 真实到预测的角度差 + fap2g: 预测到真实的角度差 + """ gt_points = gt_pa[:,:3] pred_points = pred_pa[:,:3] gt_angle = gt_pa[:,3] @@ -108,9 +135,8 @@ def compute_all(): namelst = args.name_list output_path = 'eval_results.csv' - f = open(namelst, 'r') - lines = f.readlines() - f.close() + with open(os.path.join(project_dir, 'evaluation', namelst), 'r') as f: + lines = f.readlines() d = {'name':[], 'CD':[], 'HD':[], 'HDgt2pred':[], 'HDpred2gt':[], 'AngleDiffMean':[], 'AngleDiffStd':[], 'FeaDfgt2pred':[], 'FeaDfpred2gt':[], 'FeaDf':[], 'FeaAnglegt2pred':[], 'FeaAnglepred2gt':[], 'FeaAngle':[]}