Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
3f658a36
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看板
未验证
提交
3f658a36
编写于
9月 22, 2020
作者:
M
michaelowenliu
提交者:
GitHub
9月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #396 from michaelowenliu/develop
redesign deeplab model
上级
23d69271
7dac6524
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
412 addition
and
338 deletion
+412
-338
dygraph/paddleseg/core/seg_train.py
dygraph/paddleseg/core/seg_train.py
+8
-5
dygraph/paddleseg/core/val.py
dygraph/paddleseg/core/val.py
+1
-1
dygraph/paddleseg/cvlibs/callbacks.py
dygraph/paddleseg/cvlibs/callbacks.py
+29
-29
dygraph/paddleseg/models/ann.py
dygraph/paddleseg/models/ann.py
+15
-17
dygraph/paddleseg/models/backbones/resnet_vd.py
dygraph/paddleseg/models/backbones/resnet_vd.py
+14
-6
dygraph/paddleseg/models/common/pyramid_pool.py
dygraph/paddleseg/models/common/pyramid_pool.py
+23
-25
dygraph/paddleseg/models/deeplab.py
dygraph/paddleseg/models/deeplab.py
+117
-64
dygraph/paddleseg/models/fast_scnn.py
dygraph/paddleseg/models/fast_scnn.py
+12
-12
dygraph/paddleseg/models/gcnet.py
dygraph/paddleseg/models/gcnet.py
+9
-9
dygraph/paddleseg/models/ocrnet.py
dygraph/paddleseg/models/ocrnet.py
+1
-4
dygraph/paddleseg/models/pspnet.py
dygraph/paddleseg/models/pspnet.py
+5
-4
dygraph/paddleseg/utils/metrics.py
dygraph/paddleseg/utils/metrics.py
+1
-1
dygraph/paddleseg/utils/progbar.py
dygraph/paddleseg/utils/progbar.py
+163
-160
dygraph/paddleseg/utils/utils.py
dygraph/paddleseg/utils/utils.py
+14
-1
未找到文件。
dygraph/paddleseg/core/seg_train.py
浏览文件 @
3f658a36
...
@@ -87,7 +87,8 @@ def seg_train(model,
...
@@ -87,7 +87,8 @@ def seg_train(model,
out_labels
=
[
"loss"
,
"reader_cost"
,
"batch_cost"
]
out_labels
=
[
"loss"
,
"reader_cost"
,
"batch_cost"
]
base_logger
=
callbacks
.
BaseLogger
(
period
=
log_iters
)
base_logger
=
callbacks
.
BaseLogger
(
period
=
log_iters
)
train_logger
=
callbacks
.
TrainLogger
(
log_freq
=
log_iters
)
train_logger
=
callbacks
.
TrainLogger
(
log_freq
=
log_iters
)
model_ckpt
=
callbacks
.
ModelCheckpoint
(
save_dir
,
save_params_only
=
False
,
period
=
save_interval_iters
)
model_ckpt
=
callbacks
.
ModelCheckpoint
(
save_dir
,
save_params_only
=
False
,
period
=
save_interval_iters
)
vdl
=
callbacks
.
VisualDL
(
log_dir
=
os
.
path
.
join
(
save_dir
,
"log"
))
vdl
=
callbacks
.
VisualDL
(
log_dir
=
os
.
path
.
join
(
save_dir
,
"log"
))
cbks_list
=
[
base_logger
,
train_logger
,
model_ckpt
,
vdl
]
cbks_list
=
[
base_logger
,
train_logger
,
model_ckpt
,
vdl
]
...
@@ -120,7 +121,7 @@ def seg_train(model,
...
@@ -120,7 +121,7 @@ def seg_train(model,
iter
+=
1
iter
+=
1
if
iter
>
iters
:
if
iter
>
iters
:
break
break
logs
[
"reader_cost"
]
=
timer
.
elapsed_time
()
logs
[
"reader_cost"
]
=
timer
.
elapsed_time
()
############## 2 ################
############## 2 ################
cbks
.
on_iter_begin
(
iter
,
logs
)
cbks
.
on_iter_begin
(
iter
,
logs
)
...
@@ -136,7 +137,7 @@ def seg_train(model,
...
@@ -136,7 +137,7 @@ def seg_train(model,
loss
=
ddp_model
.
scale_loss
(
loss
)
loss
=
ddp_model
.
scale_loss
(
loss
)
loss
.
backward
()
loss
.
backward
()
ddp_model
.
apply_collective_grads
()
ddp_model
.
apply_collective_grads
()
else
:
else
:
logits
=
model
(
images
)
logits
=
model
(
images
)
loss
=
loss_computation
(
logits
,
labels
,
losses
)
loss
=
loss_computation
(
logits
,
labels
,
losses
)
...
@@ -148,7 +149,7 @@ def seg_train(model,
...
@@ -148,7 +149,7 @@ def seg_train(model,
model
.
clear_gradients
()
model
.
clear_gradients
()
logs
[
'loss'
]
=
loss
.
numpy
()[
0
]
logs
[
'loss'
]
=
loss
.
numpy
()[
0
]
logs
[
"batch_cost"
]
=
timer
.
elapsed_time
()
logs
[
"batch_cost"
]
=
timer
.
elapsed_time
()
############## 3 ################
############## 3 ################
...
@@ -159,4 +160,6 @@ def seg_train(model,
...
@@ -159,4 +160,6 @@ def seg_train(model,
############### 4 ###############
############### 4 ###############
cbks
.
on_train_end
(
logs
)
cbks
.
on_train_end
(
logs
)
#################################
\ No newline at end of file
#################################
dygraph/paddleseg/core/val.py
浏览文件 @
3f658a36
...
@@ -67,7 +67,7 @@ def evaluate(model,
...
@@ -67,7 +67,7 @@ def evaluate(model,
pred
=
pred
[
np
.
newaxis
,
:,
:,
np
.
newaxis
]
pred
=
pred
[
np
.
newaxis
,
:,
:,
np
.
newaxis
]
pred
=
pred
.
astype
(
'int64'
)
pred
=
pred
.
astype
(
'int64'
)
mask
=
label
!=
ignore_index
mask
=
label
!=
ignore_index
# To-DO Test Execution Time
conf_mat
.
calculate
(
pred
=
pred
,
label
=
label
,
ignore
=
mask
)
conf_mat
.
calculate
(
pred
=
pred
,
label
=
label
,
ignore
=
mask
)
_
,
iou
=
conf_mat
.
mean_iou
()
_
,
iou
=
conf_mat
.
mean_iou
()
...
...
dygraph/paddleseg/cvlibs/callbacks.py
浏览文件 @
3f658a36
...
@@ -13,7 +13,6 @@
...
@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
os
import
os
import
time
import
time
...
@@ -24,6 +23,7 @@ from visualdl import LogWriter
...
@@ -24,6 +23,7 @@ from visualdl import LogWriter
from
paddleseg.utils.progbar
import
Progbar
from
paddleseg.utils.progbar
import
Progbar
import
paddleseg.utils.logger
as
logger
import
paddleseg.utils.logger
as
logger
class
CallbackList
(
object
):
class
CallbackList
(
object
):
"""Container abstracting a list of callbacks.
"""Container abstracting a list of callbacks.
# Arguments
# Arguments
...
@@ -44,7 +44,7 @@ class CallbackList(object):
...
@@ -44,7 +44,7 @@ class CallbackList(object):
def
set_model
(
self
,
model
):
def
set_model
(
self
,
model
):
for
callback
in
self
.
callbacks
:
for
callback
in
self
.
callbacks
:
callback
.
set_model
(
model
)
callback
.
set_model
(
model
)
def
set_optimizer
(
self
,
optimizer
):
def
set_optimizer
(
self
,
optimizer
):
for
callback
in
self
.
callbacks
:
for
callback
in
self
.
callbacks
:
callback
.
set_optimizer
(
optimizer
)
callback
.
set_optimizer
(
optimizer
)
...
@@ -82,6 +82,7 @@ class CallbackList(object):
...
@@ -82,6 +82,7 @@ class CallbackList(object):
def
__iter__
(
self
):
def
__iter__
(
self
):
return
iter
(
self
.
callbacks
)
return
iter
(
self
.
callbacks
)
class
Callback
(
object
):
class
Callback
(
object
):
"""Abstract base class used to build new callbacks.
"""Abstract base class used to build new callbacks.
"""
"""
...
@@ -94,7 +95,7 @@ class Callback(object):
...
@@ -94,7 +95,7 @@ class Callback(object):
def
set_model
(
self
,
model
):
def
set_model
(
self
,
model
):
self
.
model
=
model
self
.
model
=
model
def
set_optimizer
(
self
,
optimizer
):
def
set_optimizer
(
self
,
optimizer
):
self
.
optimizer
=
optimizer
self
.
optimizer
=
optimizer
...
@@ -110,18 +111,18 @@ class Callback(object):
...
@@ -110,18 +111,18 @@ class Callback(object):
def
on_train_end
(
self
,
logs
=
None
):
def
on_train_end
(
self
,
logs
=
None
):
pass
pass
class
BaseLogger
(
Callback
):
class
BaseLogger
(
Callback
):
def
__init__
(
self
,
period
=
10
):
def
__init__
(
self
,
period
=
10
):
super
(
BaseLogger
,
self
).
__init__
()
super
(
BaseLogger
,
self
).
__init__
()
self
.
period
=
period
self
.
period
=
period
def
_reset
(
self
):
def
_reset
(
self
):
self
.
totals
=
{}
self
.
totals
=
{}
def
on_train_begin
(
self
,
logs
=
None
):
def
on_train_begin
(
self
,
logs
=
None
):
self
.
totals
=
{}
self
.
totals
=
{}
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
logs
=
logs
or
{}
logs
=
logs
or
{}
#(iter - 1) // iters_per_epoch + 1
#(iter - 1) // iters_per_epoch + 1
...
@@ -132,13 +133,13 @@ class BaseLogger(Callback):
...
@@ -132,13 +133,13 @@ class BaseLogger(Callback):
self
.
totals
[
k
]
=
v
self
.
totals
[
k
]
=
v
if
iter
%
self
.
period
==
0
and
ParallelEnv
().
local_rank
==
0
:
if
iter
%
self
.
period
==
0
and
ParallelEnv
().
local_rank
==
0
:
for
k
in
self
.
totals
:
for
k
in
self
.
totals
:
logs
[
k
]
=
self
.
totals
[
k
]
/
self
.
period
logs
[
k
]
=
self
.
totals
[
k
]
/
self
.
period
self
.
_reset
()
self
.
_reset
()
class
TrainLogger
(
Callback
):
class
TrainLogger
(
Callback
):
def
__init__
(
self
,
log_freq
=
10
):
def
__init__
(
self
,
log_freq
=
10
):
self
.
log_freq
=
log_freq
self
.
log_freq
=
log_freq
...
@@ -154,7 +155,7 @@ class TrainLogger(Callback):
...
@@ -154,7 +155,7 @@ class TrainLogger(Callback):
return
result
.
format
(
*
arr
)
return
result
.
format
(
*
arr
)
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
if
iter
%
self
.
log_freq
==
0
and
ParallelEnv
().
local_rank
==
0
:
if
iter
%
self
.
log_freq
==
0
and
ParallelEnv
().
local_rank
==
0
:
total_iters
=
self
.
params
[
"total_iters"
]
total_iters
=
self
.
params
[
"total_iters"
]
iters_per_epoch
=
self
.
params
[
"iters_per_epoch"
]
iters_per_epoch
=
self
.
params
[
"iters_per_epoch"
]
...
@@ -167,49 +168,50 @@ class TrainLogger(Callback):
...
@@ -167,49 +168,50 @@ class TrainLogger(Callback):
reader_cost
=
logs
[
"reader_cost"
]
reader_cost
=
logs
[
"reader_cost"
]
logger
.
info
(
logger
.
info
(
"[TRAIN] epoch={}, iter={}/{}, loss={:.4f}, lr={:.6f}, batch_cost={:.4f}, reader_cost={:.4f} | ETA {}"
.
"[TRAIN] epoch={}, iter={}/{}, loss={:.4f}, lr={:.6f}, batch_cost={:.4f}, reader_cost={:.4f} | ETA {}"
format
(
current_epoch
,
iter
,
total_iters
,
.
format
(
current_epoch
,
iter
,
total_iters
,
loss
,
lr
,
batch_cost
,
loss
,
lr
,
batch_cost
,
reader_cost
,
eta
))
reader_cost
,
eta
))
class
ProgbarLogger
(
Callback
):
class
ProgbarLogger
(
Callback
):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
ProgbarLogger
,
self
).
__init__
()
super
(
ProgbarLogger
,
self
).
__init__
()
def
on_train_begin
(
self
,
logs
=
None
):
def
on_train_begin
(
self
,
logs
=
None
):
self
.
verbose
=
self
.
params
[
"verbose"
]
self
.
verbose
=
self
.
params
[
"verbose"
]
self
.
total_iters
=
self
.
params
[
"total_iters"
]
self
.
total_iters
=
self
.
params
[
"total_iters"
]
self
.
target
=
self
.
params
[
"total_iters"
]
self
.
target
=
self
.
params
[
"total_iters"
]
self
.
progbar
=
Progbar
(
target
=
self
.
target
,
verbose
=
self
.
verbose
)
self
.
progbar
=
Progbar
(
target
=
self
.
target
,
verbose
=
self
.
verbose
)
self
.
seen
=
0
self
.
seen
=
0
self
.
log_values
=
[]
self
.
log_values
=
[]
def
on_iter_begin
(
self
,
iter
,
logs
=
None
):
def
on_iter_begin
(
self
,
iter
,
logs
=
None
):
#self.seen = 0
#self.seen = 0
if
self
.
seen
<
self
.
target
:
if
self
.
seen
<
self
.
target
:
self
.
log_values
=
[]
self
.
log_values
=
[]
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
def
on_iter_end
(
self
,
iter
,
logs
=
None
):
logs
=
logs
or
{}
logs
=
logs
or
{}
self
.
seen
+=
1
self
.
seen
+=
1
for
k
in
self
.
params
[
'metrics'
]:
for
k
in
self
.
params
[
'metrics'
]:
if
k
in
logs
:
if
k
in
logs
:
self
.
log_values
.
append
((
k
,
logs
[
k
]))
self
.
log_values
.
append
((
k
,
logs
[
k
]))
#if self.verbose and self.seen < self.target and ParallelEnv.local_rank == 0:
#if self.verbose and self.seen < self.target and ParallelEnv.local_rank == 0:
#print(self.log_values)
#print(self.log_values)
if
self
.
seen
<
self
.
target
:
if
self
.
seen
<
self
.
target
:
self
.
progbar
.
update
(
self
.
seen
,
self
.
log_values
)
self
.
progbar
.
update
(
self
.
seen
,
self
.
log_values
)
class
ModelCheckpoint
(
Callback
):
class
ModelCheckpoint
(
Callback
):
def
__init__
(
self
,
save_dir
,
monitor
=
"miou"
,
save_best_only
=
False
,
save_params_only
=
True
,
mode
=
"max"
,
period
=
1
):
def
__init__
(
self
,
save_dir
,
monitor
=
"miou"
,
save_best_only
=
False
,
save_params_only
=
True
,
mode
=
"max"
,
period
=
1
):
super
(
ModelCheckpoint
,
self
).
__init__
()
super
(
ModelCheckpoint
,
self
).
__init__
()
self
.
monitor
=
monitor
self
.
monitor
=
monitor
self
.
save_dir
=
save_dir
self
.
save_dir
=
save_dir
...
@@ -241,7 +243,7 @@ class ModelCheckpoint(Callback):
...
@@ -241,7 +243,7 @@ class ModelCheckpoint(Callback):
current_save_dir
=
os
.
path
.
join
(
self
.
save_dir
,
"iter_{}"
.
format
(
iter
))
current_save_dir
=
os
.
path
.
join
(
self
.
save_dir
,
"iter_{}"
.
format
(
iter
))
current_save_dir
=
os
.
path
.
abspath
(
current_save_dir
)
current_save_dir
=
os
.
path
.
abspath
(
current_save_dir
)
#if self.iters_since_last_save % self.period and ParallelEnv().local_rank == 0:
#if self.iters_since_last_save % self.period and ParallelEnv().local_rank == 0:
#self.iters_since_last_save = 0
#self.iters_since_last_save = 0
if
iter
%
self
.
period
==
0
and
ParallelEnv
().
local_rank
==
0
:
if
iter
%
self
.
period
==
0
and
ParallelEnv
().
local_rank
==
0
:
if
self
.
verbose
>
0
:
if
self
.
verbose
>
0
:
print
(
"iter {iter_num}: saving model to {path}"
.
format
(
print
(
"iter {iter_num}: saving model to {path}"
.
format
(
...
@@ -252,11 +254,9 @@ class ModelCheckpoint(Callback):
...
@@ -252,11 +254,9 @@ class ModelCheckpoint(Callback):
if
not
self
.
save_params_only
:
if
not
self
.
save_params_only
:
paddle
.
save
(
self
.
optimizer
.
state_dict
(),
filepath
)
paddle
.
save
(
self
.
optimizer
.
state_dict
(),
filepath
)
class
VisualDL
(
Callback
):
class
VisualDL
(
Callback
):
def
__init__
(
self
,
log_dir
=
"./log"
,
freq
=
1
):
def
__init__
(
self
,
log_dir
=
"./log"
,
freq
=
1
):
super
(
VisualDL
,
self
).
__init__
()
super
(
VisualDL
,
self
).
__init__
()
self
.
log_dir
=
log_dir
self
.
log_dir
=
log_dir
...
@@ -274,4 +274,4 @@ class VisualDL(Callback):
...
@@ -274,4 +274,4 @@ class VisualDL(Callback):
self
.
writer
.
flush
()
self
.
writer
.
flush
()
def
on_train_end
(
self
,
logs
=
None
):
def
on_train_end
(
self
,
logs
=
None
):
self
.
writer
.
close
()
self
.
writer
.
close
()
\ No newline at end of file
dygraph/paddleseg/models/ann.py
浏览文件 @
3f658a36
...
@@ -28,7 +28,7 @@ class ANN(nn.Layer):
...
@@ -28,7 +28,7 @@ class ANN(nn.Layer):
"""
"""
The ANN implementation based on PaddlePaddle.
The ANN implementation based on PaddlePaddle.
The or
ginal arti
le refers to
The or
iginal artic
le refers to
Zhen, Zhu, et al. "Asymmetric Non-local Neural Networks for Semantic Segmentation."
Zhen, Zhu, et al. "Asymmetric Non-local Neural Networks for Semantic Segmentation."
(https://arxiv.org/pdf/1908.07678.pdf)
(https://arxiv.org/pdf/1908.07678.pdf)
...
@@ -37,8 +37,8 @@ class ANN(nn.Layer):
...
@@ -37,8 +37,8 @@ class ANN(nn.Layer):
Args:
Args:
num_classes (int): the unique number of target classes.
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaul
l
t to None.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
backbone_indices (tuple): two values in the tuple indic
a
te the indices of output of backbone.
the first index will be taken as low-level features; the second one will be
the first index will be taken as low-level features; the second one will be
taken as high-level features in AFNB module. Usually backbone consists of four
taken as high-level features in AFNB module. Usually backbone consists of four
downsampling stage, and return an output of each stage, so we set default (2, 3),
downsampling stage, and return an output of each stage, so we set default (2, 3),
...
@@ -48,7 +48,7 @@ class ANN(nn.Layer):
...
@@ -48,7 +48,7 @@ class ANN(nn.Layer):
Default to 256.
Default to 256.
inter_channels (int): both input and output channels of APNB modules.
inter_channels (int): both input and output channels of APNB modules.
psp_size (tuple): the out size of pooled feature maps. Default to (1, 3, 6, 8).
psp_size (tuple): the out size of pooled feature maps. Default to (1, 3, 6, 8).
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
enable_auxiliary_loss (bool): a bool values indic
a
tes whether adding auxiliary loss. Default to True.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -79,7 +79,7 @@ class ANN(nn.Layer):
...
@@ -79,7 +79,7 @@ class ANN(nn.Layer):
psp_size
=
psp_size
)
psp_size
=
psp_size
)
self
.
context
=
nn
.
Sequential
(
self
.
context
=
nn
.
Sequential
(
layer_libs
.
ConvB
nRelu
(
layer_libs
.
ConvB
NReLU
(
in_channels
=
high_in_channels
,
in_channels
=
high_in_channels
,
out_channels
=
inter_channels
,
out_channels
=
inter_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -94,9 +94,7 @@ class ANN(nn.Layer):
...
@@ -94,9 +94,7 @@ class ANN(nn.Layer):
psp_size
=
psp_size
))
psp_size
=
psp_size
))
self
.
cls
=
nn
.
Conv2d
(
self
.
cls
=
nn
.
Conv2d
(
in_channels
=
inter_channels
,
in_channels
=
inter_channels
,
out_channels
=
num_classes
,
kernel_size
=
1
)
out_channels
=
num_classes
,
kernel_size
=
1
)
self
.
auxlayer
=
layer_libs
.
AuxLayer
(
self
.
auxlayer
=
layer_libs
.
AuxLayer
(
in_channels
=
low_in_channels
,
in_channels
=
low_in_channels
,
inter_channels
=
low_in_channels
//
2
,
inter_channels
=
low_in_channels
//
2
,
...
@@ -122,7 +120,8 @@ class ANN(nn.Layer):
...
@@ -122,7 +120,8 @@ class ANN(nn.Layer):
if
self
.
enable_auxiliary_loss
:
if
self
.
enable_auxiliary_loss
:
auxiliary_logit
=
self
.
auxlayer
(
low_level_x
)
auxiliary_logit
=
self
.
auxlayer
(
low_level_x
)
auxiliary_logit
=
F
.
resize_bilinear
(
auxiliary_logit
,
input
.
shape
[
2
:])
auxiliary_logit
=
F
.
resize_bilinear
(
auxiliary_logit
,
input
.
shape
[
2
:])
logit_list
.
append
(
auxiliary_logit
)
logit_list
.
append
(
auxiliary_logit
)
return
logit_list
return
logit_list
...
@@ -219,7 +218,7 @@ class APNB(nn.Layer):
...
@@ -219,7 +218,7 @@ class APNB(nn.Layer):
SelfAttentionBlock_APNB
(
in_channels
,
out_channels
,
key_channels
,
SelfAttentionBlock_APNB
(
in_channels
,
out_channels
,
key_channels
,
value_channels
,
size
)
for
size
in
sizes
value_channels
,
size
)
for
size
in
sizes
])
])
self
.
conv_bn
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
*
2
,
in_channels
=
in_channels
*
2
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
1
)
kernel_size
=
1
)
...
@@ -280,11 +279,11 @@ class SelfAttentionBlock_AFNB(nn.Layer):
...
@@ -280,11 +279,11 @@ class SelfAttentionBlock_AFNB(nn.Layer):
if
out_channels
==
None
:
if
out_channels
==
None
:
self
.
out_channels
=
high_in_channels
self
.
out_channels
=
high_in_channels
self
.
pool
=
nn
.
Pool2D
(
pool_size
=
(
scale
,
scale
),
pool_type
=
"max"
)
self
.
pool
=
nn
.
Pool2D
(
pool_size
=
(
scale
,
scale
),
pool_type
=
"max"
)
self
.
f_key
=
layer_libs
.
ConvB
nRelu
(
self
.
f_key
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
low_in_channels
,
in_channels
=
low_in_channels
,
out_channels
=
key_channels
,
out_channels
=
key_channels
,
kernel_size
=
1
)
kernel_size
=
1
)
self
.
f_query
=
layer_libs
.
ConvB
nRelu
(
self
.
f_query
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
high_in_channels
,
in_channels
=
high_in_channels
,
out_channels
=
key_channels
,
out_channels
=
key_channels
,
kernel_size
=
1
)
kernel_size
=
1
)
...
@@ -315,7 +314,7 @@ class SelfAttentionBlock_AFNB(nn.Layer):
...
@@ -315,7 +314,7 @@ class SelfAttentionBlock_AFNB(nn.Layer):
key
=
_pp_module
(
key
,
self
.
psp_size
)
key
=
_pp_module
(
key
,
self
.
psp_size
)
sim_map
=
paddle
.
matmul
(
query
,
key
)
sim_map
=
paddle
.
matmul
(
query
,
key
)
sim_map
=
(
self
.
key_channels
**
-
.
5
)
*
sim_map
sim_map
=
(
self
.
key_channels
**
-
.
5
)
*
sim_map
sim_map
=
F
.
softmax
(
sim_map
,
axis
=-
1
)
sim_map
=
F
.
softmax
(
sim_map
,
axis
=-
1
)
context
=
paddle
.
matmul
(
sim_map
,
value
)
context
=
paddle
.
matmul
(
sim_map
,
value
)
...
@@ -358,7 +357,7 @@ class SelfAttentionBlock_APNB(nn.Layer):
...
@@ -358,7 +357,7 @@ class SelfAttentionBlock_APNB(nn.Layer):
self
.
value_channels
=
value_channels
self
.
value_channels
=
value_channels
self
.
pool
=
nn
.
Pool2D
(
pool_size
=
(
scale
,
scale
),
pool_type
=
"max"
)
self
.
pool
=
nn
.
Pool2D
(
pool_size
=
(
scale
,
scale
),
pool_type
=
"max"
)
self
.
f_key
=
layer_libs
.
ConvB
nRelu
(
self
.
f_key
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
self
.
in_channels
,
in_channels
=
self
.
in_channels
,
out_channels
=
self
.
key_channels
,
out_channels
=
self
.
key_channels
,
kernel_size
=
1
)
kernel_size
=
1
)
...
@@ -384,15 +383,14 @@ class SelfAttentionBlock_APNB(nn.Layer):
...
@@ -384,15 +383,14 @@ class SelfAttentionBlock_APNB(nn.Layer):
value
=
paddle
.
transpose
(
value
,
perm
=
(
0
,
2
,
1
))
value
=
paddle
.
transpose
(
value
,
perm
=
(
0
,
2
,
1
))
query
=
self
.
f_query
(
x
)
query
=
self
.
f_query
(
x
)
query
=
paddle
.
reshape
(
query
=
paddle
.
reshape
(
query
,
shape
=
(
batch_size
,
self
.
key_channels
,
-
1
))
query
,
shape
=
(
batch_size
,
self
.
key_channels
,
-
1
))
query
=
paddle
.
transpose
(
query
,
perm
=
(
0
,
2
,
1
))
query
=
paddle
.
transpose
(
query
,
perm
=
(
0
,
2
,
1
))
key
=
self
.
f_key
(
x
)
key
=
self
.
f_key
(
x
)
key
=
_pp_module
(
key
,
self
.
psp_size
)
key
=
_pp_module
(
key
,
self
.
psp_size
)
sim_map
=
paddle
.
matmul
(
query
,
key
)
sim_map
=
paddle
.
matmul
(
query
,
key
)
sim_map
=
(
self
.
key_channels
**
-
.
5
)
*
sim_map
sim_map
=
(
self
.
key_channels
**
-
.
5
)
*
sim_map
sim_map
=
F
.
softmax
(
sim_map
,
axis
=-
1
)
sim_map
=
F
.
softmax
(
sim_map
,
axis
=-
1
)
context
=
paddle
.
matmul
(
sim_map
,
value
)
context
=
paddle
.
matmul
(
sim_map
,
value
)
...
...
dygraph/paddleseg/models/backbones/resnet_vd.py
浏览文件 @
3f658a36
...
@@ -133,8 +133,9 @@ class BottleneckBlock(nn.Layer):
...
@@ -133,8 +133,9 @@ class BottleneckBlock(nn.Layer):
# If given dilation rate > 1, using corresponding padding
# If given dilation rate > 1, using corresponding padding
if
self
.
dilation
>
1
:
if
self
.
dilation
>
1
:
padding
=
self
.
dilation
padding
=
self
.
dilation
y
=
F
.
pad
(
y
,
[
0
,
0
,
0
,
0
,
padding
,
padding
,
padding
,
padding
])
y
=
F
.
pad
(
y
,
[
padding
,
padding
,
padding
,
padding
])
#####################################################################
#####################################################################
conv1
=
self
.
conv1
(
y
)
conv1
=
self
.
conv1
(
y
)
conv2
=
self
.
conv2
(
conv1
)
conv2
=
self
.
conv2
(
conv1
)
...
@@ -197,11 +198,10 @@ class BasicBlock(nn.Layer):
...
@@ -197,11 +198,10 @@ class BasicBlock(nn.Layer):
class
ResNet_vd
(
nn
.
Layer
):
class
ResNet_vd
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
backbone_pretrained
=
None
,
layers
=
50
,
layers
=
50
,
class_dim
=
1000
,
output_stride
=
None
,
output_stride
=
None
,
multi_grid
=
(
1
,
1
,
1
)):
multi_grid
=
(
1
,
1
,
1
),
pretrained
=
None
):
super
(
ResNet_vd
,
self
).
__init__
()
super
(
ResNet_vd
,
self
).
__init__
()
self
.
layers
=
layers
self
.
layers
=
layers
...
@@ -224,6 +224,10 @@ class ResNet_vd(nn.Layer):
...
@@ -224,6 +224,10 @@ class ResNet_vd(nn.Layer):
]
if
layers
>=
50
else
[
64
,
64
,
128
,
256
]
]
if
layers
>=
50
else
[
64
,
64
,
128
,
256
]
num_filters
=
[
64
,
128
,
256
,
512
]
num_filters
=
[
64
,
128
,
256
,
512
]
# for channels of returned stage
self
.
backbone_channels
=
[
c
*
4
for
c
in
num_filters
]
if
layers
>=
50
else
num_filters
dilation_dict
=
None
dilation_dict
=
None
if
output_stride
==
8
:
if
output_stride
==
8
:
dilation_dict
=
{
2
:
2
,
3
:
4
}
dilation_dict
=
{
2
:
2
,
3
:
4
}
...
@@ -315,6 +319,8 @@ class ResNet_vd(nn.Layer):
...
@@ -315,6 +319,8 @@ class ResNet_vd(nn.Layer):
block_list
.
append
(
basic_block
)
block_list
.
append
(
basic_block
)
shortcut
=
True
shortcut
=
True
self
.
stage_list
.
append
(
block_list
)
self
.
stage_list
.
append
(
block_list
)
utils
.
load_pretrained_model
(
self
,
pretrained
)
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
y
=
self
.
conv1_1
(
inputs
)
y
=
self
.
conv1_1
(
inputs
)
...
@@ -324,12 +330,14 @@ class ResNet_vd(nn.Layer):
...
@@ -324,12 +330,14 @@ class ResNet_vd(nn.Layer):
# A feature list saves the output feature map of each stage.
# A feature list saves the output feature map of each stage.
feat_list
=
[]
feat_list
=
[]
for
i
,
stage
in
enumerate
(
self
.
stage_list
)
:
for
stage
in
self
.
stage_list
:
for
j
,
block
in
enumerate
(
stage
)
:
for
block
in
stage
:
y
=
block
(
y
)
y
=
block
(
y
)
feat_list
.
append
(
y
)
feat_list
.
append
(
y
)
return
feat_list
return
feat_list
@
manager
.
BACKBONES
.
add_component
@
manager
.
BACKBONES
.
add_component
...
...
dygraph/paddleseg/models/common/pyramid_pool.py
浏览文件 @
3f658a36
...
@@ -13,7 +13,6 @@
...
@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
paddle
import
paddle
from
paddle
import
nn
from
paddle
import
nn
import
paddle.nn.functional
as
F
import
paddle.nn.functional
as
F
...
@@ -34,11 +33,11 @@ class ASPPModule(nn.Layer):
...
@@ -34,11 +33,11 @@ class ASPPModule(nn.Layer):
image_pooling: if augmented with image-level features.
image_pooling: if augmented with image-level features.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
aspp_ratios
,
aspp_ratios
,
in_channels
,
in_channels
,
out_channels
,
out_channels
,
sep_conv
=
False
,
sep_conv
=
False
,
image_pooling
=
False
):
image_pooling
=
False
):
super
(
ASPPModule
,
self
).
__init__
()
super
(
ASPPModule
,
self
).
__init__
()
...
@@ -47,42 +46,41 @@ class ASPPModule(nn.Layer):
...
@@ -47,42 +46,41 @@ class ASPPModule(nn.Layer):
for
ratio
in
aspp_ratios
:
for
ratio
in
aspp_ratios
:
if
sep_conv
and
ratio
>
1
:
if
sep_conv
and
ratio
>
1
:
conv_func
=
layer_libs
.
DepthwiseConvB
nRelu
conv_func
=
layer_libs
.
DepthwiseConvB
NReLU
else
:
else
:
conv_func
=
layer_libs
.
ConvB
nRelu
conv_func
=
layer_libs
.
ConvB
NReLU
block
=
conv_func
(
block
=
conv_func
(
in_channels
=
in_channels
,
in_channels
=
in_channels
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
1
if
ratio
==
1
else
3
,
kernel_size
=
1
if
ratio
==
1
else
3
,
dilation
=
ratio
,
dilation
=
ratio
,
padding
=
0
if
ratio
==
1
else
ratio
padding
=
0
if
ratio
==
1
else
ratio
)
)
self
.
aspp_blocks
.
append
(
block
)
self
.
aspp_blocks
.
append
(
block
)
out_size
=
len
(
self
.
aspp_blocks
)
out_size
=
len
(
self
.
aspp_blocks
)
if
image_pooling
:
if
image_pooling
:
self
.
global_avg_pool
=
nn
.
Sequential
(
self
.
global_avg_pool
=
nn
.
Sequential
(
nn
.
AdaptiveAvgPool2d
(
output_size
=
(
1
,
1
)),
nn
.
AdaptiveAvgPool2d
(
output_size
=
(
1
,
1
)),
layer_libs
.
ConvB
nRelu
(
in_channels
,
out_channels
,
kernel_size
=
1
,
bias_attr
=
False
)
layer_libs
.
ConvB
NReLU
(
)
in_channels
,
out_channels
,
kernel_size
=
1
,
bias_attr
=
False
)
)
out_size
+=
1
out_size
+=
1
self
.
image_pooling
=
image_pooling
self
.
image_pooling
=
image_pooling
self
.
conv_bn_relu
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
out_channels
*
out_size
,
in_channels
=
out_channels
*
out_size
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
1
)
kernel_size
=
1
)
self
.
dropout
=
nn
.
Dropout
(
p
=
0.1
)
# drop rate
self
.
dropout
=
nn
.
Dropout
(
p
=
0.1
)
# drop rate
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
outputs
=
[]
outputs
=
[]
for
block
in
self
.
aspp_blocks
:
for
block
in
self
.
aspp_blocks
:
outputs
.
append
(
block
(
x
))
outputs
.
append
(
block
(
x
))
if
self
.
image_pooling
:
if
self
.
image_pooling
:
img_avg
=
self
.
global_avg_pool
(
x
)
img_avg
=
self
.
global_avg_pool
(
x
)
img_avg
=
F
.
resize_bilinear
(
img_avg
,
out_shape
=
x
.
shape
[
2
:])
img_avg
=
F
.
resize_bilinear
(
img_avg
,
out_shape
=
x
.
shape
[
2
:])
...
@@ -93,17 +91,17 @@ class ASPPModule(nn.Layer):
...
@@ -93,17 +91,17 @@ class ASPPModule(nn.Layer):
x
=
self
.
dropout
(
x
)
x
=
self
.
dropout
(
x
)
return
x
return
x
class
PPModule
(
nn
.
Layer
):
class
PPModule
(
nn
.
Layer
):
"""
"""
Pyramid pooling module orginally in PSPNet
Pyramid pooling module or
i
ginally in PSPNet
Args:
Args:
in_channels (int): the number of intput channels to pyramid pooling module.
in_channels (int): the number of intput channels to pyramid pooling module.
out_channels (int): the number of output channels after pyramid pooling module.
out_channels (int): the number of output channels after pyramid pooling module.
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
dim_reduction (bool): a bool value represent if redu
ing diment
ion after pooling. Default to True.
dim_reduction (bool): a bool value represent if redu
cing dimens
ion after pooling. Default to True.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -125,7 +123,7 @@ class PPModule(nn.Layer):
...
@@ -125,7 +123,7 @@ class PPModule(nn.Layer):
for
size
in
bin_sizes
for
size
in
bin_sizes
])
])
self
.
conv_bn_relu2
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu2
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
+
inter_channels
*
len
(
bin_sizes
),
in_channels
=
in_channels
+
inter_channels
*
len
(
bin_sizes
),
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -135,7 +133,7 @@ class PPModule(nn.Layer):
...
@@ -135,7 +133,7 @@ class PPModule(nn.Layer):
"""
"""
Create one pooling layer.
Create one pooling layer.
In our implementation, we adopt the same dimen
t
ion reduction as the original paper that might be
In our implementation, we adopt the same dimen
s
ion reduction as the original paper that might be
slightly different with other implementations.
slightly different with other implementations.
After pooling, the channels are reduced to 1/len(bin_sizes) immediately, while some other implementations
After pooling, the channels are reduced to 1/len(bin_sizes) immediately, while some other implementations
...
@@ -151,7 +149,7 @@ class PPModule(nn.Layer):
...
@@ -151,7 +149,7 @@ class PPModule(nn.Layer):
"""
"""
prior
=
nn
.
AdaptiveAvgPool2d
(
output_size
=
(
size
,
size
))
prior
=
nn
.
AdaptiveAvgPool2d
(
output_size
=
(
size
,
size
))
conv
=
layer_libs
.
ConvB
nRelu
(
conv
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
,
out_channels
=
out_channels
,
kernel_size
=
1
)
in_channels
=
in_channels
,
out_channels
=
out_channels
,
kernel_size
=
1
)
return
nn
.
Sequential
(
prior
,
conv
)
return
nn
.
Sequential
(
prior
,
conv
)
...
@@ -167,4 +165,4 @@ class PPModule(nn.Layer):
...
@@ -167,4 +165,4 @@ class PPModule(nn.Layer):
cat
=
paddle
.
concat
(
cat_layers
,
axis
=
1
)
cat
=
paddle
.
concat
(
cat_layers
,
axis
=
1
)
out
=
self
.
conv_bn_relu2
(
cat
)
out
=
self
.
conv_bn_relu2
(
cat
)
return
out
return
out
\ No newline at end of file
dygraph/paddleseg/models/deeplab.py
浏览文件 @
3f658a36
...
@@ -29,140 +29,193 @@ class DeepLabV3P(nn.Layer):
...
@@ -29,140 +29,193 @@ class DeepLabV3P(nn.Layer):
"""
"""
The DeepLabV3Plus implementation based on PaddlePaddle.
The DeepLabV3Plus implementation based on PaddlePaddle.
The orginal artile refers to
The original article refers to
"Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, et, al. "Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, Hartwig Adam.
(https://arxiv.org/abs/1802.02611)
(https://arxiv.org/abs/1802.02611)
The DeepLabV3P consists of three main components, Backbone, ASPP and Decoder.
Args:
Args:
num_classes (int): the unique number of target classes.
num_classes (int): the unique number of target classes.
backbone (paddle.nn.Layer): backbone network, currently support Xception65, Resnet101_vd.
backbone (paddle.nn.Layer): backbone network, currently support Resnet50_vd/Resnet101_vd/Xception65.
model_pretrained (str): the path of pretrained model.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
aspp_ratios (tuple): the dilation rate using in ASSP module.
the first index will be taken as a low-level feature in Decoder component;
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
the first index will be taken as a low-level feature in Deconder component;
the second one will be taken as input of ASPP component.
the second one will be taken as input of ASPP component.
Usually backbone consists of four downsampling stage, and return an output of
Usually backbone consists of four downsampling stage, and return an output of
each stage, so we set default (0, 3), which means taking feature map of the first
each stage, so we set default (0, 3), which means taking feature map of the first
stage in backbone as low-level feature used in Decoder, and feature map of the fourth
stage in backbone as low-level feature used in Decoder, and feature map of the fourth
stage as input of ASPP.
stage as input of ASPP.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
aspp_ratios (tuple): the dilation rate using in ASSP module.
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
aspp_out_channels (int): the output channels of ASPP module.
pretrained (str): the path of pretrained model for fine tuning.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
num_classes
,
num_classes
,
backbone
,
backbone
,
backbone_pretrained
=
None
,
model_pretrained
=
None
,
backbone_indices
=
(
0
,
3
),
backbone_indices
=
(
0
,
3
),
backbone_channels
=
(
256
,
2048
),
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_out_channels
=
256
):
aspp_out_channels
=
256
,
pretrained
=
None
):
super
(
DeepLabV3P
,
self
).
__init__
()
super
(
DeepLabV3P
,
self
).
__init__
()
self
.
backbone
=
backbone
self
.
backbone
=
backbone
self
.
backbone_pretrained
=
backbone_pretrained
backbone_channels
=
backbone
.
backbone_channels
self
.
model_pretrained
=
model_pretrained
self
.
head
=
DeepLabV3PHead
(
num_classes
,
backbone_indices
,
backbone_channels
,
aspp_ratios
,
aspp_out_channels
)
utils
.
load_entire_model
(
self
,
pretrained
)
def
forward
(
self
,
input
):
feat_list
=
self
.
backbone
(
input
)
logit_list
=
self
.
head
(
feat_list
)
return
[
F
.
resize_bilinear
(
logit
,
input
.
shape
[
2
:])
for
logit
in
logit_list
]
class
DeepLabV3PHead
(
nn
.
Layer
):
"""
The DeepLabV3PHead implementation based on PaddlePaddle.
Args:
num_classes (int): the unique number of target classes.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as a low-level feature in Decoder component;
the second one will be taken as input of ASPP component.
Usually backbone consists of four downsampling stage, and return an output of
each stage, so we set default (0, 3), which means taking feature map of the first
stage in backbone as low-level feature used in Decoder, and feature map of the fourth
stage as input of ASPP.
backbone_channels (tuple): returned channels of backbone
aspp_ratios (tuple): the dilation rate using in ASSP module.
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
aspp_out_channels (int): the output channels of ASPP module.
"""
def
__init__
(
self
,
num_classes
,
backbone_indices
,
backbone_channels
,
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_out_channels
=
256
):
super
(
DeepLabV3PHead
,
self
).
__init__
()
self
.
aspp
=
pyramid_pool
.
ASPPModule
(
self
.
aspp
=
pyramid_pool
.
ASPPModule
(
aspp_ratios
,
backbone_channels
[
1
],
aspp_out_channels
,
sep_conv
=
True
,
image_pooling
=
True
)
aspp_ratios
,
self
.
decoder
=
Decoder
(
num_classes
,
backbone_channels
[
0
])
backbone_channels
[
backbone_indices
[
1
]],
aspp_out_channels
,
sep_conv
=
True
,
image_pooling
=
True
)
self
.
decoder
=
Decoder
(
num_classes
,
backbone_channels
[
backbone_indices
[
0
]])
self
.
backbone_indices
=
backbone_indices
self
.
backbone_indices
=
backbone_indices
self
.
init_weight
()
self
.
init_weight
()
def
forward
(
self
,
input
,
label
=
None
):
def
forward
(
self
,
feat_list
):
logit_list
=
[]
logit_list
=
[]
_
,
feat_list
=
self
.
backbone
(
input
)
low_level_feat
=
feat_list
[
self
.
backbone_indices
[
0
]]
low_level_feat
=
feat_list
[
self
.
backbone_indices
[
0
]]
x
=
feat_list
[
self
.
backbone_indices
[
1
]]
x
=
feat_list
[
self
.
backbone_indices
[
1
]]
x
=
self
.
aspp
(
x
)
x
=
self
.
aspp
(
x
)
logit
=
self
.
decoder
(
x
,
low_level_feat
)
logit
=
self
.
decoder
(
x
,
low_level_feat
)
logit
=
F
.
resize_bilinear
(
logit
,
input
.
shape
[
2
:])
logit_list
.
append
(
logit
)
logit_list
.
append
(
logit
)
return
logit_list
return
logit_list
def
init_weight
(
self
):
def
init_weight
(
self
):
"""
pass
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if
self
.
model_pretrained
is
not
None
:
utils
.
load_pretrained_model
(
self
,
self
.
model_pretrained
)
elif
self
.
backbone_pretrained
is
not
None
:
utils
.
load_pretrained_model
(
self
.
backbone
,
self
.
backbone_pretrained
)
@
manager
.
MODELS
.
add_component
@
manager
.
MODELS
.
add_component
class
DeepLabV3
(
nn
.
Layer
):
class
DeepLabV3
(
nn
.
Layer
):
"""
"""
The DeepLabV3 implementation based on PaddlePaddle.
The DeepLabV3 implementation based on PaddlePaddle.
The orginal article refers to
The original article refers to
"Rethinking Atrous Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, et, al. "Rethinking Atrous Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, George Papandreou, Florian Schroff, Hartwig Adam.
(https://arxiv.org/pdf/1706.05587.pdf)
(https://arxiv.org/pdf/1706.05587.pdf)
Args:
Args:
Refer to DeepLabV3P above
Refer to DeepLabV3P above
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
num_classes
,
num_classes
,
backbone
,
backbone
,
backbone_pretrained
=
None
,
pretrained
=
None
,
model_pretrained
=
None
,
backbone_indices
=
(
3
,
),
backbone_indices
=
(
3
,),
backbone_channels
=
(
2048
,),
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_out_channels
=
256
):
aspp_out_channels
=
256
):
super
(
DeepLabV3
,
self
).
__init__
()
super
(
DeepLabV3
,
self
).
__init__
()
self
.
backbone
=
backbone
self
.
backbone
=
backbone
backbone_channels
=
backbone
.
backbone_channels
self
.
head
=
DeepLabV3Head
(
num_classes
,
backbone_indices
,
backbone_channels
,
aspp_ratios
,
aspp_out_channels
)
utils
.
load_entire_model
(
self
,
pretrained
)
def
forward
(
self
,
input
):
feat_list
=
self
.
backbone
(
input
)
logit_list
=
self
.
head
(
feat_list
)
return
[
F
.
resize_bilinear
(
logit
,
input
.
shape
[
2
:])
for
logit
in
logit_list
]
class
DeepLabV3Head
(
nn
.
Layer
):
def
__init__
(
self
,
num_classes
,
backbone_indices
=
(
3
,
),
backbone_channels
=
(
2048
,
),
aspp_ratios
=
(
1
,
6
,
12
,
18
),
aspp_out_channels
=
256
):
super
(
DeepLabV3Head
,
self
).
__init__
()
self
.
aspp
=
pyramid_pool
.
ASPPModule
(
self
.
aspp
=
pyramid_pool
.
ASPPModule
(
aspp_ratios
,
backbone_channels
[
0
],
aspp_out_channels
,
aspp_ratios
,
sep_conv
=
False
,
image_pooling
=
True
)
backbone_channels
[
backbone_indices
[
0
]],
aspp_out_channels
,
sep_conv
=
False
,
image_pooling
=
True
)
self
.
cls
=
nn
.
Conv2d
(
self
.
cls
=
nn
.
Conv2d
(
in_channels
=
backbone_channels
[
0
],
in_channels
=
backbone_channels
[
backbone_indices
[
0
]
],
out_channels
=
num_classes
,
out_channels
=
num_classes
,
kernel_size
=
1
)
kernel_size
=
1
)
self
.
backbone_indices
=
backbone_indices
self
.
backbone_indices
=
backbone_indices
self
.
init_weight
(
model_pretrained
)
self
.
init_weight
()
def
forward
(
self
,
input
,
label
=
None
):
def
forward
(
self
,
feat_list
):
logit_list
=
[]
logit_list
=
[]
_
,
feat_list
=
self
.
backbone
(
input
)
x
=
feat_list
[
self
.
backbone_indices
[
0
]]
x
=
feat_list
[
self
.
backbone_indices
[
0
]]
logit
=
self
.
cls
(
x
)
logit
=
self
.
cls
(
x
)
logit
=
F
.
resize_bilinear
(
logit
,
input
.
shape
[
2
:])
logit_list
.
append
(
logit
)
logit_list
.
append
(
logit
)
return
logit_list
return
logit_list
def
init_weight
(
self
,
pretrained_model
=
None
):
def
init_weight
(
self
):
"""
pass
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if
pretrained_model
is
not
None
:
if
os
.
path
.
exists
(
pretrained_model
):
utils
.
load_pretrained_model
(
self
,
pretrained_model
)
else
:
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained_model
))
class
Decoder
(
nn
.
Layer
):
class
Decoder
(
nn
.
Layer
):
...
@@ -178,12 +231,12 @@ class Decoder(nn.Layer):
...
@@ -178,12 +231,12 @@ class Decoder(nn.Layer):
def
__init__
(
self
,
num_classes
,
in_channels
):
def
__init__
(
self
,
num_classes
,
in_channels
):
super
(
Decoder
,
self
).
__init__
()
super
(
Decoder
,
self
).
__init__
()
self
.
conv_bn_relu1
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu1
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
,
out_channels
=
48
,
kernel_size
=
1
)
in_channels
=
in_channels
,
out_channels
=
48
,
kernel_size
=
1
)
self
.
conv_bn_relu2
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
conv_bn_relu2
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
304
,
out_channels
=
256
,
kernel_size
=
3
,
padding
=
1
)
in_channels
=
304
,
out_channels
=
256
,
kernel_size
=
3
,
padding
=
1
)
self
.
conv_bn_relu3
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
conv_bn_relu3
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
256
,
out_channels
=
256
,
kernel_size
=
3
,
padding
=
1
)
in_channels
=
256
,
out_channels
=
256
,
kernel_size
=
3
,
padding
=
1
)
self
.
conv
=
nn
.
Conv2d
(
self
.
conv
=
nn
.
Conv2d
(
in_channels
=
256
,
out_channels
=
num_classes
,
kernel_size
=
1
)
in_channels
=
256
,
out_channels
=
num_classes
,
kernel_size
=
1
)
...
...
dygraph/paddleseg/models/fast_scnn.py
浏览文件 @
3f658a36
...
@@ -26,15 +26,15 @@ class FastSCNN(nn.Layer):
...
@@ -26,15 +26,15 @@ class FastSCNN(nn.Layer):
As mentioned in the original paper, FastSCNN is a real-time segmentation algorithm (123.5fps)
As mentioned in the original paper, FastSCNN is a real-time segmentation algorithm (123.5fps)
even for high resolution images (1024x2048).
even for high resolution images (1024x2048).
The or
ginal arti
le refers to
The or
iginal artic
le refers to
Poudel, Rudra PK, et al. "Fast-scnn: Fast semantic segmentation network."
Poudel, Rudra PK, et al. "Fast-scnn: Fast semantic segmentation network."
(https://arxiv.org/pdf/1902.04502.pdf)
(https://arxiv.org/pdf/1902.04502.pdf)
Args:
Args:
num_classes (int): the unique number of target classes. Default to 2.
num_classes (int): the unique number of target classes. Default to 2.
model_pretrained (str): the path of pretrained model. Defaul
l
t to None.
model_pretrained (str): the path of pretrained model. Default to None.
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss.
enable_auxiliary_loss (bool): a bool values indic
a
tes whether adding auxiliary loss.
if true, auxiliary loss will be added after LearningToDownsample module, where the weight is 0.4. Default to False.
if true, auxiliary loss will be added after LearningToDownsample module, where the weight is 0.4. Default to False.
"""
"""
...
@@ -105,15 +105,15 @@ class LearningToDownsample(nn.Layer):
...
@@ -105,15 +105,15 @@ class LearningToDownsample(nn.Layer):
def
__init__
(
self
,
dw_channels1
=
32
,
dw_channels2
=
48
,
out_channels
=
64
):
def
__init__
(
self
,
dw_channels1
=
32
,
dw_channels2
=
48
,
out_channels
=
64
):
super
(
LearningToDownsample
,
self
).
__init__
()
super
(
LearningToDownsample
,
self
).
__init__
()
self
.
conv_bn_relu
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
3
,
out_channels
=
dw_channels1
,
kernel_size
=
3
,
stride
=
2
)
in_channels
=
3
,
out_channels
=
dw_channels1
,
kernel_size
=
3
,
stride
=
2
)
self
.
dsconv_bn_relu1
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
dsconv_bn_relu1
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
dw_channels1
,
in_channels
=
dw_channels1
,
out_channels
=
dw_channels2
,
out_channels
=
dw_channels2
,
kernel_size
=
3
,
kernel_size
=
3
,
stride
=
2
,
stride
=
2
,
padding
=
1
)
padding
=
1
)
self
.
dsconv_bn_relu2
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
dsconv_bn_relu2
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
dw_channels2
,
in_channels
=
dw_channels2
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -208,13 +208,13 @@ class LinearBottleneck(nn.Layer):
...
@@ -208,13 +208,13 @@ class LinearBottleneck(nn.Layer):
expand_channels
=
in_channels
*
expansion
expand_channels
=
in_channels
*
expansion
self
.
block
=
nn
.
Sequential
(
self
.
block
=
nn
.
Sequential
(
# pw
# pw
layer_libs
.
ConvB
nRelu
(
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
,
in_channels
=
in_channels
,
out_channels
=
expand_channels
,
out_channels
=
expand_channels
,
kernel_size
=
1
,
kernel_size
=
1
,
bias_attr
=
False
),
bias_attr
=
False
),
# dw
# dw
layer_libs
.
ConvB
nRelu
(
layer_libs
.
ConvB
NReLU
(
in_channels
=
expand_channels
,
in_channels
=
expand_channels
,
out_channels
=
expand_channels
,
out_channels
=
expand_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -239,7 +239,7 @@ class LinearBottleneck(nn.Layer):
...
@@ -239,7 +239,7 @@ class LinearBottleneck(nn.Layer):
class
FeatureFusionModule
(
nn
.
Layer
):
class
FeatureFusionModule
(
nn
.
Layer
):
"""
"""
Feature Fusion Module Impleme
m
tation.
Feature Fusion Module Impleme
n
tation.
This module fuses high-resolution feature and low-resolution feature.
This module fuses high-resolution feature and low-resolution feature.
...
@@ -253,7 +253,7 @@ class FeatureFusionModule(nn.Layer):
...
@@ -253,7 +253,7 @@ class FeatureFusionModule(nn.Layer):
super
(
FeatureFusionModule
,
self
).
__init__
()
super
(
FeatureFusionModule
,
self
).
__init__
()
# There only depth-wise conv is used WITHOUT point-wise conv
# There only depth-wise conv is used WITHOUT point-wise conv
self
.
dwconv
=
layer_libs
.
ConvB
nRelu
(
self
.
dwconv
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
low_in_channels
,
in_channels
=
low_in_channels
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -301,13 +301,13 @@ class Classifier(nn.Layer):
...
@@ -301,13 +301,13 @@ class Classifier(nn.Layer):
def
__init__
(
self
,
input_channels
,
num_classes
):
def
__init__
(
self
,
input_channels
,
num_classes
):
super
(
Classifier
,
self
).
__init__
()
super
(
Classifier
,
self
).
__init__
()
self
.
dsconv1
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
dsconv1
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
input_channels
,
in_channels
=
input_channels
,
out_channels
=
input_channels
,
out_channels
=
input_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
padding
=
1
)
padding
=
1
)
self
.
dsconv2
=
layer_libs
.
DepthwiseConvB
nRelu
(
self
.
dsconv2
=
layer_libs
.
DepthwiseConvB
NReLU
(
in_channels
=
input_channels
,
in_channels
=
input_channels
,
out_channels
=
input_channels
,
out_channels
=
input_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
...
dygraph/paddleseg/models/gcnet.py
浏览文件 @
3f658a36
...
@@ -27,15 +27,15 @@ class GCNet(nn.Layer):
...
@@ -27,15 +27,15 @@ class GCNet(nn.Layer):
"""
"""
The GCNet implementation based on PaddlePaddle.
The GCNet implementation based on PaddlePaddle.
The or
ginal arti
le refers to
The or
iginal artic
le refers to
Cao, Yue, et al. "GCnet: Non-local networks meet squeeze-excitation networks and beyond."
Cao, Yue, et al. "GCnet: Non-local networks meet squeeze-excitation networks and beyond."
(https://arxiv.org/pdf/1904.11492.pdf)
(https://arxiv.org/pdf/1904.11492.pdf)
Args:
Args:
num_classes (int): the unique number of target classes.
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaul
l
t to None.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
backbone_indices (tuple): two values in the tuple indic
a
te the indices of output of backbone.
the first index will be taken as a deep-supervision feature in auxiliary layer;
the first index will be taken as a deep-supervision feature in auxiliary layer;
the second one will be taken as input of GlobalContextBlock. Usually backbone
the second one will be taken as input of GlobalContextBlock. Usually backbone
consists of four downsampling stage, and return an output of each stage, so we
consists of four downsampling stage, and return an output of each stage, so we
...
@@ -43,8 +43,8 @@ class GCNet(nn.Layer):
...
@@ -43,8 +43,8 @@ class GCNet(nn.Layer):
and the fourth stage (res5c) in backbone.
and the fourth stage (res5c) in backbone.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
gc_channels (int): input channels to Global Context Block. Default to 512.
gc_channels (int): input channels to Global Context Block. Default to 512.
ratio (float): it indictes the ratio of attention channels and gc_channels. Default to 1/4.
ratio (float): it indic
a
tes the ratio of attention channels and gc_channels. Default to 1/4.
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
enable_auxiliary_loss (bool): a bool values indic
a
tes whether adding auxiliary loss. Default to True.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -63,7 +63,7 @@ class GCNet(nn.Layer):
...
@@ -63,7 +63,7 @@ class GCNet(nn.Layer):
self
.
backbone
=
backbone
self
.
backbone
=
backbone
in_channels
=
backbone_channels
[
1
]
in_channels
=
backbone_channels
[
1
]
self
.
conv_bn_relu1
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu1
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
,
in_channels
=
in_channels
,
out_channels
=
gc_channels
,
out_channels
=
gc_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -71,13 +71,13 @@ class GCNet(nn.Layer):
...
@@ -71,13 +71,13 @@ class GCNet(nn.Layer):
self
.
gc_block
=
GlobalContextBlock
(
in_channels
=
gc_channels
,
ratio
=
ratio
)
self
.
gc_block
=
GlobalContextBlock
(
in_channels
=
gc_channels
,
ratio
=
ratio
)
self
.
conv_bn_relu2
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu2
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
gc_channels
,
in_channels
=
gc_channels
,
out_channels
=
gc_channels
,
out_channels
=
gc_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
padding
=
1
)
padding
=
1
)
self
.
conv_bn_relu3
=
layer_libs
.
ConvB
nRelu
(
self
.
conv_bn_relu3
=
layer_libs
.
ConvB
NReLU
(
in_channels
=
in_channels
+
gc_channels
,
in_channels
=
in_channels
+
gc_channels
,
out_channels
=
gc_channels
,
out_channels
=
gc_channels
,
kernel_size
=
3
,
kernel_size
=
3
,
...
@@ -154,7 +154,7 @@ class GlobalContextBlock(nn.Layer):
...
@@ -154,7 +154,7 @@ class GlobalContextBlock(nn.Layer):
in_channels
=
in_channels
,
out_channels
=
1
,
kernel_size
=
1
)
in_channels
=
in_channels
,
out_channels
=
1
,
kernel_size
=
1
)
self
.
softmax
=
nn
.
Softmax
(
axis
=
2
)
self
.
softmax
=
nn
.
Softmax
(
axis
=
2
)
inter_channels
=
int
(
in_channels
*
ratio
)
inter_channels
=
int
(
in_channels
*
ratio
)
self
.
channel_add_conv
=
nn
.
Sequential
(
self
.
channel_add_conv
=
nn
.
Sequential
(
nn
.
Conv2d
(
nn
.
Conv2d
(
...
...
dygraph/paddleseg/models/ocrnet.py
浏览文件 @
3f658a36
...
@@ -124,7 +124,6 @@ class ObjectAttentionBlock(nn.Layer):
...
@@ -124,7 +124,6 @@ class ObjectAttentionBlock(nn.Layer):
class
OCRHead
(
nn
.
Layer
):
class
OCRHead
(
nn
.
Layer
):
"""
"""
The Object contextual representation head.
The Object contextual representation head.
Args:
Args:
num_classes(int): the unique number of target classes.
num_classes(int): the unique number of target classes.
in_channels(tuple): the number of input channels.
in_channels(tuple): the number of input channels.
...
@@ -179,11 +178,9 @@ class OCRHead(nn.Layer):
...
@@ -179,11 +178,9 @@ class OCRHead(nn.Layer):
class
OCRNet
(
nn
.
Layer
):
class
OCRNet
(
nn
.
Layer
):
"""
"""
The OCRNet implementation based on PaddlePaddle.
The OCRNet implementation based on PaddlePaddle.
The original article refers to
The original article refers to
Yuan, Yuhui, et al. "Object-Contextual Representations for Semantic Segmentation"
Yuan, Yuhui, et al. "Object-Contextual Representations for Semantic Segmentation"
(https://arxiv.org/pdf/1909.11065.pdf)
(https://arxiv.org/pdf/1909.11065.pdf)
Args:
Args:
num_classes(int): the unique number of target classes.
num_classes(int): the unique number of target classes.
backbone(Paddle.nn.Layer): backbone network.
backbone(Paddle.nn.Layer): backbone network.
...
@@ -234,4 +231,4 @@ class OCRNet(nn.Layer):
...
@@ -234,4 +231,4 @@ class OCRNet(nn.Layer):
utils
.
load_pretrained_model
(
self
,
pretrained
)
utils
.
load_pretrained_model
(
self
,
pretrained
)
else
:
else
:
raise
Exception
(
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained
))
'Pretrained model is not found: {}'
.
format
(
pretrained
))
\ No newline at end of file
dygraph/paddleseg/models/pspnet.py
浏览文件 @
3f658a36
...
@@ -26,7 +26,7 @@ class PSPNet(nn.Layer):
...
@@ -26,7 +26,7 @@ class PSPNet(nn.Layer):
"""
"""
The PSPNet implementation based on PaddlePaddle.
The PSPNet implementation based on PaddlePaddle.
The or
ginal arti
le refers to
The or
iginal artic
le refers to
Zhao, Hengshuang, et al. "Pyramid scene parsing network."
Zhao, Hengshuang, et al. "Pyramid scene parsing network."
Proceedings of the IEEE conference on computer vision and pattern recognition. 2017.
Proceedings of the IEEE conference on computer vision and pattern recognition. 2017.
(https://openaccess.thecvf.com/content_cvpr_2017/papers/Zhao_Pyramid_Scene_Parsing_CVPR_2017_paper.pdf)
(https://openaccess.thecvf.com/content_cvpr_2017/papers/Zhao_Pyramid_Scene_Parsing_CVPR_2017_paper.pdf)
...
@@ -34,8 +34,8 @@ class PSPNet(nn.Layer):
...
@@ -34,8 +34,8 @@ class PSPNet(nn.Layer):
Args:
Args:
num_classes (int): the unique number of target classes.
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaul
l
t to None.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
backbone_indices (tuple): two values in the tuple indic
a
te the indices of output of backbone.
the first index will be taken as a deep-supervision feature in auxiliary layer;
the first index will be taken as a deep-supervision feature in auxiliary layer;
the second one will be taken as input of Pyramid Pooling Module (PPModule).
the second one will be taken as input of Pyramid Pooling Module (PPModule).
Usually backbone consists of four downsampling stage, and return an output of
Usually backbone consists of four downsampling stage, and return an output of
...
@@ -44,7 +44,7 @@ class PSPNet(nn.Layer):
...
@@ -44,7 +44,7 @@ class PSPNet(nn.Layer):
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
pp_out_channels (int): output channels after Pyramid Pooling Module. Default to 1024.
pp_out_channels (int): output channels after Pyramid Pooling Module. Default to 1024.
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
enable_auxiliary_loss (bool): a bool values indic
a
tes whether adding auxiliary loss. Default to True.
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -107,6 +107,7 @@ class PSPNet(nn.Layer):
...
@@ -107,6 +107,7 @@ class PSPNet(nn.Layer):
def
init_weight
(
self
,
pretrained_model
=
None
):
def
init_weight
(
self
,
pretrained_model
=
None
):
"""
"""
Initialize the parameters of model parts.
Initialize the parameters of model parts.
Args:
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
"""
...
...
dygraph/paddleseg/utils/metrics.py
浏览文件 @
3f658a36
...
@@ -41,7 +41,7 @@ class ConfusionMatrix(object):
...
@@ -41,7 +41,7 @@ class ConfusionMatrix(object):
label
=
np
.
asarray
(
label
)[
mask
]
label
=
np
.
asarray
(
label
)[
mask
]
pred
=
np
.
asarray
(
pred
)[
mask
]
pred
=
np
.
asarray
(
pred
)[
mask
]
one
=
np
.
ones_like
(
pred
)
one
=
np
.
ones_like
(
pred
)
# Accumuate ([row=label, col=pred], 1) into sparse
matrix
# Accumuate ([row=label, col=pred], 1) into sparse
spm
=
csr_matrix
((
one
,
(
label
,
pred
)),
spm
=
csr_matrix
((
one
,
(
label
,
pred
)),
shape
=
(
self
.
num_classes
,
self
.
num_classes
))
shape
=
(
self
.
num_classes
,
self
.
num_classes
))
spm
=
spm
.
todense
()
spm
=
spm
.
todense
()
...
...
dygraph/paddleseg/utils/progbar.py
浏览文件 @
3f658a36
...
@@ -17,8 +17,9 @@ import time
...
@@ -17,8 +17,9 @@ import time
import
numpy
as
np
import
numpy
as
np
class
Progbar
(
object
):
class
Progbar
(
object
):
"""Displays a progress bar.
"""Displays a progress bar.
refers to https://github.com/keras-team/keras/blob/keras-2/keras/utils/generic_utils.py
refers to https://github.com/keras-team/keras/blob/keras-2/keras/utils/generic_utils.py
Arguments:
Arguments:
target: Total number of steps expected, None if unknown.
target: Total number of steps expected, None if unknown.
...
@@ -31,39 +32,39 @@ class Progbar(object):
...
@@ -31,39 +32,39 @@ class Progbar(object):
unit_name: Display name for step counts (usually "step" or "sample").
unit_name: Display name for step counts (usually "step" or "sample").
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
target
,
target
,
width
=
30
,
width
=
30
,
verbose
=
1
,
verbose
=
1
,
interval
=
0.05
,
interval
=
0.05
,
stateful_metrics
=
None
,
stateful_metrics
=
None
,
unit_name
=
'step'
):
unit_name
=
'step'
):
self
.
target
=
target
self
.
target
=
target
self
.
width
=
width
self
.
width
=
width
self
.
verbose
=
verbose
self
.
verbose
=
verbose
self
.
interval
=
interval
self
.
interval
=
interval
self
.
unit_name
=
unit_name
self
.
unit_name
=
unit_name
if
stateful_metrics
:
if
stateful_metrics
:
self
.
stateful_metrics
=
set
(
stateful_metrics
)
self
.
stateful_metrics
=
set
(
stateful_metrics
)
else
:
else
:
self
.
stateful_metrics
=
set
()
self
.
stateful_metrics
=
set
()
self
.
_dynamic_display
=
((
hasattr
(
sys
.
stdout
,
'isatty'
)
and
self
.
_dynamic_display
=
((
hasattr
(
sys
.
stdout
,
'isatty'
)
sys
.
stdout
.
isatty
())
or
and
sys
.
stdout
.
isatty
())
'ipykernel'
in
sys
.
modules
or
or
'ipykernel'
in
sys
.
modules
'posix'
in
sys
.
modules
or
or
'posix'
in
sys
.
modules
'PYCHARM_HOSTED'
in
os
.
environ
)
or
'PYCHARM_HOSTED'
in
os
.
environ
)
self
.
_total_width
=
0
self
.
_total_width
=
0
self
.
_seen_so_far
=
0
self
.
_seen_so_far
=
0
# We use a dict + list to avoid garbage collection
# We use a dict + list to avoid garbage collection
# issues found in OrderedDict
# issues found in OrderedDict
self
.
_values
=
{}
self
.
_values
=
{}
self
.
_values_order
=
[]
self
.
_values_order
=
[]
self
.
_start
=
time
.
time
()
self
.
_start
=
time
.
time
()
self
.
_last_update
=
0
self
.
_last_update
=
0
def
update
(
self
,
current
,
values
=
None
,
finalize
=
None
):
def
update
(
self
,
current
,
values
=
None
,
finalize
=
None
):
"""Updates the progress bar.
"""Updates the progress bar.
Arguments:
Arguments:
current: Index of current step.
current: Index of current step.
values: List of tuples: `(name, value_for_last_step)`. If `name` is in
values: List of tuples: `(name, value_for_last_step)`. If `name` is in
...
@@ -72,129 +73,131 @@ class Progbar(object):
...
@@ -72,129 +73,131 @@ class Progbar(object):
finalize: Whether this is the last update for the progress bar. If
finalize: Whether this is the last update for the progress bar. If
`None`, defaults to `current >= self.target`.
`None`, defaults to `current >= self.target`.
"""
"""
if
finalize
is
None
:
if
finalize
is
None
:
if
self
.
target
is
None
:
if
self
.
target
is
None
:
finalize
=
False
finalize
=
False
else
:
else
:
finalize
=
current
>=
self
.
target
finalize
=
current
>=
self
.
target
values
=
values
or
[]
values
=
values
or
[]
for
k
,
v
in
values
:
for
k
,
v
in
values
:
if
k
not
in
self
.
_values_order
:
if
k
not
in
self
.
_values_order
:
self
.
_values_order
.
append
(
k
)
self
.
_values_order
.
append
(
k
)
if
k
not
in
self
.
stateful_metrics
:
if
k
not
in
self
.
stateful_metrics
:
# In the case that progress bar doesn't have a target value in the first
# In the case that progress bar doesn't have a target value in the first
# epoch, both on_batch_end and on_epoch_end will be called, which will
# epoch, both on_batch_end and on_epoch_end will be called, which will
# cause 'current' and 'self._seen_so_far' to have the same value. Force
# cause 'current' and 'self._seen_so_far' to have the same value. Force
# the minimal value to 1 here, otherwise stateful_metric will be 0s.
# the minimal value to 1 here, otherwise stateful_metric will be 0s.
value_base
=
max
(
current
-
self
.
_seen_so_far
,
1
)
value_base
=
max
(
current
-
self
.
_seen_so_far
,
1
)
if
k
not
in
self
.
_values
:
if
k
not
in
self
.
_values
:
self
.
_values
[
k
]
=
[
v
*
value_base
,
value_base
]
self
.
_values
[
k
]
=
[
v
*
value_base
,
value_base
]
else
:
else
:
self
.
_values
[
k
][
0
]
+=
v
*
value_base
self
.
_values
[
k
][
0
]
+=
v
*
value_base
self
.
_values
[
k
][
1
]
+=
value_base
self
.
_values
[
k
][
1
]
+=
value_base
else
:
else
:
# Stateful metrics output a numeric value. This representation
# Stateful metrics output a numeric value. This representation
# means "take an average from a single value" but keeps the
# means "take an average from a single value" but keeps the
# numeric formatting.
# numeric formatting.
self
.
_values
[
k
]
=
[
v
,
1
]
self
.
_values
[
k
]
=
[
v
,
1
]
self
.
_seen_so_far
=
current
self
.
_seen_so_far
=
current
now
=
time
.
time
()
now
=
time
.
time
()
info
=
' - %.0fs'
%
(
now
-
self
.
_start
)
info
=
' - %.0fs'
%
(
now
-
self
.
_start
)
if
self
.
verbose
==
1
:
if
self
.
verbose
==
1
:
if
now
-
self
.
_last_update
<
self
.
interval
and
not
finalize
:
if
now
-
self
.
_last_update
<
self
.
interval
and
not
finalize
:
return
return
prev_total_width
=
self
.
_total_width
prev_total_width
=
self
.
_total_width
if
self
.
_dynamic_display
:
if
self
.
_dynamic_display
:
sys
.
stdout
.
write
(
'
\b
'
*
prev_total_width
)
sys
.
stdout
.
write
(
'
\b
'
*
prev_total_width
)
sys
.
stdout
.
write
(
'
\r
'
)
sys
.
stdout
.
write
(
'
\r
'
)
else
:
else
:
sys
.
stdout
.
write
(
'
\n
'
)
sys
.
stdout
.
write
(
'
\n
'
)
if
self
.
target
is
not
None
:
if
self
.
target
is
not
None
:
numdigits
=
int
(
np
.
log10
(
self
.
target
))
+
1
numdigits
=
int
(
np
.
log10
(
self
.
target
))
+
1
bar
=
(
'%'
+
str
(
numdigits
)
+
'd/%d ['
)
%
(
current
,
self
.
target
)
bar
=
(
'%'
+
str
(
numdigits
)
+
'd/%d ['
)
%
(
current
,
self
.
target
)
prog
=
float
(
current
)
/
self
.
target
prog
=
float
(
current
)
/
self
.
target
prog_width
=
int
(
self
.
width
*
prog
)
prog_width
=
int
(
self
.
width
*
prog
)
if
prog_width
>
0
:
if
prog_width
>
0
:
bar
+=
(
'='
*
(
prog_width
-
1
))
bar
+=
(
'='
*
(
prog_width
-
1
))
if
current
<
self
.
target
:
if
current
<
self
.
target
:
bar
+=
'>'
bar
+=
'>'
else
:
else
:
bar
+=
'='
bar
+=
'='
bar
+=
(
'.'
*
(
self
.
width
-
prog_width
))
bar
+=
(
'.'
*
(
self
.
width
-
prog_width
))
bar
+=
']'
bar
+=
']'
else
:
else
:
bar
=
'%7d/Unknown'
%
current
bar
=
'%7d/Unknown'
%
current
self
.
_total_width
=
len
(
bar
)
self
.
_total_width
=
len
(
bar
)
sys
.
stdout
.
write
(
bar
)
sys
.
stdout
.
write
(
bar
)
if
current
:
if
current
:
time_per_unit
=
(
now
-
self
.
_start
)
/
current
time_per_unit
=
(
now
-
self
.
_start
)
/
current
else
:
else
:
time_per_unit
=
0
time_per_unit
=
0
if
self
.
target
is
None
or
finalize
:
if
self
.
target
is
None
or
finalize
:
if
time_per_unit
>=
1
or
time_per_unit
==
0
:
if
time_per_unit
>=
1
or
time_per_unit
==
0
:
info
+=
' %.0fs/%s'
%
(
time_per_unit
,
self
.
unit_name
)
info
+=
' %.0fs/%s'
%
(
time_per_unit
,
self
.
unit_name
)
elif
time_per_unit
>=
1e-3
:
elif
time_per_unit
>=
1e-3
:
info
+=
' %.0fms/%s'
%
(
time_per_unit
*
1e3
,
self
.
unit_name
)
info
+=
' %.0fms/%s'
%
(
time_per_unit
*
1e3
,
self
.
unit_name
)
else
:
else
:
info
+=
' %.0fus/%s'
%
(
time_per_unit
*
1e6
,
self
.
unit_name
)
info
+=
' %.0fus/%s'
%
(
time_per_unit
*
1e6
,
self
.
unit_name
)
else
:
else
:
eta
=
time_per_unit
*
(
self
.
target
-
current
)
eta
=
time_per_unit
*
(
self
.
target
-
current
)
if
eta
>
3600
:
if
eta
>
3600
:
eta_format
=
'%d:%02d:%02d'
%
(
eta
//
3600
,
eta_format
=
'%d:%02d:%02d'
%
(
eta
//
3600
,
(
eta
%
3600
)
//
60
,
eta
%
60
)
(
eta
%
3600
)
//
60
,
eta
%
60
)
elif
eta
>
60
:
elif
eta
>
60
:
eta_format
=
'%d:%02d'
%
(
eta
//
60
,
eta
%
60
)
eta_format
=
'%d:%02d'
%
(
eta
//
60
,
eta
%
60
)
else
:
else
:
eta_format
=
'%ds'
%
eta
eta_format
=
'%ds'
%
eta
info
=
' - ETA: %s'
%
eta_format
info
=
' - ETA: %s'
%
eta_format
for
k
in
self
.
_values_order
:
for
k
in
self
.
_values_order
:
info
+=
' - %s:'
%
k
info
+=
' - %s:'
%
k
if
isinstance
(
self
.
_values
[
k
],
list
):
if
isinstance
(
self
.
_values
[
k
],
list
):
avg
=
np
.
mean
(
self
.
_values
[
k
][
0
]
/
max
(
1
,
self
.
_values
[
k
][
1
]))
avg
=
np
.
mean
(
if
abs
(
avg
)
>
1e-3
:
self
.
_values
[
k
][
0
]
/
max
(
1
,
self
.
_values
[
k
][
1
]))
info
+=
' %.4f'
%
avg
if
abs
(
avg
)
>
1e-3
:
else
:
info
+=
' %.4f'
%
avg
info
+=
' %.4e'
%
avg
else
:
else
:
info
+=
' %.4e'
%
avg
info
+=
' %s'
%
self
.
_values
[
k
]
else
:
info
+=
' %s'
%
self
.
_values
[
k
]
self
.
_total_width
+=
len
(
info
)
if
prev_total_width
>
self
.
_total_width
:
self
.
_total_width
+=
len
(
info
)
info
+=
(
' '
*
(
prev_total_width
-
self
.
_total_width
))
if
prev_total_width
>
self
.
_total_width
:
info
+=
(
' '
*
(
prev_total_width
-
self
.
_total_width
))
if
finalize
:
info
+=
'
\n
'
if
finalize
:
info
+=
'
\n
'
sys
.
stdout
.
write
(
info
)
sys
.
stdout
.
flush
()
sys
.
stdout
.
write
(
info
)
sys
.
stdout
.
flush
()
elif
self
.
verbose
==
2
:
if
finalize
:
elif
self
.
verbose
==
2
:
numdigits
=
int
(
np
.
log10
(
self
.
target
))
+
1
if
finalize
:
count
=
(
'%'
+
str
(
numdigits
)
+
'd/%d'
)
%
(
current
,
self
.
target
)
numdigits
=
int
(
np
.
log10
(
self
.
target
))
+
1
info
=
count
+
info
count
=
(
'%'
+
str
(
numdigits
)
+
'd/%d'
)
%
(
current
,
self
.
target
)
for
k
in
self
.
_values_order
:
info
=
count
+
info
info
+=
' - %s:'
%
k
for
k
in
self
.
_values_order
:
avg
=
np
.
mean
(
self
.
_values
[
k
][
0
]
/
max
(
1
,
self
.
_values
[
k
][
1
]))
info
+=
' - %s:'
%
k
if
avg
>
1e-3
:
avg
=
np
.
mean
(
info
+=
' %.4f'
%
avg
self
.
_values
[
k
][
0
]
/
max
(
1
,
self
.
_values
[
k
][
1
]))
else
:
if
avg
>
1e-3
:
info
+=
' %.4e'
%
avg
info
+=
' %.4f'
%
avg
info
+=
'
\n
'
else
:
info
+=
' %.4e'
%
avg
sys
.
stdout
.
write
(
info
)
info
+=
'
\n
'
sys
.
stdout
.
flush
()
sys
.
stdout
.
write
(
info
)
self
.
_last_update
=
now
sys
.
stdout
.
flush
()
def
add
(
self
,
n
,
values
=
None
):
self
.
_last_update
=
now
self
.
update
(
self
.
_seen_so_far
+
n
,
values
)
\ No newline at end of file
def
add
(
self
,
n
,
values
=
None
):
self
.
update
(
self
.
_seen_so_far
+
n
,
values
)
dygraph/paddleseg/utils/utils.py
浏览文件 @
3f658a36
...
@@ -44,6 +44,19 @@ def seconds_to_hms(seconds):
...
@@ -44,6 +44,19 @@ def seconds_to_hms(seconds):
return
hms_str
return
hms_str
def
load_entire_model
(
model
,
pretrained
):
if
pretrained
is
not
None
:
if
os
.
path
.
exists
(
pretrained
):
load_pretrained_model
(
model
,
pretrained
)
else
:
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained
))
else
:
logger
.
warning
(
'Not all pretrained params of {} to load, '
\
'training from scratch or a pretrained backbone'
.
format
(
model
.
__class__
.
__name__
))
def
load_pretrained_model
(
model
,
pretrained_model
):
def
load_pretrained_model
(
model
,
pretrained_model
):
if
pretrained_model
is
not
None
:
if
pretrained_model
is
not
None
:
logger
.
info
(
'Load pretrained model from {}'
.
format
(
pretrained_model
))
logger
.
info
(
'Load pretrained model from {}'
.
format
(
pretrained_model
))
...
@@ -82,7 +95,7 @@ def load_pretrained_model(model, pretrained_model):
...
@@ -82,7 +95,7 @@ def load_pretrained_model(model, pretrained_model):
model_state_dict
[
k
]
=
para_state_dict
[
k
]
model_state_dict
[
k
]
=
para_state_dict
[
k
]
num_params_loaded
+=
1
num_params_loaded
+=
1
model
.
set_dict
(
model_state_dict
)
model
.
set_dict
(
model_state_dict
)
logger
.
info
(
"There are {}/{} var
ai
bles are loaded."
.
format
(
logger
.
info
(
"There are {}/{} var
ia
bles are loaded."
.
format
(
num_params_loaded
,
len
(
model_state_dict
)))
num_params_loaded
,
len
(
model_state_dict
)))
else
:
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录