python - 如何使用 Detectron2 的张量板获得测试精度?

标签 python object-detection tensorboard

我正在学习使用 Detecron2。我关注了this链接以创建自定义对象检测器。 我的训练代码-

# training Detectron2
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
import os

cfg = get_cfg()
cfg.merge_from_file("./detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
cfg.DATASETS.TRAIN = ("pedestrian",)
cfg.DATASETS.TEST = ()   # no metrics implemented for this dataset
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"  # initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.02
cfg.SOLVER.MAX_ITER = 300    # 300 iterations seems good enough, but you can certainly train longer
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this dataset
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  

os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
trainer = DefaultTrainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()

它在输出目录中保存了一个日志文件,因此我可以使用 tensorboard 来显示训练精度 -

%load_ext tensorboard
%tensorboard --logdir output

它工作正常,我可以看到我的模型的训练准确性。但是在测试/验证模型时 -

cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7   # set the testing threshold for this model
cfg.DATASETS.TEST = ("pedestrian_day", )
predictor = DefaultPredictor(cfg)

尽管从 Detectron2 教程中我得到了 -

from detectron2.evaluation import COCOEvaluator, inference_on_dataset
from detectron2.data import build_detection_test_loader
evaluator = COCOEvaluator("pedestrian_day", cfg, False, output_dir="./output/")
val_loader = build_detection_test_loader(cfg, "pedestrian_day", mapper=None)
inference_on_dataset(trainer.model, val_loader, evaluator)

但这为训练和测试提供了 AP、AP50、AP75、APm、AP1 和 AP。 我的问题是如何才能像训练版那样在 tensorboard 中看到测试精度?

最佳答案

默认 evaluation during training is disabled

如果你想启用它,你必须设置下面的参数

# set eval step intervals
cfg.TEST.EVAL_PERIOD = 

但要使评估生效,您必须修改 build_evaluator detectron2/engine/defaults.py 中的函数

build_evaluator 的例子https://github.com/facebookresearch/detectron2 的 tools/train_net.py 脚本中提供了函数 repo

This issue in detectron2讨论了创建自定义 LossEvalHook 来监控 eval 损失,听起来是一个不错的尝试方法

关于python - 如何使用 Detectron2 的张量板获得测试精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60258696/

相关文章:

python - 在 django 中使用 'old' 数据库

python-3.x - 如何在Tensorflow-2.0中绘制tf.keras模型图?

python - 为 Tensorflow 对象检测 API 创建 PASCAL Voc

python - 为什么 ptb_word_ln.py 中 embedding_lookup 仅用作编码器而不用作解码器

tensorflow - 有人找到了使 Tensorboard 受密码保护的方法吗?

python - 在Python中的单个列表上使用多个变量(for循环)

python - 在 Python Azure Function 中使用 AzureML 时出现 "Failure Exception: OSError: [Errno 30] Read-only file system"

python - sqlalchemy 和 asyncpg – 设置 postgres statements_timeout

python - 用python opencv检测圆 - 霍夫变换

python - 在python opencv中写入和/或读取后的图像像素值