Ask questions about Tensorflow's Single Shot MultiBox Detector algorithm.
Changed the source code SSD/Tensorflow/notebooks/ssd_notebook.ipynb to python format for the reference URL provided with the tutorial.
I wanted to recognize the object from the image file stored in the demo folder.
When I tried, the following error occurred.
If you know how to solve these problems, please let me know.
I look forward to your kind cooperation.
Reference URL
https://github.com/balancap/SSD-Tensorflow (tutorial)
Environment
win10
opencv3.1.0
python 3.6.0
Anaconda 4.3.1 (64bit)
tensorflow '0.12.0-rc1' (CPU version)
Folder Configuration
C:/Ana3/
└ SSD /
nnotebooks/ssd_notebook.ipynb (file for jupyter)
│ sssd_notebook.py (main file)
<
d demo / (discovery image folder)
c checkpoints/ssd_300_vgg (SSD model file)
Source Code
#https://github.com/balancap/SSD-Tensorflow( Tutorial)
# http://blog.csdn.net/jnulzl/article/details/68947496#t2 (change to python format)
importos
import path
import random
import numpy as np
import tensorflow as tf
import cv2
slim = tf.contrib.slim
#%matplotlib inline
import matplotlib.pyplot asplt
import matplotlib.image asmpimg
import sys
sys.path.append('../')
from nets import ssd_vgg_300, ssd_common, np_methods
from preprocessing import ssd_vgg_preprocessing
from notebooks import visualization
# 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 None
ssd_net=ssd_vgg_300.SSDNet()
with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
predictions, localizations,_,_=ssd_net.net(image_4d, is_training=False, reuse=reuse)
# Restore SSD model.Model file
ckpt_filename = 'C:\Ana3\SSD\checkpoints\ssd_300_vgg\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, rlocalizations, rbox_img = isess.run([image_4d, predictions, localizations, bbox_img],
feed_dict={img_input:img})
# Get classes and bboxes from the net outputs.
rclasses, rscores, rboxes = np_methods.ssd_bboxes_select(
rpredictions, rlocalizations, ssd_anchors,
select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)
rboxes=np_methods.bboxes_clip(rbox_img,rboxes)
rclasses, rscores, rboxes=np_methods.bboxes_sort (rclasses, rscores, rboxes, top_k=400)
rclasses, rscores, rboxes=np_methods.bboxes_nms (rclasses, rscores, rboxes, nms_threshold=nms_threshold)
# Resize bboxes to original image shape.Note: useless for Resize.WARP!
rboxes=np_methods.bboxes_resize(rbox_img,rboxes)
return rclasses, rscores, rboxes
# Test on some demo image and visualize output.
path='C:\Ana3\SSD\demo'
image_names=sorted(os.listdir(path))
img=mpimg.imread (path+image_names[-5])
for it in image_names:
img=mpimg.imread(path+it)
rclasses, rscores, rboxes=process_image(img)
# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
visualization.plt_bboxes(img,rclasses,rscores,rboxes)
It seems that SSD-Tensorflow does not match the current type of sensorflow zeros_initializer.
https://www.tensorflow.org/versions/master/api_docs/python/tf/zeros_initializer
As mentioned above, shape is required for the first argument.
I think the only way to do this is to wait for the author (balancap) to correct the part where tf.zeros_initializer() is used in the SSD-Tensorflow code as follows.
tf.constant_initializer(0.0)
Note:
The type seems to have changed between tensorflow 0.11 and 0.12.
If you are not particular about the sensorflow version, you can use 0.11 or earlier.
© 2024 OneMinuteCode. All rights reserved.