Windows10+YOLOv3实现检测自己的数据集(1)——制作自己的数据集
<p><strong>本文将从以下三个方面先容如何制作自己的数据集</strong></p><ol>
<li>数据标注</li>
<li>数据扩增</li>
<li>将数据转化为COCO的json格式</li>
<li>参考资料</li>
</ol>
<h3>一、数据标注</h3>
<p>在深度学习的目标检测使掷中,起首要使用练习集举行模型练习。练习的数据集好坏决定了使命的上限。下面先容两种常用的图像目标检测标注工具:<strong>Labelme</strong>和<strong>LabelImg。</strong></p>
<p><strong>(1)Labelme</strong></p>
<p>Labelme实用于图像分割使命和目标检测使命的数据集制作,它来自该项目:https://github.com/wkentaro/labelme 。</p>
<p>按照项目中的教程安装完毕后,应用界面如下图所示:</p>
<p><div align="center"></div></p>
<p>它可以大概提供多边形、矩形、圆形、直线和点的图像标注,并将效果生存为 JSON 文件。</p>
<p><strong>(2)LabelImg</strong></p>
<p>LabelImg实用于目标检测使命的数据集制作。它来自该项目:https://github.com/tzutalin/labelImg</p>
<p>应用界面如下图所示:</p>
<p><div align="center"></div></p>
<p>它可以大概提供矩形的图像标注,并将效果生存为txt(YOLO)或xml(PascalVOC)格式。如果须要修改标签的类别内容,则在主目录data文件夹中的predefined_classes.txt文件中修改。</p>
<p>我使用的就是这一个标注软件,标注效果生存为xml格式,后续还须要举行标注格式的转换。</p>
<p><strong>操作快捷键:</strong></p>
<blockquote>
Ctrl + u加载目录中的全部图像,鼠标点击Open dir同功能
Ctrl + r更改默认解释目标目录(xml文件生存的地址)
Ctrl + s生存
Ctrl + d复制当前标签和矩形框
space 将当前图像标记为已验证
w 创建一个矩形框
d 下一张图片
a 上一张图片
del 删除选定的矩形框
Ctrl++ 放大
Ctrl-- 缩小
↑→↓← 键盘箭头移动选定的矩形框
</blockquote>
<h3>二、数据扩增</h3>
<p>在某些场景下的目标检测中,样本数目较小,导致检测的效果比力差,这时就须要举行数据扩增。本文先容常用的6类数据扩增方式,包罗裁剪、平移、改变亮度、加入噪声、旋转角度以及镜像。</p>
<p><strong>思量到篇幅标题,将这一部分单列出来,详细请参考本篇博客:https://www.cnblogs.com/lky-learning/p/11653861.html</strong></p>
<h3>三、将数据转换至COCO的json格式</h3>
<p>起首让我们明确一下几种格式,参考自【点此处】:</p>
<h4><strong>3.1 csv</strong></h4>
<ul>
<li><code>csv/</code>
<ul>
<li><code>labels.csv</code></li>
<li><code>images/</code>
<ul>
<li><code>image1.jpg</code></li>
<li><code>image2.jpg</code></li>
<li><code>...</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong><code>labels.csv</code> 的形式:</strong></p>
<ul>
<li><code>/path/to/image,xmin,ymin,xmax,ymax,label</code></li>
</ul>
<p>比方:</p>
<ul>
<li><code>/mfs/dataset/face/image1.jpg,450,154,754,341,face</code></li>
<li><code>/mfs/dataset/face/image2.jpg,143,154,344,341,face</code></li>
</ul>
<h4><strong>3.2 voc</strong></h4>
<p>标准的voc数据格式如下:</p>
<p><code>VOC2007/</code></p>
<ul>
<li><code>Annotations/</code>
<ul>
<li><code>0d4c5e4f-fc3c-4d5a-906c-105.xml</code></li>
<li><code>0ddfc5aea-fcdac-421-92dad-144/xml</code></li>
<li><code>...</code></li>
</ul>
</li>
<li><code>ImageSets/</code>
<ul>
<li><code>Main/</code>
<ul>
<li><code>train.txt</code></li>
<li><code>test.txt</code></li>
<li><code>val.txt</code></li>
<li><code>trainval.txt</code></li>
</ul>
</li>
</ul>
</li>
<li><code>JPEGImages/</code>
<ul>
<li><code>0d4c5e4f-fc3c-4d5a-906c-105.jpg</code></li>
<li><code>0ddfc5aea-fcdac-421-92dad-144.jpg</code></li>
<li><code>...</code></li>
</ul>
</li>
</ul>
<h4><strong>3.3 COCO</strong></h4>
<p><code>coco/</code></p>
<ul>
<li><code>annotations/</code>
<ul>
<li><code>instances_train2017.json</code></li>
<li><code>instances_val2017.json</code></li>
</ul>
</li>
<li><code>images/</code>
<ul>
<li><code>train2017/</code>
<ul>
<li><code>0d4c5e4f-fc3c-4d5a-906c-105.jpg</code></li>
<li><code>...</code></li>
</ul>
</li>
<li><code>val2017</code>
<ul>
<li><code>0ddfc5aea-fcdac-421-92dad-144.jpg</code></li>
<li><code>...</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong>Json file 格式:</strong> (imageData那一块太长了,不展示了)</p>
{
"version": "3.6.16",
"flags": {},
"shapes": [
{
"label": "helmet",
"line_color": null,
"fill_color": null,
"points": [
[
131,
269
],
[
388,
457
]
],
"shape_type": "rectangle"
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "004ffe6f-c3e2-3602-84a1-ecd5f437b113.jpg",
"imageData": "" # too long ,so not show here
"imageHeight": 1080,
"imageWidth": 1920
}
<p>在上一节中提到,经过标注后的效果生存为xml格式,我们起首要把这些xml标注文件整合成一个csv文件。</p>
<p>整合代码如下:</p>
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
## xml文件的路径
os.chdir('./data/annotations/scratches')
path = 'C:/Users/Admin/Desktop/data/annotations/scratches' # 绝对路径
img_path = 'C:/Users/Admin/Desktop/data/images'
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):#返回全部匹配的文件路径列表。
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
# value = (root.find('filename').text,
# int(root.find('size').text),
# int(root.find('size').text),
# member.text,
# int(member.text),
# int(member.text),
# int(member.text),
# int(member.text)
# )
value = (img_path +'/' + root.find('filename').text,
int(member.text),
int(member.text),
int(member.text),
int(member.text),
member.text
)
xml_list.append(value)
#column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
column_name = ['filename', 'xmin', 'ymin', 'xmax', 'ymax', 'class']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
if __name__ == '__main__':
image_path = path
xml_df = xml_to_csv(image_path)
## 修改文件名称
xml_df.to_csv('scratches.csv', index=None)
print('Successfully converted xml to csv.')
<p>当显示 Successfully converted xml to csv 后,我们就得到了整理后的标记文件。</p>
<p>在有些模型下,有了图像数据和csv格式的标注文件后,就可以举行练习了。但是在YOLOv3中,标记文件的类型为COCO的json格式,因此我们还得将其转换至json格式。</p>
<p>转换代码:</p>
import os
import json
import numpy as np
import pandas as pd
import glob
import cv2
import shutil
from IPython import embed
from sklearn.model_selection import train_test_split
np.random.seed(41)
# 0为配景
classname_to_id = {"scratches": 1,"inclusion": 2}
class Csv2CoCo:
def __init__(self,image_dir,total_annos):
self.images = []
self.annotations = []
self.categories = []
self.img_id = 0
self.ann_id = 0
self.image_dir = image_dir
self.total_annos = total_annos
def save_coco_json(self, instance, save_path):
json.dump(instance, open(save_path, 'w'), ensure_ascii=False, indent=2)# indent=2 更加美观显示
# 由txt文件构建COCO
def to_coco(self, keys):
self._init_categories()
for key in keys:
self.images.append(self._image(key))
shapes = self.total_annos
for shape in shapes:
bboxi = []
for cor in shape[:-1]:
bboxi.append(int(cor))
label = shape[-1]
annotation = self._annotation(bboxi,label)
self.annotations.append(annotation)
self.ann_id += 1
self.img_id += 1
instance = {}
instance['info'] = 'spytensor created'
instance['license'] = ['license']
instance['images'] = self.images
instance['annotations'] = self.annotations
instance['categories'] = self.categories
return instance
# 构建类别
def _init_categories(self):
for k, v in classname_to_id.items():
category = {}
category['id'] = v
category['name'] = k
self.categories.append(category)
# 构建COCO的image字段
def _image(self, path):
image = {}
img = cv2.imread(self.image_dir + path)
image['height'] = img.shape
image['width'] = img.shape
image['id'] = self.img_id
image['file_name'] = path
return image
# 构建COCO的annotation字段
def _annotation(self, shape,label):
# label = shape[-1]
points = shape[:4]
annotation = {}
annotation['id'] = self.ann_id
annotation['image_id'] = self.img_id
annotation['category_id'] = int(classname_to_id)
annotation['segmentation'] = self._get_seg(points)
annotation['bbox'] = self._get_box(points)
annotation['iscrowd'] = 0
annotation['area'] = 1.0
return annotation
# COCO的格式: 对应COCO的bbox格式
def _get_box(self, points):
min_x = points
min_y = points
max_x = points
max_y = points
return
# segmentation
def _get_seg(self, points):
min_x = points
min_y = points
max_x = points
max_y = points
h = max_y - min_y
w = max_x - min_x
a = []
a.append()
return a
if __name__ == '__main__':
## 修改目录
csv_file = "data/annotations/scratches/scratches.csv"
image_dir = "data/images/"
saved_coco_path = "./"
# 整合csv格式标注文件
total_csv_annotations = {}
annotations = pd.read_csv(csv_file,header=None).values
for annotation in annotations:
key = annotation.split(os.sep)[-1]
value = np.array(])
if key in total_csv_annotations.keys():
total_csv_annotations = np.concatenate((total_csv_annotations,value),axis=0)
else:
total_csv_annotations = value
# 按照键值划分数据
total_keys = list(total_csv_annotations.keys())
train_keys, val_keys = train_test_split(total_keys, test_size=0.2)
print("train_n:", len(train_keys), 'val_n:', len(val_keys))
## 创建必须的文件夹
if not os.path.exists('%ssteel/annotations/'%saved_coco_path):
os.makedirs('%ssteel/annotations/'%saved_coco_path)
if not os.path.exists('%ssteel/images/train/'%saved_coco_path):
os.makedirs('%ssteel/images/train/'%saved_coco_path)
if not os.path.exists('%ssteel/images/val/'%saved_coco_path):
os.makedirs('%ssteel/images/val/'%saved_coco_path)
## 把练习集转化为COCO的json格式
l2c_train = Csv2CoCo(image_dir=image_dir,total_annos=total_csv_annotations)
train_instance = l2c_train.to_coco(train_keys)
l2c_train.save_coco_json(train_instance, '%ssteel/annotations/instances_train.json'%saved_coco_path)
for file in train_keys:
shutil.copy(image_dir+file,"%ssteel/images/train/"%saved_coco_path)
for file in val_keys:
shutil.copy(image_dir+file,"%ssteel/images/val/"%saved_coco_path)
## 把验证集转化为COCO的json格式
l2c_val = Csv2CoCo(image_dir=image_dir,total_annos=total_csv_annotations)
val_instance = l2c_val.to_coco(val_keys)
l2c_val.save_coco_json(val_instance, '%ssteel/annotations/instances_val.json'%saved_coco_path)
<p>至此,我们的数据预处理惩罚工作就做好了</p>
<h3>四、参考资料</h3>
<ul>
<li>https://blog.csdn.net/sty945/article/details/79387054</li>
<li>https://blog.csdn.net/saltriver/article/details/79680189</li>
<li>https://www.ctolib.com/topics-44419.html</li>
<li>https://www.zhihu.com/question/20666664</li>
<li>https://github.com/spytensor/prepare_detection_dataset#22-voc</li>
<li>https://blog.csdn.net/chaipp0607/article/details/79036312</li>
</ul>
<p> </p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/lky-learning/p/11640180.html" target="_blank">https://www.cnblogs.com/lky-learning/p/11640180.html</a>
页:
[1]