当前位置:网站首页>[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 .
边栏推荐
- 7.依赖注入
- 0x80131500 solution for not opening Microsoft Store
- 自定义mvc项目登录注册和树形菜单
- easyui入门
- Today, I went to oppo for an interview and was asked numbly
- 2W word detailed data Lake: concept, characteristics, architecture and cases
- Various useful forms of London Silver K-line chart
- 2D semantic segmentation -- deeplabv3plus reproduction
- The presentation logic of mail sending and receiving inbox outbox and reply to the problem of broken chain
- 简述redis集群的实现原理
猜你喜欢

Quickly deploy mqtt clusters on AWS using terraform

微信公众号开发之消息的自动回复

百度富文本编辑器UEditor单张图片上传跨域

Breakthrough in core technology of the large humanoid Service Robot Walker x

easyui修改以及datagrid dialog form控件使用

Enterprise live broadcast: witness focused products, praise and embrace ecology

测试驱动开发(TDD)在线练功房 | 9月17日开课

为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台

3D semantic segmentation - scribed supervised lidar semantic segmentation

2D 语义分割——DeepLabV3plus 复现
随机推荐
152. 乘积最大子数组
LVGL 7.11 tileview界面循环切换
中国芯片自给率大幅提升,导致外国芯片库存高企而损失惨重,美国芯片可谓捧起石头砸自己的脚...
7.依赖注入
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
测试框架-unittest-命令行操作、断言方法
152. Product maximum subarray
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
Communication between processes (pipeline details)
win10设备管理认不到GTX1080Ti 显示设备的解决办法
IaaS基础架构云 —— 云网络
【读书会第13期】+FFmpeg开源项目
win10如何删除微软拼音输入法
在华为昇腾Ascend910上复现swin_transformer
[OBS] frame loss and frame priority before transmission
首页门户分类查询
激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
unity 最好用热更方案卧龙 wolong
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
Use huggingface to quickly load pre training models and datasets in moment pool cloud