deeplab_demo.ipynb 10.4 KB
Newer Older
yukun's avatar
yukun committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DeepLab Demo\n",
    "\n",
    "This demo will demostrate the steps to run deeplab semantic segmentation model on sample input images.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "Running this demo requires the following libraries:\n",
    "\n",
    "* Jupyter notebook (Python 2)\n",
    "* Tensorflow (>= v1.5.0)\n",
    "* Matplotlib\n",
    "* Pillow\n",
    "* numpy\n",
    "* ipywidgets (follow the setup [here](https://ipywidgets.readthedocs.io/en/stable/user_install.html))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
33
34
35
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
   "outputs": [],
   "source": [
    "import collections\n",
    "import os\n",
    "import StringIO\n",
    "import sys\n",
    "import tarfile\n",
    "import tempfile\n",
    "import urllib\n",
    "\n",
    "from IPython import display\n",
    "from ipywidgets import interact\n",
    "from ipywidgets import interactive\n",
    "from matplotlib import gridspec\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "if tf.__version__ < '1.5.0':\n",
    "    raise ImportError('Please upgrade your tensorflow installation to v1.5.0 or newer!')\n",
    "\n",
    "# Needed to show segmentation colormap labels\n",
    "sys.path.append('utils')\n",
    "import get_dataset_colormap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select and download models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
74
75
76
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
   "outputs": [],
   "source": [
    "_MODEL_URLS = {\n",
    "    'xception_coco_voctrainaug': 'http://download.tensorflow.org/models/deeplabv3_pascal_train_aug_2018_01_04.tar.gz',\n",
    "    'xception_coco_voctrainval': 'http://download.tensorflow.org/models/deeplabv3_pascal_trainval_2018_01_04.tar.gz',\n",
    "}\n",
    "\n",
    "Config = collections.namedtuple('Config', 'model_url, model_dir')\n",
    "\n",
    "def get_config(model_name, model_dir):\n",
    "    return Config(_MODEL_URLS[model_name], model_dir)\n",
    "\n",
    "config_widget = interactive(get_config, model_name=_MODEL_URLS.keys(), model_dir='')\n",
    "display.display(config_widget)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
96
97
98
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
   "outputs": [],
   "source": [
    "# Check configuration and download the model\n",
    "\n",
    "_TARBALL_NAME = 'deeplab_model.tar.gz'\n",
    "\n",
    "config = config_widget.result\n",
    "\n",
    "model_dir = config.model_dir or tempfile.mkdtemp()\n",
    "tf.gfile.MakeDirs(model_dir)\n",
    "\n",
    "download_path = os.path.join(model_dir, _TARBALL_NAME)\n",
    "print 'downloading model to %s, this might take a while...' % download_path\n",
    "urllib.urlretrieve(config.model_url, download_path)\n",
    "print 'download completed!'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load model in TensorFlow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
126
127
128
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
   "outputs": [],
   "source": [
    "_FROZEN_GRAPH_NAME = 'frozen_inference_graph'\n",
    "\n",
    "\n",
    "class DeepLabModel(object):\n",
    "    \"\"\"Class to load deeplab model and run inference.\"\"\"\n",
    "    \n",
    "    INPUT_TENSOR_NAME = 'ImageTensor:0'\n",
    "    OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'\n",
    "    INPUT_SIZE = 513\n",
    "\n",
    "    def __init__(self, tarball_path):\n",
    "        \"\"\"Creates and loads pretrained deeplab model.\"\"\"\n",
    "        self.graph = tf.Graph()\n",
    "        \n",
    "        graph_def = None\n",
    "        # Extract frozen graph from tar archive.\n",
    "        tar_file = tarfile.open(tarball_path)\n",
    "        for tar_info in tar_file.getmembers():\n",
    "            if _FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):\n",
    "                file_handle = tar_file.extractfile(tar_info)\n",
    "                graph_def = tf.GraphDef.FromString(file_handle.read())\n",
    "                break\n",
    "\n",
    "        tar_file.close()\n",
    "        \n",
    "        if graph_def is None:\n",
    "            raise RuntimeError('Cannot find inference graph in tar archive.')\n",
    "\n",
    "        with self.graph.as_default():      \n",
    "            tf.import_graph_def(graph_def, name='')\n",
    "        \n",
    "        self.sess = tf.Session(graph=self.graph)\n",
    "            \n",
    "    def run(self, image):\n",
    "        \"\"\"Runs inference on a single image.\n",
    "        \n",
    "        Args:\n",
    "            image: A PIL.Image object, raw input image.\n",
    "            \n",
    "        Returns:\n",
    "            resized_image: RGB image resized from original input image.\n",
    "            seg_map: Segmentation map of `resized_image`.\n",
    "        \"\"\"\n",
    "        width, height = image.size\n",
    "        resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)\n",
    "        target_size = (int(resize_ratio * width), int(resize_ratio * height))\n",
    "        resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)\n",
    "        batch_seg_map = self.sess.run(\n",
    "            self.OUTPUT_TENSOR_NAME,\n",
    "            feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})\n",
    "        seg_map = batch_seg_map[0]\n",
    "        return resized_image, seg_map\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
188
189
190
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
   "outputs": [],
   "source": [
    "model = DeepLabModel(download_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helper methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
206
207
208
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
   "outputs": [],
   "source": [
    "LABEL_NAMES = np.asarray([\n",
    "    'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',\n",
    "    'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',\n",
    "    'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa',\n",
    "    'train', 'tv'\n",
    "])\n",
    "\n",
    "FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)\n",
    "FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP)\n",
    "\n",
    "\n",
    "def vis_segmentation(image, seg_map):\n",
    "    plt.figure(figsize=(15, 5))\n",
    "    grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1])\n",
    "\n",
    "    plt.subplot(grid_spec[0])\n",
    "    plt.imshow(image)\n",
    "    plt.axis('off')\n",
    "    plt.title('input image')\n",
    "    \n",
    "    plt.subplot(grid_spec[1])\n",
    "    seg_image = get_dataset_colormap.label_to_color_image(\n",
    "        seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8)\n",
    "    plt.imshow(seg_image)\n",
    "    plt.axis('off')\n",
    "    plt.title('segmentation map')\n",
    "\n",
    "    plt.subplot(grid_spec[2])\n",
    "    plt.imshow(image)\n",
    "    plt.imshow(seg_image, alpha=0.7)\n",
    "    plt.axis('off')\n",
    "    plt.title('segmentation overlay')\n",
    "    \n",
    "    unique_labels = np.unique(seg_map)\n",
    "    ax = plt.subplot(grid_spec[3])\n",
    "    plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest')\n",
    "    ax.yaxis.tick_right()\n",
    "    plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])\n",
    "    plt.xticks([], [])\n",
YoGeSh's avatar
YoGeSh committed
250
    "    ax.tick_params(width=0)\n",
Yogesh K's avatar
Yogesh K committed
251
    "    plt.show()"
yukun's avatar
yukun committed
252
253
254
255
256
257
258
259
260
261
262
263
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run on sample images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
264
265
266
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
   "outputs": [],
   "source": [
    "# Note that we are using single scale inference in the demo for fast\n",
    "# computation, so the results may slightly differ from the visualizations\n",
    "# in README, which uses multi-scale and left-right flipped inputs.\n",
    "\n",
    "IMAGE_DIR = 'g3doc/img'\n",
    "\n",
    "def run_demo_image(image_name):\n",
    "    try:\n",
    "        image_path = os.path.join(IMAGE_DIR, image_name)\n",
    "        orignal_im = Image.open(image_path)\n",
    "    except IOError:\n",
    "        print 'Failed to read image from %s.' % image_path \n",
    "        return \n",
    "    print 'running deeplab on image %s...' % image_name\n",
    "    resized_im, seg_map = model.run(orignal_im)\n",
    "    \n",
    "    vis_segmentation(resized_im, seg_map)\n",
    "\n",
    "_ = interact(run_demo_image, image_name=['image1.jpg', 'image2.jpg', 'image3.jpg'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run on internet images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
YoGeSh's avatar
YoGeSh committed
301
    "collapsed": true,
yukun's avatar
yukun committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def get_an_internet_image(url):\n",
    "    if not url:\n",
    "        return\n",
    "\n",
    "    try:\n",
    "        # Prefix with 'file://' for local file.\n",
    "        if os.path.exists(url):\n",
    "            url = 'file://' + url\n",
    "        f = urllib.urlopen(url)\n",
    "        jpeg_str = f.read()\n",
    "    except IOError:\n",
    "        print 'invalid url: ' + url\n",
    "        return\n",
    "\n",
    "    orignal_im = Image.open(StringIO.StringIO(jpeg_str))\n",
    "    print 'running deeplab on image %s...' % url\n",
    "    resized_im, seg_map = model.run(orignal_im)\n",
    "    \n",
    "    vis_segmentation(resized_im, seg_map)\n",
    "\n",
    "_ = interact(get_an_internet_image, url='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
YoGeSh's avatar
YoGeSh committed
332
333
334
   "metadata": {
    "collapsed": true
   },
yukun's avatar
yukun committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}