Search code examples
pythonobject-detectiontensorboard

How can I get testing accuracy using tensorboard for Detectron2?


I'm learning to use Detecron2. I've followed this link to create a custom object detector. My training code -

# 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()

It saves a log file in output dir thus I can use tensorboard to show the training accuracy -

%load_ext tensorboard
%tensorboard --logdir output

It works fine and I can see my model's training accuracy. But When testing/validating the model -

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)

Although from Detectron2 tutorial I've got -

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)

but this gives the AP, AP50, AP75, APm, APl and APs for both training and testing. My question is how can I able to see the testing accuracy in tensorboard like the training one?


Solution

  • By default evaluation during training is disabled

    If you would like to enable it you have to set below param

    # set eval step intervals
    cfg.TEST.EVAL_PERIOD = 
    

    But for evaluation to work you have to modify build_evaluator function in detectron2/engine/defaults.py

    An example of build_evaluator function is provided in tools/train_net.py script of https://github.com/facebookresearch/detectron2 repo

    This issue in detectron2 discusses about creating custom LossEvalHook to monitor eval loss, sounds like a good approach to try