当前位置:网站首页>[target detection] yolov5 Runtong voc2007 dataset (repair version)
[target detection] yolov5 Runtong voc2007 dataset (repair version)
2022-07-25 16:47:00 【zstar-_】
Preface
stay 【 object detection 】YOLOv5 Run through VOC2007 Data sets In the article , I wrote a script to extract VOC in Segmentation Divided data sets , But after observation , This train.txt Only in 209 Data , and VOC2007 There are pictures of 9963 Zhang , This means that a lot of pictures are wasted , Not input into the model for training .
therefore , This chapter will revise the data set processing flow , To solve this problem .
Data set partitioning
My idea is to divide directly according to the pictures , Never mind ImageSets The information of this folder .
Create in the root directory split_data.py
import os
import random
img_path = 'D:/Dataset/VOC2007/images/' # Picture folder path ( Don't miss the last slash )
label_path = 'D:/Dataset/VOC2007/' # Generate partition data path
img_list = os.listdir(img_path)
train_ratio = 0.8 # Training set ratio
val_ratio = 0.1 # Verification set ratio
shuffle = True # Whether to divide randomly
def data_split(full_list, train_ratio, val_ratio, shuffle=True):
n_total = len(full_list)
train_set_num = int(n_total * train_ratio)
val_set_num = int(n_total * val_ratio)
if shuffle:
random.shuffle(full_list)
train_set = full_list[:train_set_num]
val_set = full_list[train_set_num:(train_set_num + val_set_num)]
test_set = full_list[(train_set_num + val_set_num):]
return train_set, val_set, test_set
if __name__ == '__main__':
train_set, val_set, test_set = data_split(img_list, train_ratio, val_ratio, shuffle=True)
with open(label_path + 'train.txt', 'w') as f:
for img_name in train_set:
f.write(img_name.split('.jpg')[0] + '\n')
with open(label_path + 'val.txt', 'w') as f:
for img_name in val_set:
f.write(img_name.split('.jpg')[0] + '\n')
with open(label_path + 'test.txt', 'w') as f:
for img_name in test_set:
f.write(img_name.split('.jpg')[0] + '\n')
Here I set up a training set / Verification set / The scale of the test set is :8:1:1, And the division can be randomly disrupted , It can be modified if necessary .
After running , Generate divided data under the dataset folder :

Label conversion
and 【 object detection 】YOLOv5 Run through VOC2007 Data sets Same as in the text , We can still use the previous script for conversion , The difference is that the pointing path of the data partition set changes .
New in the root directory voc2yolo.py file
import xml.etree.ElementTree as ET
import os
sets = ['train', 'test', 'val']
Imgpath = 'D:/Dataset/VOC2007/images/'
xmlfilepath = 'D:/Dataset/VOC2007/Annotations/'
ImageSets_path = 'D:/Dataset/VOC2007/'
Label_path = 'D:/Dataset/VOC2007/'
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open(xmlfilepath + '%s.xml' % (image_id))
out_file = open(Label_path + 'labels/%s.txt' % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
for image_set in sets:
if not os.path.exists(Label_path + 'labels/'):
os.makedirs(Label_path + 'labels/')
image_ids = open(ImageSets_path + '%s.txt' % (image_set)).read().strip().split()
list_file = open(Label_path + '%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write(Imgpath + '%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
After running , Generate the corresponding labels, And the data set direction changes .

Training preparation
stay data New under folder mydata.yaml
train: D:/Dataset/VOC2007/train.txt
val: D:/Dataset/VOC2007/val.txt
test: D:/Dataset/VOC2007/test.txt
# number of classes
nc: 20
# class names
names: [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ]
The direction of the data is modified to its own path .
The remaining steps and above equally .
边栏推荐
- Exception handling mechanism topic 1
- MySQL linked table query, common functions, aggregate functions
- C # introductory basic tutorial
- Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
- Doget and dopost
- 测试框架-unittest-命令行操作、断言方法
- 月薪1万在中国是什么水平?答案揭露残酷的收入真相
- 【redis】redis安装
- Use huggingface to quickly load pre training models and datasets in moment pool cloud
- 【数学建模绘图系列教程】二、折线图的绘制与优化
猜你喜欢

2W word detailed data Lake: concept, characteristics, architecture and cases

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类

QT ListView 列表显示组件笔记
![[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code](/img/9e/138e4b160fa9bd6486fac44a788d09.png)
[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code

【目标检测】YOLOv5跑通VOC2007数据集(修复版)

How does win11's own drawing software display the ruler?

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

搜狗批量推送软件-搜狗批量推送工具【2022最新】

论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)

MySQL linked table query, common functions, aggregate functions
随机推荐
152. 乘积最大子数组
jenkins的文件参数,可以用来上传文件
Baidu rich text editor ueeditor image width 100% adaptive, mobile terminal
QT ListView 列表显示组件笔记
[book club issue 13] +ffmpeg open source project
Verifiable random function VRF
为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
IAAs infrastructure cloud cloud network
7.依赖注入
MySQL视图
MyBaits
3D semantic segmentation - PVD
C Music
Is it safe to open a securities account in Huatai VIP account
新增批量删除
Getting started with easyUI
【redis】redis安装
Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
城市燃气安全再拉警钟,如何防患于未“燃”?
异常处理机制专题1