TensorFlow Object Detection API批量评估检查点及TensorBoard可视化问题
批量评估所有检查点并在TensorBoard展示mAP曲线
一、批量评估所有检查点
方法1:脚本遍历检查点(推荐)
针对TensorFlow 2.x版本的Object Detection API,可通过脚本遍历训练目录下的所有检查点,逐个调用评估命令,确保每个检查点的结果都被记录。
Shell脚本(Linux/macOS)
# 替换为你的实际路径 TRAIN_DIR="/path/to/your/training_dir" EVAL_DIR="/path/to/your/eval_dir" PIPELINE_CONFIG="/path/to/your/pipeline.config" # 提取所有检查点的步数并逐个评估 for ckpt_step in $(ls ${TRAIN_DIR}/model.ckpt-*.index | sed -e 's/.*model.ckpt-\([0-9]*\).index/\1/'); do echo "Evaluating checkpoint step ${ckpt_step}" python model_main_tf2.py \ --pipeline_config_path=${PIPELINE_CONFIG} \ --model_dir=${TRAIN_DIR} \ --checkpoint_path=${TRAIN_DIR}/model.ckpt-${ckpt_step} \ --eval_dir=${EVAL_DIR} \ --num_eval_steps=500 # 根据验证集大小调整,设为-1表示遍历全部样本 done
PowerShell脚本(Windows)
# 替换为你的实际路径 $TRAIN_DIR = "C:\path\to\training_dir" $EVAL_DIR = "C:\path\to\eval_dir" $PIPELINE_CONFIG = "C:\path\to\pipeline.config" # 遍历所有检查点文件 Get-ChildItem -Path $TRAIN_DIR -Filter "model.ckpt-*.index" | ForEach-Object { $ckpt_step = $_.Name -replace 'model.ckpt-(\d+).index', '$1' Write-Host "Evaluating checkpoint step $ckpt_step" python model_main_tf2.py ` --pipeline_config_path=$PIPELINE_CONFIG ` --model_dir=$TRAIN_DIR ` --checkpoint_path="$TRAIN_DIR\model.ckpt-$ckpt_step" ` --eval_dir=$EVAL_DIR ` --num_eval_steps=500 }
方法2:修改配置文件自动评估
修改pipeline.config中的eval_config部分,让评估脚本自动扫描训练目录下的所有检查点:
eval_config { metrics_set: "coco_detection_metrics" # 对应mAP指标 use_moving_averages: false checkpoint_dir: "/path/to/your/training_dir" # 指定训练目录而非单个检查点 min_eval_interval_secs: 0 # 关闭定期评估间隔 start_delay_secs: 0 # 立即开始评估 max_num_eval_steps: 500 # 每次评估的步数 }
运行评估命令:
python model_main_tf2.py --pipeline_config_path=${PIPELINE_CONFIG} --model_dir=${TRAIN_DIR} --eval_dir=${EVAL_DIR}
二、在TensorBoard中展示mAP折线图
- 确保所有检查点的评估结果都写入同一个
eval_dir,TensorBoard会自动收集不同步数对应的mAP数据。 - 启动TensorBoard并指向该评估目录:
tensorboard --logdir=${EVAL_DIR}
- 在TensorBoard的
SCALARS面板中,找到DetectionBoxes_Precision/mAP指标,即可查看随训练步数变化的mAP折线图。
关键注意事项
- 验证
pipeline.config中的eval_input_reader已正确指向你的验证集数据(TFRecord或其他格式)。 - 若使用TensorFlow 1.x版本,将命令中的
model_main_tf2.py替换为eval.py并调整对应参数。 - 设置
num_eval_steps为-1可遍历整个验证集,得到更准确的mAP值,但会增加评估时间。
内容的提问来源于stack exchange,提问作者Annie
相关产品推荐
相关产品推荐

