如何通过TensorFlow Object Detection API获取Mask-RCNN的类级精确率与召回率
问题:如何在TensorFlow Object Detection API的Mask-RCNN评估中获取分类级Precision和Recall指标
我用TensorFlow Object Detection API在自定义数据集上训练了Mask-RCNN,运行评估的命令如下:
python object_detection/model_main_tf2.py \ --pipeline_config_path ${PIPELINE_CONFIG_PATH} \ --model_dir ${MODEL_DIR} \ --checkpoint_dir ${MODEL_DIR} \ --eval_timeout 10
我的eval_config配置如下:
eval_config: { metrics_set: "pascal_voc_instance_segmentation_metrics" eval_instance_masks: true use_moving_averages: false batch_size: 1 include_metrics_per_category: false num_visualizations: 20 visualization_export_dir: "/mypath" }
目前只能拿到'PerformanceByCategory/AP@{}IOU/{}'这类指标,但我需要获取PerformanceByCategory/Precision@{}IOU/{}和PerformanceByCategory/Recall@{}IOU/{}指标。查看代码后发现,precisions_per_class和recalls_per_class已经在evaluate方法中计算并传递,但只有当evaluate_precision_recall设为True时才会输出。我尝试在类构造函数里默认把这个值改成True,但没效果,求解决办法。
解决方法
1. 先修正eval_config的关键配置
你当前的include_metrics_per_category设为false,这会直接导致所有分类级别的指标(包括你要的Precision和Recall)不输出,先把这个参数改成true:
eval_config: { metrics_set: "pascal_voc_instance_segmentation_metrics" eval_instance_masks: true use_moving_averages: false batch_size: 1 include_metrics_per_category: true # 必须开启这个 num_visualizations: 20 visualization_export_dir: "/mypath" }
2. 开启evaluate_precision_recall参数(修改源码)
直接修改指标类的初始化配置:
- 找到
object_detection/metrics/pascal_voc_evaluation.py文件 - 定位到
PascalVOCInstanceSegmentationMetrics类的__init__方法,把evaluate_precision_recall的默认值从False改成True:def __init__(self, num_classes, matching_iou_threshold=0.5, evaluate_precision_recall=True, # 这里改成True use_weighted_ap=False, label_id_offset=0, **kwargs): - 保存修改后,重新运行评估脚本即可。
3. 不修改源码的替代方案(自定义指标配置)
如果不想改动源码,可以创建一个自定义的metrics配置文件(比如custom_pascal_metrics.proto):
metric_set { name: "custom_pascal_voc_instance_segmentation_metrics" metric { class_name: "PascalVOCInstanceSegmentationMetrics" parameters { key: "evaluate_precision_recall" value: true } parameters { key: "include_metrics_per_category" value: true } } }
然后修改eval_config引用这个自定义指标集,并在运行评估时指定配置路径:
eval_config: { metrics_set: "custom_pascal_voc_instance_segmentation_metrics" eval_instance_masks: true use_moving_averages: false batch_size: 1 num_visualizations: 20 visualization_export_dir: "/mypath" }
运行命令添加--metrics_config_path参数:
python object_detection/model_main_tf2.py \ --pipeline_config_path ${PIPELINE_CONFIG_PATH} \ --model_dir ${MODEL_DIR} \ --checkpoint_dir ${MODEL_DIR} \ --metrics_config_path ./custom_pascal_metrics.proto \ --eval_timeout 10
内容的提问来源于stack exchange,提问作者Abdeelrahman Hassanein
相关产品推荐
相关产品推荐

