unet_inference.ipynb 6.44 KB
Newer Older
1
2
{
 "cells": [
3
4
	{
		"cell_type": "code",
5
		"execution_count": null,
6
		"metadata": {},
7
		"outputs": [],
8
		"source": [
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
			"#  The MIT License (MIT)\n",
			"#\n",
			"#  Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.\n",
			"#\n",
			"#  Permission is hereby granted, free of charge, to any person obtaining a copy\n",
			"#  of this software and associated documentation files (the 'Software'), to deal\n",
			"#  in the Software without restriction, including without limitation the rights\n",
			"#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
			"#  copies of the Software, and to permit persons to whom the Software is\n",
			"#  furnished to do so, subject to the following conditions:\n",
			"#\n",
			"#  The above copyright notice and this permission notice shall be included in\n",
			"#  all copies or substantial portions of the Software.\n",
			"#\n",
			"#  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
			"#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
			"#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\n",
			"#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
			"#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
			"#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n",
			"#  THE SOFTWARE.\n"
30
31
		]
	},
32
33
  {
   "cell_type": "markdown",
34
   "id": "cd7a3990",
35
36
37
38
39
40
41
   "metadata": {},
   "source": [
    "## Import MIGraphX Python Library"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
42
   "execution_count": null,
43
   "id": "3930d7b8",
44
45
46
   "metadata": {},
   "outputs": [],
   "source": [
47
48
49
    "import migraphx\n",
    "from PIL import Image\n",
    "import numpy as np\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
50
    "import matplotlib.pyplot as plt"
51
52
53
54
   ]
  },
  {
   "cell_type": "markdown",
55
   "id": "b350c333",
56
57
58
59
60
61
62
   "metadata": {},
   "source": [
    "## Fetch U-NET ONNX Model"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
63
   "execution_count": null,
64
65
   "id": "02a7b7de",
   "metadata": {},
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
66
   "outputs": [],
67
   "source": [
68
    "!wget -nc https://www.dropbox.com/s/3ntkhyk30x05uuv/unet_13_256.onnx"
69
70
71
72
73
74
75
76
77
78
79
80
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6cfe6e9",
   "metadata": {},
   "source": [
    "## Load ONNX Model"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
81
   "execution_count": null,
82
83
84
85
   "id": "e05a13dc",
   "metadata": {},
   "outputs": [],
   "source": [
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
86
87
88
89
90
    "model = migraphx.parse_onnx(\"unet_13_256.onnx\")"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
91
   "execution_count": null,
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
92
93
94
95
   "id": "52c67023",
   "metadata": {},
   "outputs": [],
   "source": [
96
97
98
99
100
101
102
103
104
105
106
107
108
    "model.compile(migraphx.get_target(\"gpu\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80edb6f1",
   "metadata": {},
   "source": [
    "## Print model parameters"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
109
   "execution_count": null,
110
111
   "id": "fd5c3269",
   "metadata": {},
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
112
   "outputs": [],
113
114
115
116
117
118
119
   "source": [
    "print(model.get_parameter_names())\n",
    "print(model.get_parameter_shapes())"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
120
   "execution_count": null,
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
121
   "id": "47f956c7",
122
123
124
   "metadata": {},
   "outputs": [],
   "source": [
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    "def preprocess(pil_img, newW, newH):\n",
    "    w, h = pil_img.size\n",
    "    assert newW > 0 and newH > 0, 'Scale is too small'\n",
    "    pil_img = pil_img.resize((newW, newH))\n",
    "\n",
    "    img_nd = np.array(pil_img)\n",
    "\n",
    "    if len(img_nd.shape) == 2:\n",
    "        img_nd = np.expand_dims(img_nd, axis=2)\n",
    "\n",
    "    # HWC to CHW\n",
    "    img_print = pil_img\n",
    "    img_trans = img_nd.transpose((2, 0, 1))\n",
    "    if img_trans.max() > 1:\n",
    "        img_trans = img_trans / 255\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
140
141
    "        \n",
    "    img_trans = np.expand_dims(img_trans, 0)\n",
142
143
144
145
    "\n",
    "    return img_trans, img_print\n",
    "\n",
    "def plot_img_and_mask(img, mask):\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
146
147
    "    classes = mask.shape[0] if len(mask.shape) > 3 else 1\n",
    "    print(classes)\n",
148
149
150
151
152
153
154
155
156
    "    fig, ax = plt.subplots(1, classes + 1)\n",
    "    ax[0].set_title('Input image')\n",
    "    ax[0].imshow(img)\n",
    "    if classes > 1:\n",
    "        for i in range(classes):\n",
    "            ax[i+1].set_title(f'Output mask (class {i+1})')\n",
    "            ax[i+1].imshow(mask[:, :, i])\n",
    "    else:\n",
    "        ax[1].set_title(f'Output mask')\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
157
    "        ax[1].imshow(mask[0,0])\n",
158
159
160
161
162
163
    "    plt.xticks([]), plt.yticks([])\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
164
   "execution_count": null,
165
166
   "id": "389ddc4d",
   "metadata": {},
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
167
   "outputs": [],
168
   "source": [
169
    "img = Image.open(\"./car1.jpeg\")\n",
170
    "img, imPrint = preprocess(img, 256, 256)\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
171
172
173
174
    "input_im = np.zeros((1,3,256,256),dtype='float32') \n",
    "np.lib.stride_tricks.as_strided(input_im, shape=img.shape, strides=input_im.strides)[:] = img #getting correct stride\n",
    "print(input_im.strides)\n",
    "print(input_im.shape)\n",
175
    "imPrint.show()"
176
177
   ]
  },
178
179
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
180
   "execution_count": null,
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
181
   "id": "9de6f2a7",
182
   "metadata": {},
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
183
   "outputs": [],
184
   "source": [
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
185
186
    "mask = model.run({'inputs':input_im}) # Your first inference would take longer than the following ones.\n",
    "output_mask = np.array(mask[0])\n",
187
    "print(output_mask.shape)"
188
189
   ]
  },
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
190
191
192
193
194
195
196
197
198
199
200
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acbd68e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "  return 1 / (1 + np.exp(-x))"
   ]
  },
201
202
  {
   "cell_type": "code",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
203
   "execution_count": null,
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
204
   "id": "58e3062c",
205
   "metadata": {},
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
206
   "outputs": [],
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
207
   "source": [
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
208
    "probs = sigmoid(output_mask)\n",
209
    "full_mask = probs > 0.996\n",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
210
    "plot_img_and_mask(imPrint, full_mask)"
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
211
   ]
212
  },
213
214
215
216
217
218
219
  {
   "cell_type": "markdown",
   "id": "6126df0b",
   "metadata": {},
   "source": [
    "<b>NOTE:</b> The model weights utilized here are trained by using car images with plain backgrounds. The imperfect result on a \"real-world\" image as shown above is expected. To get a better result fine-tuning the model on a dataset of real-world examples is recommended. "
   ]
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
237
   "pygments_lexer": "ipython3"
238
239
240
241
242
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}