Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8c75b255
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
8c75b255
编写于
11月 18, 2020
作者:
X
xiaoting
提交者:
GitHub
11月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support Tensor for attr_scale and attr_size (#28677)
* update interpolate, test=develop * fix coverage, test=develop
上级
e880c90c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
77 addition
and
4 deletion
+77
-4
python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
...addle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
+63
-0
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+14
-4
未找到文件。
python/paddle/fluid/tests/unittests/test_bilinear_interp_v2_op.py
浏览文件 @
8c75b255
...
...
@@ -623,5 +623,68 @@ class TestBilinearInterpOpAPI_dy(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
expect_res
))
class
TestBilinearInterpOpAPI_dy2
(
unittest
.
TestCase
):
def
test_case
(
self
):
import
paddle
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
else
:
place
=
core
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
place
):
input_data
=
np
.
random
.
random
((
2
,
3
,
6
,
6
)).
astype
(
"float32"
)
size_np
=
np
.
array
([
12
,
12
]).
astype
(
"int64"
)
input_x
=
paddle
.
to_tensor
(
input_data
)
size
=
paddle
.
to_tensor
(
size_np
)
expect_res
=
bilinear_interp_np
(
input_data
,
out_h
=
12
,
out_w
=
12
,
align_corners
=
False
)
out
=
interpolate
(
x
=
input_x
,
size
=
size
,
mode
=
"bilinear"
,
align_corners
=
False
)
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
expect_res
))
class
TestBilinearInterpOpAPI_dy3
(
unittest
.
TestCase
):
def
test_case
(
self
):
import
paddle
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
else
:
place
=
core
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
place
):
input_data
=
np
.
random
.
random
((
2
,
3
,
6
,
6
)).
astype
(
"float32"
)
size_1
=
np
.
array
([
12
]).
astype
(
"int64"
)
input_x
=
paddle
.
to_tensor
(
input_data
)
size
=
paddle
.
to_tensor
(
size_1
)
expect_res
=
bilinear_interp_np
(
input_data
,
out_h
=
12
,
out_w
=
12
,
align_corners
=
False
)
out
=
interpolate
(
x
=
input_x
,
size
=
[
size
,
size
],
mode
=
"bilinear"
,
align_corners
=
False
)
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
expect_res
))
class
TestBilinearInterpOpAPI_dy4
(
unittest
.
TestCase
):
def
test_case
(
self
):
import
paddle
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
else
:
place
=
core
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
place
):
input_data
=
np
.
random
.
random
((
2
,
3
,
6
,
6
)).
astype
(
"float32"
)
scale_np
=
np
.
array
([
2
,
2
]).
astype
(
"int64"
)
input_x
=
paddle
.
to_tensor
(
input_data
)
scale
=
paddle
.
to_tensor
(
scale_np
)
expect_res
=
bilinear_interp_np
(
input_data
,
out_h
=
12
,
out_w
=
12
,
align_corners
=
False
)
out
=
interpolate
(
x
=
input_x
,
scale_factor
=
scale
,
mode
=
"bilinear"
,
align_corners
=
False
)
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
expect_res
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/nn/functional/common.py
浏览文件 @
8c75b255
...
...
@@ -366,10 +366,18 @@ def interpolate(x,
if
out_shape
is
not
None
and
scale
is
not
None
:
raise
ValueError
(
"Only one of size or scale_factor should be defined."
)
if
out_shape
is
not
None
:
if
isinstance
(
out_shape
,
Variable
):
if
isinstance
(
out_shape
,
Variable
)
and
not
in_dygraph_mode
():
out_shape
.
stop_gradient
=
True
inputs
[
'OutSize'
]
=
out_shape
else
:
if
in_dygraph_mode
():
if
isinstance
(
out_shape
,
Variable
):
out_shape
=
list
(
out_shape
.
numpy
())
for
i
,
dim
in
enumerate
(
out_shape
):
if
isinstance
(
dim
,
Variable
):
out_shape
[
i
]
=
dim
.
numpy
()[
0
]
if
not
(
_is_list_or_turple_
(
out_shape
)):
raise
TypeError
(
"size should be a list or tuple or Variable."
)
# Validate the shape
...
...
@@ -435,6 +443,8 @@ def interpolate(x,
attrs
[
'out_w'
]
=
out_shape
[
2
]
else
:
if
in_dygraph_mode
()
and
isinstance
(
scale
,
Variable
):
scale
=
list
(
scale
.
numpy
())
if
isinstance
(
scale
,
Variable
):
scale
.
stop_gradient
=
True
inputs
[
"Scale"
]
=
scale
...
...
@@ -1240,7 +1250,7 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]]
# example 2
x_shape = (1, 1, 2, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
...
...
@@ -1364,7 +1374,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8):
Examples:
.. code-block:: text
Case 0:
x1 = [[0.8024077 0.9927354 0.27238318 0.8344984 ]
[0.48949873 0.5797396 0.65444374 0.66510963]
...
...
@@ -1380,7 +1390,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8):
Code Examples:
.. code-block:: python
import paddle
import paddle.nn as nn
import numpy as np
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录