SSD-tensorflow使用文档(-)——测试训练基本流程

SSD: Single Shot MultiBox Detector in TensorFlow

这里只从工程的角度介绍这份代码的使用方法,参照github上 balancap这个人 根据论文作者提供的caffe源码复现的tensorflow代码及其给出的教程,并加入了自己的一些补充和理解

补充:在ssd-tensorflow代码的fix_training分支中,作者增加了如下几点:
1. use a very simple data pre-processing pipeline at beginning to test out the training script;
2. use the argument trainable_scopes for only training the new parts of the network. Then, in a second time, fine tune the full network.
3. I implement an hard mining which is equivalent to the SSD Caffe, and looking at how to improve the data augmentation part.


1.基本介绍

  • 这里只实现了 SSD-300 VGG-basedSSD-512 VGG-based 两个版本,但这个project的框架的模块化应该可以保证SSD的其他变种(如ResNet or Inception 版本)实现起来也不难,等有时间我尝试好之后会分享给大家的。

  • 本project的组织结构受启发于TF-Slim models repository,TF-slim包含了当下流行的ResNet, Inception and VGG结构。主要包括3个部分:

    1. datasets:(Pasca VOC 2007_trainval2007_test2012COCO)这些数据集的接口和 将其转为tensorflow专用TF-Records格式的脚本文件。
    2. networks: SSD网络结构的定义,常用的encoding和decoding方法(refer to the paper on this precise topic)
    3. pre-processing: pre-processing and data augmentation routines, inspired by original VGG and Inception implementations.

2.SSD minimal example

利用jupyter Notebook可以轻松的运行SSD的一个测试用例,你要做的就是:
1. 下载模型ssd_300_vgg,存放在SSD-Tensorflow-master/checkpoints/这个目录下
2. 打开终端,在主目录 SSD-Tensorflow-master 下,运行cd notebooks/进入noteboo目录,运行 jupyter-notebook进入http://localhost:8892/tree服务器,打开 ssd_notebook.ipynb.ipynb文件
3. 配置一些路径:第4个cell中from notebooks import visualization改成import notebooks,不改的话目录结构不对,会报错
4. 使用自己的图片,改变path = '../demo/'成自己的图片所在路径
5. 从头到尾运行所有cell即可。

我在网上随便找了张图片测试了一下,效果确实还不错,只是这个代码没有把标签对应的种类显示在上面,懒得改了,以后用的时候再说,上图啦:
pedestrian
image

分析:估计这里2代表自行车把,15代表人把,但是第二张图的手势也是别未15(人类),估计分类里面没有手势这一项把。后续将进行更多的测试,这里先介绍到这里。


3.论文效果

小例子跑起来之后,当然不能就这么满足了,要给自己定个小目标,自然就是要通过自己的训练达到接近论文的效果。
- SSD作者本人用caffe实现的代码所达到的效果如下:

SystemVOC2007 test mAPFPS (Titan X)Number of BoxesInput resolution
Faster R-CNN (VGG16)73.27~6000~1000 x 600
YOLO (customized)63.44598448 x 448
SSD300* (VGG16)77.2468732300 x 300
SSD512* (VGG16)79.81924564512 x 512

image Note: SSD300* and SSD512* are the latest models. Current code should reproduce these results.

对比发现,tensorflow版本的效果与caffe版本代码相当的效果。


4. 行动指南

4.1 制作数据集

以VOC2007为例,目录结构如下

+VOCtrainval_06-Nov-2007
    +VOCdevkit
        +VOC2007
            +Annotations
            +ImageSets
            +JPEGImages
            +SegmentationClass
            +SegmentationObject
+VOCtest_06-Nov-2007
    +VOCdevkit
        +VOC2007
            +Annotations
            +ImageSets
            +JPEGImages
            +SegmentationClass
            +SegmentationObject

为了避免每次在终端敲那么多字母,建议新建一个.sh文件,把以下脚本粘贴进去,只要在这里更改dataset的路径就可以了,最后在文件所在目录打开终端并输入bash tf_convert_data.sh:(本教程所建立的所有shell脚本都存放在/root/shell/这个文件夹下)

#filename = tf_convert_data.sh
#!/bin/bash
#This is a shell script to convert Pascal VOC datasets(2007 and 2012) into TF-Records only.

#Directory where the original dataset is stored
DATASET_DIR=/root/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/

#Output directory where to store TFRecords files
OUTPUT_DIR=/root/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007_tfrecord/

python ../tf_convert_data.py \
    --dataset_name=pascalvoc \
    --dataset_dir=${DATASET_DIR} \
    --output_name=voc_2007_train \
    --output_dir=${OUTPUT_DIR}

4.2 验证

下载好三个checkpoint:SSD-300 VGG-based, SSD-300 VGG-based, SSD-512 VGG-based,为了 reproduce evaluation metrics on the recall-precision curve && compute mAP metrics following the Pascal VOC 2007 and 2012 guidelines. 跟制作数据集的shell 脚本一样,可以在tf_convert_data.sh脚本所在目录”/shell”,新建脚本并将如下代码粘贴进去,运行bash eval_ssd_network.sh执行:

#!/bin/bash
# This is the eval script.

DATASET_DIR=/home/doctorimage/kindlehe/common/dataset/VOC2007/VOCtest_06-Nov-2007/VOCdevkit/VOC2007_tfrecord/
#/home/doctorimage/kindlehe/common/dataset/VOCdevkit/
#../../../../common/dataset/VOC2007/VOCtest_06-Nov-2007/VOCdevkit/
EVAL_DIR=../log_files/log_eval/    # Directory where the results are saved to
CHECKPOINT_PATH=/home/doctorimage/kindlehe/common/models/tfmodlels/SSD/VGG_VOC0712_SSD_300x300_ft_iter_120000/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt
#../../../../common/models/tfmodlels/SSD/VGG_VOC0712_SSD_300x300_ft_iter_120000/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt


#dataset_name这个参数在代码里面写死了
python3 ../eval_ssd_network.py \
    --eval_dir=${EVAL_DIR} \
    --dataset_dir=${DATASET_DIR} \
    --dataset_name=pascalvoc_2007 \
    --dataset_split_name=test \
    --model_name=ssd_300_vgg \
    --checkpoint_path=${CHECKPOINT_PATH} \
    --batch_size=1

#python ../eval_ssd_network.py \
#    --eval_dir=${EVAL_DIR} \        # Directory where the results are saved to
#    --dataset_dir=${DATASET_DIR} \  # The directory where the dataset files are stored
#    --dataset_name=voc2007_test \ # The name of the dataset to load
#    --dataset_split_name=test \     # The name of the train/test split
#    --model_name=ssd_300_vgg \      # The name of the architecture to evaluate
#    --checkpoint_path=${CHECKPOINT_PATH} \  #The directory where the model was written to or an absolute path to a
                                            #checkpoint file
#    --batch_size=1                  # The number of samples in each batch

执行脚本之前,先替换一下作者eval_ssd_network.py这个代码的开头,由于是在python3环境下运行,我重新安装了python3版的tensorflow-gpu,采用的native pip安装,不建议哈,建议大家以后去使用docker,据说是可以用docker使用同一台机器的多个人分配GPU资源,防止撞车,谁玩过docker的欢迎来知道一下我,我叫kindle,唯一的一个kinlde!

至于注释的那一段,可能由于我重新安装的tensorflow,运行官方测试小demo居然会报这个错误 ++Internal: failed initializing StreamExecutor for CUDA device ordinal 1++,但是小demo中的sess = tf.Session()这条语句连续第二次运行的时候,一切又回复正常,wtf!!!

from tensorflow.python.framework import ops
from datasets import dataset_factory
from nets import nets_factory
from preprocessing import preprocessing_factory

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1" #原因是在创建session时没有使用我想让它用的gpu,难道每次都只能在这里制定一块GPU才能运行?有别的办法吗?

import math
import sys
import six
import time

import numpy as np
import tensorflow as tf
import tf_extended as tfe
import tf_utils

slim = tf.contrib.slim

../log_files/log_eval/下生成三个文件:events.out.tfevents.1498225326.doctorimagePC, events.out.tfevents.1498225563.doctorimagePC,training_config.txt

INFO:tensorflow:Evaluation [4952/4952] #测试机是VOC07 test
2017-06-23 22:47:38.481517: I tensorflow/core/kernels/logging_ops.cc:79] AP_VOC07/mAP[0.74313211395020917]
2017-06-23 22:47:44.241878: I tensorflow/core/kernels/logging_ops.cc:79] AP_VOC12/mAP[0.7665967841531145]
INFO:tensorflow:Finished evaluation at 2017-06-23-14:47:44
Time spent : 3703.294 seconds.  
Time spent per BATCH: 0.748 seconds.
tensorboard显示

在目录~/kindlehe/project/SSD/SSD-Tensorflow-master/log_files下运行tensorboard --logdir=log_eval,在浏览器中输入++http://localhost:6006++即可


4.3 训练

终于可以开始训练了,话不多说,先准备在VOC07+12 trainval训练着,等周末来了再看,机器配置是:Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz 显卡:1080 Ti 12G显存

依然是.sh脚本的形式:

#!/bin/bash

#The directory where the dataset files are stored.
DATASET_DIR=/home/doctorimage/kindlehe/common/dataset/VOC2007/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007_tfrecord/
#../../../../common/dataset/VOC2007/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007_tfrecord/

#Directory where checkpoints and event logs are written to.
TRAIN_DIR=.././log_files/log_finetune/

#The path to a checkpoint from which to fine-tune
CHECKPOINT_PATH=/home/doctorimage/kindlehe/common/models/tfmodlels/SSD/VGG_VOC0712_SSD_300x300_ft_iter_120000/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt
#../../../../common/models/tfmodlels/SSD/VGG_VOC0712_SSD_300x300_ft_iter_120000/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt



python3 ../train_ssd_network.py \
    --train_dir=${TRAIN_DIR} \
    --dataset_dir=${DATASET_DIR} \
    --dataset_name=pascalvoc_2007 \
    --dataset_split_name=train \
    --model_name=ssd_300_vgg \
    --checkpoint_path=${CHECKPOINT_PATH} \
    --save_summaries_secs=60 \
    --save_interval_secs=600 \
    --weight_decay=0.0005 \
    --optimizer=adam \
    --learning_rate=0.001 \
    --batch_size=32

5. 遇到的一些坑

  1. –dataset_name=pascalvoc_2007 、–dataset_split_name=train、–model_name=ssd_300_vgg这三个参数不要自己随便取,在代码里,这三个参数是if…else…语句,有固定的判断值,所以要根据实际情况取选择
  2. TypeError: expected bytes, NoneType found
    SystemError: returned a result with an error set
    这是由于CHECKPOINT_PATH定义的时候不小心多了个#号键,将输入给注释掉了,如果不想使用预训练的模型,需要将--checkpoint_path=${CHECKPOINT_PATH} \注释掉即可
  3. SSD有在VOC07+12的训练集上一起训练的,用一个笨一点的办法:
    pascalvoc_to_tfrecords.py文件中,改变SAMPLES_PER_FILES,减少输出tfrecord文件的个数,再修改tf_convert_data.py的dataset参数,记得将前后两次的输出名改变一下,前后两次转换的tfrecords放在同一个文件夹下,然后手工重命名。(这里由于只是验证论文的训练方法是否有效,所以没必要写这些自动化代码实现合并,以后要用自己的数据集训练的时候就可以写一些自动化脚本)
  4. 有时候运行脚本会报错,可能是之前依次运行导致显存占满。
  5. 从pyCharm运行时,如果模型保存路径里之前的模型未删除,将会报错,必须保证该文件夹为空。

  • 20
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 88
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值