Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
c7f64eeb
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看板
提交
c7f64eeb
编写于
8月 27, 2020
作者:
M
michaelowenliu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add comomon usages in models such as loss and prediction computing
上级
e3340a1e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
102 addition
and
0 deletion
+102
-0
dygraph/models/model_utils.py
dygraph/models/model_utils.py
+102
-0
未找到文件。
dygraph/models/model_utils.py
0 → 100644
浏览文件 @
c7f64eeb
# -*- encoding: utf-8 -*-
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
paddle
import
paddle.nn.functional
as
F
from
paddle
import
fluid
from
paddle.fluid
import
dygraph
from
paddle.fluid.dygraph
import
Conv2D
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
dygraph.models.architectures
import
layer_utils
class
FCNHead
(
fluid
.
dygraph
.
Layer
):
"""
The FCNHead implementation used in auxilary layer
Args:
in_channels (int): the number of input channels
out_channels (int): the number of output channels
"""
def
__init__
(
self
,
in_channels
,
out_channels
):
super
(
FCNHead
,
self
).
__init__
()
inter_channels
=
in_channels
//
4
self
.
conv_bn_relu
=
layer_utils
.
ConvBnRelu
(
num_channels
=
in_channels
,
num_filters
=
inter_channels
,
filter_size
=
3
,
padding
=
1
)
self
.
conv
=
Conv2D
(
num_channels
=
inter_channels
,
num_filters
=
out_channels
,
filter_size
=
1
)
def
forward
(
self
,
x
):
x
=
self
.
conv_bn_relu
(
x
)
x
=
F
.
dropout
(
x
,
p
=
0.1
)
x
=
self
.
conv
(
x
)
return
x
def
get_loss
(
logit
,
label
,
ignore_index
=
255
,
EPS
=
1e-5
):
"""
compute forward loss of the model
Args:
logit (tensor): the logit of model output
label (tensor): ground truth
Returns:
avg_loss (tensor): forward loss
"""
logit
=
fluid
.
layers
.
transpose
(
logit
,
[
0
,
2
,
3
,
1
])
label
=
fluid
.
layers
.
transpose
(
label
,
[
0
,
2
,
3
,
1
])
mask
=
label
!=
ignore_index
mask
=
fluid
.
layers
.
cast
(
mask
,
'float32'
)
loss
,
probs
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logit
,
label
,
ignore_index
=
ignore_index
,
return_softmax
=
True
,
axis
=-
1
)
loss
=
loss
*
mask
avg_loss
=
paddle
.
mean
(
loss
)
/
(
paddle
.
mean
(
mask
)
+
EPS
)
label
.
stop_gradient
=
True
mask
.
stop_gradient
=
True
return
avg_loss
def
get_pred_score_map
(
logit
):
"""
Get prediction and score map output in inference phase.
Args:
logit (tensor): output logit of network
Returns:
pred (tensor): predition map
score_map (tensor): score map
"""
score_map
=
F
.
softmax
(
logit
,
axis
=
1
)
score_map
=
fluid
.
layers
.
transpose
(
score_map
,
[
0
,
2
,
3
,
1
])
pred
=
fluid
.
layers
.
argmax
(
score_map
,
axis
=
3
)
pred
=
fluid
.
layers
.
unsqueeze
(
pred
,
axes
=
[
3
])
return
pred
,
score_map
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录