tf-faster-rcnn实战篇

准备篇:
TensorFlow:首先查看TensorFlow的版本,代码支持的是1.2的版本:

import tensorflow as tf
print tf.__version__
1.1.0

因此卸载并安装指定版本Tensorflow:

pip install -I tensorflow==1.2.1

安装:
1、下载tf-faster-rcnn代码:

git clone https://github.com/endernewton/tf-faster-rcnn.git

2、到tf-faster-rcnn/lib下编译Cython 模块:
需要注意:首先根据GPU的型号来修改计算能力(Architecture), 官网提供了5种模型对应的计算能力值,我的机子是Tesla K40,所以这里修改sm_52为sm_35,然后执行下面代码进行编译,否则去重框会出问题

CUDA.jpeg

 

cd tf-faster-rcnn/lib
make clean
make
cd ..

3、安装Python COCO API:

cd data
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
make
cd ../../..

4、下载模型

./data/scripts/fetch_faster_rcnn_models.sh

5、使用预训练模型进行测试

./tools/demo.py

但是GPU上跑测试时用ssh进行远程解释器绘图时出错:RuntimeError: Invalid DISPLAY variable, 这时需要修改demo.py文件:

import matplotlib.pyplot as plt
plt.switch_backend('agg')

上述是GPU版本测试的方法,如果是基于CPU,还需要对以下几个.py文件进行改动:
a. tf-faster-rcnn/lib/model/nms_wrapper.py:

from model.config import cfg
#from nms.gpu_nms import gpu_nms
from nms.cpu_nms import cpu_nms

def nms(dets, thresh, force_cpu=False):
  """Dispatch to either CPU or GPU NMS implementations."""

  if dets.shape[0] == 0:
    return []
  return cpu_nms(dets, thresh)
  # if cfg.USE_GPU_NMS and not force_cpu:
  #   return gpu_nms(dets, thresh, device_id=0)
  # else:
  #   return cpu_nms(dets, thresh)

b. tf-faster-rcnn/lib/model/config.py: 注释以下代码

__C.USE_GPU_NMS = False

c. tf-faster-rcnn/lib/setup.py: 注释以下语句

CUDA = locate_cuda()
self.src_extensions.append('.cu')
Extension('nms.gpu_nms',
        ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
        library_dirs=[CUDA['lib64']],
        libraries=['cudart'],
        language='c++',
        runtime_library_dirs=[CUDA['lib64']],
        # this syntax is specific to this build system
        # we're only going to use certain compiler args with nvcc and not with gcc
        # the implementation of this trick is in customize_compiler() below
        extra_compile_args={'gcc': ["-Wno-unused-function"],
                            'nvcc': ['-arch=sm_52',
                                     '--ptxas-options=-v',
                                     '-c',
                                     '--compiler-options',
                                     "'-fPIC'"]},
        include_dirs = [numpy_include, CUDA['include']]

4、运行./tools/demo.py就可以看到结果啦!
5、当然如果想看到自己的图片的检测结果,将图片放在tf-faster-rcnn/data/demo下,修改源文件demo.py,在im_names下面添加图片的名称即可。

im_names = ['000456.jpg', '000542.jpg', '001150.jpg',
                '001763.jpg', '004545.jpg']   

训练:
1、准备训练数据:数据集需要参考VOC2007的数据集格式,主要包括三个部分:
JPEGImages:存放用来训练的原始图像,图片编号要以6为数字命名,例如000034.jpg,图片要是JPEG/JPG格式的,图片的长宽比(width/height)要在0.462-6.828之间;
Annotations :存放原始图像中的Object的坐标信息,一个训练图片对应Annotations下的一个同名的XML文件;
ImageSets/Main :指定用来train,trainval,val和test的图片的编号,因为VOC的数据集可以做很多的CV任务,比如Object detection, Semantic segementation, Edge detection等,所以Imageset下有几个子文件夹(Layout, Main, Segementation),修改下Main下的文件 (train.txt, trainval.txt, val.txt, test.txt),里面写上想要进行任务的图片的编号
将上述你的数据集放在tf-faster-rcnn/data/VOCdevkit2007/VOC2007下面,替换原始VOC2007的JPEGIMages,Imagesets,Annotations,这里也可以直接更换文件夹名称。
2、为训练数据创建软连接
3、修改源代码:tf-faster-rcnn/experiments/train_faster+rcnn.sh和tf-faster-rcnn/lib/datasets/pascal_voc.py文件
4、运行下面命令开始训练,下次训练之前,需要将data/cache和output(输出的model存放的位置,不训练此文件夹没有)两个文件夹删除。

ln -s /Users/steven/data/Trainingdata Trainingdata
./experiments/scripts/test_faster_rcnn.sh 0 TrainData res101

tf-faster-rcnn的工程目录进行简单介绍:
data: 存放数据,以及读取文件的cache;
experiments: 存放配置文件以及运行的log文件,配置文件
lib: python接口
output: 输出的model存放的位置,不训练此文件夹没有
tensorboard: 可视化部分
tools: 训练和测试的python文件

下面几篇是caffe框架下faster-rcnn的相关博客: http://www.cnblogs.com/dudumiaomiao/p/6556111.html
http://blog.csdn.net/u012841667/article/details/69555074
http://blog.csdn.net/samylee/article/details/51201744
http://blog.csdn.net/Gavin__Zhou/article/details/52052915
http://blog.csdn.net/sinat_30071459/article/details/51332084


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值