gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:13:26 25 4
gpt4 key购买 nike

我正在学习使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com