Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
0017c6d5
P
PaddleSeg
项目概览
PaddlePaddle
/
PaddleSeg
通知
286
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看板
提交
0017c6d5
编写于
6月 02, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update utils.py
上级
8062ca40
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
0 addition
and
161 deletion
+0
-161
dygraph/utils/utils.py
dygraph/utils/utils.py
+0
-161
未找到文件。
dygraph/utils/utils.py
浏览文件 @
0017c6d5
...
...
@@ -33,10 +33,6 @@ def seconds_to_hms(seconds):
def
setting_environ_flags
():
if
'FLAGS_eager_delete_tensor_gb'
not
in
os
.
environ
:
os
.
environ
[
'FLAGS_eager_delete_tensor_gb'
]
=
'0.0'
if
'FLAGS_allocator_strategy'
not
in
os
.
environ
:
os
.
environ
[
'FLAGS_allocator_strategy'
]
=
'auto_growth'
if
"CUDA_VISIBLE_DEVICES"
in
os
.
environ
:
if
os
.
environ
[
"CUDA_VISIBLE_DEVICES"
].
count
(
"-1"
)
>
0
:
os
.
environ
[
"CUDA_VISIBLE_DEVICES"
]
=
""
...
...
@@ -62,163 +58,6 @@ def get_environ_info():
return
info
def
parse_param_file
(
param_file
,
return_shape
=
True
):
from
paddle.fluid.proto.framework_pb2
import
VarType
f
=
open
(
param_file
,
'rb'
)
version
=
np
.
fromstring
(
f
.
read
(
4
),
dtype
=
'int32'
)
lod_level
=
np
.
fromstring
(
f
.
read
(
8
),
dtype
=
'int64'
)
for
i
in
range
(
int
(
lod_level
)):
_size
=
np
.
fromstring
(
f
.
read
(
8
),
dtype
=
'int64'
)
_
=
f
.
read
(
_size
)
version
=
np
.
fromstring
(
f
.
read
(
4
),
dtype
=
'int32'
)
tensor_desc
=
VarType
.
TensorDesc
()
tensor_desc_size
=
np
.
fromstring
(
f
.
read
(
4
),
dtype
=
'int32'
)
tensor_desc
.
ParseFromString
(
f
.
read
(
int
(
tensor_desc_size
)))
tensor_shape
=
tuple
(
tensor_desc
.
dims
)
if
return_shape
:
f
.
close
()
return
tuple
(
tensor_desc
.
dims
)
if
tensor_desc
.
data_type
!=
5
:
raise
Exception
(
"Unexpected data type while parse {}"
.
format
(
param_file
))
data_size
=
4
for
i
in
range
(
len
(
tensor_shape
)):
data_size
*=
tensor_shape
[
i
]
weight
=
np
.
fromstring
(
f
.
read
(
data_size
),
dtype
=
'float32'
)
f
.
close
()
return
np
.
reshape
(
weight
,
tensor_shape
)
def
fuse_bn_weights
(
exe
,
main_prog
,
weights_dir
):
import
paddle.fluid
as
fluid
logging
.
info
(
"Try to fuse weights of batch_norm..."
)
bn_vars
=
list
()
for
block
in
main_prog
.
blocks
:
ops
=
list
(
block
.
ops
)
for
op
in
ops
:
if
op
.
type
==
'affine_channel'
:
scale_name
=
op
.
input
(
'Scale'
)[
0
]
bias_name
=
op
.
input
(
'Bias'
)[
0
]
prefix
=
scale_name
[:
-
5
]
mean_name
=
prefix
+
'mean'
variance_name
=
prefix
+
'variance'
if
not
osp
.
exists
(
osp
.
join
(
weights_dir
,
mean_name
))
or
not
osp
.
exists
(
osp
.
join
(
weights_dir
,
variance_name
)):
logging
.
info
(
"There's no batch_norm weight found to fuse, skip fuse_bn."
)
return
bias
=
block
.
var
(
bias_name
)
pretrained_shape
=
parse_param_file
(
osp
.
join
(
weights_dir
,
bias_name
))
actual_shape
=
tuple
(
bias
.
shape
)
if
pretrained_shape
!=
actual_shape
:
continue
bn_vars
.
append
(
[
scale_name
,
bias_name
,
mean_name
,
variance_name
])
eps
=
1e-5
for
names
in
bn_vars
:
scale_name
,
bias_name
,
mean_name
,
variance_name
=
names
scale
=
parse_param_file
(
osp
.
join
(
weights_dir
,
scale_name
),
return_shape
=
False
)
bias
=
parse_param_file
(
osp
.
join
(
weights_dir
,
bias_name
),
return_shape
=
False
)
mean
=
parse_param_file
(
osp
.
join
(
weights_dir
,
mean_name
),
return_shape
=
False
)
variance
=
parse_param_file
(
osp
.
join
(
weights_dir
,
variance_name
),
return_shape
=
False
)
bn_std
=
np
.
sqrt
(
np
.
add
(
variance
,
eps
))
new_scale
=
np
.
float32
(
np
.
divide
(
scale
,
bn_std
))
new_bias
=
bias
-
mean
*
new_scale
scale_tensor
=
fluid
.
global_scope
().
find_var
(
scale_name
).
get_tensor
()
bias_tensor
=
fluid
.
global_scope
().
find_var
(
bias_name
).
get_tensor
()
scale_tensor
.
set
(
new_scale
,
exe
.
place
)
bias_tensor
.
set
(
new_bias
,
exe
.
place
)
if
len
(
bn_vars
)
==
0
:
logging
.
info
(
"There's no batch_norm weight found to fuse, skip fuse_bn."
)
else
:
logging
.
info
(
"There's {} batch_norm ops been fused."
.
format
(
len
(
bn_vars
)))
def
load_pdparams
(
exe
,
main_prog
,
model_dir
):
import
paddle.fluid
as
fluid
from
paddle.fluid.proto.framework_pb2
import
VarType
from
paddle.fluid.framework
import
Program
vars_to_load
=
list
()
import
pickle
with
open
(
osp
.
join
(
model_dir
,
'model.pdparams'
),
'rb'
)
as
f
:
params_dict
=
pickle
.
load
(
f
)
if
six
.
PY2
else
pickle
.
load
(
f
,
encoding
=
'latin1'
)
unused_vars
=
list
()
for
var
in
main_prog
.
list_vars
():
if
not
isinstance
(
var
,
fluid
.
framework
.
Parameter
):
continue
if
var
.
name
not
in
params_dict
:
raise
Exception
(
"{} is not in saved model"
.
format
(
var
.
name
))
if
var
.
shape
!=
params_dict
[
var
.
name
].
shape
:
unused_vars
.
append
(
var
.
name
)
logging
.
warning
(
"[SKIP] Shape of pretrained weight {} doesn't match.(Pretrained: {}, Actual: {})"
.
format
(
var
.
name
,
params_dict
[
var
.
name
].
shape
,
var
.
shape
))
continue
vars_to_load
.
append
(
var
)
logging
.
debug
(
"Weight {} will be load"
.
format
(
var
.
name
))
for
var_name
in
unused_vars
:
del
params_dict
[
var_name
]
fluid
.
io
.
set_program_state
(
main_prog
,
params_dict
)
if
len
(
vars_to_load
)
==
0
:
logging
.
warning
(
"There is no pretrain weights loaded, maybe you should check you pretrain model!"
)
else
:
logging
.
info
(
"There are {} varaibles in {} are loaded."
.
format
(
len
(
vars_to_load
),
model_dir
))
def
load_pretrained_weights
(
exe
,
main_prog
,
weights_dir
,
fuse_bn
=
False
):
if
not
osp
.
exists
(
weights_dir
):
raise
Exception
(
"Path {} not exists."
.
format
(
weights_dir
))
if
osp
.
exists
(
osp
.
join
(
weights_dir
,
"model.pdparams"
)):
return
load_pdparams
(
exe
,
main_prog
,
weights_dir
)
import
paddle.fluid
as
fluid
vars_to_load
=
list
()
for
var
in
main_prog
.
list_vars
():
if
not
isinstance
(
var
,
fluid
.
framework
.
Parameter
):
continue
if
not
osp
.
exists
(
osp
.
join
(
weights_dir
,
var
.
name
)):
logging
.
debug
(
"[SKIP] Pretrained weight {}/{} doesn't exist"
.
format
(
weights_dir
,
var
.
name
))
continue
pretrained_shape
=
parse_param_file
(
osp
.
join
(
weights_dir
,
var
.
name
))
actual_shape
=
tuple
(
var
.
shape
)
if
pretrained_shape
!=
actual_shape
:
logging
.
warning
(
"[SKIP] Shape of pretrained weight {}/{} doesn't match.(Pretrained: {}, Actual: {})"
.
format
(
weights_dir
,
var
.
name
,
pretrained_shape
,
actual_shape
))
continue
vars_to_load
.
append
(
var
)
logging
.
debug
(
"Weight {} will be load"
.
format
(
var
.
name
))
params_dict
=
fluid
.
io
.
load_program_state
(
weights_dir
,
var_list
=
vars_to_load
)
fluid
.
io
.
set_program_state
(
main_prog
,
params_dict
)
if
len
(
vars_to_load
)
==
0
:
logging
.
warning
(
"There is no pretrain weights loaded, maybe you should check you pretrain model!"
)
else
:
logging
.
info
(
"There are {} varaibles in {} are loaded."
.
format
(
len
(
vars_to_load
),
weights_dir
))
if
fuse_bn
:
fuse_bn_weights
(
exe
,
main_prog
,
weights_dir
)
def
visualize
(
image
,
result
,
save_dir
=
None
,
weight
=
0.6
):
"""
Convert segment result to color image, and save added image.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录