0.运行tensorflow官方示例:
注:本文基于window10,python3
在官网model里的research文件夹
https://github.com/tensorflow/models/tree/master/research
在页面中往下翻找到object_detection文件夹

1.下载代码库
在models代码库的界面中,有下载整个代码库的按钮,如下图红色箭头标注处所示


通过vpn访问github,下载速度会加快。
对于本文的读者来说,其实只需要代码库models中的research/object_detection文件夹就可以。
代码库models文件大小有439M,但是object_detection文件夹只有17M左右。



2.proto文件转py文件
下载和安装protoc:
Protobuf(Google Protocol Buffers)是google开发的的一套用于数据存储,网络通信时用于协议编解码的工具库。它和XML和Json数据差不多,把数据以某种形式保存起来。Protobuf相对与XML和Json的不同之处,它是一种二进制的数据格式,具有更高的传输,打包和解包效率。
下载Protobuf网址:https://github.com/google/protobuf/releases
下载Protobuf网址页面如下图所示,下图中红色箭头标注处是Protobuf在操作系统Windows中可以直接运行的protoc程序,下载该压缩文件protoc-3.7.0-win64.zip。

解压后将bin文件夹中protoc.exe复制到c/window


使用protoc:
在工程object_detection文件夹中,找到文件夹protos,如下图所示:

进入文件夹protos,从下图中可以看出文件夹中有很多以proto为后缀的文件,接下来的操作,就是把这些proto文件转换为py文件

与工程object_detection文件夹同级目录中,打开cmd,不进入object_detection文件夹

运行jupyter notebook

新建一个ipynb文件,即点击选择New->Python3

复制下面一段代码
import os
file_list = os.listdir('object_detection/protos/')
proto_list = [file for file in file_list if '.proto' in file]
print('object_detection/proto文件夹中共有%d个proto文件' %len(proto_list))
for proto in proto_list:
execute_command = 'protoc object_detection/protos/%s --python_out=.' %proto
os.popen(execute_command)
file_list = os.listdir('object_detection/protos/')
py_list = [file for file in file_list if '.py' in file]
print('通过protoc命令产生的py文件共有%d个' %(len(py_list) - 1))

重新进入文件夹object_detection/protos,文件按照文件名排序,从下图中可以看出文件夹中每个proto文件后都有一个py文件,即将proto文件转py文件成功。

3.下载模型
下载模型指的是下载已经训练好的模型。在文件object_detection_tutorial.ipynb中有下载模型的代码语句。
代码可以运行,但是无法得到结果,因为代码中的下载链接是国外的网址。
本文作者提供百度云盘供读者下载:
链接:https://pan.baidu.com/s/1a4u-Xeu8KDKABlE6DfmM3Q
提取码:ek3n
将下载好的压缩文件放到工程object_detection文件夹中,如下图所示



文件夹ssd_mobilenet_v1_coco_2017_11_17中有文件夹saved_model和6个文件

4.运行ipynb文件
在工程object_detection文件夹中运行cmd

打开jupter notebook


删除代码块,再run all 模块


5.总结
作者本人在运行时出现两个报错:
1.删除红色框中内容,运行成功

2.pillow库版本之前-U升级过,版本过高,按提示安装旧版:先pip uninstall,再用阿里云镜像安装,具体步骤见文章tensorflow-gpu安装:https://www.jianshu.com/p/e528eeb2226e

网友评论