网站空间查询工具,WordPress对象储存插件,学做网站论坛vip账号破解,企业网站广告COCO 数据集格式及mmdetection中的转换方法
COCO格式
CV中的目标检测任务不同于分类#xff0c;其标签的形式稍为复杂#xff0c;有几种常用检测数据集格式#xff0c;本文将简要介绍最为常见的COCO数据集的格式。
完整的官方样例可自行查阅#xff0c;以下是几项关键的…COCO 数据集格式及mmdetection中的转换方法
COCO格式
CV中的目标检测任务不同于分类其标签的形式稍为复杂有几种常用检测数据集格式本文将简要介绍最为常见的COCO数据集的格式。
完整的官方样例可自行查阅以下是几项关键的字段
{images: [image],annotations: [annotation],categories: [category]
}image {id: int,width: int,height: int,file_name: str,
}annotation {id: int,image_id: int,category_id: int,segmentation: RLE or [polygon],area: float,bbox: [x,y,width,height],iscrowd: 0 or 1,
}categories [{id: int,name: str,supercategory: str,
}]以下是一组实例
dict{images: list[dict{id: 0,file_name: 34020010494_e5cb88e1c4_k.jpg,height: 1536,width: 2048}, ...annotations: list[dict{image_id: 0,id: 1,category_id: 0,bbox: [135, 115, 676, 850],area: 574600,segmentation: [[586.5, ...], []],is_crowd: 0}, ...catgories: list[dict{id: 0,name: balloon},...]]]
}首先最外层json文件读进来是一个字典该字典有三个键images, annotations, categories 其中images是一个列表长度就是数据集图像数 列表中每个元素又是一个字典有四个键id, file_name, height, width和他们的值 其中annotations也是一个列表长度就是数据集所有标注instances数 列表中每个元素又是一个字典有七个键image_id, id, category_id, bbox, area, segmentation, is_crowd 其中categories是一个列表长度为数据集所含有的类别数 列表中每个元素又是一个字典有两个键id, namemmdetection中的转换方法
官方例程
mmdetection中提供了几种训练自己的数据集的方法其中之一就是将自己的数据集转换为COCO格式进行训练。
官方给出了balloon数据集的转换样例初次尝试可以按照此例程学习这里同时提供balloon数据集下载链接。
其他例程
除此之外笔者想在这里介绍一下另一个非官方的数据集转换为COCO格式的例程供读者参考。
首先介绍一下这个pig数据集。
与balloon数据集类似pig数据集只有pig一个类共700个样本原始给出的数据集格式如下 文件路径 imagesxxx.jpg...
labelsxxx.json...本数据集有两个文件夹分别存放images和labels对应文件名相同扩展名分别为jpg和json。 标注格式 json文件中给出的标注格式如下 dict{shape: list[dict{label: pig,boxes: [x1, x2, y1, y2],points: [[x1, y1], [x2, y2], ..., [xn, yn]]}imagePath: xxx.jpg]
}json文件最外层是一个字典该字典有两个键shapeimagePath; 其中shape内是一个列表列表的长度是该张图片内pig的个数 列表内每个元素又是一个字典有三个键labelboxespoints 其中imagePath内则是本标注文件所对应的图片名。原本数据集内700个样本有500个box标注其他200个是mask标注经过我的初步处理已经将没有box标注的样本根据mask标注生成了box标注。其中box标注是给出了左上右下两个对角坐标而mask标注也是给出了全部点的坐标。注意此处与COCO数据集给出标注的格式不同需要做一定的转换。
转换为COCO数据集格式的函数如下
import os
import os.path as osp
import mmcvdef convert_pig_to_coco(img_dir, label_dir, out_file):samples_list [sample.split(.)[0] for sample in os.listdir(label_dir)]obj_count 0images []annotations []for image_id, sample in enumerate(mmcv.track_iter_progress(samples_list)):prev_dict mmcv.load(osp.join(label_dir, sample.json))height, width mmcv.imread(osp.join(img_dir, sample.jpg)).shape[: 2]images.append(dict(id image_id,file_name sample.jpg,height height,width width))for pig in prev_dict[shape]:# print(pig[boxes])x_min, y_min, x_max, y_max pig[boxes]mask pig[points]if mask ! None:px [item[0] for item in mask]py [item[1] for item in mask]poly [(x 0.5, y 0.5) for x, y in zip(px, py)]poly [p for x in poly for p in x]else:poly Nonedata_anno dict(image_idimage_id,idobj_count,category_id0,bbox[x_min, y_min, x_max - x_min, y_max - y_min],area(x_max - x_min) * (y_max - y_min),segmentation[poly],iscrowd0)annotations.append(data_anno)obj_count 1coco_format_json dict(imagesimages,annotationsannotations,categories[{id:0, name: pig}])mmcv.dump(coco_format_json, out_file)
提供这个额外的例程是为了说明不必拘泥于官方给出的转换例程框架要抓住数据集的本质按照合理的方式将原格式内的数据读取出来并以COCO数据集的格式存放到一整个json文件中即可。只要完成这项任务就是成功完成了自己的数据集到COCO格式的数据集的转换。 另外如果有与本数据集原格式高度相似的检测数据集要转换为COCO格式也可直接参考本例程的内容。