1.命名

文件夹名VOC2007。图片名六位数字。将数据集相应的替换掉VOC2007中的数据。
 

2.画目标包围框

由于每张图片需要选取目标框,所需时间较长,需要工具辅助。

下面文字和代码源自wuzuyu365的博文深度学习python图像标记工具labelTool

深度学习训练需要标记图像位置和类别,之前用的时候是叫做BBox-Label-Tool-master,遇到大图像就显示不完整了,没有自适应缩放, 这是改进后的Python脚本。

目录结构:图片目录名images, 标签目录名labels,图像目录下各类别目录名要以001,002,003,...的格式命名。

 # -*- coding:utf-8 -*-
# -------------------------------------------------------------------------------
# Name: Object bounding box label tool
# Purpose: Label object bboxes for ImageNet Detection data
# Author: Qiushi
# Created: 06/06/2014 #
# -------------------------------------------------------------------------------
from __future__ import division
from Tkinter import *
import tkMessageBox
from PIL import Image, ImageTk
import os
import glob
import random w0 = 1; # 图片原始宽度
h0 = 1; # 图片原始高度 # colors for the bboxes
COLORS = ['red', 'blue', 'yellow', 'pink', 'cyan', 'green', 'black']
# image sizes for the examples
SIZE = 256, 256 # 指定缩放后的图像大小
DEST_SIZE = 500, 500 class LabelTool():
def __init__(self, master):
# set up the main frame
self.parent = master
self.parent.title("LabelTool")
self.frame = Frame(self.parent)
self.frame.pack(fill=BOTH, expand=1)
self.parent.resizable(width=TRUE, height=TRUE) # initialize global state
self.imageDir = ''
self.imageList = []
self.egDir = ''
self.egList = []
self.outDir = ''
self.cur = 0
self.total = 0
self.category = 0
self.imagename = ''
self.labelfilename = ''
self.tkimg = None # initialize mouse state
self.STATE = {}
self.STATE['click'] = 0
self.STATE['x'], self.STATE['y'] = 0, 0 # reference to bbox
self.bboxIdList = []
self.bboxId = None
self.bboxList = []
self.hl = None
self.vl = None # ----------------- GUI stuff ---------------------
# dir entry & load
self.label = Label(self.frame, text="Image Dir:")
self.label.grid(row=0, column=0, sticky=E)
self.entry = Entry(self.frame)
self.entry.grid(row=0, column=1, sticky=W + E)
self.ldBtn = Button(self.frame, text="Load", command=self.loadDir)
self.ldBtn.grid(row=0, column=2, sticky=W + E) # main panel for labeling
self.mainPanel = Canvas(self.frame, cursor='tcross')
self.mainPanel.bind("<Button-1>", self.mouseClick)
self.mainPanel.bind("<Motion>", self.mouseMove)
self.parent.bind("<Escape>", self.cancelBBox) # press <Espace> to cancel current bbox
self.parent.bind("s", self.cancelBBox)
self.parent.bind("a", self.prevImage) # press 'a' to go backforward
self.parent.bind("d", self.nextImage) # press 'd' to go forward
self.mainPanel.grid(row=1, column=1, rowspan=4, sticky=W + N) # showing bbox info & delete bbox
self.lb1 = Label(self.frame, text='Bounding boxes:')
self.lb1.grid(row=1, column=2, sticky=W + N) self.listbox = Listbox(self.frame, width=28, height=12)
self.listbox.grid(row=2, column=2, sticky=N) self.btnDel = Button(self.frame, text='Delete', command=self.delBBox)
self.btnDel.grid(row=3, column=2, sticky=W + E + N)
self.btnClear = Button(self.frame, text='ClearAll', command=self.clearBBox)
self.btnClear.grid(row=4, column=2, sticky=W + E + N) # control panel for image navigation
self.ctrPanel = Frame(self.frame)
self.ctrPanel.grid(row=5, column=1, columnspan=2, sticky=W + E)
self.prevBtn = Button(self.ctrPanel, text='<< Prev', width=10, command=self.prevImage)
self.prevBtn.pack(side=LEFT, padx=5, pady=3)
self.nextBtn = Button(self.ctrPanel, text='Next >>', width=10, command=self.nextImage)
self.nextBtn.pack(side=LEFT, padx=5, pady=3)
self.progLabel = Label(self.ctrPanel, text="Progress: / ")
self.progLabel.pack(side=LEFT, padx=5)
self.tmpLabel = Label(self.ctrPanel, text="Go to Image No.")
self.tmpLabel.pack(side=LEFT, padx=5)
self.idxEntry = Entry(self.ctrPanel, width=5)
self.idxEntry.pack(side=LEFT)
self.goBtn = Button(self.ctrPanel, text='Go', command=self.gotoImage)
self.goBtn.pack(side=LEFT) # example pannel for illustration
self.egPanel = Frame(self.frame, border=10)
self.egPanel.grid(row=1, column=0, rowspan=5, sticky=N)
self.tmpLabel2 = Label(self.egPanel, text="Examples:")
self.tmpLabel2.pack(side=TOP, pady=5) self.egLabels = []
for i in range(3):
self.egLabels.append(Label(self.egPanel))
self.egLabels[-1].pack(side=TOP) # display mouse position
self.disp = Label(self.ctrPanel, text='')
self.disp.pack(side=RIGHT) self.frame.columnconfigure(1, weight=1)
self.frame.rowconfigure(4, weight=1) # for debugging ## self.setImage()
## self.loadDir() def loadDir(self, dbg=False):
if not dbg:
s = self.entry.get()
self.parent.focus()
self.category = int(s)
else:
s = r'D:\workspace\python\labelGUI'
## if not os.path.isdir(s):
## tkMessageBox.showerror("Error!", message = "The specified dir doesn't exist!")
## return
# get image list print 'self.category =%d' % (self.category) self.imageDir = os.path.join(r'./images', '%03d' % (self.category))
print(self.imageDir)
self.imageList = glob.glob(os.path.join(self.imageDir, '*.jpg'))
if len(self.imageList) == 0:
print 'No .jpg images found in the specified dir!'
return
else:
print 'num=%d' % (len(self.imageList)) # default to the 1st image in the collection
self.cur = 1
self.total = len(self.imageList) # set up output dir
self.outDir = os.path.join(r'./labels', '%03d' % (self.category))
if not os.path.exists(self.outDir):
os.mkdir(self.outDir) # load example bboxes
self.egDir = os.path.join(r'./Examples', '%03d' % (self.category))
# if not os.path.exists(self.egDir):
# return filelist = glob.glob(os.path.join(self.egDir, '*.jpg'))
self.tmp = []
self.egList = []
random.shuffle(filelist)
for (i, f) in enumerate(filelist):
if i == 3:
break
im = Image.open(f)
r = min(SIZE[0] / im.size[0], SIZE[1] / im.size[1])
new_size = int(r * im.size[0]), int(r * im.size[1])
self.tmp.append(im.resize(new_size, Image.ANTIALIAS))
self.egList.append(ImageTk.PhotoImage(self.tmp[-1]))
self.egLabels[i].config(image=self.egList[-1], width=SIZE[0], height=SIZE[1]) self.loadImage()
print '%d images loaded from %s' % (self.total, s) def loadImage(self):
# load image
imagepath = self.imageList[self.cur - 1]
pil_image = Image.open(imagepath) # get the size of the image
# 获取图像的原始大小
global w0, h0
w0, h0 = pil_image.size # 缩放到指定大小
pil_image = pil_image.resize((DEST_SIZE[0], DEST_SIZE[1]), Image.ANTIALIAS) # pil_image = imgresize(w, h, w_box, h_box, pil_image)
self.img = pil_image self.tkimg = ImageTk.PhotoImage(pil_image) self.mainPanel.config(width=max(self.tkimg.width(), 400), height=max(self.tkimg.height(), 400))
self.mainPanel.create_image(0, 0, image=self.tkimg, anchor=NW)
self.progLabel.config(text="%04d/%04d" % (self.cur, self.total)) # load labels
self.clearBBox()
self.imagename = os.path.split(imagepath)[-1].split('.')[0]
labelname = self.imagename + '.txt'
self.labelfilename = os.path.join(self.outDir, labelname)
bbox_cnt = 0
if os.path.exists(self.labelfilename):
with open(self.labelfilename) as f:
for (i, line) in enumerate(f):
if i == 0:
bbox_cnt = int(line.strip())
continue
print line
tmp = [(t.strip()) for t in line.split()] print "********************"
print DEST_SIZE
# tmp = (0.1, 0.3, 0.5, 0.5)
print "tmp[0,1,2,3]===%.2f, %.2f, %.2f, %.2f" % (
float(tmp[0]), float(tmp[1]), float(tmp[2]), float(tmp[3]))
# print "%.2f,%.2f,%.2f,%.2f" %(tmp[0] tmp[1] tmp[2] tmp[3] ) print "********************" # tx = (10, 20, 30, 40)
# self.bboxList.append(tuple(tx))
self.bboxList.append(tuple(tmp))
tmp[0] = float(tmp[0])
tmp[1] = float(tmp[1])
tmp[2] = float(tmp[2])
tmp[3] = float(tmp[3]) tx0 = int(tmp[0] * DEST_SIZE[0])
ty0 = int(tmp[1] * DEST_SIZE[1]) tx1 = int(tmp[2] * DEST_SIZE[0])
ty1 = int(tmp[3] * DEST_SIZE[1])
print "tx0, ty0, tx1, ty1"
print tx0, ty0, tx1, ty1 tmpId = self.mainPanel.create_rectangle(tx0, ty0, tx1, ty1, \
width=2, \
outline=COLORS[(len(self.bboxList) - 1) % len(COLORS)]) self.bboxIdList.append(tmpId)
self.listbox.insert(END, '(%.2f,%.2f)-(%.2f,%.2f)' % (tmp[0], tmp[1], tmp[2], tmp[3])) # self.listbox.insert(END, '(%d, %d) -> (%d, %d)' %(tmp[0], tmp[1], tmp[2], tmp[3]))
self.listbox.itemconfig(len(self.bboxIdList) - 1,
fg=COLORS[(len(self.bboxIdList) - 1) % len(COLORS)]) def saveImage(self):
# print "-----1--self.bboxList---------"
print self.bboxList
# print "-----2--self.bboxList---------" with open(self.labelfilename, 'w') as f:
f.write('%d\n' % len(self.bboxList))
for bbox in self.bboxList:
f.write(' '.join(map(str, bbox)) + '\n')
print 'Image No. %d saved' % (self.cur) def mouseClick(self, event):
if self.STATE['click'] == 0:
self.STATE['x'], self.STATE['y'] = event.x, event.y
else:
x1, x2 = min(self.STATE['x'], event.x), max(self.STATE['x'], event.x)
y1, y2 = min(self.STATE['y'], event.y), max(self.STATE['y'], event.y) x1, x2 = x1 / DEST_SIZE[0], x2 / DEST_SIZE[0];
y1, y2 = y1 / DEST_SIZE[1], y2 / DEST_SIZE[1]; self.bboxList.append((x1, y1, x2, y2))
self.bboxIdList.append(self.bboxId)
self.bboxId = None
self.listbox.insert(END, '(%.2f, %.2f)-(%.2f, %.2f)' % (x1, y1, x2, y2))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg=COLORS[(len(self.bboxIdList) - 1) % len(COLORS)])
self.STATE['click'] = 1 - self.STATE['click'] def mouseMove(self, event):
self.disp.config(text='x: %.2f, y: %.2f' % (event.x / DEST_SIZE[0], event.y / DEST_SIZE[1]))
if self.tkimg:
if self.hl:
self.mainPanel.delete(self.hl)
self.hl = self.mainPanel.create_line(0, event.y, self.tkimg.width(), event.y, width=2)
if self.vl:
self.mainPanel.delete(self.vl)
self.vl = self.mainPanel.create_line(event.x, 0, event.x, self.tkimg.height(), width=2)
if 1 == self.STATE['click']:
if self.bboxId:
self.mainPanel.delete(self.bboxId)
self.bboxId = self.mainPanel.create_rectangle(self.STATE['x'], self.STATE['y'], \
event.x, event.y, \
width=2, \
outline=COLORS[len(self.bboxList) % len(COLORS)]) def cancelBBox(self, event):
if 1 == self.STATE['click']:
if self.bboxId:
self.mainPanel.delete(self.bboxId)
self.bboxId = None
self.STATE['click'] = 0 def delBBox(self):
sel = self.listbox.curselection()
if len(sel) != 1:
return
idx = int(sel[0])
self.mainPanel.delete(self.bboxIdList[idx])
self.bboxIdList.pop(idx)
self.bboxList.pop(idx)
self.listbox.delete(idx) def clearBBox(self):
for idx in range(len(self.bboxIdList)):
self.mainPanel.delete(self.bboxIdList[idx])
self.listbox.delete(0, len(self.bboxList))
self.bboxIdList = []
self.bboxList = [] def prevImage(self, event=None):
self.saveImage()
if self.cur > 1:
self.cur -= 1
self.loadImage() def nextImage(self, event=None):
self.saveImage()
if self.cur < self.total:
self.cur += 1
self.loadImage() def gotoImage(self):
idx = int(self.idxEntry.get())
if 1 <= idx and idx <= self.total:
self.saveImage()
self.cur = idx
self.loadImage() ## def setImage(self, imagepath = r'test2.png'):
## self.img = Image.open(imagepath)
## self.tkimg = ImageTk.PhotoImage(self.img)
## self.mainPanel.config(width = self.tkimg.width())
## self.mainPanel.config(height = self.tkimg.height())
## self.mainPanel.create_image(0, 0, image = self.tkimg, anchor=NW) def imgresize(w, h, w_box, h_box, pil_image):
'''
resize a pil_image object so it will fit into
a box of size w_box times h_box, but retain aspect ratio
'''
f1 = 1.0 * w_box / w # 1.0 forces float division in Python2
f2 = 1.0 * h_box / h
factor = min([f1, f2])
# print(f1, f2, factor) # test
# use best down-sizing filter
width = int(w * factor)
height = int(h * factor)
return pil_image.resize((width, height), Image.ANTIALIAS) if __name__ == '__main__':
root = Tk()
tool = LabelTool(root)
root.mainloop()

快捷键a,d前后切换图像,esc取消当前框,使用效果如下。

 

3.制作xml

上一步完成后得到的结果是TXT文件。(我没有先重命名)

需要的XML文件格式如下。

<annotation>
<folder>VOC2007</folder>
<filename>000002.jpg</filename>
<source>
<database>My Database</database>
<annotation>VOC2007</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>J</name>
</owner>
<size>
<width>256</width>
<height>256</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>31</xmin>
<ymin>20</ymin>
<xmax>241</xmax>
<ymax>250</ymax>
</bndbox>
</object>
</annotation>

生成代码如下。

 import glob

 s1="""    <object>
<name>{0}</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>{1}</xmin>
<ymin>{2}</ymin>
<xmax>{3}</xmax>
<ymax>{4}</ymax>
</bndbox>
</object>""" s2="""<annotation>
<folder>VOC2007</folder>
<filename>{0}</filename>
<source>
<database>My Database</database>
<annotation>VOC2007</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>J</name>
</owner>
<size>
<width>256</width>
<height>256</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>{1}</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>{2}</xmin>
<ymin>{3}</ymin>
<xmax>{4}</xmax>
<ymax>{5}</ymax>
</bndbox>
</object>{6}
</annotation>
""" textlist=glob.glob('labels\\001\*.txt')
for text_ in textlist:
flabel = open(text_, 'r')
lb = flabel.readlines()
flabel.close()
ob2 = ""
if len(lb)<2:
continue # no annotation
x1=2
x2=lb[1].split(' ')
x3 = [int(float(i) * 256) for i in x2]
if len(lb)>2: # extra annotation
for i in range(2,len(lb)):
y2 = lb[i].split(' ')
y3 = [int(float(i) * 256) for i in y2]
ob2+='\n' + s1.format(x1,y3[0],y3[1],y3[2],y3[3])
imgname=('%06d' % (int(text_[13:-4])))+'.jpg'
savename='Annotations\\'+str('%06d' % (int(text_[13:-4])))+'.xml'
f = open(savename, 'w')
ob1=s2.format(imgname, x1, x3[0],x3[1],x3[2],x3[3], ob2)
f.write(ob1)
f.close()

4.ImageSets\Main里的四个txt文件

test.txt是测试集,train.txt是训练集,val.txt是验证集,trainval.txt是训练和验证集。内容为文件名。

VOC2007中,trainval大概是整个数据集的50%,test也大概是整个数据集的50%;train大概是trainval的50%,val大概是trainval的50%。

由于本次数据较少,两类共计约400张,train、val、test各占33%。

 import os
import random trainval_percent = 0.66
train_percent = 0.5
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath) num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr) ftrainval = open('ImageSets\Main\\trainval.txt', 'w')
ftest = open('ImageSets\Main\\test.txt', 'w')
ftrain = open('ImageSets\Main\\train.txt', 'w')
fval = open('ImageSets\Main\\val.txt', 'w') for i in list:
name=total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name) ftrainval.close()
ftrain.close()
fval.close()
ftest .close()

数据制作就完成了。

将数据集做成VOC2007格式用于Faster-RCNN训练的更多相关文章

  1. faster rcnn训练自己的数据集

    采用Pascal VOC数据集的组织结构,来构建自己的数据集,这种方法是faster rcnn最便捷的训练方式

  2. caffe学习三:使用Faster RCNN训练自己的数据

    本文假设你已经完成了安装,并可以运行demo.py 不会安装且用PASCAL VOC数据集的请看另来两篇博客. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于c ...

  3. 如何才能将Faster R-CNN训练起来?

    如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...

  4. python3 + Tensorflow + Faster R-CNN训练自己的数据

    之前实现过faster rcnn, 但是因为各种原因,有需要实现一次,而且发现许多博客都不全面.现在发现了一个比较全面的博客.自己根据这篇博客实现的也比较顺利.在此记录一下(照搬). 原博客:http ...

  5. faster rcnn训练详解

    http://blog.csdn.net/zy1034092330/article/details/62044941 py-faster-rcnn训练自己的数据:流程很详细并附代码 https://h ...

  6. 使用labelImg制作自己的数据集(VOC2007格式)用于Faster-RCNN训练

    https://blog.csdn.net/u011956147/article/details/53239325 https://blog.csdn.net/u011574296/article/d ...

  7. Faster Rcnn训练自己的数据集过程大白话记录

    声明:每人都有自己的理解,动手实践才能对细节更加理解! 一.算法理解 此处省略一万字.................. 二.训练及源码理解 首先配置: 在./lib/utils文件下....运行 p ...

  8. caffe 用faster rcnn 训练自己的数据 遇到的问题

    1 . 怎么处理那些pyx和.c .h文件 在lib下有一些文件为.pyx文件,遇到不能import可以cython 那个文件,然后把lib文件夹重新make一下. 遇到.c 和 .h一样的操作. 2 ...

  9. faster rcnn训练过程讲解

    http://blog.csdn.net/u014696921/article/details/60321425

随机推荐

  1. Android:padding和android:layout_margin的区别

    padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离. margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离

  2. hdu 2177 取(2堆)石子游戏(威佐夫博奕)

    题目链接:hdu 2177 这题不是普通的 Nim 博弈,我想它应该是另一种博弈吧,于是便推 sg 函数打了个 20*20 的表来看,为了方便看一些,我用颜色作了标记,打表代码如下: #include ...

  3. Spring—Quartz定时调度CronTrigger时间配置格式说明与实例

    1 .CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 ...

  4. Linux 系统目录结构

    登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin: bin是Binary的缩写, 这个目录存放着最经常使用的命令. /boot ...

  5. SPSS数据分析—多重线性回归

    只有一个自变量和因变量的线性回归称为简单线性回归,但是实际上,这样单纯的关系在现实世界中几乎不存在,万事万物都是互相联系的,一个问题的产生必定多种因素共同作用的结果. 对于有多个自变量和一个因变量的线 ...

  6. angular router-ui

    将模块注入到控制器中的方法: 1.export module 2.在router中resolve解决: 2.1 resolve中直接return值 /*ngInject*/ worker : 'hi' ...

  7. 51nod1693 水群

    题目链接:51nod1693 水群 题解参考大神的博客:http://www.cnblogs.com/fighting-to-the-end/p/5874763.html 这题时限0.4秒,真的够狠的 ...

  8. Shell脚本的追踪与debug

    [xd502djj@linux ~]#sh [-nvx] scripts.sh 参数: -n:不执行脚本,仅仅查询语法错误 -v:执行脚本前,先将脚本scripts的内容输出到屏幕上 -x:将使用到的 ...

  9. Octopus系列之价格计算公式

    百分比:在原价的基础上优惠5%,相当于95%折销售实际价格:最后实际销售的价格[基准价格:如果有特价就基于特价,没有特价基于原价]优惠金额:减少的金额 批量优惠 百分比: 原价*(1-5%)的算法实际 ...

  10. 介绍开源的.net通信框架NetworkComms框架之一 首字节验证

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  开源地 ...