2018年3月30日星期五

darkflow训练自己的数据集

首先把数据转换为VOC格式。
VOC文件夹格式如下:
  1. --VOC  
  2.     --Annotations  
  3.     --ImageSets  
  4.       --Main  
  5.       --Layout  
  6.       --Segmentation  
  7.     --JPEGImages  
  8.     --SegmentationClass  
  9.     --SegmentationObject

1)JPEGImages文件夹
文件夹里包含了训练图片和测试图片,混放在一起
2)Annatations文件夹
文件夹存放的是xml格式的标签文件,每个xml文件都对应于JPEGImages文件夹的一张图片
3)ImageSets文件夹
Action存放的是人的动作,暂时不用。
Layout存放的人体部位的数据,暂时不用。

第1步:搞定JPEGSImages文件夹
1)把图片放到JPEGSImages里面,在VOC2007里面,图片文件名都是000001.jpg类似这样的,我们也统一格式,把图片名字重命名成这样的。如果文件太多,需要批量重命名文件。代码如下:
import os  
  
class BatchRename():  
    ''''' 
    批量重命名文件夹中的图片文件 

    '''  
    def __init__(self):  
        #我的图片文件夹路径horse  
        #self.path = '/home/ghz/fast-rcnn/data/VOCdevkit/VOC2007/horse' 
        self.path = '/Users/sisyphus/darkflow/VOCtest2018/crazing'
  
    def rename(self):  
        filelist = os.listdir(self.path)
        #filelist.sort()#+
        filelist.sort(key=lambda x:int(x[8:-4]))#针对crazing_*.jpg
        total_num = len(filelist)  
        i = 1  
        n = 6  
        for item in filelist:  
            #if item.endswith('.jpg'):
            if item.endswith('.xml'):
                print(item)
                n = 6 - len(str(i))  
                src = os.path.join(os.path.abspath(self.path), item)  
                #dst = os.path.join(os.path.abspath(self.path), str(0)*n + str(i) + '.jpg')
                dst = os.path.join(os.path.abspath(self.path), str(0)*n + str(i) + '.xml')
                try:  
                    os.rename(src, dst)  
                    print('converting %s to %s ...' % (src, dst)) 
                    i = i + 1  
              
                except:  
                    continue  
        print('total %d to rename & converted %d jpgs/xml' % (total_num, i-1))  
  
if __name__ == '__main__':  
    demo = BatchRename()  
    demo.rename()

第2步:搞定Annatations文件夹()
1)在这里下载:https://github.com/tzutalin/labelImg。
2)保存的路径就是我们的Annatations文件夹。
这一步没有实践,因我的数据集标注本身已经是xml格式。但是xml文件中有部分内容与VOC格式内容不一致,需要做一些修改。
xml标准格式如下:
  1. <annotation>  
  2.     <folder>VOC2012</folder>                             
  3.     <filename>000001.jpg</filename>                               //文件名  
  4.     <source>                                                           //图像来源(不重要)  
  5.         <database>The VOC2007 Database</database>  
  6.         <annotation>PASCAL VOC2007</annotation>  
  7.         <image>flickr</image>  
  8.     </source>  
  9.     <size>                                               //图像尺寸(长宽以及通道数)                        
  10.         <width>500</width>  
  11.         <height>332</height>  
  12.         <depth>3</depth>  
  13.     </size>  
  14.     <segmented>1</segmented>                                   //是否用于分割(在图像物体识别中01无所谓)  
  15.     <object>                                                           //检测到的物体  
  16.         <name>horse</name>                                         //物体类别  
  17.         <pose>Right</pose>                                         //拍摄角度  
  18.         <truncated>0</truncated>                                   //是否被截断(0表示完整)  
  19.         <difficult>0</difficult>                                   //目标是否难以识别(0表示容易识别)  
  20.         <bndbox>                                                   //bounding-box(包含左下角和右上角xy坐标)  
  21.             <xmin>100</xmin>  
  22.             <ymin>96</ymin>  
  23.             <xmax>355</xmax>  
  24.             <ymax>324</ymax>  
  25.         </bndbox>  
  26.     </object>  
  27.     <object>                                                           //检测到多个物体  
  28.         <name>person</name>  
  29.         <pose>Unspecified</pose>  
  30.         <truncated>0</truncated>  
  31.         <difficult>0</difficult>  
  32.         <bndbox>  
  33.             <xmin>198</xmin>  
  34.             <ymin>58</ymin>  
  35.             <xmax>286</xmax>  
  36.             <ymax>197</ymax>  
  37.         </bndbox>  
  38.     </object>  
  39. </annotation>  

下载的xml中文件名不是类似000001.jpg、000002.jpg。。。因此需要修改。
代码如下:

from xml.etree import ElementTree
import os

jpgpath='/Users/sisyphus/darkflow/VOCtest2018/JPEGImages/'
jpglist = os.listdir(jpgpath)
jpglist.sort()

xmlpath='/Users/sisyphus/darkflow/VOCtest2018/Annotation/' 
xmllist = os.listdir(xmlpath)
xmllist.sort()

i=0
for item in xmllist:
    #print(item)
    xmldoc = ElementTree.parse(xmlpath+xmllist[i])
    node = xmldoc.find('./filename')
    node.text = jpglist[i]
    xmldoc.write(xmlpath+xmllist[i])
    i=i+1

以上两个文件夹都存放训练集数据,我这里设为每个种类300张图片的前250张,余下50*6张图片作为测试集。
至此数据准备完毕.
darkflow/cfg文件夹复制yolo-voc.cfg重命名为yolo-voc-6c.cfg。修改相关代码为六分类(参考我之前那篇博文)。
1)修改 [region] layer (the last layer) to the number of classes you are going to train for. In our case, classes are set to 6:
...

[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=6###!!!
coords=4
num=5
softmax=1

...

2)change filters in the [convolutional] layer (the second to last layer(倒数第二层?)) to num * (classes + 5). In our case, num is 5 and classes are 3 so 5 * (3 + 5) = 40 therefore filters are set to 40.
对于六分类5*(6+5=55
...

[convolutional]
size=1
stride=1
pad=1
filters=55##
activation=linear

3)修改labels.txt ,列出6类标签
crazing
inclusion
patches
pitted_surface
rolled-in_scale
scratches


开始训练:
cd到darkflow
python flow --model cfg/yolo-voc-6c.cfg --load bin/yolo.weights --train --annotation /Users/sisyphus/darkflow/VOCtest2018/Annotation --dataset /Users/sisyphus/darkflow/VOCtest2018/JPEGImages --gpu 1.0

训练完毕后测试命令:
python flow --model cfg/yolo-voc-6c.cfg --load -1 --gpu 1.0


完整形式,且转换为json形式为:
flow --imgdir sample_img/ --model cfg/yolo-voc-6c.cfg --load -1 --json

摄像头测试命令:

python flow --model cfg/yolo-voc-6c.cfg --load -1 --demo camera

从ckpt文件夹最新保存的训练参数继续训练:
python flow --model cfg/yolo-voc-6c.cfg --load -1 --train --annotation /Users/sisyphus/darkflow/VOCtest2018/Annotation --dataset /Users/sisyphus/darkflow/VOCtest2018/JPEGImages --gpu 1.0




没有评论:

发表评论

Failed to find TIFF library

ImportError: Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|.. 解决方法: ...