当前位置:网站首页>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)
查看结果:
边栏推荐
- 【微服务】Nacos集群搭建以及加载文件配置
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- SQL injection - injection based on MSSQL (SQL Server)
- 1480. 一维数组的动态和
- ANSYS command
- gslb(global server load balance)技术的一点理解
- 每周小结(*63):关于正能量
- Upper computer software development - log information is stored in the database based on log4net
- 一键过滤选择百度网盘文件
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
猜你喜欢
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
BUU-Real-[PHP]XXE
Etcd database source code analysis - initialization overview
卸载Google Drive 硬盘-必须退出程序才能卸载
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Letter meaning and parameter abbreviation of optical module Daquan
JS arguments parameter usage and explanation
Solar insect killing system based on single chip microcomputer
(4) Canal multi instance use
Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
随机推荐
19.Frambuffer应用编程
谷歌 Chrome 浏览器将支持选取文字翻译功能
[excel] PivotChart
卸载Google Drive 硬盘-必须退出程序才能卸载
Zzulioj:1201: mode problem
Flink1.13 basic SQL syntax (II) join operation
十二. golang其他
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
Talk about the SQL server version of DTM sub transaction barrier function
基于单片机的太阳能杀虫系统
JS arguments parameter usage and explanation
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
Simulink and Arduino serial port communication
Excel comparator
LC weekly 300
Notepad++ -- display related configurations
Grounding relay dd-1/60
1480. Dynamic sum of one-dimensional array
Actual cases and optimization solutions of cloud native architecture
Descriptive analysis of data distribution characteristics (data exploration)