pc機tensorflow框架下ssd的一個demo

最近好不容易在自己的PC機上跑通了一個tensorflow框架在的ssd模型,網上很多都是在linux系統下的,因此寫下來記錄一下,以防忘記,大家遇到問題也可以相互探討;

平臺環境:win10 64位+tensorflow1.2版本cpu版本+ssd模型

源碼github地址:https://github.com/balancap/SSD-Tensorflow

目前pc機上好像只支持1.1和1.2版本的tensorflow,其他版本試了下裝不上,有裝上的可以請教下。關於tensorflow的安裝可以參考我的另一篇博文https://blog.csdn.net/u013230291/article/details/98961492

1.代碼地址:https://github.com/balancap/SSD-Tensorflow,下載到本地,由於是國外的網站,下載慢的話可以在百度雲或者CSDN上找資源;

2.解壓ssd_300_vgg.ckpt.zip 到checkpoint文件夾下,一定要放在checkpoint文件夾下,而不是放到子文件夾下;  

                             工程目錄

   

                                         模型目錄

3.在資源管理器中定位到notebook文件夾下,裏面有ssd_notebook.ipynb文件,該文件使用時需要用到工具jupyter,務必在運行前安裝好,(可使用pip list在控制檯查看是否已經安裝,如沒安裝則用命令即可安裝pip install jupyter),在該目錄下輸入cmd,如下圖所示:

    

然後在cmd中輸入

jupyter notebook

此時會打開一個網頁。

  

                    網頁端目錄

然後單擊打開ssd_notebook.ipynb文件,此時可以看到一個網頁端的編輯器,使用方式可以百度;

     

                             網頁端編輯器

點擊紅色框內的Run,即可運行一個模塊,一直運行到最後一個模塊即可看到結果。

 

                                 運行結果

修改紅色框中的數字即可測試不同的圖片。

這裏給出download as 轉換後的python源碼:


# coding: utf-8

# In[1]:


import os
import math
import random

import numpy as np
import tensorflow as tf
import cv2

slim = tf.contrib.slim


# In[2]:


get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


# In[3]:


import sys
sys.path.append('../')


# In[4]:


from nets import ssd_vgg_300, ssd_common, np_methods
from preprocessing import ssd_vgg_preprocessing
from notebooks import visualization


# In[5]:


# 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)


# ## SSD 300 Model
# 
# The SSD 300 network takes 300x300 image inputs. In order to feed any image, the latter is resize to this input shape (i.e.`Resize.WARP_RESIZE`). Note that even though it may change the ratio width / height, the SSD model performs well on resized images (and it is the default behaviour in the original Caffe implementation).
# 
# SSD anchors correspond to the default bounding boxes encoded in the network. The SSD net output provides offset on the coordinates and dimensions of these anchors.

# In[6]:


# 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 None
ssd_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 = 'E:/tensorflow_model/SSD-Tensorflow-master/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)


# ## Post-processing pipeline
# 
# The SSD outputs need to be post-processed to provide proper detections. Namely, we follow these common steps:
# 
# * Select boxes above a classification threshold;
# * Clip boxes to the image shape;
# * Apply the Non-Maximum-Selection algorithm: fuse together boxes whose Jaccard score > threshold;
# * If necessary, resize bounding boxes to original image shape.

# In[7]:


# 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.
    ssd_net = ssd_vgg_300.SSDNet()
    ssd_anchors = ssd_net.anchors(net_shape)
    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


# In[10]:


# Test on some demo image and visualize output.
path = 'E:/tensorflow_model/SSD-Tensorflow-master/demo/'
image_names = sorted(os.listdir(path))

img = mpimg.imread(path + image_names[-2])
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)

在運行過程中如有出現一些警告:

   

可添加如下命令忽略警告,以下命令是一個警告等級設置:

import os 
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

 

發佈了65 篇原創文章 · 獲贊 34 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章