Pengjin commited on
Commit
6f8f7d3
1 Parent(s): 5f2a430

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +2 -0
  2. UsonicSimple.ipynb +418 -0
  3. mymodel.pdparams +3 -0
  4. optimizer.pdopt +3 -0
.gitattributes CHANGED
@@ -32,3 +32,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ mymodel.pdparams filter=lfs diff=lfs merge=lfs -text
36
+ optimizer.pdopt filter=lfs diff=lfs merge=lfs -text
UsonicSimple.ipynb ADDED
@@ -0,0 +1,418 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "请点击[此处](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576)查看本环境基本用法. <br>\n",
8
+ "Please click [here ](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576) for more detailed instructions. "
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 11,
14
+ "metadata": {
15
+ "execution": {
16
+ "iopub.execute_input": "2022-10-11T15:15:49.511879Z",
17
+ "iopub.status.busy": "2022-10-11T15:15:49.511286Z",
18
+ "iopub.status.idle": "2022-10-11T15:15:51.568549Z",
19
+ "shell.execute_reply": "2022-10-11T15:15:51.567597Z",
20
+ "shell.execute_reply.started": "2022-10-11T15:15:49.511839Z"
21
+ },
22
+ "jupyter": {
23
+ "outputs_hidden": false
24
+ },
25
+ "scrolled": true,
26
+ "tags": []
27
+ },
28
+ "outputs": [],
29
+ "source": [
30
+ "import os\n",
31
+ "import io\n",
32
+ "import numpy as np\n",
33
+ "import matplotlib.pyplot as plt\n",
34
+ "from PIL import Image\n",
35
+ "import paddle\n",
36
+ "from paddle.nn import functional as F\n",
37
+ "import random\n",
38
+ "from paddle.io import Dataset\n",
39
+ "from visualdl import LogWriter\n",
40
+ "from paddle.vision.transforms import transforms as T\n",
41
+ "import warnings\n",
42
+ "import cv2 as cv\n",
43
+ "from PIL import Image\n",
44
+ "import re\n",
45
+ "warnings.filterwarnings(\"ignore\")\n",
46
+ "os.environ[\"KMP_DUPLICATE_LIB_OK\"]=\"TRUE\""
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 12,
52
+ "metadata": {
53
+ "execution": {
54
+ "iopub.execute_input": "2022-10-11T15:16:14.415916Z",
55
+ "iopub.status.busy": "2022-10-11T15:16:14.415245Z",
56
+ "iopub.status.idle": "2022-10-11T15:16:14.428584Z",
57
+ "shell.execute_reply": "2022-10-11T15:16:14.427470Z",
58
+ "shell.execute_reply.started": "2022-10-11T15:16:14.415874Z"
59
+ },
60
+ "jupyter": {
61
+ "outputs_hidden": false
62
+ },
63
+ "tags": []
64
+ },
65
+ "outputs": [],
66
+ "source": [
67
+ "class SeparableConv2D(paddle.nn.Layer):\n",
68
+ " def __init__(self,\n",
69
+ " in_channels,\n",
70
+ " out_channels,\n",
71
+ " kernel_size,\n",
72
+ " stride=1,\n",
73
+ " padding=0,\n",
74
+ " dilation=1,\n",
75
+ " groups=None,\n",
76
+ " weight_attr=None,\n",
77
+ " bias_attr=None,\n",
78
+ " data_format=\"NCHW\"):\n",
79
+ " super(SeparableConv2D, self).__init__()\n",
80
+ "\n",
81
+ " self._padding = padding\n",
82
+ " self._stride = stride\n",
83
+ " self._dilation = dilation\n",
84
+ " self._in_channels = in_channels\n",
85
+ " self._data_format = data_format\n",
86
+ "\n",
87
+ " # 第一次卷积参数,没有偏置参数\n",
88
+ " filter_shape = [in_channels, 1] + self.convert_to_list(kernel_size, 2, 'kernel_size')\n",
89
+ " self.weight_conv = self.create_parameter(shape=filter_shape, attr=weight_attr)\n",
90
+ "\n",
91
+ " # 第二次卷积参数\n",
92
+ " filter_shape = [out_channels, in_channels] + self.convert_to_list(1, 2, 'kernel_size')\n",
93
+ " self.weight_pointwise = self.create_parameter(shape=filter_shape, attr=weight_attr)\n",
94
+ " self.bias_pointwise = self.create_parameter(shape=[out_channels],\n",
95
+ " attr=bias_attr,\n",
96
+ " is_bias=True)\n",
97
+ "\n",
98
+ " def convert_to_list(self, value, n, name, dtype=np.int):\n",
99
+ " if isinstance(value, dtype):\n",
100
+ " return [value, ] * n\n",
101
+ " else:\n",
102
+ " try:\n",
103
+ " value_list = list(value)\n",
104
+ " except TypeError:\n",
105
+ " raise ValueError(\"The \" + name +\n",
106
+ " \"'s type must be list or tuple. Received: \" + str(\n",
107
+ " value))\n",
108
+ " if len(value_list) != n:\n",
109
+ " raise ValueError(\"The \" + name + \"'s length must be \" + str(n) +\n",
110
+ " \". Received: \" + str(value))\n",
111
+ " for single_value in value_list:\n",
112
+ " try:\n",
113
+ " dtype(single_value)\n",
114
+ " except (ValueError, TypeError):\n",
115
+ " raise ValueError(\n",
116
+ " \"The \" + name + \"'s type must be a list or tuple of \" + str(\n",
117
+ " n) + \" \" + str(dtype) + \" . Received: \" + str(\n",
118
+ " value) + \" \"\n",
119
+ " \"including element \" + str(single_value) + \" of type\" + \" \"\n",
120
+ " + str(type(single_value)))\n",
121
+ " return value_list\n",
122
+ "\n",
123
+ " def forward(self, inputs):\n",
124
+ " conv_out = F.conv2d(inputs,\n",
125
+ " self.weight_conv,\n",
126
+ " padding=self._padding,\n",
127
+ " stride=self._stride,\n",
128
+ " dilation=self._dilation,\n",
129
+ " groups=self._in_channels,\n",
130
+ " data_format=self._data_format)\n",
131
+ "\n",
132
+ " out = F.conv2d(conv_out,\n",
133
+ " self.weight_pointwise,\n",
134
+ " bias=self.bias_pointwise,\n",
135
+ " padding=0,\n",
136
+ " stride=1,\n",
137
+ " dilation=1,\n",
138
+ " groups=1,\n",
139
+ " data_format=self._data_format)\n",
140
+ "\n",
141
+ " return out\n",
142
+ "class Encoder(paddle.nn.Layer):\n",
143
+ " def __init__(self, in_channels, out_channels):\n",
144
+ " super(Encoder, self).__init__()\n",
145
+ "\n",
146
+ " self.relus = paddle.nn.LayerList(\n",
147
+ " [paddle.nn.ReLU() for i in range(2)])\n",
148
+ " self.separable_conv_01 = SeparableConv2D(in_channels,\n",
149
+ " out_channels,\n",
150
+ " kernel_size=3,\n",
151
+ " padding='same')\n",
152
+ " self.bns = paddle.nn.LayerList(\n",
153
+ " [paddle.nn.BatchNorm2D(out_channels) for i in range(2)])\n",
154
+ "\n",
155
+ " self.separable_conv_02 = SeparableConv2D(out_channels,\n",
156
+ " out_channels,\n",
157
+ " kernel_size=3,\n",
158
+ " padding='same')\n",
159
+ " self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)\n",
160
+ " self.residual_conv = paddle.nn.Conv2D(in_channels,\n",
161
+ " out_channels,\n",
162
+ " kernel_size=1,\n",
163
+ " stride=2,\n",
164
+ " padding='same')\n",
165
+ "\n",
166
+ " def forward(self, inputs):\n",
167
+ " previous_block_activation = inputs\n",
168
+ "\n",
169
+ " y = self.relus[0](inputs)\n",
170
+ " y = self.separable_conv_01(y)\n",
171
+ " y = self.bns[0](y)\n",
172
+ " y = self.relus[1](y)\n",
173
+ " y = self.separable_conv_02(y)\n",
174
+ " y = self.bns[1](y)\n",
175
+ " y = self.pool(y)\n",
176
+ "\n",
177
+ " residual = self.residual_conv(previous_block_activation)\n",
178
+ " y = paddle.add(y, residual)\n",
179
+ "\n",
180
+ " return y\n",
181
+ "class Decoder(paddle.nn.Layer):\n",
182
+ " def __init__(self, in_channels, out_channels):\n",
183
+ " super(Decoder, self).__init__()\n",
184
+ "\n",
185
+ " self.relus = paddle.nn.LayerList(\n",
186
+ " [paddle.nn.ReLU() for i in range(2)])\n",
187
+ " self.conv_transpose_01 = paddle.nn.Conv2DTranspose(in_channels,\n",
188
+ " out_channels,\n",
189
+ " kernel_size=3,\n",
190
+ " padding=1)\n",
191
+ " self.conv_transpose_02 = paddle.nn.Conv2DTranspose(out_channels,\n",
192
+ " out_channels,\n",
193
+ " kernel_size=3,\n",
194
+ " padding=1)\n",
195
+ " self.bns = paddle.nn.LayerList(\n",
196
+ " [paddle.nn.BatchNorm2D(out_channels) for i in range(2)]\n",
197
+ " )\n",
198
+ " self.upsamples = paddle.nn.LayerList(\n",
199
+ " [paddle.nn.Upsample(scale_factor=2.0) for i in range(2)]\n",
200
+ " )\n",
201
+ " self.residual_conv = paddle.nn.Conv2D(in_channels,\n",
202
+ " out_channels,\n",
203
+ " kernel_size=1,\n",
204
+ " padding='same')\n",
205
+ "\n",
206
+ " def forward(self, inputs):\n",
207
+ " previous_block_activation = inputs\n",
208
+ "\n",
209
+ " y = self.relus[0](inputs)\n",
210
+ " y = self.conv_transpose_01(y)\n",
211
+ " y = self.bns[0](y)\n",
212
+ " y = self.relus[1](y)\n",
213
+ " y = self.conv_transpose_02(y)\n",
214
+ " y = self.bns[1](y)\n",
215
+ " y = self.upsamples[0](y)\n",
216
+ "\n",
217
+ " residual = self.upsamples[1](previous_block_activation)\n",
218
+ " residual = self.residual_conv(residual)\n",
219
+ "\n",
220
+ " y = paddle.add(y, residual)\n",
221
+ "\n",
222
+ " return y\n",
223
+ "class PetNet(paddle.nn.Layer):\n",
224
+ " def __init__(self, num_classes):\n",
225
+ " super(PetNet, self).__init__()\n",
226
+ "\n",
227
+ " self.conv_1 = paddle.nn.Conv2D(3, 32,\n",
228
+ " kernel_size=3,\n",
229
+ " stride=2,\n",
230
+ " padding='same')\n",
231
+ " self.bn = paddle.nn.BatchNorm2D(32)\n",
232
+ " self.relu = paddle.nn.ReLU()\n",
233
+ "\n",
234
+ " in_channels = 32\n",
235
+ " self.encoders = []\n",
236
+ " self.encoder_list = [64, 128, 256]\n",
237
+ " self.decoder_list = [256, 128, 64, 32]\n",
238
+ "\n",
239
+ " for out_channels in self.encoder_list:\n",
240
+ " block = self.add_sublayer('encoder_{}'.format(out_channels),\n",
241
+ " Encoder(in_channels, out_channels))\n",
242
+ " self.encoders.append(block)\n",
243
+ " in_channels = out_channels\n",
244
+ "\n",
245
+ " self.decoders = []\n",
246
+ "\n",
247
+ " for out_channels in self.decoder_list:\n",
248
+ " block = self.add_sublayer('decoder_{}'.format(out_channels),\n",
249
+ " Decoder(in_channels, out_channels))\n",
250
+ " self.decoders.append(block)\n",
251
+ " in_channels = out_channels\n",
252
+ "\n",
253
+ " self.output_conv = paddle.nn.Conv2D(in_channels,\n",
254
+ " num_classes,\n",
255
+ " kernel_size=3,\n",
256
+ " padding='same')\n",
257
+ "\n",
258
+ " def forward(self, inputs):\n",
259
+ " y = self.conv_1(inputs)\n",
260
+ " y = self.bn(y)\n",
261
+ " y = self.relu(y)\n",
262
+ "\n",
263
+ " for encoder in self.encoders:\n",
264
+ " y = encoder(y)\n",
265
+ "\n",
266
+ " for decoder in self.decoders:\n",
267
+ " y = decoder(y)\n",
268
+ "\n",
269
+ " y = self.output_conv(y)\n",
270
+ " return y\n",
271
+ "IMAGE_SIZE = (512, 512)\n",
272
+ "num_classes = 2\n",
273
+ "network = PetNet(num_classes)\n",
274
+ "model = paddle.Model(network)"
275
+ ]
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "execution_count": 13,
280
+ "metadata": {
281
+ "execution": {
282
+ "iopub.execute_input": "2022-10-11T15:16:14.415916Z",
283
+ "iopub.status.busy": "2022-10-11T15:16:14.415245Z",
284
+ "iopub.status.idle": "2022-10-11T15:16:14.428584Z",
285
+ "shell.execute_reply": "2022-10-11T15:16:14.427470Z",
286
+ "shell.execute_reply.started": "2022-10-11T15:16:14.415874Z"
287
+ },
288
+ "jupyter": {
289
+ "outputs_hidden": false
290
+ },
291
+ "scrolled": true,
292
+ "tags": []
293
+ },
294
+ "outputs": [],
295
+ "source": [
296
+ "#加载训练好的权重\n",
297
+ "optimizer = paddle.optimizer.RMSProp(learning_rate=0.001, parameters=network.parameters())\n",
298
+ "layer_state_dict = paddle.load(\"mymodel.pdparams\")\n",
299
+ "opt_state_dict = paddle.load(\"optimizer.pdopt\")\n",
300
+ "\n",
301
+ "network.set_state_dict(layer_state_dict)\n",
302
+ "optimizer.set_state_dict(opt_state_dict)"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "execution_count": 14,
308
+ "metadata": {
309
+ "execution": {
310
+ "iopub.execute_input": "2022-10-11T16:07:50.639995Z",
311
+ "iopub.status.busy": "2022-10-11T16:07:50.639338Z",
312
+ "iopub.status.idle": "2022-10-11T16:07:50.941928Z",
313
+ "shell.execute_reply": "2022-10-11T16:07:50.940805Z",
314
+ "shell.execute_reply.started": "2022-10-11T16:07:50.639949Z"
315
+ },
316
+ "jupyter": {
317
+ "outputs_hidden": false
318
+ },
319
+ "tags": []
320
+ },
321
+ "outputs": [],
322
+ "source": [
323
+ "def FinalImage(mask,image):\n",
324
+ " # 这个函数的作用是把mask高斯模糊之后的遮罩和原始的image叠加起来\n",
325
+ " #输入 mask [0,255]的这招图\n",
326
+ " #image 必须无条件转化为512*512 三通道彩图\n",
327
+ " \n",
328
+ " th = cv.threshold(mask,140,255,cv.THRESH_BINARY)[1]\n",
329
+ " blur = cv.GaussianBlur(th,(33,33), 15)\n",
330
+ " heatmap_img = cv.applyColorMap(blur, cv.COLORMAP_OCEAN)\n",
331
+ " Blendermap = cv.addWeighted(heatmap_img, 0.5, image, 1, 0)\n",
332
+ " return Blendermap"
333
+ ]
334
+ },
335
+ {
336
+ "cell_type": "code",
337
+ "execution_count": 15,
338
+ "metadata": {},
339
+ "outputs": [
340
+ {
341
+ "name": "stdout",
342
+ "output_type": "stream",
343
+ "text": [
344
+ "IMPORTANT: You are using gradio version 3.12.0, however version 3.14.0 is available, please upgrade.\n",
345
+ "--------\n",
346
+ "Running on local URL: http://127.0.0.1:7864\n",
347
+ "Running on public URL: https://317fc297694e39a2.gradio.app\n",
348
+ "\n",
349
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
350
+ ]
351
+ },
352
+ {
353
+ "data": {
354
+ "text/html": [
355
+ "<div><iframe src=\"https://317fc297694e39a2.gradio.app\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
356
+ ],
357
+ "text/plain": [
358
+ "<IPython.core.display.HTML object>"
359
+ ]
360
+ },
361
+ "metadata": {},
362
+ "output_type": "display_data"
363
+ },
364
+ {
365
+ "data": {
366
+ "text/plain": []
367
+ },
368
+ "execution_count": 15,
369
+ "metadata": {},
370
+ "output_type": "execute_result"
371
+ }
372
+ ],
373
+ "source": [
374
+ "import gradio as gr\n",
375
+ "def Showsegmentation(image):\n",
376
+ " mask = paddle.argmax(network(paddle.to_tensor([((image - 127.5) / 127.5).transpose(2, 0, 1)]))[0], axis=0).numpy()\n",
377
+ " mask=mask.astype('uint8')*255\n",
378
+ " immask=cv.resize(mask, (512, 512))\n",
379
+ " image=cv.resize(image,(512,512))\n",
380
+ " blendmask=FinalImage(immask,image)\n",
381
+ " return blendmask\n",
382
+ "\n",
383
+ "gr.Interface(fn=Showsegmentation, inputs=\"image\", outputs=\"image\").launch(share=True)"
384
+ ]
385
+ },
386
+ {
387
+ "cell_type": "code",
388
+ "execution_count": null,
389
+ "metadata": {},
390
+ "outputs": [],
391
+ "source": []
392
+ }
393
+ ],
394
+ "metadata": {
395
+ "kernelspec": {
396
+ "display_name": "Python 3 (ipykernel)",
397
+ "language": "python",
398
+ "name": "python3"
399
+ },
400
+ "language_info": {
401
+ "codemirror_mode": {
402
+ "name": "ipython",
403
+ "version": 3
404
+ },
405
+ "file_extension": ".py",
406
+ "mimetype": "text/x-python",
407
+ "name": "python",
408
+ "nbconvert_exporter": "python",
409
+ "pygments_lexer": "ipython3",
410
+ "version": "3.8.5"
411
+ },
412
+ "toc-autonumbering": true,
413
+ "toc-showcode": true,
414
+ "toc-showmarkdowntxt": true
415
+ },
416
+ "nbformat": 4,
417
+ "nbformat_minor": 4
418
+ }
mymodel.pdparams ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4799775b1a4c96d435f52814aff2a7f4c085b61d23bc508a435fd6a9309b1c5
3
+ size 8245289
optimizer.pdopt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d131089d6ef5b45ee64d61ac2419b7f86b2331e2c89b124eda3881613cc4a057
3
+ size 24685981