1、detectron2中的DatasetMapper类——detectron2如何做数据增强_小麦先生的博客-CSDN博客_detectron2数据增强
2、 目标检测tricks(基于detectron2)_任小喵r的博客-CSDN博客
3、 detectron2/train_net.py at main · facebookresearch/detectron2 · GitHub
注:这个跟我们自己定义的很像,我们自定义的训练脚本如下:
import os
import cv2
import logging
from collections import OrderedDict
import detectron2.utils.comm as comm
from detectron2.utils.visualizer import Visualizer
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.data.datasets.coco import load_coco_json
from detectron2.engine import DefaultTrainer, default_argument_parser, default_setup, launch
from detectron2.evaluation import COCOEvaluator, verify_results
from detectron2.modeling import GeneralizedRCNNWithTTA
#使用指定的GPU
# os.environ["CUDA_VISIBLE_DEVICES"] = '1'
# 数据集路径
DATASET_ROOT = './datasets/coco'
ANN_ROOT = os.path.join(DATASET_ROOT, 'annotations')
TRAIN_PATH = os.path.join(DATASET_ROOT, 'images/train2017')
VAL_PATH = os.path.join(DATASET_ROOT, 'images/val2017')
TRAIN_JSON = os.path.join(ANN_ROOT, 'instances_train2017.json')
VAL_JSON = os.path.join(ANN_ROOT, 'instances_val2017.json')
# 数据集类别元数据
# 需修改!!
DATASET_CATEGORIES = [
{"name": "DECP2", "id": 1, "isthing": 1, "color": [110, 20, 60]},
{"name": "DECP3", "id": 2, "isthing": 1, "color": [220, 120, 60]},
{"name": "PACO", "id": 3, "isthing": 1, "color": [220, 20, 120]},
{"name": "PDCR", "id": 4, "isthing": 1, "color": [120, 20, 160]},
{"name": "TARE", "id": 5, "isthing": 1, "color": [220, 120, 160]},
{"name": "PAMS", "id": 6, "isthing": 1, "color": [15, 20, 60]},
{"name": "TFDE", "id": 7, "isthing": 1, "color": [110, 190, 60]},
{"name": "TMMS", "id": 8, "isthing": 1, "color": [15, 120, 160]},
{"name": "EPDF", "id": 9, "isthing": 1, "color": [2, 120, 160]},
{"name": "PASC", "id": 10, "isthing": 1, "color": [15, 1, 160]},
]
# 数据集的子集
PREDEFINED_SPLITS_DATASET = {
"train_2019": (TRAIN_PATH, TRAIN_JSON),
"val_2019": (VAL_PATH, VAL_JSON),
}
def register_dataset():
"""
purpose: register all splits of dataset with PREDEFINED_SPLITS_DATASET
"""
for key, (image_root, json_file) in PREDEFINED_SPLITS_DATASET.items():
register_dataset_instances(name=key,
metadate=get_dataset_instances_meta(),
json_file=json_file,
image_root=image_root)
def get_dataset_instances_meta():
"""
purpose: get metadata of dataset from DATASET_CATEGORIES
return: dict[metadata]
"""
thing_ids = [k["id"] for k in DATASET_CATEGORIES if k["isthing"] == 1]
thing_colors = [k["color"] for k in DATASET_CATEGORIES if k["isthing"] == 1]
# assert len(thing_ids) == 2, len(thing_ids)
thing_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(thing_ids)}
thing_classes = [k["name"] for k in DATASET_CATEGORIES if k["isthing"] == 1]
ret = {
"thing_dataset_id_to_contiguous_id": thing_dataset_id_to_contiguous_id,
"thing_classes": thing_classes,
"thing_colors": thing_colors,
}
return ret
def register_dataset_instances(name, metadate, json_file, image_root):
"""
purpose: register dataset to DatasetCatalog,
register metadata to MetadataCatalog and set attribute
"""
DatasetCatalog.register(name, lambda: load_coco_json(json_file, image_root, name))
MetadataCatalog.get(name).set(json_file=json_file,
image_root=image_root,
evaluator_type="coco",
**metadate)
# 查看数据集标注
def checkout_dataset_annotation(name="val_2019"):
dataset_dicts = load_coco_json(TRAIN_JSON, TRAIN_PATH, name)
for d in dataset_dicts:
img = cv2.imread(d["file_name"])
visualizer = Visualizer(img[:, :, ::-1], metadata=MetadataCatalog.get(name), scale=1.5)
vis = visualizer.draw_dataset_dict(d)
cv2.imshow('show', vis.get_image()[:, :, ::-1])
cv2.waitKey(0)
class Trainer(DefaultTrainer):
@classmethod
def build_evaluator(cls, cfg, dataset_name, output_folder=None):
if output_folder is None:
output_folder = os.path.join(cfg.OUTPUT_DIR, "inference")
return COCOEvaluator(dataset_name, cfg, distributed=False, output_dir=output_folder)
@classmethod
def test_with_TTA(cls, cfg, model):
logger = logging.getLogger("detectron2.trainer")
# In the end of training, run an evaluation with TTA
# Only support some R-CNN models.
logger.info("Running inference with test-time augmentation ...")
model = GeneralizedRCNNWithTTA(cfg, model)
evaluators = [
cls.build_evaluator(
cfg, name, output_folder=os.path.join(cfg.OUTPUT_DIR, "inference_TTA")
)
for name in cfg.DATASETS.TEST
]
res = cls.test(cfg, model, evaluators)
res = OrderedDict({k + "_TTA": v for k, v in res.items()})
return res
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg() # 拷贝default config副本
# args.config_file = "configs/COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml"
args.config_file = "configs/COCO-Detection/retinanet_R_101_FPN_3x.yaml"
# args.config_file = "configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml"
# args.config_file = "configs/Misc/mask_rcnn_R_50_FPN_3x_dconv_c3-c5.yaml"
# args.config_file = "configs/Misc/cascade_mask_rcnn_R_50_FPN_3x.yaml"
# args.config_file = "configs/Misc/cascade_mask_rcnn_X_152_32x8d_FPN_IN5k_gn_dconv.yaml"
# args.config_file = "configs/Misc/cascade_mask_rcnn_X_152_32x8d_FPN_IN5k_gn_dconv.yaml"
cfg.merge_from_file(args.config_file) # 从config file 覆盖配置
cfg.merge_from_list(args.opts) # 从CLI参数 覆盖配置
# 更改配置参数
cfg.DATASETS.TRAIN = ("train_2019",)
cfg.DATASETS.TEST = ("val_2019",)
cfg.DATALOADER.NUM_WORKERS = 16 #
cfg.INPUT.MAX_SIZE_TRAIN = 1300
cfg.INPUT.MAX_SIZE_TEST = 1300 #test
cfg.INPUT.MIN_SIZE_TRAIN = (1000,) # (160,)
cfg.INPUT.MIN_SIZE_TEST = 1000 #test
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 10 # 类别数
cfg.MODEL.MASK_ON = False
cfg.MODEL.RETINANET.NUM_CLASSES = 10 #80 retrainet
# cfg.MODEL.WEIGHTS = "./models/cascade_mask_rcnn_R_50_FPN_3x/model_final_480dd8.pkl" # 预训练模型权重
# cfg.MODEL.WEIGHTS = "./models/R-50.pkl" # 预训练模型权重
# cfg.MODEL.WEIGHTS = "./models/R-101.pkl" # 预训练模型权重
# cfg.MODEL.WEIGHTS = "./models/X-152-32x8d-IN5k.pkl"
# cfg.MODEL.WEIGHTS = "./models/mask_rcnn_X_101_32x8d_FPN_3x/model_final_2d9806.pkl"
# cfg.MODEL.WEIGHTS = "./output/model_0003999.pth" # 预训练模型权重
# cfg.MODEL.WEIGHTS = "./output/model_final.pth" # 预训练模型权重
cfg.MODEL.WEIGHTS = "./models/retinanet_R_101_FPN_3x/model_final_971ab9.pkl"
# cfg.MODEL.WEIGHTS = "./models/cascade_mask_rcnn_x152/model_0039999_e76410.pkl" # cat uesd it
# cfg.MODEL.WEIGHTS = "./models/mask_rcnn_R_50_FPN_3x_dconv_c3-c5/model_final_821d0b.pkl" # 预训练模型权重
cfg.SOLVER.IMS_PER_BATCH = 8 # batch_size=2; iters_in_one_epoch = dataset_imgs/batch_size
ITERS_IN_ONE_EPOCH = int(960 / cfg.SOLVER.IMS_PER_BATCH)
cfg.SOLVER.MAX_ITER = (ITERS_IN_ONE_EPOCH * 40) - 1 # 4k
cfg.SOLVER.BASE_LR = 0.002 # LR
cfg.SOLVER.MOMENTUM = 0.9
cfg.SOLVER.WEIGHT_DECAY = 0.0001
cfg.SOLVER.WEIGHT_DECAY_NORM = 0.0
cfg.MODEL.RETINANET.SCORE_THRESH_TEST = 0.8
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = 0.8
#学习率线性
# cfg.SOLVER.WARMUP_METHOD = "linear"
# cfg.SOLVER.GAMMA = 0.7 # lr down rate
# cfg.SOLVER.STEPS = (1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500) # lr delay each step
# cfg.SOLVER.WARMUP_FACTOR = 1.0 / 1000
# cfg.SOLVER.WARMUP_ITERS = 1000 # lr up under to base_lr
#学习率cos
cfg.SOLVER.LR_SCHEDULER_NAME ="WarmupCosineLR"
cfg.SOLVER.MAX_ITER = 4600
cfg.SOLVER.WARMUP_FACTOR = 1.0 / 1000
cfg.SOLVER.WARMUP_ITERS = 500 # lr up under to base_lr
cfg.SOLVER.CHECKPOINT_PERIOD = 500 # save model interval
#cfg.SOLVER.CHECKPT_PERIOD = 1 # save model interval??? no work
cfg.TEST.EVAL_PERIOD = 750 # 迭代到指定次数,进行一次评估
cfg.freeze()
default_setup(cfg, args)
return cfg
def main(args):
cfg = setup(args)
print(cfg)
# 注册数据集
register_dataset()
#只测试不训练
if args.eval_only:
model = Trainer.build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
res = Trainer.test(cfg, model)
if comm.is_main_process():
verify_results(cfg, res)
return res
#开始训练
trainer = Trainer(cfg)
trainer.resume_or_load(resume=args.resume) # If `resume==True`, and last checkpoint exists, resume from it, load all checkpointables
return trainer.train()
if __name__ == "__main__":
args = default_argument_parser().parse_args()
print("Command Line Args:", args)
launch(
main,
args.num_gpus, #使用的gpu数量
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)
4、 https://github.com/facebookresearch/detectron2/blob/main/tools/plain_train_net.py
5、Detectron2 代码 难 解读 - 连载中_空洞卷积-重新思考底层特征-程序员资料 - 程序员资料
6、目标检测tricks(基于detectron2)_任小喵r的博客-CSDN博客
7、detectron2和mmdetection对比_小涵涵的博客-程序员秘密_mmdetection和detectron2 - 程序员秘密
8、[Detectron2] 03-数据加载模块(dataloader) - 知乎
9、使用 Detectron2 中的 mask-RCNN 检测建筑物轮廓_RS2GIS的博客-CSDN博客_武汉大学建筑物数据集
10、[Detectron2] 03-自定义数据增强样例-StrictRandomCrop - 知乎
11、detectron2模型使用、读写、训练及测试_alex1801的博客-CSDN博客_detectron2模型导出
12、Detectron2训练自己的数据集(较详细)_qq_29750461的博客-CSDN博客_detectron2训练自己的数据集
13、detectron2(目标检测框架)无死角玩转-00:目录_江南才尽,年少无知!的博客-CSDN博客_detectron2框架
(后面自己写推理导出onnx时可以参考)
可以参考推理:
https://github.com/aengusng8/Sartorius-Cell-Instance-Segmentation)
16、Detectron2-数据增强(Data Augmentation)官方文档中文翻译__KATANA_的博客-CSDN博客_detectron2 数据增强
(copy-paste技术!)