Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5f36e775
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
5f36e775
编写于
11月 23, 2022
作者:
V
Vvsmile
提交者:
GitHub
11月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove API: dice_loss (#47933)
remove dice_loss which is not used in paddle 2.0
上级
9666979d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
0 addition
and
121 deletion
+0
-121
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+0
-48
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+0
-32
python/paddle/fluid/tests/unittests/test_nn_dice_loss.py
python/paddle/fluid/tests/unittests/test_nn_dice_loss.py
+0
-41
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
5f36e775
...
...
@@ -107,7 +107,6 @@ __all__ = [
'label_smooth'
,
'roi_pool'
,
'roi_align'
,
'dice_loss'
,
'image_resize'
,
'image_resize_short'
,
'resize_linear'
,
...
...
@@ -6270,53 +6269,6 @@ def roi_align(
return
align_out
def
dice_loss
(
input
,
label
,
epsilon
=
0.00001
,
name
=
None
):
r
"""
Dice loss for comparing the similarity between the input predictions and the label.
This implementation is for binary classification, where the input is sigmoid
predictions of each pixel, usually used for segmentation task. The dice loss can
be defined as the following equation:
.. math::
dice\_loss &= 1 - \frac{2 * intersection\_area}{total\_area} \\
&= \frac{(total\_area - intersection\_area) - intersection\_area}{total\_area} \\
&= \frac{(union\_area - intersection\_area)}{total\_area}
Parameters:
input (Tensor): Tensor, rank>=2, shape is :math:`[N_1, N_2, ..., N_k, D]`, where :math:`N_1` is
the batch_size, :math:`D` is the number of categories. It is usually the output
predictions of sigmoid activation. The data type can be float32 or float64.
label (Tensor): Tensor, the groud truth with the same rank as input, shape is :math:`[N_1, N_2, ..., N_k, 1]`.
where :math:`N_1` is the batch_size. The data type can be int32 or int64.
epsilon (float): The epsilon will be added to the numerator and denominator.
If both input and label are empty, it makes sure dice is 1.
Default: 0.00001
name(str, optional): The default value is None.
Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name`
Returns:
Tensor, which shape is [1], data type is the same as `input` .
Example:
.. code-block:: python
import paddle
import paddle.nn.functional as F
x = paddle.randn((3,224,224,2))
label = paddle.randint(high=2, shape=(3,224,224,1))
predictions = F.softmax(x)
loss = F.dice_loss(input=predictions, label=label)
"""
return
paddle
.
nn
.
functional
.
dice_loss
(
input
,
label
,
epsilon
=
epsilon
,
name
=
name
)
def
image_resize
(
input
,
out_shape
=
None
,
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
5f36e775
...
...
@@ -4141,38 +4141,6 @@ class TestBook(LayerTest):
np
.
testing
.
assert_array_equal
(
static_res
,
dy_eager_res_value
)
np
.
testing
.
assert_array_equal
(
static_res
,
dy_res_value
)
def
test_dice_loss
(
self
):
num_classes
=
4
eps
=
1e-6
input_np
=
np
.
random
.
rand
(
2
,
3
,
num_classes
).
astype
(
'float32'
)
label_np
=
np
.
random
.
randint
(
0
,
num_classes
,
[
2
,
3
,
1
],
dtype
=
np
.
int64
)
with
self
.
static_graph
():
input_
=
layers
.
data
(
name
=
"input"
,
shape
=
[
None
,
3
,
num_classes
],
dtype
=
"float32"
)
label_
=
layers
.
data
(
name
=
"label"
,
shape
=
[
None
,
3
,
1
],
dtype
=
"int64"
)
output
=
layers
.
dice_loss
(
input_
,
label_
,
eps
)
static_res
=
self
.
get_static_graph_result
(
feed
=
{
'input'
:
input_np
,
'label'
:
label_np
},
fetch_list
=
[
output
]
)[
0
]
with
self
.
dynamic_graph
():
with
_test_eager_guard
():
input_
=
base
.
to_variable
(
input_np
)
label_
=
base
.
to_variable
(
label_np
)
dy_eager_res
=
layers
.
dice_loss
(
input_
,
label_
,
eps
)
dy_eager_res_value
=
dy_eager_res
.
numpy
()
input_
=
base
.
to_variable
(
input_np
)
label_
=
base
.
to_variable
(
label_np
)
dy_res
=
layers
.
dice_loss
(
input_
,
label_
,
eps
)
dy_res_value
=
dy_res
.
numpy
()
np
.
testing
.
assert_array_equal
(
static_res
,
dy_res_value
)
np
.
testing
.
assert_array_equal
(
static_res
,
dy_eager_res_value
)
def
test_roi_perspective_transform
(
self
):
# TODO(minqiyang): dygraph do not support lod now
with
self
.
static_graph
():
...
...
python/paddle/fluid/tests/unittests/test_nn_dice_loss.py
浏览文件 @
5f36e775
...
...
@@ -13,51 +13,10 @@
# limitations under the License.
import
unittest
import
numpy
as
np
import
paddle
import
paddle.fluid.layers.nn
as
nn
num_classes
=
4
eps
=
1e-6
class
TestDiceLossValue
(
unittest
.
TestCase
):
def
test_dice_loss
(
self
):
input_
=
paddle
.
rand
([
2
,
3
,
num_classes
])
label_
=
paddle
.
randint
(
0
,
num_classes
,
[
2
,
3
,
1
],
dtype
=
paddle
.
int64
)
input_np
,
label_np
=
input_
.
numpy
(),
label_
.
numpy
()
eye_np
=
np
.
eye
(
num_classes
)
label_np
=
np
.
float32
(
eye_np
[
np
.
squeeze
(
label_np
)])
input_np
=
np
.
reshape
(
input_np
,
[
2
,
-
1
])
label_np
=
np
.
reshape
(
label_np
,
[
2
,
-
1
])
intersection_np
=
np
.
sum
(
input_np
*
label_np
,
axis
=-
1
)
union_np
=
input_np
.
sum
(
-
1
)
+
label_np
.
sum
(
-
1
)
dice_np
=
np
.
mean
(
1
-
2
*
intersection_np
/
(
union_np
+
eps
))
dice_paddle
=
nn
.
dice_loss
(
input_
,
label_
,
eps
)
np
.
testing
.
assert_allclose
(
dice_np
,
dice_paddle
.
numpy
(),
rtol
=
1e-05
)
class
TestDiceLossInvalidInput
(
unittest
.
TestCase
):
def
test_error
(
self
):
def
test_invalid_dtype
():
input_
=
paddle
.
rand
([
2
,
3
,
num_classes
],
dtype
=
paddle
.
float32
)
label_
=
paddle
.
randint
(
0
,
num_classes
,
[
2
,
3
,
1
],
dtype
=
paddle
.
int64
)
nn
.
dice_loss
(
input_
,
label_
.
astype
(
paddle
.
float32
))
self
.
assertRaises
(
AssertionError
,
test_invalid_dtype
)
def
test_zero_shape_input
():
input_
=
paddle
.
rand
([
0
,
3
,
num_classes
],
dtype
=
paddle
.
float32
)
label_
=
paddle
.
randint
(
0
,
num_classes
,
[
0
,
3
,
1
],
dtype
=
paddle
.
int64
)
nn
.
dice_loss
(
input_
,
label_
)
self
.
assertRaises
(
AssertionError
,
test_zero_shape_input
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录