Add AdaptivePool to ngraph python API (#6579)

* Add AdaptivePool to ngraph python API

* Disable tests until CPU implementation is ready

* Change ticket number

* Add create op tests

* Fix quotes

* Fix typo
This commit is contained in:
Maxim Vafin 2021-07-15 18:03:51 +03:00 committed by GitHub
parent cd3d48f98f
commit e25bb7e6ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 0 deletions

View File

@ -32,6 +32,8 @@ from ngraph.opset8 import absolute
from ngraph.opset8 import absolute as abs
from ngraph.opset8 import acos
from ngraph.opset8 import acosh
from ngraph.opset8 import adaptive_avg_pool
from ngraph.opset8 import adaptive_max_pool
from ngraph.opset8 import add
from ngraph.opset8 import asin
from ngraph.opset8 import asinh

View File

@ -5,6 +5,8 @@ from ngraph.opset1.ops import absolute
from ngraph.opset1.ops import absolute as abs
from ngraph.opset1.ops import acos
from ngraph.opset4.ops import acosh
from ngraph.opset8.ops import adaptive_avg_pool
from ngraph.opset8.ops import adaptive_max_pool
from ngraph.opset1.ops import add
from ngraph.opset1.ops import asin
from ngraph.opset4.ops import asinh

View File

@ -101,6 +101,43 @@ def deformable_convolution(
)
@nameable_op
def adaptive_avg_pool(
data: NodeInput,
output_shape: NodeInput
) -> Node:
"""Return a node which performs AdaptiveAvgPool operation.
@param data: The list of input nodes
@param output_shape: the shape of spatial dimentions after operation
@return: The new node performing AdaptiveAvgPool operation on the data
"""
inputs = as_nodes(data, output_shape)
return _get_node_factory_opset8().create("AdaptiveAvgPool", inputs)
@nameable_op
def adaptive_max_pool(
data: NodeInput,
output_shape: NodeInput,
index_element_type: str = "i64"
) -> Node:
"""Return a node which performs AdaptiveMaxPool operation.
@param data: The list of input nodes
@param output_shape: the shape of spatial dimentions after operation
@param index_element_type: Type of indices output.
@return: The new node performing AdaptiveMaxPool operation on the data
"""
inputs = as_nodes(data, output_shape)
attributes = {
"index_element_type": index_element_type,
}
return _get_node_factory_opset8().create("AdaptiveMaxPool", inputs, attributes)
@nameable_op
def multiclass_nms(
boxes: NodeInput,

View File

@ -145,3 +145,5 @@ xfail_issue_52463 = xfail_test(reason="test_operator_add_size1_singleton_broadca
xfail_issue_58033 = xfail_test(reason="Einsum operation misses support for complex ellipsis equations")
xfail_issue_58676 = xfail_test(reason="AssertionError: Not equal to tolerance rtol=0.001, atol=1e-07")
xfail_issue_onnx_models_140 = xfail_test(reason="https://github.com/onnx/models/issues/140")
xfail_issue_59935 = xfail_test(reason="AdaptivePool is not implemented in CPU plugin.")

View File

@ -0,0 +1,66 @@
import ngraph as ng
import numpy as np
from tests import xfail_issue_59935
from tests.runtime import get_runtime
@xfail_issue_59935
def test_adaptive_avg_pool():
runtime = get_runtime()
input = np.reshape([0, 4, 1, 3, -2, -5, -2,
-2, 1, -3, 1, -3, -4, 0,
-2, 1, -1, -2, 3, -1, -3,
-1, -2, 3, 4, -3, -4, 1,
2, 0, -4, -5, -2, -2, -3,
2, 3, 1, -5, 2, -4, -2], (2, 3, 7))
input_tensor = ng.constant(input)
output_shape = ng.constant(np.array([3], dtype=np.int32))
adaptive_pool_node = ng.adaptive_avg_pool(input_tensor, output_shape)
computation = runtime.computation(adaptive_pool_node)
adaptive_pool_results = computation()
expected_results = np.reshape([1.66666663, 0.66666669, -3.,
-1.33333337, -1.66666663, -2.33333325,
-0.66666669, 0., -0.33333334,
0., 1.33333337, -2.,
-0.66666669, -3.66666675, -2.33333325,
2., -0.66666669, -1.33333337], (2, 3, 3))
assert np.allclose(adaptive_pool_results, expected_results)
@xfail_issue_59935
def test_adaptive_max_pool():
runtime = get_runtime()
input = np.reshape([0, 4, 1, 3, -2, -5, -2,
-2, 1, -3, 1, -3, -4, 0,
-2, 1, -1, -2, 3, -1, -3,
-1, -2, 3, 4, -3, -4, 1,
2, 0, -4, -5, -2, -2, -3,
2, 3, 1, -5, 2, -4, -2], (2, 3, 7))
input_tensor = ng.constant(input)
output_shape = ng.constant(np.array([3], dtype=np.int32))
adaptive_pool_node = ng.adaptive_max_pool(input_tensor, output_shape)
computation = runtime.computation(adaptive_pool_node)
adaptive_pool_results = computation()
expected_results = np.reshape([4, 3, -2,
1, 1, 0,
1, 3, 3,
3, 4, 1,
2, -2, -2,
3, 2, 2], (2, 3, 3))
expected_indices = np.reshape([1, 3, 4,
1, 3, 6,
1, 4, 4,
2, 3, 6,
0, 4, 4,
1, 4, 4], (2, 3, 3))
assert np.allclose(adaptive_pool_results, [expected_results, expected_indices])

View File

@ -23,6 +23,33 @@ integral_np_types = [
]
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_adaptive_avg_pool(dtype):
data = ng.parameter([2, 24, 34, 62], name="input", dtype=dtype)
output_shape = ng.constant(np.array([16, 16], dtype=np.int32))
node = ng.adaptive_avg_pool(data, output_shape)
assert node.get_type_name() == "AdaptiveAvgPool"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [2, 24, 16, 16]
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
@pytest.mark.parametrize("ind_type", ["i32", "i64"])
def test_adaptive_max_pool(dtype, ind_type):
data = ng.parameter([2, 24, 34, 62], name="input", dtype=dtype)
output_shape = ng.constant(np.array([16, 16], dtype=np.int32))
node = ng.adaptive_max_pool(data, output_shape, ind_type)
assert node.get_type_name() == "AdaptiveMaxPool"
assert node.get_output_size() == 2
assert list(node.get_output_shape(0)) == [2, 24, 16, 16]
assert list(node.get_output_shape(1)) == [2, 24, 16, 16]
assert node.get_output_element_type(1) == Type.i32 if ind_type == "i32" else Type.i64
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_binary_convolution(dtype):
strides = np.array([1, 1])