Ubuntu 16.04 安装Tensorflow Object Detection API (一)

1、安装Tensorflow

请参考官方说明及其它博客安装tensorflow,要使用Tensorflow Object Detction API,tensorflow版本需1.4及以后版本。

我的系统已经安装Ubuntu 16.04系统,基础软件安装如下表,tensorflow,opencv均正常工作

软件版本说明
Tensorflow1.4.0-rc1源码编译,支持GPU
CUDA9.0.176 
CUDNN7.0.3 
OpenCV3.3.1 

2、安装必要的包及库

sudo apt-get install protobuf-compiler
sudo apt-get install python-pil
sudo apt-get install python-lxml
sudo pip3 install jupyter
sudo pip3 install matplotlib

3、安装TensorFlow Object Detection API

在home目录下新建tensroflow目录。TensorFlow Object Detection API 存放在 tensorflow /models 项目下的。(地址: https://github.com/tensorflow/models

mkdir tensorflow
cd tensorflow
git clone https://github.com/tensorflow/models.git

 在clone的时候有可能比较慢,或提示网络超时。多试几次即可。clone完成后在tensorflow文件夹下有名为models的文件夹,在models文件夹下有research文件夹

4、protobuf编译

在research/object_detection/protos目录下有很多.proto文件,需要将这些.ptoto文件编译成.py文件。使用protoc对proto文件进行编译

protoc ~/tensorflow/models/research/object_detection/protos/*.proto --python_out=.

执行上述命令后,提示如下错误:

object_detection/protos/ssd.proto:104:3: Expected "required", "optional", or "repeated".
object_detection/protos/ssd.proto:104:12: Expected field name.
object_detection/protos/model.proto: Import "object_detection/protos/ssd.proto" was not found or had errors.
object_detection/protos/model.proto:12:5: "Ssd" is not defined.


查看protoc的版本是2.6.1,在以前的文章中说明2.6.1版本上OK的。但我下载的TensorFlow Object Detection API是2018年9月初的版本,protoc的2.6.1版本太低了。

protoc --version
libprotoc 2.6.1

需要升级protoc版本。首先卸载protoc旧版本。注意与第1步骤中安装protoc的命令参数对应

sudo apt-get remove protobuf-compiler
protoc --version
bash: /usr/bin/protoc: No such file or directory

到 https://github.com/google/protobuf/releases 下载最新的protoc安装包 protobuf-all-3.6.1.tar.gz

下载完后解压,然后执行下面的命令编译安装:

$ cd protobuf-3.6.1
$ ./configure --prefix=/usr
$ make -j15
$ make check -j15
$ sudo make install -j15
$ sudo ldconfig
$ protoc --version
libprotoc 3.6.1
  

安装后查看protoc版本为3.6.1 版本。再重新执行编译命令

$ protoc ./object_detection/protos/*.proto --python_out=.

命令执行成功,没再报错。查看protos目录下,有相应的.py文件

anchor_generator_pb2.py
anchor_generator.proto
argmax_matcher_pb2.py
 argmax_matcher.proto
bipartite_matcher_pb2.py
bipartite_matcher.proto

5、将slim添加到PYTHONPATH

TensorFlow Object Detection API 是以 Slim 为基础实现的,需要将 Slim 的目录加入 PYTHONPATH 后才能正确运行。为方便起见,直接在.bashrc  文件中添加如下一行。

export PYTHONPATH=$PYTHONPATH:/your home path/tensorflow/models/research:/your home path/tensorflow/models/research/slim

其中/your home path/请根据你自己的安装目录修改。然后执行

source ~/.bashrc

6、验证测试

  执行如下命令:

kou@aikou:~/tensorflow/models/research$ python object_detection/builders/model_builder_test.py

很遗憾,出现了如下错误:

Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 21, in <module>
    from object_detection.builders import model_builder
  File "/home/kou/tensorflow/models/research/object_detection/builders/model_builder.py", line 18, in <module>
    from object_detection.builders import anchor_generator_builder
  File "/home/kou/tensorflow/models/research/object_detection/builders/anchor_generator_builder.py", line 21, in <module>
    from object_detection.protos import anchor_generator_pb2
  File "/home/kou/tensorflow/models/research/object_detection/protos/anchor_generator_pb2.py", line 15, in <module>
    from object_detection.protos import grid_anchor_generator_pb2 as object__detection_dot_protos_dot_grid__anchor__generator__pb2
  File "/home/kou/tensorflow/models/research/object_detection/protos/grid_anchor_generator_pb2.py", line 22, in <module>
    serialized_pb=_b('\n3object_detection/protos/grid_anchor_generator.proto\x12\x17object_detection.protos\"\xcd\x01\n\x13GridAnchorGenerator\x12\x13\n\x06height\x18\x01 \x01(\x05:\x03\x32\x35\x36\x12\x12\n\x05width\x18\x02 \x01(\x05:\x03\x32\x35\x36\x12\x19\n\rheight_stride\x18\x03 \x01(\x05:\x02\x31\x36\x12\x18\n\x0cwidth_stride\x18\x04 \x01(\x05:\x02\x31\x36\x12\x18\n\rheight_offset\x18\x05 \x01(\x05:\x01\x30\x12\x17\n\x0cwidth_offset\x18\x06 \x01(\x05:\x01\x30\x12\x0e\n\x06scales\x18\x07 \x03(\x02\x12\x15\n\raspect_ratios\x18\x08 \x03(\x02')
TypeError: __new__() got an unexpected keyword argument 'serialized_options'

从上面的描述看,问题出在grid_anchor_generator_pb2.py文件的第22行,相关代码片段为:

DESCRIPTOR = _descriptor.FileDescriptor(
  name='object_detection/protos/grid_anchor_generator.proto',
  package='object_detection.protos',
  syntax='proto2',
  serialized_options=None,
  serialized_pb=_b('\n3object_detection/protos/grid_anchor_generator.proto\x12\x17object_detection.protos\"\xcd\x01\n\x13GridAnchorGenerator\x12\x13\n\x06height\x18\x01 \x01(\x05:\x03\x32\x35\x36\x12\x12\n\x05width\x18\x02 \x01(\x05:\x03\x32\x35\x36\x12\x19\n\rheight_stride\x18\x03 \x01(\x05:\x02\x31\x36\x12\x18\n\x0cwidth_stride\x18\x04 \x01(\x05:\x02\x31\x36\x12\x18\n\rheight_offset\x18\x05 \x01(\x05:\x01\x30\x12\x17\n\x0cwidth_offset\x18\x06 \x01(\x05:\x01\x30\x12\x0e\n\x06scales\x18\x07 \x03(\x02\x12\x15\n\raspect_ratios\x18\x08 \x03(\x02')
)

注释掉serialized_options=None,再次执行 python object_detection/builders/model_builder_test.py

Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 21, in <module>
    from object_detection.builders import model_builder
  File "/home/kou/tensorflow/models/research/object_detection/builders/model_builder.py", line 18, in <module>
    from object_detection.builders import anchor_generator_builder
  File "/home/kou/tensorflow/models/research/object_detection/builders/anchor_generator_builder.py", line 21, in <module>
    from object_detection.protos import anchor_generator_pb2
  File "/home/kou/tensorflow/models/research/object_detection/protos/anchor_generator_pb2.py", line 15, in <module>
    from object_detection.protos import grid_anchor_generator_pb2 as object__detection_dot_protos_dot_grid__anchor__generator__pb2
  File "/home/kou/tensorflow/models/research/object_detection/protos/grid_anchor_generator_pb2.py", line 41, in <module>
    serialized_options=None, file=DESCRIPTOR),
TypeError: __new__() got an unexpected keyword argument 'serialized_options'

从上面的描述看,问题出在grid_anchor_generator_pb2.py文件的第41行。将该文件中的所有serialized_options=None, 全部注释掉。注意只注释掉serialized_options=None, 其它内容不变。

再次执行,结果还是有类似错误有出现,都是serialized_options=None, 问题。通过如下命令发现protos目录下的.py文件中总共有652处serialized_options=None,

kou@aikou:~/tensorflow/models/research/object_detection/protos$ grep 'serialized_options=None' *.py | wc -l
652

从网上搜索发现可能是tensorflow的版本1.4.0太低,有1.4.0的网友反馈有此问题。我的刚好是这个版本。

继续注释掉serialized_options=None, 显然不是好的解决办法。要考虑升级tensorflow。

下一篇写如何升级tensorflow

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值