You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
603 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import os\n",
"import math\n",
"import random\n",
"\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import cv2\n",
"\n",
"slim = tf.contrib.slim"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as mpimg"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from nets import ssd_vgg_300, ssd_common, np_methods\n",
"from preprocessing import ssd_vgg_preprocessing\n",
"from notebooks import visualization"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!!\n",
"gpu_options = tf.GPUOptions(allow_growth=True)\n",
"config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options)\n",
"isess = tf.InteractiveSession(config=config)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"source": [
"## SSD 300 Model\n",
"\n",
"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).\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": false
},
"outputs": [],
"source": [
"# Input placeholder.\n",
"net_shape = (300, 300)\n",
"data_format = 'NHWC'\n",
"img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))\n",
"# Evaluation pre-processing: resize to SSD net shape.\n",
"image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(\n",
" img_input, None, None, net_shape, data_format, resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)\n",
"image_4d = tf.expand_dims(image_pre, 0)\n",
"\n",
"# Define the SSD model.\n",
"reuse = True if 'ssd_net' in locals() else None\n",
"ssd_net = ssd_vgg_300.SSDNet()\n",
"with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):\n",
" predictions, localisations, _, _ = ssd_net.net(image_4d, is_training=False, reuse=reuse)\n",
"\n",
"# Restore SSD model.\n",
"ckpt_filename = '../checkpoints/ssd_300_vgg.ckpt'\n",
"# ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'\n",
"isess.run(tf.global_variables_initializer())\n",
"saver = tf.train.Saver()\n",
"saver.restore(isess, ckpt_filename)\n",
"\n",
"# SSD default anchor boxes.\n",
"ssd_anchors = ssd_net.anchors(net_shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## Post-processing pipeline\n",
"\n",
"The SSD outputs need to be post-processed to provide proper detections. Namely, we follow these common steps:\n",
"\n",
"* Select boxes above a classification threshold;\n",
"* Clip boxes to the image shape;\n",
"* Apply the Non-Maximum-Selection algorithm: fuse together boxes whose Jaccard score > threshold;\n",
"* If necessary, resize bounding boxes to original image shape."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": true
},
"outputs": [],
"source": [
"# Main image processing routine.\n",
"def process_image(img, select_threshold=0.5, nms_threshold=.45, net_shape=(300, 300)):\n",
" # Run SSD network.\n",
" rimg, rpredictions, rlocalisations, rbbox_img = isess.run([image_4d, predictions, localisations, bbox_img],\n",
" feed_dict={img_input: img})\n",
" \n",
" # Get classes and bboxes from the net outputs.\n",
" rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(\n",
" rpredictions, rlocalisations, ssd_anchors,\n",
" select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)\n",
" \n",
" rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)\n",
" rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)\n",
" rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)\n",
" # Resize bboxes to original image shape. Note: useless for Resize.WARP!\n",
" rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)\n",
" return rclasses, rscores, rbboxes"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAloAAAHFCAYAAAAqg1fhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3HmMJNl92Pnvizsi76ys+67u6mu6p+e+yDlE8dDBW5Jl\nrSlpobUO7MKGF9gFDPiPpQSvbOxi13/Y1h+CJcEyuZKswyTNGQ4pHjPkHJyZnu7p+67quqsyK+/M\nuOO9/aM4JCVDWArrwQqL+hQSyIiKyMiMyHzvl7/3eymUUhw6dOjQoUOHDh36r0/7//oJHDp06NCh\nQ4cO/f/VYaB16NChQ4cOHTr0HjkMtA4dOnTo0KFDh94jh4HWoUOHDh06dOjQe+Qw0Dp06NChQ4cO\nHXqPHAZahw4dOnTo0KFD75H3LNASQvyEEOKmEOKOEOKfvlfHOXTo0KFDhw4d+rtKvBe/oyWE0IFb\nwIeATeAt4BeUUtf+qx/s0KFDhw4d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28s9TWd7kjb/wuH1tiFeC\nne09hDQwrIBB38FzHLY21pmancD2DOKBQNM0PM9hEA0YxiFxJukPfZaXjrC7s00hl8fQIO/liNOE\n9e1d7jt1Btv1uHDhLfL5PLYw0VWGqZtomAwGPdrtJqWxMqaj0R8OGRufZTAY8ODJx1m9d4uvfPWr\n/NQHnkaQoSmda7duEocZ9x15gpkFjWLO5fzrtwiDlF6nz098/EOcu/Jdjp6aQyaSN7/7Og9PnkQT\nFjLJmJ2Z5LkPPM2FV77NtYs3eOjMUwyHPdrtXf63/+m3uHj1EnOnF/jS9e+wdHwZw8+otw1qtYTU\nrxJETaqVBCe1cbRFXnn
"text/plain": [
"<matplotlib.figure.Figure at 0x7f349ab800f0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Test on some demo image and visualize output.\n",
"path = '../demo/'\n",
"image_names = sorted(os.listdir(path))\n",
"\n",
"img = mpimg.imread(path + image_names[-5])\n",
"rclasses, rscores, rbboxes = process_image(img)\n",
"\n",
"# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)\n",
"visualization.plt_bboxes(img, rclasses, rscores, rbboxes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}