TensorFlow Object Detection API 实践

原文地址:https://mp.weixin.qq.com/s/eN41M1sMLxvQMEl0IhyC9w
官网地址:
https://github.com/tensorflow/models/tree/master/research/object_detection

1、API安装

TF Object Detection API 安装步骤。

  • 安装最新版 TensorFlow:
# For CPU
pip3 install tensorflow # 会自动安装protobuf
# For GPU
pip3 install tensorflow-gpu # 会自动安装protobuf

# conda
conda install tensorflow # 会自动安装protobuf
  • 接着安装必要的 Python 包:
# on Ubuntu 16.04 using via apt-get
sudo apt-get install protobuf-compiler # centos 跳过这步
sudo pip3 install pillow
sudo pip3 install lxml
sudo pip3 install jupyter
# 启动jupyter 输入 jupyter notebook
sudo pip3 install matplotlib
  • 获取 TensorFlow Models 源码
# 我这里是克隆到/root/myfloder/
git clone https://github.com/tensorflow/models --recursive
# or git clone https://github.com/tensorflow/models.git
cd models/research 
  • (可选)如果protobuf安装出错可以手动编译
wget https://github.com/google/protobuf/archive/v3.3.0.tar.gz
tar zxvf v3.3.0.tar.gz
cd protobuf-3.3.0/
./autogen.sh
./configure
make -j
make install
  • Protobuf Compilation
cd /root/myfloder/models/research
protoc object_detection/protos/*.proto --python_out=.
  • Add Libraries to PYTHONPATH
cd /root/myfloder/models/research
vim ~/.bashrc 
export PYTHONPATH=$PYTHONPATH:'/root/myfloder/models/research':'/root/myfloder/models/research/slim'

通过这一步,可以看到 TF Object Detection API 使用了 TF-Slim API,参考TF-Slim 设计复杂网络。
测试安装是否成功:

python3 object_detection/builders/model_builder_test.py

这里写图片描述

2、使用API 训练一个 Faster RCNN 目标检测模型

今天我们利用 TF Object Detection API 从头训练一个 Faster RCNN 目标检测模型,使用 ResNet-152 网络和 Oxford-IIIT 宠物数据集。

  • 下载训练数据
cd /root/myfloder/models/research
wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz
tar -xvf images.tar.gz
tar -xvf annotations.tar.gz
  • 将训练数据转换为 tfrecord 格式
cd /root/myfloder/models/research
mkdir datas
mv images annotations ./datas
python3 object_detection/create_pet_tf_record.py \
    --label_map_path=object_detection/data/pet_label_map.pbtxt \
    --data_dir='./datas/' \
    --output_path='./'

运行结束后,你会看到 pet_train.record 和 pet_val.record 两个文件。
准备工作目录 20170820,将转换的 tfrecord 文件放入这个目录。将 object_detection/data/pet_label_map.pbtxt 和 object_detection/samples/configs/faster_rcnn_resnet152_pets.config 两个文件也拷贝到这个目录。

mkdir 20170820
mv pet_train.record pet_val.record 20170820
cp -r object_detection/data/pet_label_map.pbtxt ./20170820
cp -r object_detection/samples/configs/faster_rcnn_resnet152_pets.config ./20170820

该目录内容如下:
这里写图片描述

  • 运行训练
vim ./20170820/faster_rcnn_resnet152_pets.config
'''
将PATH_TO_BE_CONFIGURED 都替换成 20170820
第 110 和 111 行内容改为:
# fine_tune_checkpoint: "20170820/model.ckpt"
from_detection_checkpoint: false
'''
cd /root/myfloder/models/research
python3 object_detection/train.py \
    --logtostderr \
    --pipeline_config_path='./20170820/faster_rcnn_resnet152_pets.config' \
    --train_dir='./20170820/'

其中–train_dir 指向 20170820/,训练产生的中间结果都存放在该目录。 ${PATH_TO_YOUR_PIPELINE_CONFIG} 指向配置文件

训练中
这里写图片描述

  • 查看 TensorBoard

先启动 tensorboard 服务
tensorboard --logdir='./20170820'

  • 评估训练好的网络
# From the tensorflow/models/research/ directory
python3 object_detection/eval.py \
    --logtostderr \
    --pipeline_config_path='./20170820/faster_rcnn_resnet152_pets.config' \
    --checkpoint_dir='./20170820/' \
    --eval_dir='./20170820/'

这里 –eval_dir= 路径与之前 –train_dir 相同。

  • 进阶

完成这些任务之后,可以尝试(1)将训练数据改为自己的数据集;(2)将模型改为使用其他网络如 VGG、MobileNets;(3)利用 TF Object Detection API 实现 YOLOv2 。

参考文献
【1】 https://github.com/tensorflow/models/tree/master/object_detection
【2】https://github.com/tensorflow/models/blob/master/object_detection/g3doc/installation.md
【3】 “Speed/accuracy trade-offs for modern convolutional object detectors.” Huang J, Rathod V, Sun C, Zhu M, Korattikara A, Fathi A, Fischer I, Wojna Z, Song Y, Guadarrama S, Murphy K, CVPR 2017
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/preparing_inputs.md
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_locally.md

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值