ubuntu下谷歌开源的TensorFlow Object Detection API的安装教程
环境
Ubuntu16.04/anaconda+tensorflow(gpu)1.4.0+python=3.5
下载
估计下载的很慢,提供一个百度云链接:https://pan.baidu.com/s/1EB1VLj5Znw_exAAUa1ftIg 密码:2o0a
直接下载整个文件就行了,因为后面要在jupyter notebook中运行,所以直接放在了jupyter notebook的工作目录里面(关于工作目录的配置我也写过,看一下就可以了),我的工作目录是Documents/jworkplace/
cd Documents/jworkplace/
unzip models-master.zip
安装
官方教程,其实教程就在这个文件里面models/research/object_detection/g3doc/installation.md(可以在github直接观看) 。
1.依赖
这里面需要安装的依赖
Protobuf
Pillow
lxml
Jupyter notebook
Matplotlib
Tensorflow
命令安装依赖
source activate tensorflow
conda install Protobuf
conda install lxml
conda install matplotlib
conda install pillow
2.编译
继续上面,使用Protobuf编译一下,需要在object_detection所在的父目录下进行及research下,
cd Documents/jworkplace/models-master/research/
protoc object_detection/protos/*.proto --python_out=.
不输出任何东西就可以了,输出这个Missing output directives也不行。
3.添加环境变量
格式是这样的
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
首先代开~/.bashrc
sudo gedit ~/.bashrc
然后在后面添加
export PYTHONPATH=$PYTHONPATH:/home/pc314/Documents/jworkplace/models-master/research:/home/pc314/Documents/jworkplace/models-master/research/slim
4.测试
代开jupyter notebook的话,应该会有models-master文件,打开models-master/research/object_detection/object_detection_tutorial.ipynb
然后点击Cell->Run all应该就可以了。正常的话会出现两张图。
(1)然而我运行这个的时候出现了
ImportError: No module named 'object_detection'
具体的解决方法是
打开/home/pc314/anaconda3/envs/tensorflow/lib/python3.5/site-packages,在这个目录下添加一个tensorflow_model.pth,里面的内容写上
/home/pc314/Documents/jworkplace/models-master/research
/home/pc314/Documents/jworkplace/models-master/research/slim
(2).ImportError: No module named 'utils'
pip install utils
这样子就可以了
(2)还有一个就是代码没有对齐,里面有一个最大的一个cell里面的代码,我这里面贴出来(省下时间)
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
上一篇: kafka常用的命令
下一篇: Play搭建腾讯云COS