-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API 2.0] add pool2d3d API,test=develop #26331
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
02edddb
add pool2d3d API,test=develop
LDOUBLEV 94614cb
add api unitest,test=develop
LDOUBLEV 086225d
fix unittest, test=develop
LDOUBLEV b5389ec
fix reviews, test=develop
LDOUBLEV 4244206
return one element when return indices is true, test=develop
LDOUBLEV 54039d9
fix low converage; to_variable to to_tensor, test=develop
LDOUBLEV d54ba32
sort API params, test=develop
LDOUBLEV 550c260
fix conflicts, test=develop
LDOUBLEV a89c65f
fix en doc, merge PR#26108 to here, test=develop
LDOUBLEV a6d7806
fix en doc, test=develop
LDOUBLEV 149b466
fix conflicts
LDOUBLEV c581fc8
fix conflicts, test=develop
LDOUBLEV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# 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. | ||
|
||
from test_pool2d_op import adaptive_start_index, adaptive_end_index, pool2D_forward_naive | ||
import unittest | ||
from op_test import OpTest | ||
import numpy as np | ||
import paddle.fluid.core as core | ||
from paddle.nn.functional import * | ||
import paddle.fluid as fluid | ||
import paddle | ||
|
||
|
||
class TestPool2d_API(unittest.TestCase): | ||
def setUp(self): | ||
np.random.seed(123) | ||
self.places = [fluid.CPUPlace()] | ||
if core.is_compiled_with_cuda(): | ||
self.places.append(fluid.CUDAPlace(0)) | ||
|
||
def check_avg_static_results(self, place): | ||
with fluid.program_guard(fluid.Program(), fluid.Program()): | ||
input = fluid.data( | ||
name="input", shape=[2, 3, 32, 32], dtype="float32") | ||
result = avg_pool2d(input=input, kernel_size=2, stride=2, padding=0) | ||
|
||
input_np = np.random.random([2, 3, 32, 32]).astype("float32") | ||
result_np = pool2D_forward_naive( | ||
input_np, | ||
ksize=[2, 2], | ||
strides=[2, 2], | ||
paddings=[0, 0], | ||
pool_type='avg') | ||
|
||
exe = fluid.Executor(place) | ||
fetches = exe.run(fluid.default_main_program(), | ||
feed={"input": input_np}, | ||
fetch_list=[result]) | ||
self.assertTrue(np.allclose(fetches[0], result_np)) | ||
|
||
def check_avg_dygraph_results(self, place): | ||
with fluid.dygraph.guard(place): | ||
input_np = np.random.random([2, 3, 32, 32]).astype("float32") | ||
input = fluid.dygraph.to_variable(input_np) | ||
result = avg_pool2d(input, kernel_size=2, stride=2, padding=0) | ||
|
||
result_np = pool2D_forward_naive( | ||
input_np, | ||
ksize=[2, 2], | ||
strides=[2, 2], | ||
paddings=[0, 0], | ||
pool_type='avg') | ||
|
||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
avg_pool2d_dg = paddle.nn.layer.AvgPool2d( | ||
kernel_size=2, stride=2, padding=0) | ||
result = avg_pool2d_dg(input) | ||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
def check_max_static_results(self, place): | ||
with fluid.program_guard(fluid.Program(), fluid.Program()): | ||
input = fluid.data( | ||
name="input", shape=[2, 3, 32, 32], dtype="float32") | ||
result = max_pool2d(input=input, kernel_size=2, stride=2, padding=0) | ||
|
||
input_np = np.random.random([2, 3, 32, 32]).astype("float32") | ||
result_np = pool2D_forward_naive( | ||
input_np, | ||
ksize=[2, 2], | ||
strides=[2, 2], | ||
paddings=[0, 0], | ||
pool_type='max') | ||
|
||
exe = fluid.Executor(place) | ||
fetches = exe.run(fluid.default_main_program(), | ||
feed={"input": input_np}, | ||
fetch_list=[result]) | ||
self.assertTrue(np.allclose(fetches[0], result_np)) | ||
|
||
def check_max_dygraph_results(self, place): | ||
with fluid.dygraph.guard(place): | ||
input_np = np.random.random([2, 3, 32, 32]).astype("float32") | ||
input = fluid.dygraph.to_variable(input_np) | ||
result = max_pool2d(input, kernel_size=2, stride=2, padding=0) | ||
|
||
result_np = pool2D_forward_naive( | ||
input_np, | ||
ksize=[2, 2], | ||
strides=[2, 2], | ||
paddings=[0, 0], | ||
pool_type='max') | ||
|
||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
max_pool2d_dg = paddle.nn.layer.MaxPool2d( | ||
kernel_size=2, stride=2, padding=0) | ||
result = max_pool2d_dg(input) | ||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
def test_pool2d(self): | ||
for place in self.places: | ||
|
||
self.check_max_dygraph_results(place) | ||
self.check_avg_dygraph_results(place) | ||
self.check_max_static_results(place) | ||
self.check_avg_static_results(place) | ||
|
||
|
||
class TestPool2dError_API(unittest.TestCase): | ||
def test_error_api(self): | ||
def run1(): | ||
with fluid.dygraph.guard(): | ||
input_np = np.random.uniform(-1, 1, | ||
[2, 3, 32, 32]).astype(np.float32) | ||
res_pd = avg_pool2d( | ||
input_np, kernel_size=2, stride=2, padding=0) | ||
|
||
def run2(): | ||
with fluid.dygraph.guard(): | ||
input_np = np.random.uniform(-1, 1, | ||
[2, 3, 32, 32]).astype(np.uint8) | ||
input_pd = fluid.dygraph.to_variable(input_np) | ||
res_pd = avg_pool2d( | ||
input_pd, kernel_size=2, stride=2, padding=0) | ||
|
||
def run3(): | ||
with fluid.dygraph.guard(): | ||
input_np = np.random.uniform(-1, 1, | ||
[2, 3, 32, 32]).astype(np.float32) | ||
res_pd = max_pool2d( | ||
input_np, kernel_size=2, stride=2, padding=0) | ||
|
||
def run4(): | ||
with fluid.dygraph.guard(): | ||
input_np = np.random.uniform(-1, 1, | ||
[2, 3, 32, 32]).astype(np.uint8) | ||
input_pd = fluid.dygraph.to_variable(input_np) | ||
res_pd = max_pool2d( | ||
input_pd, kernel_size=2, stride=2, padding=0) | ||
|
||
#self.assertRaises(ValueError, run1) | ||
#self.assertRaises(TypeError, run2) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# 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. | ||
|
||
from __future__ import print_function | ||
from __future__ import division | ||
|
||
import unittest | ||
import numpy as np | ||
import paddle | ||
import paddle.fluid.core as core | ||
from op_test import OpTest | ||
import paddle.fluid as fluid | ||
from paddle.nn.functional import avg_pool3d, max_pool3d | ||
from test_pool3d_op import adaptive_start_index, adaptive_end_index, pool3D_forward_naive | ||
|
||
|
||
class TestPool3d_API(unittest.TestCase): | ||
def setUp(self): | ||
np.random.seed(123) | ||
self.places = [fluid.CPUPlace()] | ||
if core.is_compiled_with_cuda(): | ||
self.places.append(fluid.CUDAPlace(0)) | ||
|
||
def check_avg_static_results(self, place): | ||
with fluid.program_guard(fluid.Program(), fluid.Program()): | ||
input = fluid.data( | ||
name="input", shape=[2, 3, 32, 32, 32], dtype="float32") | ||
result = avg_pool3d(input=input, kernel_size=2, stride=2, padding=0) | ||
|
||
input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") | ||
result_np = pool3D_forward_naive( | ||
input_np, | ||
ksize=[2, 2, 2], | ||
strides=[2, 2, 2], | ||
paddings=[0, 0, 0], | ||
pool_type='avg') | ||
|
||
exe = fluid.Executor(place) | ||
fetches = exe.run(fluid.default_main_program(), | ||
feed={"input": input_np}, | ||
fetch_list=[result]) | ||
self.assertTrue(np.allclose(fetches[0], result_np)) | ||
|
||
def check_avg_dygraph_results(self, place): | ||
with fluid.dygraph.guard(place): | ||
input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") | ||
input = fluid.dygraph.to_variable(input_np) | ||
result = avg_pool3d(input, kernel_size=2, stride=2, padding=0) | ||
|
||
result_np = pool3D_forward_naive( | ||
input_np, | ||
ksize=[2, 2, 2], | ||
strides=[2, 2, 2], | ||
paddings=[0, 0, 0], | ||
pool_type='avg') | ||
|
||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
avg_pool3d_dg = paddle.nn.layer.AvgPool3d( | ||
kernel_size=2, stride=2, padding=0) | ||
result = avg_pool3d_dg(input) | ||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
def check_max_static_results(self, place): | ||
with fluid.program_guard(fluid.Program(), fluid.Program()): | ||
input = fluid.data( | ||
name="input", shape=[2, 3, 32, 32, 32], dtype="float32") | ||
result = max_pool3d(input=input, kernel_size=2, stride=2, padding=0) | ||
|
||
input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") | ||
result_np = pool3D_forward_naive( | ||
input_np, | ||
ksize=[2, 2, 2], | ||
strides=[2, 2, 2], | ||
paddings=[0, 0, 0], | ||
pool_type='max') | ||
|
||
exe = fluid.Executor(place) | ||
fetches = exe.run(fluid.default_main_program(), | ||
feed={"input": input_np}, | ||
fetch_list=[result]) | ||
self.assertTrue(np.allclose(fetches[0], result_np)) | ||
|
||
def check_max_dygraph_results(self, place): | ||
with fluid.dygraph.guard(place): | ||
input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") | ||
input = fluid.dygraph.to_variable(input_np) | ||
result = max_pool3d(input, kernel_size=2, stride=2, padding=0) | ||
|
||
result_np = pool3D_forward_naive( | ||
input_np, | ||
ksize=[2, 2, 2], | ||
strides=[2, 2, 2], | ||
paddings=[0, 0, 0], | ||
pool_type='max') | ||
|
||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
max_pool3d_dg = paddle.nn.layer.MaxPool3d( | ||
kernel_size=2, stride=2, padding=0) | ||
result = max_pool3d_dg(input) | ||
self.assertTrue(np.allclose(result.numpy(), result_np)) | ||
|
||
def test_pool3d(self): | ||
for place in self.places: | ||
|
||
self.check_max_dygraph_results(place) | ||
self.check_avg_dygraph_results(place) | ||
self.check_max_static_results(place) | ||
self.check_avg_static_results(place) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上 #DEFINE_ALIAS 标示,做api映射
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done