Mac跑tensorflow版SSD

来源:互联网 发布:linux php 目录权限 编辑:程序博客网 时间:2024/06/06 17:56

1,安装virtual

sudo easy_install pippip install --upgrade virtualenv 

2,创建和激活虚拟环境

virtualenv ~/tensorflowsource ~/tensorflow/bin/activate

3,安装tensorflow

easy_install -U pipsudo pip install --upgrade tensorflow

4,虚拟环境下安装和启动jupyter

sudo pip install jupytercd /Users/kylefan/DeepLearning/caffe-ssd-source/SSD-Tensorflow/notebooksjupyter notebook(先别启动,要安装完下面的)

5,安装matplotlib,pillow

sudo pip install matplotlibpip install pillow

6,修改ckpt文件:
解压开ckpt压缩包,把两个文件拷出来替代解压开的文件夹(如果是右击解压),也就是说
‘./checkpoints/ssd_300_vgg.ckpt’
这个路径下是ssd_300_vgg.ckpt.index和ssd_300_vgg.ckpt.data-00000-of-00001,而没有ssd_300_vgg.ckpt

7,ubuntu上出现ImportError: No module named matplotlib.pyplot

python -mpip install -U matplotlib

8,ubuntu上出现ImportError: No module named cv2:
看看下面这个位置有cv2不,然后在python文件最前加入:

import syssys.path.append('/usr/local/lib/python2.7/dist-packages')

完整的ssd-depoly.py,此文件放在SSD-Tensorflow下面,并且把notebooks/visualization.py拷贝到上一层目录

import osimport mathimport randomimport numpy as npimport tensorflow as tfimport cv2slim = tf.contrib.slim#%matplotlib inlineimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport syssys.path.append('./')sys.path.append('../')from nets import ssd_vgg_300, ssd_common, np_methodsfrom preprocessing import ssd_vgg_preprocessingimport visualization#import notebooks# TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!!gpu_options = tf.GPUOptions(allow_growth=True)config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options)isess = tf.InteractiveSession(config=config)# Input placeholder.net_shape = (300, 300)data_format = 'NHWC'img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))# Evaluation pre-processing: resize to SSD net shape.image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(    img_input, None, None, net_shape, data_format, resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)image_4d = tf.expand_dims(image_pre, 0)# Define the SSD model.reuse = True if 'ssd_net' in locals() else Nonessd_net = ssd_vgg_300.SSDNet()with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):    predictions, localisations, _, _ = ssd_net.net(image_4d, is_training=False, reuse=reuse)# Restore SSD model.ckpt_filename = './checkpoints/ssd_300_vgg.ckpt'# ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'isess.run(tf.global_variables_initializer())saver = tf.train.Saver()saver.restore(isess, ckpt_filename)# SSD default anchor boxes.ssd_anchors = ssd_net.anchors(net_shape)# Main image processing routine.def process_image(img, select_threshold=0.5, nms_threshold=.45, net_shape=(300, 300)):    # Run SSD network.    rimg, rpredictions, rlocalisations, rbbox_img = isess.run([image_4d, predictions, localisations, bbox_img],                                                              feed_dict={img_input: img})    # Get classes and bboxes from the net outputs.    rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(            rpredictions, rlocalisations, ssd_anchors,            select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)    rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)    rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)    rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)    # Resize bboxes to original image shape. Note: useless for Resize.WARP!    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)    return rclasses, rscores, rbboxes    # Test on some demo image and visualize output.path = './demo/'image_names = sorted(os.listdir(path))#img = mpimg.imread(path + image_names[-5])img = mpimg.imread('./demo/person.jpg')rclasses, rscores, rbboxes =  process_image(img)# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)visualization.plt_bboxes(img, rclasses, rscores, rbboxes)