detectron2注册数据集和各种踩坑记录

version:
detectron2 :0.6

1 注册数据集

注册数据集之前先做成coco格式,coco格式中categories的id从1开始,不包括背景类
在/root/instance_seg/detectron2/detectron2/data/datasets/builtin.py中新增以下

from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.data.datasets.coco import load_coco_json
import pycocotools
#声明类别,尽量保持
CLASS_NAMES =["__background__","Neoplastic","Inflammatory","Connective","Dead","Epithelial"]
# 数据集路径
DATASET_ROOT = '../../../data/DCIS/data'
ANN_ROOT = os.path.join(DATASET_ROOT, 'patch_512_results/annotations')

TRAIN_PATH = os.path.join(DATASET_ROOT, 'patch_512_results/image')
VAL_PATH = os.path.join(DATASET_ROOT, 'patch_512_results/image')

TRAIN_JSON = os.path.join(ANN_ROOT, 'train_512_2w.json')
#VAL_JSON = os.path.join(ANN_ROOT, 'val.json')
VAL_JSON = os.path.join(ANN_ROOT, 'val_512_1k.json')

# 声明数据集的子集
PREDEFINED_SPLITS_DATASET = {
    "coco_my_train": (TRAIN_PATH, TRAIN_JSON), #coco_my_train 新数据集关键字
    "coco_my_val": (VAL_PATH, VAL_JSON),
}
def plain_register_dataset():
    #训练集
    DatasetCatalog.register("coco_my_train", lambda: load_coco_json(TRAIN_JSON, TRAIN_PATH))
    MetadataCatalog.get("coco_my_train").set(thing_classes=CLASS_NAMES,  # 可以选择开启,但是不能显示中文,这里需要注意,中文的话最好关闭
                                                    evaluator_type='coco', # 指定评估方式
                                                    json_file=TRAIN_JSON,
                                                    image_root=TRAIN_PATH)

    #DatasetCatalog.register("coco_my_val", lambda: load_coco_json(VAL_JSON, VAL_PATH, "coco_2017_val"))
    #验证/测试集
    DatasetCatalog.register("coco_my_val", lambda: load_coco_json(VAL_JSON, VAL_PATH))
    MetadataCatalog.get("coco_my_val").set(thing_classes=CLASS_NAMES, # 可以选择开启,但是不能显示中文,这里需要注意,中文的话最好关闭
                                                evaluator_type='coco', # 指定评估方式
                                                json_file=VAL_JSON,
                                                image_root=VAL_PATH)

在builtin.py中 if name.endswith(".builtin"):处加入以下:

# True for open source;
# Internally at fb, we register them elsewhere
if __name__.endswith(".builtin"):
    # Assume pre-defined datasets live in `./datasets`.
    _root = os.path.expanduser(os.getenv("DETECTRON2_DATASETS", "datasets"))
    # _root = os.path.expanduser(os.getenv("DETECTRON2_DATASETS", ""))
    register_all_coco(_root)
    register_all_lvis(_root)
    register_all_cityscapes(_root)
    register_all_cityscapes_panoptic(_root)
    register_all_pascal_voc(_root)
    register_all_ade20k(_root)
    plain_register_dataset() #新增

2 修改config

在/root/instance_seg/detectron2/detectron2/config/defaults.py处修改_C.DATALOADER.NUM_WORKERS = 8 和图像 train/test的size还有类别,原始coco是80个类,每n个iters保存一次checkpoint

3 避坑

1,不要直接修改coco数据集的格式为自己的数据路径,最好直接注册新数据集
2. 用 python demo/demo.py --config-file configs/BlendMask/R_50_1x.yaml --input …/…/…/data/DCIS/data/patch_512_results/test/image --output …/…/…/data/DCIS/results/BlendMaks/R_50_1x/visulization --opts MODEL.WEIGHTS …/…/…/data/DCIS/exp_pth/BlendMaks/R_50_1x/model_0024999.pth
可视化结果 加output参数
3 训练时加–resume 断点继续训练

你可能感兴趣的:(目标检测)