Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
886a6fea
P
PaddleSeg
项目概览
PaddlePaddle
/
PaddleSeg
通知
285
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSeg
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
886a6fea
编写于
8月 31, 2020
作者:
W
wuzewu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix infer bug
上级
5ae538c0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
15 deletion
+26
-15
contrib/SpatialEmbeddings/infer.py
contrib/SpatialEmbeddings/infer.py
+26
-15
未找到文件。
contrib/SpatialEmbeddings/infer.py
浏览文件 @
886a6fea
...
...
@@ -15,14 +15,15 @@ cfg = getattr(config, 'cfg')
cluster
=
Cluster
()
# 预测数据集类
class
TestDataSet
():
def
__init__
(
self
):
self
.
data_dir
=
cfg
.
data_dir
self
.
data_dir
=
cfg
.
data_dir
self
.
data_list_file
=
cfg
.
data_list_file
self
.
data_list
=
self
.
get_data_list
()
self
.
data_num
=
len
(
self
.
data_list
)
def
get_data_list
(
self
):
# 获取预测图像路径列表
data_list
=
[]
...
...
@@ -41,7 +42,7 @@ class TestDataSet():
h
,
w
=
img
.
shape
[:
2
]
h_new
,
w_new
=
cfg
.
input_size
img
=
np
.
pad
(
img
,
((
0
,
h_new
-
h
),
(
0
,
w_new
-
w
),
(
0
,
0
)),
'edge'
)
img
=
img
.
astype
(
np
.
float32
)
/
255.0
img
=
img
.
astype
(
np
.
float32
)
/
255.0
img
=
img
.
transpose
((
2
,
0
,
1
))
img
=
np
.
expand_dims
(
img
,
axis
=
0
)
return
img
...
...
@@ -51,30 +52,33 @@ class TestDataSet():
img_path
=
self
.
data_list
[
index
]
img
=
np
.
array
(
PILImage
.
open
(
img_path
))
if
img
is
None
:
return
img
,
img
,
img_path
,
None
return
img
,
img
,
img_path
,
None
img_name
=
img_path
.
split
(
os
.
sep
)[
-
1
]
name_prefix
=
img_name
.
replace
(
'.'
+
img_name
.
split
(
'.'
)[
-
1
],
''
)
name_prefix
=
img_name
.
replace
(
'.'
+
img_name
.
split
(
'.'
)[
-
1
],
''
)
img_shape
=
img
.
shape
[:
2
]
img_process
=
self
.
preprocess
(
img
)
return
img_process
,
name_prefix
,
img_shape
def
get_model
(
main_prog
,
startup_prog
):
img_shape
=
[
3
,
cfg
.
input_size
[
0
],
cfg
.
input_size
[
1
]]
with
fluid
.
program_guard
(
main_prog
,
startup_prog
):
with
fluid
.
unique_name
.
guard
():
input
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
img_shape
,
dtype
=
'float32'
)
input
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
img_shape
,
dtype
=
'float32'
)
output
=
SpatialEmbeddings
(
input
)
return
input
,
output
def
infer
():
if
not
os
.
path
.
exists
(
cfg
.
vis_dir
):
os
.
makedirs
(
cfg
.
vis_dir
)
startup_prog
=
fluid
.
Program
()
test_prog
=
fluid
.
Program
()
input
,
output
=
get_model
(
test_prog
,
startup_prog
)
test_prog
=
test_prog
.
clone
(
for_test
=
True
)
...
...
@@ -82,11 +86,17 @@ def infer():
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
if
not
os
.
path
.
exists
(
cfg
.
model_path
):
raise
RuntimeError
(
'No pre-trained model found under path {}'
.
format
(
cfg
.
model_path
))
# 加载预测模型
def
if_exist
(
var
):
return
os
.
path
.
exists
(
os
.
path
.
join
(
cfg
.
model_path
,
var
.
name
))
fluid
.
io
.
load_vars
(
exe
,
cfg
.
model_path
,
main_program
=
test_prog
,
predicate
=
if_exist
)
fluid
.
io
.
load_vars
(
exe
,
cfg
.
model_path
,
main_program
=
test_prog
,
predicate
=
if_exist
)
#加载预测数据集
test_dataset
=
TestDataSet
()
data_num
=
test_dataset
.
data_num
...
...
@@ -97,9 +107,10 @@ def infer():
if
image
is
None
:
print
(
im_name
,
'is None'
)
continue
# 预测
outputs
=
exe
.
run
(
program
=
test_prog
,
feed
=
{
'image'
:
image
},
fetch_list
=
output
)
outputs
=
exe
.
run
(
program
=
test_prog
,
feed
=
{
'image'
:
image
},
fetch_list
=
output
)
instance_map
,
predictions
=
cluster
.
cluster
(
outputs
[
0
][
0
],
n_sigma
=
cfg
.
n_sigma
,
\
min_pixel
=
cfg
.
min_pixel
,
threshold
=
cfg
.
threshold
)
...
...
@@ -109,14 +120,14 @@ def infer():
output_im
=
PILImage
.
fromarray
(
np
.
asarray
(
instance_map
,
dtype
=
np
.
uint8
))
palette
=
get_palette
(
len
(
predictions
)
+
1
)
output_im
.
putpalette
(
palette
)
result_path
=
os
.
path
.
join
(
cfg
.
vis_dir
,
im_name
+
'.png'
)
result_path
=
os
.
path
.
join
(
cfg
.
vis_dir
,
im_name
+
'.png'
)
output_im
.
save
(
result_path
)
if
(
idx
+
1
)
%
100
==
0
:
print
(
'%d processd'
%
(
idx
+
1
))
print
(
'%d processd done'
%
(
idx
+
1
))
print
(
'%d processd done'
%
(
idx
+
1
))
return
0
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录