Faster-RCNN-TensorFlow-Python3.5 在Ubuntu16.04下的配置方法

安装过程

1. 深度学习环境Tensorflow的安装

参考这一篇博客:https://www.cnblogs.com/pprp/p/9463974.html

2. 安装python包

cython, python-opencv, easydict,numpy

pip install Cython
pip install opencv-python
pip install matplotlib

opencv 的包下载安装,安装教程

  • ImportError: No module named 'PIL'

    • solution:easy_install PIL & pip install image
  • ImportError: No module named 'scipy'

    • solution:

       pip install numpy
      pip install scipy
  • ImportError: No module named 'lib.utils.cython_bbox'

    git clone --recursive https://github.com/rbgirshick/fast-rcnn.git
    cd $FRCN_ROOT/lib
    make
    cp fast-rcnn/lib/utils/cython_bbox.cpython-35m-x86_64-linux-gnu.so Faster-RCNN-TensorFlow-Python3.5/lib/utils/

3.  Go to ./data/coco/PythonAPI

​ Run python setup.py build_ext --inplace

​ Run python setup.py build_ext install

4. git clone

用git命令将这个库下载到本地 Faster-RCNN-TensorFlow-Python3.5

git clone https://github.com/dBeker/Faster-RCNN-TensorFlow-Python3.5.git
# 将cython_bbox.cpython-36m-x86_64-linux-gnu.so下载下来
wget https://github.com/pprp/DL/blob/master/cython_bbox.cpython-36m-x86_64-linux-gnu.so

把Faster-RCNN_TF 中的cython_bbox.xxx.so 复制到 lib/utils

cp cython_bbox.cpython-36m-x86_64-linux-gnu.so Faster-RCNN-TensorFlow-Python3.5/lib/utils

5. 下载预训练模型

Download pre-trained VGG16 from here and place it as "data\imagenet_weights\vgg16.ckpt"

wget http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz
unzip vgg*
#重命名为vgg16.ckpt
cp vgg* vgg16.ckpt
#放到以下位置
mv vgg16.ckpt data\imagenet_weights\vgg16.ckpt

6. 自己数据集的制作

  • xml文件的制作需要labelImg软件的帮助,这里提供在window下的exe包,打开即可使用

    wget https://github.com/pprp/DL/blob/master/LabelIMG.zip
    • 使用方法:
    Ctrl + u  加载目录中的所有图像,鼠标点击Open dir同功能
    Ctrl + r 更改默认注释目标目录(xml文件保存的地址)
    Ctrl + s 保存
    Ctrl + d 复制当前标签和矩形框
    space 将当前图像标记为已验证
    w 创建一个矩形框
    d 下一张图片
    a 上一张图片
    del 删除选定的矩形框
    Ctrl++ 放大
    Ctrl-- 缩小
    ↑→↓← 键盘箭头移动选定的矩形框
  • 数据的放置结构(自己手动建立)

-data
- VOCdevkit2007
- VOC2007
- Annotations (标签XML文件,用对应的图片处理工具人工生成的)
- ImageSets (生成的方法是用sh或者MATLAB语言生成)
- Main
- test.txt
- trian.txt
- trainval.txt
- val.txt
- JPEGImages(原始文件)
  • Main中的四个txt文件的制作

    详见附件二,注意要修改路径位置,最好是绝对路径

xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'

7. 替换成自己的数据

将制作好的数据按照以上的目录结构进行放置

用你的Annotations,ImagesSets和JPEGImages替换…\Faster-RCNN-TensorFlow-Python3.5\data\VOCdevkit2007\VOC2007中对应文件夹

8. 将原始代码修改为适配你自己的代码

    1. Pascal_VOC.py,修改自己的标注的类别

      self._classes = ('__background__',  # always index 0
      'aeroplane', 'bicycle', 'bird', 'boat',
      'bottle', 'bus', 'car', 'cat', 'chair',
      'cow', 'diningtable', 'dog', 'horse',
      'motorbike', 'person', 'pottedplant',
      'sheep', 'sofa', 'train', 'tvmonitor')

      将这些类别替换成你自己的类别。

      self.__classes=('__background__',
      '你的标签1','你的标签2',你的标签3','你的标签4')
    1. demo.py, 修改为自己的标注类别

      CLASSES = ('__background__',
      'aeroplane', 'bicycle', 'bird', 'boat',
      'bottle', 'bus', 'car', 'cat', 'chair',
      'cow', 'diningtable', 'dog', 'horse',
      'motorbike', 'person', 'pottedplant',
      'sheep', 'sofa', 'train', 'tvmonitor')

      更改为:

      CLASSES = ('__background__',
      '你的标签1','你的标签2',你的标签3','你的标签4')
    1. demo.py 更改另外一处代码:

      net.create_architecture(sess, "TEST", 21,tag='default', anchor_scales=[8, 16, 32])

      更改为:

      net.create_architecture(sess, "TEST", 5,tag='default', anchor_scales=[8, 16, 32])

      原本的代码是有20类物体+背景,所以是21。 把类别数改为,你的类别+背景。如果是只检测一类物体,那就改为2

    9. 出现的问题

  1. 可能出现以下报错:
m = cv2.imread(roidb[i][‘image’])
KeyError

解决:

将Faster-RCNN-TensorFlow-Python3.5-master\data\cache文件夹中之前生成的文件模型删除。

因为会自己读取cache中的文本,导致训练出现错误。

10. 作出的其他调整

具体方案如下:

  • 将demo.py中main函数中进行如下修改:

        # -----------------------
    # demonet = args.demo_net
    # dataset = args.dataset
    # -----------------------
    demonet = 'vgg16'
    dataset = 'pascal_voc'
    # -----------------------
  • demo.py中main函数中将im_names中的内容替换成自己的测试图片

    im_names = ['IMG_1.jpg', 'IMG_40.jpg', 'IMG_23.jpg', 'IMG_127.jpg',
    'IMG_134.jpg', 'IMG_185.jpg']

    然后将Faster-RCNN-TensorFlow-Python3.5/data/demo中替换上相应的图片

  • 将NETS,DATASETS进行如下修改

    NETS = {'vgg16': ('vgg16.ckpt',)}

    DATASETS = {'pascal_voc': ('voc_2007_trainval',)}

  • 可以运行了python demo.py

11. 开始训练

python train.py

12. 跑demo.py

#如果可以直接跑,就直接跑
python demo.py

如果不能运行demo.py,则进行以下处理:

找到通过训练得到的训练结果:路径为:/Faster-RCNN-TensorFlow-Python3.5/default/voc_2007_trainval/default

内容如下:

checkpoint
vgg16.ckpt.data-00000-of-00001
vgg16.ckpt.index
vgg16.ckpt.meta
vgg16_faster_rcnn_iter_10000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_10000.ckpt.index
vgg16_faster_rcnn_iter_10000.ckpt.meta
vgg16_faster_rcnn_iter_10000.pkl
vgg16_faster_rcnn_iter_15000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_15000.ckpt.index
vgg16_faster_rcnn_iter_15000.ckpt.meta
vgg16_faster_rcnn_iter_15000.pkl
vgg16_faster_rcnn_iter_20000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_20000.ckpt.index
vgg16_faster_rcnn_iter_20000.ckpt.meta
vgg16_faster_rcnn_iter_20000.pkl
vgg16_faster_rcnn_iter_25000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_25000.ckpt.index
vgg16_faster_rcnn_iter_25000.ckpt.meta
vgg16_faster_rcnn_iter_25000.pkl
vgg16_faster_rcnn_iter_30000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_30000.ckpt.index
vgg16_faster_rcnn_iter_30000.ckpt.meta
vgg16_faster_rcnn_iter_30000.pkl
vgg16_faster_rcnn_iter_35000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_35000.ckpt.index
vgg16_faster_rcnn_iter_35000.ckpt.meta
vgg16_faster_rcnn_iter_35000.pkl
vgg16_faster_rcnn_iter_40000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_40000.ckpt.index
vgg16_faster_rcnn_iter_40000.ckpt.meta
vgg16_faster_rcnn_iter_40000.pkl
vgg16_faster_rcnn_iter_5000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_5000.ckpt.index
vgg16_faster_rcnn_iter_5000.ckpt.meta
vgg16_faster_rcnn_iter_5000.pkl
vgg16.pkl

将其中的

vgg16_faster_rcnn_iter_40000.ckpt.data-00000-of-00001
vgg16_faster_rcnn_iter_40000.ckpt.index
vgg16_faster_rcnn_iter_40000.ckpt.meta
vgg16_faster_rcnn_iter_40000.pkl

文件复制到以下路径

/Faster-RCNN-TensorFlow-Python3.5/output/vgg16/voc_2007_trainval/default/

并重命名为:

vgg16.ckpt.data-00000-of-00001
vgg16.ckpt.index
vgg16.ckpt.meta
vgg16.pkl

修改完成,再次运行python demo.py

附件代码:


  1. 下面是适用于我本地环境的MATLAB代码
%注意修改下面四个值
xmlfilepath='/home/pprp/github/Faster-RCNN-TensorFlow-Python3.5/data/VOC2007/Annotations';
txtsavepath='/home/pprp/github/Faster-RCNN-TensorFlow-Python3.5/data/VOC2007/ImageSets/Main';
trainval_percent=0.5; #trainval占整个数据集的百分比,剩下部分就是test所占百分比
train_percent=0.5; #train占trainval的百分比,剩下部分就是val所占百分比 xmlfile=dir(xmlfilepath);
numOfxml=length(xmlfile)-2;#减去.和.. 总的数据集大小 trainval=sort(randperm(numOfxml,floor(numOfxml*trainval_percent)));
test=sort(setdiff(1:numOfxml,trainval)); trainvalsize=length(trainval); #trainval的大小
train=sort(trainval(randperm(trainvalsize,floor(trainvalsize*train_percent))));
val=sort(setdiff(trainval,train)); ftrainval=fopen([txtsavepath 'trainval.txt'],'w');
ftest=fopen([txtsavepath 'test.txt'],'w');
ftrain=fopen([txtsavepath 'train.txt'],'w');
fval=fopen([txtsavepath 'val.txt'],'w'); for i=1:numOfxml
if ismember(i,trainval)
fprintf(ftrainval,'%s\n',xmlfile(i+2).name(1:end-4));
if ismember(i,train)
fprintf(ftrain,'%s\n',xmlfile(i+2).name(1:end-4));
else
fprintf(fval,'%s\n',xmlfile(i+2).name(1:end-4));
end
else
fprintf(ftest,'%s\n',xmlfile(i+2).name(1:end-4));
end
end
fclose(ftrainval);
fclose(ftrain);
fclose(fval);
fclose(ftest);
  1. Python代码:
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()
  1. 批量修改图片名称,以及XML名称:
import os
import glob
import shutil
# 目录名称,你要自己修改
_dir = "H:/mypic/"
file_name = os.listdir(_dir)
print(file_name) n=1
for file in file_name:
pic_name = os.listdir(_dir+file)
#print(pic_name)
for pic in pic_name:
if os.path.isdir(_dir+file+'/'+pic):
xml_name=os.listdir(_dir+file+"/"+pic)
for xml in xml_name:
shutil.copy( _dir+file+'/'+pic+"/"+xml, 'G:/data/VOCdevkit2007/VOC2007/Annotations/')
# print(pic) #修改个别XML名称
# if xml == '1000010.xml':
#print(dir + file + '/' + pic)
# os.rename(_dir+file + '/' + pic + '/' + xml, _dir + file + '/' + pic + "/" + "100010.xml") # oldname = _dir+file+'/'+pic+"/"+xml
# newname = _dir+file+'/'+pic+"/"+ str(n).zfill(6) + ".xml"
#os.rename(oldname, newname)
#n = n + 1
# print(oldname, '--->', newname) # zfill是一个自动补零的函数 6 就是一共六位 不足的补零 只有字符串才有这个函数 所以转换了
# newname = _dir+str(xml_name.index(xml)+1).zfill(6)+".xml" """
#批量修改图片的名称
n=1
for file in file_name:
pic_name = os.listdir(_dir+file)
print(pic_name)
for pic in pic_name:
oldname = _dir+file+"/"+pic
newname = _dir+file+"/" + str(n).zfill(6) + ".jpg"
os.rename(oldname, newname)
n = n + 1
print(oldname, '--->', newname)
# zfill是一个自动补零的函数 6 就是一共六位 不足的补零 只有字符串才有这个函数 所以转换了
# newname = _dir+str(xml_name.index(xml)+1).zfill(6)+".xml"
"""

修改对应xml文件

import os
import re _dir = "G:/Annotations/"
xmlList = os.listdir(_dir)
n = 1
for xml in xmlList:
#f = open(_dir + xml, "r")
f = open(_dir + xml, "r", encoding='utf-8')
xmldata = f.read()
xmldata = re.sub('\<path>(.*?)\</path>', '<path>G:/data/VOCdevkit2007/VOC2007/JPEGImages/' + str(n).zfill(6) + '.jpg</path>', xmldata)
f.close()
f = open(_dir + xml, "w")
f.write(xmldata)
f.close()
n += 1

pascal_voc.py的一些解读

 def init(self, image_set, year,devkit_path=None)
/*这个是初始化函数,它对应着的是pascal_voc的数据集访问格式,其实我们将其接口修改的更简单一点*/ def image_path_at(self, i)
/*根据第i个图像样本返回其对应的path,其调用了image_path_from_index(self, index)作为其具体实现*/ def image_path_from_index(self, index)
//实现了 image_path的具体功能 def _load_image_set_index(self)
//加载了样本的list文件 def _get_default_path(self)
//获得数据集地址 def gt_roidb(self)
//读取并返回ground_truth(真实值,设定的一个正确的基准)的db def selective_search_roidb
//读取并返回ROI的db def _load_selective_search_roidb(self, gt_roidb)
//加载预选框的文件 def selective_search_IJCV_roidb(self)
//在这里调用读取Ground_truth和ROI db并将db合并 def _load_selective_search_IJCV_roidb(self, gt_roidb)
//这里是专门读取作者在IJCV上用的dataset def _load_pascal_annotation(self, index)
//这个函数是读取gt的具体实现 def _write_voc_results_file(self, all_boxes)
//voc的检测结果写入到文件 def _do_matlab_eval(self, comp_id, output_dir='output')
//根据matlab的evluation接口来做结果的分析 def evaluate_detections
//其调用了_do_matlab_eval def competition_mode
//设置competitoin_mode,加了一些噪点

reference

https://blog.csdn.net/jcli1_14/article/details/81458847

https://blog.csdn.net/hzhj2007/article/details/79399587

Faster-RCNN-TensorFlow-Python3.5 在Ubuntu16.04下的配置方法的更多相关文章

  1. ubuntu16.04下安装配置深度学习环境(Ubuntu 16.04/16.10+ cuda7.5/8+cudnn4/5+caffe)

    主要参照以下两篇博文:http://blog.csdn.net/g0m3e/article/details/51420565   http://blog.csdn.net/xuzhongxiong/a ...

  2. Ubuntu16.04下安装配置numpy,scipy,matplotlibm,pandas 以及sklearn+深度学习tensorflow配置+Keras2.0.6(非Anaconda环境)

    1.ubuntu镜像源准备(防止下载过慢): 参考博文:http://www.cnblogs.com/top5/archive/2009/10/07/1578815.html 步骤如下: 首先,备份一 ...

  3. (mark)ubuntu16.04下安装&配置anaconda+tensorflow新手教程

    https://blog.csdn.net/m0_37864814/article/details/82112029

  4. Ubuntu16.04下安装配置phpmyadmin

    在项目配置了阿里的druid,可以监控session,spring和详细sql的执行情况以及具体到表等信息,但是仍然发现不够详细,而且作为一个半吊子dba,一直用linux命令分配权限和管理数据库,效 ...

  5. ubuntu16.04下安装配置深度学习环境(一、cuda7.5的安装)

    1.下载所需要的软件 cuda7.5下载(点击下载链接),cudnn4.0下载 2.安装NVIDIA驱动. 一般有两种方法:1)一种方法是利用"软件和更新"来安装,依次选择 系统设 ...

  6. ubuntu16.04下安装配置pl-svo

    Semi-direct Visual Odometry(SVO)安装配置 https://blog.csdn.net/seymour163/article/details/53947764 http: ...

  7. ubuntu16.04下安装配置nodejs及npm

    官网下载ubuntu对应的二进制文件(Linux Binaries (x64)这一行) https://nodejs.org/en/download/ 解压并安装 ``` $tar zxvf node ...

  8. ubuntu16.04下caffe以cpu运行faster rcnn demo

    参考https://haoyu.love/blog404.html 获取并修改代码 首先,我们需要获取源代码: git clone --recursive https://github.com/rbg ...

  9. Ubuntu16.04下caffe CPU版的详细安装步骤

    一.caffe简介 Caffe,是一个兼具表达性.速度和思维模块化的深度学习框架. 由伯克利人工智能研究小组和伯克利视觉和学习中心开发. 虽然其内核是用C++编写的,但Caffe有Python和Mat ...

随机推荐

  1. 对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解

    对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解

  2. 006-springboot2.0.4 配置log4j2,以及打印mybatis的sql

    一.pom配置 普通项目 <!-- log4j2 --> <dependency> <groupId>org.apache.logging.log4j</gr ...

  3. Spark Storage(二) 集群下的broadcast

    Broadcast 简单来说就是将数据从一个节点复制到其他各个节点,常见用于数据复制到节点本地用于计算,在前面一章中讨论过Storage模块中BlockManager,Block既可以保存在内存中,也 ...

  4. Session实例

    Session常用方法(一) session对象用来保存一些在与每个用户回话期间需要保存的数据信息,这样就方便了回话期间的一些处理程序.如可以用session变量记住用户的用户名,以后就不必在其他的网 ...

  5. python排序函数sort()与sorted()区别

    sort是容器的函数:sort(cmp=None, key=None, reverse=False) sorted是python的内建函数:sorted(iterable, cmp=None, key ...

  6. LNMPA架构剖析

    LAMP或LNMP的劣势: Nginx是小巧而高效的Linux下Web服务器,跟Apache相比,它消耗资源更少,支持的并发连接更多,反向代理功能效率高.静态文件处理更快等等,Nginx可以承受3万以 ...

  7. Java序列化总结

    什么是序列化? 序列化是将对象的状态信息转化为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后可以通过存储区中读取或反序列化对象的状态重新创建对象. 为什么要序 ...

  8. nodejs+express的(前端跨域请求)

    1.后端代码 var dp = 456; var back = 'callback(\{\dp\ : \ ' + dp + '\ }\)'; res.send(back); 2.前端代码 <sc ...

  9. js 参数声明用var和不用var的区别

    var 声明的变量,作用域是当前 function 没有声明的变量,直接赋值的话, 会自动创建变量 ,但作用域是全局的. //----------------- function doSth() { ...

  10. tfs项目解绑及svn上传

    1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹