Ubuntu 16 下配置Faster_rcnn 訓練自己的数据

来源:互联网 发布:python 程序运行时间 编辑:程序博客网 时间:2024/06/06 19:15

之前用Fast_rcnn 的例子训练自己的数据集,但是由于Fast_rcnn 例子中用的是预先处理好的图片文件(就是吧图片先用selective-search 把图片预先处理成matlab的.mat文件)。所以再用Fast_rcnn 训练自己的数据图图片时,也要把自己的图片先预处理。但是最后网络不收敛。改用Faster_rcnn。 相比Fast_rcnn 好用的多。

一 首先下载Faster_rcnn 项目:

  1)项目地址 https://github.com/rbgirshick/py-faster-rcnn 在你想放置的文件夹下面执行:
        git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
      一定要有--recursive 否则不会下载caffe 部分
  2)进入Faster_rcnn 根目录下
     cd $FRCN_ROOT/lib
     make
  3)编译Caffe and pyCaffe。 这里的caffe并不是正常的caffe,也不需要自己下载。系统需要安装caffe 的环境,具体参考caffe    官方文档,在编译之前需要修改Makefile文件。
     cd $FRCN_ROOT/caffe-fast-rcnn
        cp Makefile.config.example Makefile.config
    修改如下两处

     WITH_PYTHON_LAYER := 1
     USE_CUDNN := 1
   如果编译时提示找不到hdf5,则需要添加hdf5的路径。我的配如下
     INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
     LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu  /usr/lib/x86_64-linux-gnu/hdf5/serial
    配置好后开始编译:
     make -j8 && make pycaffe
   4)编译完成后下载预训练的模型
     cd $FRCN_ROOT
     ./data/scripts/fetch_faster_rcnn_models.sh
    有可能需要翻墙下载。总之把模型下好。跑一下demo
        cd $FRCN_ROOT
     ./tools/demo.py
    没错误的话应该看到几张图片的识别实例(要是ssh 登录服务器会提示没有显示设备)

二 训练自己的数据

  训练自己的图片数据需要将自己的图片变为VOC 格式。可以按照github上的文档,下载pascalVOC数据集,然后将文件都删掉,用自己的数据替换。我这里没这样做,下载太慢了。 因为之前弄过Fast_rcnn,目录结构直接自己建立就行了。先说一下目录组织结构。

$FRCN_ROOT--|
                      |-data-|
                      |-VOCdevkit2007-|
                                    |-VOC2007-|
                                                              |-Annotations- (存放图片的.xml)
                                              |-JPEGImages-(存放图片本身)
                                              |-ImageSets-|
                                                       |-Main-(存放 train.txt test.txt trainval.txt val.txt) )

1)将图片中的区域提取出来变为xml文件 我用的是Python-Based-Labeling-Tool-for-PASCAL-VOC-master 这个工具,也可 以在github 上有个py-faster-rcnn-data-interface-generator 这个工具。

2)Main 文件夹下的四个文件由python 脚本生成createdata.py

#!/usr/bin/env pythonimport numpy as npimport osimport random""" create_imageset.pyCreate .txt files containing the names of the imageset for training, validation and testing for the pascal_voc benchmark."""DATASET_PATH = '~/fast-rcnn/fast-rcnn/data/VOC2007'test = []train = []val = []trainval = []#test_probality = 0.2#train_probablity = 0.5test_probability = 0.2train_probability = 0.8def save_imagesets(imageset_path):    with open(os.path.join(imageset_path, "test.txt"), "w") as test_file:test_file.write('\n'.join(i for i in test))    with open(os.path.join(imageset_path, "train.txt"), "w") as train_file:train_file.write('\n'.join(i for i in train))    with open(os.path.join(imageset_path, "val.txt"), "w") as val_file:val_file.write('\n'.join(i for i in val))    with open(os.path.join(imageset_path, "trainval.txt"), "w") as trainval_f:trainval_f.write('\n'.join(i for i in trainval))    f __name__ == '__main__':    # get all files that have an existing annotation    annotation_path = os.path.join(DATASET_PATH, 'Annotations')    imageset_path = os.path.join(DATASET_PATH, 'ImageSets', 'Main')    files = [f for f in os.listdir(annotation_path)]    files.sort()    for f in files:# strip extenstionshort_name = os.path.splitext(f)[0]# decide whether its testing (p=0.5) or trainval(p=0.5)if random.random() < test_probability :    test.append(short_name)else:    trainval.append(short_name)    # train (p=0.5) or val (p=0.5)    if random.random() < train_probability:        train.append(short_name)    else:        val.append(short_name)    print("ImageSets saved")    save_imagesets(imageset_path)


3) 将自己的图片和文件放到对应的文件夹下面
4) 下载ImageNet数据集下预训练得到的模型参数(用来初始化)


参考博客 http://blog.csdn.net/sinat_30071459/article/details/51332084

0 0
原创粉丝点击