目录
作为标准数据集,voc-2007 是衡量图像分类识别能力的基准。
可视化txt格式检测标注:
txt格式是 x_center,y_center, width,height
import os
import cv2
import numpy as np
import torch
def xyxy2xywh(x):
# Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center
y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center
y[:, 2] = x[:, 2] - x[:, 0] # width
y[:, 3] = x[:, 3] - x[:, 1] # height
return y
def segments2boxes(segments):
# Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh)
boxes = []
for s in segments:
x, y = s.T # segment xy
boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy
return xyxy2xywh(np.array(boxes)) # cls, xywh
if __name__ == '__main__':
dir_a=r'D:\xxx'
debug_show=True
img_files = ['%s/%s' % (i[0], j) for i in os.walk(dir_a) for j in i[-1] if
j.endswith(('bmp', 'jpg', 'png', 'jpeg', 'JPG'))]
for im_file in img_files:
img = cv2.imread(im_file)
height,width=img.shape[:2]
lb_file=im_file[:-4]+".txt"
# verify labels
if os.path.isfile(lb_file):
with open(lb_file, 'r') as f:
l = [x.split() for x in f.read().strip().splitlines()]
if any([len(x) > 8 for x in l]): # is segment
classes = np.array([x[0] for x in l], dtype=np.float32)
segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...)
l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
l = np.array(l, dtype=np.float32)
for row_data in l:
c_x1 = float(row_data[1])*width
c_y1 = float(row_data[2])*height
b_width = float(row_data[3])*width
b_height = float(row_data[4])*height
x1=c_x1-b_width/2.0
y1=c_y1-b_height/2.0
x2=c_x1+b_width/2.0
y2=c_y1+b_height/2.0
if debug_show:
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), thickness=1)
cv2.imshow("asdf",img)
cv2.waitKey()
VOC2007基本信息 xml格式
Pascal VOC 2007 数据集简介
Annotations:XML文件集合。
文件名、长、宽、通道数、目标类型、目标坐标......
ImageSets:Layout、Main、Segmentation
Layout:train文件名、val文件名、train+val总文件名
Main:各种类型文件名集合
Segmentation:分割train、val、train+val
JPEGImages:图片集合
SegmentationClass:分割图集合
SegmentationObject:实例分割图集合
作为标准数据集,voc-2007 是衡量图像分类识别能力的基准。
faster-rcnn,yolo -v1, yolo-v2都以此数据集为最为演示样例,因此,有必要了解一下本数据集的组成架构。
VOC数据集共包含:训练集(5011幅),测试集(4952幅),共计9963幅图,共包含20个种类。
aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
pottedplant
sheep
sofa
train
tvmonitor
数据集的组成架构如下:
- Annotations —目标真值区域
- ImageSets —-类别标签
- JPEGImages —–图像
- SegmentationClass
- SegmentationObjec
JPEGImages 中存放原始图像,jpg格式。大小一般为 500*375 或 375*500;
ImageSets 中有三个文件夹【Layout】【Main】【Segmentation】,分类识别我们只关注【Main】,它内部存储类别标签,-1表示负样本,+1为正样本
*_train.txt 训练样本集
*_val.txt 评估样本集
*_trainval.txt 训练与评估样本汇总
2 各类别统计信息
20个类别中,后面数字代表数据集中对应的的正样本图像个数(非目标个数)。
- 训练集
aeroplane 238
bicycle 243
bird 330
boat 181
bottle 244
bus 186
car 713
cat 337
chair 445
cow 141
diningtable 200
dog 421
horse 287
motorbike 245
person 2008
pottedplant 245
sheep 96
sofa 229
train 261
tvmonitor 256
- 测试集
aeroplane 204
bicycle 239
bird 282
boat 172
bottle 212
bus 174
car 721
cat 322
chair 417
cow 127
diningtable 190
dog 418
horse 274
motorbike 222
person 2007
pottedplant 224
sheep 97
sofa 223
train 259
tvmonitor 229
可以看出,除了person数量较多,其他类别样本个数不算多,在如此小的数据集上,深度学习能获得较高的分类识别结果,足以说明深度学习的强大性能。
现在深度学习很多框架都在使用VOC数据集,来研究一下voc数据集的具体内容
一般voc解压出来后都包括Annotations,ImageSets,JPEFImages,SegmentationClass ,SegmentationObject;
Annotations中是放着所有图片的标记信息,以xml为后缀名.以分类检测的数据为例,打开ImageSets中的layout,会有train,trainval,val三个txt格式数据,:
1 train 很明显是训练数据(注意,均为图片名,没有后缀)
2 val 验证数据
3 trainval 则是所有训练和验证数据
4 test 测试数据
而ImageSets中的Main文件夹中保存的是各类数据出现的统计,以areoplane为例,有三个相关文件aeroplane_train.txt,areoplane_val,areoplane_trainval.txt,以areoplain_train.txt为例,分为两列,第一列为图像名如00012(注意没有后缀),第二列为-1和1,-1表示目标在对应的图像没有出现,1则表示出现。
segmentationclass和segmentationobject中均为分割后的结果
Anotation文件夹中包含了所有train和val的标记信息,标记信息均以xml结尾,其中,以(x,y)的格式保存坐标点.在实际应用中,要不把数据集改为voc形式,如果使用txt
JPEGImages
这个文件夹主要放置数据的原始图片,图片的文件名用00001.jpg进行命名。
Annotations
这个文件夹放置的是对每一张图片的标注。标注使用XML文件的格式。XML是标记语言,形如HTML,详细概念参看百度。每一个XML文件对应一张图片的标注结果,我们以上图000005.jpg的标注为例进行说明。
首先该图的内容是这样的:
这张图片对应的XML文件(000005.xml)标记结果如下:
<annotation>
<folder>VOC2007</folder>
<!--文件名-->
<filename>000005.jpg</filename>.
<!--数据来源-->
<source>
<!--数据来源-->
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<!--来源是flickr,一个雅虎的图像分享网站,下面是id,对于我们没有用-->
<image>flickr</image>
<flickrid>325991873</flickrid>
</source>
<!--图片的所有者,也没有用-->
<owner>
<flickrid>archintent louisville</flickrid>
<name>?</name>
</owner>
<!--图像尺寸,宽、高、长-->
<size>
<width>500</width>
<height>375</height>
<depth>3</depth>
</size>
<!--是否用于分割,0表示用于,1表示不用于-->
<segmented>0</segmented>
<!--下面是图像中标注的物体,每一个object包含一个标准的物体-->
<object>
<!--物体名称,拍摄角度-->
<name>chair</name>
<pose>Rear</pose>
<!--是否被裁减,0表示完整,1表示不完整-->
<truncated>0</truncated>
<!--是否容易识别,0表示容易,1表示困难-->
<difficult>0</difficult>
<!--bounding box的四个坐标-->
<bndbox>
<xmin>263</xmin>
<ymin>211</ymin>
<xmax>324</xmax>
<ymax>339</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>165</xmin>
<ymin>264</ymin>
<xmax>253</xmax>
<ymax>372</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>1</difficult>
<bndbox>
<xmin>5</xmin>
<ymin>244</ymin>
<xmax>67</xmax>
<ymax>374</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>241</xmin>
<ymin>194</ymin>
<xmax>295</xmax>
<ymax>299</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>1</difficult>
<bndbox>
<xmin>277</xmin>
<ymin>186</ymin>
<xmax>312</xmax>
<ymax>220</ymax>
</bndbox>
</object>
</annotation>