Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
UNet_migraphx
Commits
76d4e7b9
Commit
76d4e7b9
authored
May 30, 2023
by
liucong
Browse files
修改python代码中的预处理过程
parent
0b73611d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
20 deletions
+26
-20
Doc/Tutorial_Python/Unet.md
Doc/Tutorial_Python/Unet.md
+14
-10
Python/Segmentation/Unet.py
Python/Segmentation/Unet.py
+10
-8
README.md
README.md
+2
-2
No files found.
Doc/Tutorial_Python/Unet.md
View file @
76d4e7b9
...
...
@@ -14,23 +14,27 @@
1.尺度变换,将图像resize到256x256大小
2.
归一化,将数据归一化到[0.0, 1.0]之间
2.
数据排布,将数据从HWC转换为CHW,再将维度转换为NCHW
3.
数据排布,将数据从HWC转换为CHW,再将维度转换为NCHW
3.
归一化,将数据归一化到[0.0, 1.0]之间
本示例代码通过如下方式实现预处理操作:
```
python
def
Preprocessing
(
pil_img
,
newW
,
newH
):
assert
newW
>
0
and
newH
>
0
,
'Scale is too small'
img_nd
=
cv2
.
resize
(
pil_img
,
(
newW
,
newH
))
# 将图像尺寸修改为256x256
img_nd
=
cv2
.
cvtColor
(
img_nd
,
cv2
.
COLOR_BGR2RGB
)
# BGR转换为RGB
img_trans
=
img_nd
.
transpose
((
2
,
0
,
1
))
# HWC转换为CHW
if
img_trans
.
max
()
>
1
:
# 保证数据处于0-1之间的浮点数
img_trans
=
img_trans
/
255.0
img_trans
=
np
.
expand_dims
(
img_trans
,
0
)
# CHW转换为NCHW
img
=
img_trans
.
astype
(
np
.
float32
)
# 转换成浮点型数据
img_nd
=
cv2
.
cvtColor
(
pil_img
,
cv2
.
COLOR_BGR2RGB
)
# BGR转换为RGB
img_nd
=
cv2
.
resize
(
img_nd
,
(
newW
,
newH
))
# 将图像尺寸修改为256x256
if
len
(
img_nd
.
shape
)
==
2
:
img_nd
=
np
.
expand_dims
(
img_nd
,
axis
=
2
)
img_trans
=
img_nd
.
transpose
((
2
,
0
,
1
))
# HWC转换为CHW
img_trans
=
np
.
expand_dims
(
img_trans
,
0
)
# CHW扩展为NCHW
img_trans
=
np
.
ascontiguousarray
(
img_trans
)
# 保证内存连续存储
img_trans
=
img_trans
.
astype
(
np
.
float32
)
# 转换成浮点型数据
if
img_trans
.
max
()
>
1
:
img
=
img_trans
/
255.0
# 保证数据处于0-1之间的浮点数
return
img
```
...
...
Python/Segmentation/Unet.py
View file @
76d4e7b9
...
...
@@ -4,17 +4,19 @@ import migraphx
def
Preprocessing
(
pil_img
,
newW
,
newH
):
assert
newW
>
0
and
newH
>
0
,
'Scale is too small'
img_nd
=
cv2
.
resize
(
pil_img
,
(
newW
,
newH
))
# 将图像尺寸修改为256x256
img_nd
=
cv2
.
cvtColor
(
img_nd
,
cv2
.
COLOR_BGR2RGB
)
# BGR转换为RGB
img_nd
=
cv2
.
cvtColor
(
pil_img
,
cv2
.
COLOR_BGR2RGB
)
# BGR转换为RGB
img_nd
=
cv2
.
resize
(
img_nd
,
(
newW
,
newH
))
# 将图像尺寸修改为256x256
if
len
(
img_nd
.
shape
)
==
2
:
img_nd
=
np
.
expand_dims
(
img_nd
,
axis
=
2
)
img_trans
=
img_nd
.
transpose
((
2
,
0
,
1
))
# HWC转换为CHW
if
img_trans
.
max
()
>
1
:
# 保证数据处于0-1之间的浮点数
img_trans
=
img_trans
/
255.0
img_trans
=
np
.
expand_dims
(
img_trans
,
0
)
# CHW扩展为NCHW
img
=
img_trans
.
astype
(
np
.
float32
)
# 转换成浮点型数据
img_trans
=
img_nd
.
transpose
((
2
,
0
,
1
))
# HWC转换为CHW
img_trans
=
np
.
expand_dims
(
img_trans
,
0
)
# CHW扩展为NCHW
img_trans
=
np
.
ascontiguousarray
(
img_trans
)
# 保证内存连续存储
img_trans
=
img_trans
.
astype
(
np
.
float32
)
# 转换成浮点型数据
if
img_trans
.
max
()
>
1
:
img
=
img_trans
/
255.0
# 保证数据处于0-1之间的浮点数
return
img
def
Sigmoid
(
x
):
...
...
README.md
View file @
76d4e7b9
...
...
@@ -84,7 +84,7 @@ pip install -r requirements.txt
python
Unet
.
py
```
输出结果为:
会在当前目录中生成分割图像
<img
src=
"./Doc/Images/Unet_03.jpg"
style=
"zoom:100%;"
align=
middle
>
...
...
@@ -103,7 +103,7 @@ cd ./build/
.
/
MIGraphX_Samples
0
```
会在当前目录中生成分割
结果图像Result.jpg
会在当前目录中生成分割
图像
<img
src=
"./Doc/Images/Unet_02.jpg"
style=
"zoom:100%;"
align=
middle
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment