Win10调试ssd_tensorflow的目标检测
生活随笔
收集整理的這篇文章主要介紹了
Win10调试ssd_tensorflow的目标检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、環境:win10+tensorflow-gpu==1.14.0
2、下載代碼:到https://github.com/balancap/SSD-Tensorflow到本地
3、解壓代碼,并將checkpoints下的ssd_300_vgg.ckpt.zip進行解壓在checkpoints目錄下。否則后果不堪設想
4、如果你的電腦裝有jupyter notebook.則將此SSD-Tensorflow文件復制在jupyter文件目錄下,然后啟動jupyter notebook。
? ? ? 打開notebooks下的ssd_notebook.ipynb文件,運行每個cell。
? ? ? 也可以將此ipynb下的所有代碼復制在SSD_Tensorflow目錄新建的ssd_python.py中,運行
? ? ? 也可以直接搜網上教程,新建py文件,復制代碼,然后運行。
這個附上我復制的代碼:(切記修改路徑ckpt_file)
# coding: utf-8 # In[1]:import os import math import randomimport numpy as np import tensorflow as tf import cv2slim = tf.contrib.slim# In[2]: import matplotlib.pyplot as plt import matplotlib.image as mpimg# In[3]:import syssys.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:\gitcode\ssd\chde222-SSD-Tensorflow-master\SSD-Tensorflow\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.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[21]:# Test on some demo image and visualize output. path = 'E:/gitcode/ssd/chde222-SSD-Tensorflow-master/SSD-Tensorflow/demo/' image_names = sorted(os.listdir(path))img = mpimg.imread(path + image_names[-1]) 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)運行結果:
參考自https://blog.csdn.net/hezuo1181/article/details/91380182
總結
以上是生活随笔為你收集整理的Win10调试ssd_tensorflow的目标检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度贴吧发帖软件_贴吧自动发帖软件
- 下一篇: C语言-字符串处理函数strcmp