当前位置:网站首页>Detectron: train your own data set -- convert your own data format to coco format
Detectron: train your own data set -- convert your own data format to coco format
2022-07-04 05:53:00 【Jayce~】
2021/12/6 to update : See here for the complete code :
turn COCO Format code
https://download.csdn.net/download/qq_15969343/85088683
2021/7/6 to update : still windows It's more comfortable to use ! Welcome to use faster , Algorithm more Detectron2:
WIN10 The installation is based on Pytorch1.8.1、Cuda11.1.1 Of Detectron2(0.1 or 0.4.1)
The following is the original :
Detectron series :
1. Antecedents feed
Fackbook Open source detection framework Detectron It has been open source for some time :
https://github.com/facebookresearch/Detectron
But I used to use Keras and Tensorflow, No contact caffe2, So I haven't tried ,~~~ And I haven't used it before linux System , Basically don't understand commands ~~, Fortunately, I can use the browser , If you encounter problems, check , It can be regarded as stepping on all the pits , Finally, there was no danger , With powerful Detectron La .
About installation caffe2 and Detectron I'm writing when I'm free ~! This article mainly talks about how to make the annotation format required by the framework ! But what? ~~~ Because customizing your own dataset is cumbersome , Recommended Detectron Self contained COCO Dataset name , And convert your data set into COCO The format of the dataset , Of course, if you're big , Ignore this ~~ Now? , you 're right , The time is now ~~~ Let's share how to generate this COCO Format ~~~
2.COCO Dataset format
First , adopt COCO - Common Objects in Context(COCO Official website ) I understand coco The format of the data set is as follows :

For target detection , We need to pay attention to BBOX The format is as follows :

that , We just need to convert our data format to the above ~
3. Transform your own data set
3.1 Data placement
We need to create a folder , Used for storing pictures and marking data , The specific placement method is as follows :
├── annos.txt
├── annotations
├── classes.txt
└── images
among ,annos Place the original annotation file of the dataset , May be txt, perhaps csv Format ;classes.txt Place the category name you marked , One category per line , No background ;images Place the original image file of the dataset .annotations Prepare to place with COCO Annotation file in dataset format . Next, we need to generate our own annotation file COCO Format label file .
3.2 Convert your own data
My own data is shown below :

Each line corresponds to one BBOX Mark :filename,label, x_min, y_min, x_max, y_max , Now let's start the conversion :
We use os extract images The name of the picture in the folder , And will BBox Read it all :
import json
import os
import cv2
# The root path , It contains images( Picture folder ),annos.txt(bbox mark ),classes.txt( Category label ), as well as annotations Folder ( If not, it will be created automatically , Used to save the last json)
root_path = 'E:\dogcat\\f_train\data\\get_json\\'
# Used to create training sets or validation sets
phase = 'val'
# The boundary between training set and verification set
split = 8000
# Open the category tab
with open(os.path.join(root_path, 'classes.txt')) as f:
classes = f.read().strip().split()
# Create category labels and numbers id Correspondence of
for i, cls in enumerate(classes, 1):
dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})
# Read images The picture name of the folder
indexes = [f for f in os.listdir(os.path.join(root_path, 'images'))]
# Determine whether to establish a training set or a verification set
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]
# Read Bbox Information
with open(os.path.join(root_path, 'annos.txt')) as tr:
annos = tr.readlines()And then , The above data is converted to COCO The required
for k, index in enumerate(indexes):
# use opencv Read the picture , Get the width and height of the image
im = cv2.imread(os.path.join(root_path, 'images/') + index)
height, width, _ = im.shape
# Add image information to dataset in
dataset['images'].append({'file_name': index,
'id': k,
'width': width,
'height': height})For a graph with multiple boxes, judge :
for ii, anno in enumerate(annos):
parts = anno.strip().split()
# If the name of the image and the name of the tag are aligned , Then add a mark
if parts[0] == index:
# Category
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, The rectangle is four vertices clockwise from the top left corner
'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
})Then save the results :
# The folder where the results are saved
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)View results :

边栏推荐
- APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
- Basic concept of bus
- Actual cases and optimization solutions of cloud native architecture
- 724. 寻找数组的中心下标
- fastjson
- 【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
- Canoe panel learning video
- Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
- LC weekly 300
- BUU-Crypto-Cipher
猜你喜欢
![BUU-Crypto-[HDCTF2019]basic rsa](/img/d0/8e451dabb2a6897f6680220d16d04d.jpg)
BUU-Crypto-[HDCTF2019]basic rsa

检漏继电器JY82-2P

input显示当前选择的图片

一键过滤选择百度网盘文件

19. Framebuffer application programming

测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文

js arguments参数使用和详解

报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。

BUU-Crypto-Cipher

A little understanding of GSLB (global server load balance) technology
随机推荐
ES6 模块化
FRP intranet penetration, reverse proxy
XII Golang others
Nexus 6p从8.0降级6.0+root
BUU-Real-[PHP]XXE
js如何将秒转换成时分秒显示
The end of the Internet is rural revitalization
js获取对象中嵌套的属性值
Invalid revision: 3.18.1-g262b901-dirty
Leakage detection relay jy82-2p
My NVIDIA developer journey - optimizing graphics card performance
How to configure static IP for Kali virtual machine
Excel comparator
冲击继电器JC-7/11/DC110V
JS flattened array of number shape structure
ANSYS command
C语言练习题(递归)
Easy change
left_ and_ right_ Net interpretable design
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块