如何使用 Detectron2 的 tensorboard 获得测试准确性?

数据挖掘 Python 火炬 物体检测
2022-02-11 18:06:00

我正在学习使用 Detecron2。我已经按照这个链接创建了一个自定义对象检测器。我的培训代码 -

# 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、APl 和 AP。我的问题是我怎样才能像训练一样看到 tensorboard 中的测试准确性?

注意:我已经在statckoverflow上问过这个问题,但由于我没有得到回应,所以我在这里问了同样的问题。

0个回答
没有发现任何回复~