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

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

转载于:https://www.cnblogs.com/pprp/p/9465065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值