当前位置:网站首页>Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
2022-07-04 05:34:00 【Jayce~】
2021/12/6更新:完整代码看这里:
转COCO格式代码https://download.csdn.net/download/qq_15969343/85088683
2021/7/6更新:还是windows上面用起来比较舒服!欢迎使用更快,算法更多的Detectron2:
以下为原文:
Detectron系列:
1.前情提要
Fackbook的开源检测框架Detectron已开源了一段时间:
https://github.com/facebookresearch/Detectron
但苦于之前都是用Keras和Tensorflow,没接触过caffe2,所以一直没有尝试,~~~并且之前没用过linux系统,对于命令基本不懂~~,好在会用浏览器,遇到问题就查查,也算是把坑都给踩遍了,最后总算有惊无险,用上了强大的Detectron啦。
关于安装caffe2和Detectron等有空了在写~! 这篇文章主要说一下如何制作该框架所需的标注格式!但是呢~~~由于自定义自己的数据集比较繁琐,推荐使用Detectron自带的COCO数据集名称,并将自己的数据集转化为COCO数据集的格式,当然如果你是大佬,忽略这个~~现在,没错,就是现在~~~来分享一下如何生成这种COCO格式~~~
2.COCO数据集格式
首先,通过COCO - Common Objects in Context(COCO官网)了解了coco数据集的格式如下:
对于目标检测的话,还需要关注BBOX格式如下:
那么,我们只要将我们的数据格式转换为以上即可~
3.转化自己的数据集
3.1数据放置方式
我们需要创建一个文件夹,用于存放图片以及标注数据,具体的放置方式如下:
├── annos.txt
├── annotations
├── classes.txt
└── images
其中,annos放置数据集的原始标注文件,可能是txt,或者csv格式;classes.txt放置你标注的类别名称,每行一个类别,不含背景;images放置数据集的原始图像文件。annotations预备放置与COCO数据集格式的标注文件。下面需要将我们自己的标注文件生成COCO格式的标注文件。
3.2转换自己的数据
我自己的数据如下所示:
每行对应一条BBOX标记:filename,label, x_min, y_min, x_max, y_max ,下面开始转换:
我们使用os提取images文件夹中的图片名称,并且将BBox都读进去:
import json
import os
import cv2
# 根路径,里面包含images(图片文件夹),annos.txt(bbox标注),classes.txt(类别标签),以及annotations文件夹(如果没有则会自动创建,用于保存最后的json)
root_path = 'E:\dogcat\\f_train\data\\get_json\\'
# 用于创建训练集或验证集
phase = 'val'
# 训练集和验证集划分的界线
split = 8000
# 打开类别标签
with open(os.path.join(root_path, 'classes.txt')) as f:
classes = f.read().strip().split()
# 建立类别标签和数字id的对应关系
for i, cls in enumerate(classes, 1):
dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})
# 读取images文件夹的图片名称
indexes = [f for f in os.listdir(os.path.join(root_path, 'images'))]
# 判断是建立训练集还是验证集
if phase == 'train':
indexes = [line for i, line in enumerate(_indexes) if i <= split]
elif phase == 'val':
indexes = [line for i, line in enumerate(_indexes) if i > split]
# 读取Bbox信息
with open(os.path.join(root_path, 'annos.txt')) as tr:
annos = tr.readlines()
接着将,以上数据转换为COCO所需要的
for k, index in enumerate(indexes):
# 用opencv读取图片,得到图像的宽和高
im = cv2.imread(os.path.join(root_path, 'images/') + index)
height, width, _ = im.shape
# 添加图像的信息到dataset中
dataset['images'].append({'file_name': index,
'id': k,
'width': width,
'height': height})
对于一个图有着多个框的情况判断一下:
for ii, anno in enumerate(annos):
parts = anno.strip().split()
# 如果图像的名称和标记的名称对上,则添加标记
if parts[0] == index:
# 类别
cls_id = parts[1]
# x_min
x1 = float(parts[2])
# y_min
y1 = float(parts[3])
# x_max
x2 = float(parts[4])
# y_max
y2 = float(parts[5])
width = max(0, x2 - x1)
height = max(0, y2 - y1)
dataset['annotations'].append({
'area': width * height,
'bbox': [x1, y1, width, height],
'category_id': int(cls_id),
'id': i,
'image_id': k,
'iscrowd': 0,
# mask, 矩形是从左上角点按顺时针的四个顶点
'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
})
接着将结果保存:
# 保存结果的文件夹
folder = os.path.join(root_path, 'annotations')
if not os.path.exists(folder):
os.makedirs(folder)
json_name = os.path.join(root_path, 'annotations/{}.json'.format(phase))
with open(json_name, 'w') as f:
json.dump(dataset, f)
查看结果:
边栏推荐
- 安装 Pytorch geometric
- ansys命令
- js如何将秒转换成时分秒显示
- left_and_right_net正常版本
- 简易零钱通
- How to implement lazy loading in El select (with search function)
- Thinkphp6.0 middleware with limited access frequency think throttle
- LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052
- Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
- JS flattened array of number shape structure
猜你喜欢
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
C语言简易学生管理系统(含源码)
2022 a special equipment related management (elevator) examination questions simulation examination platform operation
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
Flask
拓扑排序和关键路径的图形化显示
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
input显示当前选择的图片
Supplement the JS of a video website to decrypt the video
随机推荐
win10清除快速访问-不留下痕迹
The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
How to implement lazy loading in El select (with search function)
19.Frambuffer应用编程
BUU-Crypto-[GXYCTF2019]CheckIn
Recommended system 1 --- framework
Basic concept of bus
VB.net GIF(制作、拆解——优化代码,类库——5)
接地继电器DD-1/60
How much computing power does transformer have
What is MQ?
Viewing and using binary log of MySQL
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
JS arguments parameter usage and explanation
The end of the Internet is rural revitalization
Tutle clock improved version
el-select如何实现懒加载(带搜索功能)
JS string splicing
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052