当前位置:网站首页>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)
查看结果:
边栏推荐
- Flink1.13 basic SQL syntax (II) join operation
- LM small programmable controller software (based on CoDeSys) note XXI: error 3703
- How to determine whether an array contains an element
- [microservice] Nacos cluster building and loading file configuration
- BUU-Pwn-test_ your_ nc
- 1.1 history of Statistics
- ANSYS command
- The end of the Internet is rural revitalization
- 接地继电器DD-1/60
- JS string splicing enhancement
猜你喜欢
Grounding relay dd-1/60
2022 R2 mobile pressure vessel filling retraining question bank and answers
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
基于单片机的太阳能杀虫系统
Online shrimp music will be closed in January next year. Netizens call No
Arc135 C (the proof is not very clear)
SQL injection - injection based on MSSQL (SQL Server)
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
卸载Google Drive 硬盘-必须退出程序才能卸载
光模块字母含义及参数简称大全
随机推荐
BUU-Real-[PHP]XXE
724. Find the central subscript of the array
Install pytoch geometric
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
【微服务】Nacos集群搭建以及加载文件配置
Tutle clock improved version
如何获取el-tree中所有节点的父节点
(4) Canal multi instance use
Ping port artifact psping
卸载Google Drive 硬盘-必须退出程序才能卸载
What are the reasons for the frequent high CPU of ECS?
Topological sorting and graphical display of critical path
Thinkphp6.0 middleware with limited access frequency think throttle
VB.net 简单的处理图片,黑白(类库——7)
JS how to convert seconds into hours, minutes and seconds display
Accidentally deleted the data file of Clickhouse, can it be restored?
19. Framebuffer application programming
Appearance of LabVIEW error dialog box
2022 a special equipment related management (elevator) examination questions simulation examination platform operation