Detectron2 official Documents: https://detectron2.readthedocs.io/tutorials/datasets.html
目录
COCO格式数据集
ref: COCO - Common Objects in Contexthttps://cocodataset.org/#format-data
有两种方法,第一种是Detectron2官方提供的组织数据集格式:Standard Dataset Dict;第二种是将数据集转换为COCO format,然后用register_coco_instances()函数注册数据集。
from detectron2.data.datasets import register_coco_instances
register_coco_instances(dataset_name, {}, json_file, image_root)
在COCO格式下,所有图片需放在一个文件夹下,另有一个单独的json文件。这个json文件包含了所有的annotations, metadata, categories。如果想要对数据集做划分,那么就建立多个文件夹,并用不同的json文件。 具体来说:
- the coco dataset's json is a dict, and its keys is categories, images, annotations;
- the categories is a list, and each item is a dict which keys are supercategory,id,name;
- the images is a list, and each item is a dict which keys are file_name,height,width,id,depth;
- the annotations is a list, and each item is a dict which keys are category_id,segmentation,area,id,iscrowd
Standard dataset dict
dataset APIs: DatasetCatalog, MetadataCatalog
建立自己的数据集,包含两步:
- 登记数据集,告诉detectron2如何获得数据集;
- 为数据集登录Metadata
建立函数,范数返回如下格式的数据:
- Detectron2的Standard dataset dict
- 增加额外的key,方便在之后的downstream的任务中处理他们
对于标准任务:instance detection, instance/semantic/panoptic segmentation, keypoint detection。我们把原始数据载入到list[dict]中,类似于COCO的annotations。每一个字典包含一张照片的信息,字典应该有以下部分:
- file_name:到这张图片的完整路径,如果图片有EXIF metadata,可能会在图片上做随机旋转或反转
- height, width:整型,图片的形状
- image_id:整型/字符,unique id
- annotations:list[dict],每一个dict对应图片中一个instance,如果annotations是空表,则代表该图片没有目标,会在训练中被去除,具体每个dict包含:
bbox list[float]:代表bbox的四个数
bbox_mode:整型,bbox的格式,必须是structures.BoxMode的成员,目前支持BoxMode.XYXY_ABS和BoxMode.XYWH_ABS
category_id:整型,属于[0, num_categories - 1]
- 对于Fast R-CNN (with precomputed proposals),还有一些额外的keys需要:
proposal_bboxes (array) 2维numpy array,形状是(K, 4),代表K个precomputed proposal boxes
proposal_objectness_logits numpy array,形状是(K, )和proposal在proposal_boxes的objectness logits相关
proposal_bbox_mode 类似于bbox_mode
Datasets的Metadata
通过MetadataCatalog.get(dataset_name).some_metadata获取,Metadata是一个键值对,包含了整个数据的一些信息,例如,类别的名字,类别的颜色,文件的根目录。这对于augmentation,evaluation,visualization,Logging等有用。
通过MetadataCatalog.get(dataset_name).some_key = some_value来填充
- thing_classes: list[str]
- thing_colors: list[tuple(r, g, b)] r,g,b in [0,255],如果不给,则随机产生
另外还需更新新数据集的config:
cfg.Datasets.TRAIN或cfg.Datasets.TEST
MODEL.ROI_HEADS.NUM_CLASSES和MODEL.RETINANET.NUM_CLASSES是R-CNN和RetinaNet的类