Commit b255b211 authored by Ewa21's avatar Ewa21

[Py] Added depth_to_space operator to Python API.

parent 5ece6de2
...@@ -30,6 +30,7 @@ ngraph.ops ...@@ -30,6 +30,7 @@ ngraph.ops
convolution_backprop_data convolution_backprop_data
cos cos
cosh cosh
depth_to_space
divide divide
dot dot
elu elu
......
...@@ -43,6 +43,7 @@ from ngraph.ops import convolution ...@@ -43,6 +43,7 @@ from ngraph.ops import convolution
from ngraph.ops import convolution_backprop_data from ngraph.ops import convolution_backprop_data
from ngraph.ops import cos from ngraph.ops import cos
from ngraph.ops import cosh from ngraph.ops import cosh
from ngraph.ops import depth_to_space
from ngraph.ops import divide from ngraph.ops import divide
from ngraph.ops import dot from ngraph.ops import dot
from ngraph.ops import elu from ngraph.ops import elu
......
...@@ -67,6 +67,7 @@ from _pyngraph.op import ConvolutionBackpropData ...@@ -67,6 +67,7 @@ from _pyngraph.op import ConvolutionBackpropData
from _pyngraph.op import ConvolutionBackpropFilters from _pyngraph.op import ConvolutionBackpropFilters
from _pyngraph.op import Cos from _pyngraph.op import Cos
from _pyngraph.op import Cosh from _pyngraph.op import Cosh
from _pyngraph.op import DepthToSpace
from _pyngraph.op import Divide from _pyngraph.op import Divide
from _pyngraph.op import Dot from _pyngraph.op import Dot
from _pyngraph.op import Elu from _pyngraph.op import Elu
......
...@@ -22,11 +22,11 @@ from ngraph.impl import AxisSet, AxisVector, Coordinate, CoordinateDiff, Functio ...@@ -22,11 +22,11 @@ from ngraph.impl import AxisSet, AxisVector, Coordinate, CoordinateDiff, Functio
from ngraph.impl.op import Abs, Acos, Add, And, Asin, ArgMax, ArgMin, Atan, AvgPool, \ from ngraph.impl.op import Abs, Acos, Add, And, Asin, ArgMax, ArgMin, Atan, AvgPool, \
BatchNormTraining, BatchNormInference, Broadcast, Ceiling, Concat, Constant, Convert, \ BatchNormTraining, BatchNormInference, Broadcast, Ceiling, Concat, Constant, Convert, \
Convolution, ConvolutionBackpropData, Cos, Cosh, Divide, Dot, Elu, Equal, Exp, Floor, \ Convolution, ConvolutionBackpropData, Cos, Cosh, DepthToSpace, Divide, Dot, Elu, Equal, \
GetOutputElement, Greater, GreaterEq, Less, LessEq, Log, LRN, Max, Maximum, MaxPool, \ Exp, Floor, GetOutputElement, Greater, GreaterEq, Less, LessEq, Log, LRN, Max, Maximum, \
Min, Minimum, Multiply, Negative, Not, NotEqual, OneHot, Or, Pad, Parameter, Product, \ MaxPool, Min, Minimum, Multiply, Negative, Not, NotEqual, OneHot, Or, Pad, Parameter, \
Power, Relu, ReplaceSlice, Reshape, Reverse, Select, Sign, Sin, Sinh, Slice, Softmax, \ Product, Power, Relu, ReplaceSlice, Reshape, Reverse, Select, Sign, Sin, Sinh, Slice, \
Sqrt, Subtract, Sum, Tan, Tanh, TopK Softmax, Sqrt, Subtract, Sum, Tan, Tanh, TopK
from typing import Callable, Iterable, List, Union from typing import Callable, Iterable, List, Union
...@@ -527,6 +527,27 @@ def convert(node, new_type, name=None): # type: (Node, NumericType, str) -> Nod ...@@ -527,6 +527,27 @@ def convert(node, new_type, name=None): # type: (Node, NumericType, str) -> Nod
return Convert(node, new_element_type) return Convert(node, new_element_type)
@nameable_op
def depth_to_space(node, block_size, name=None): # type: (Node, int, str) -> Node
"""Rearranges input tensor from depth into blocks of spatial data.
Values from the height and width dimensions are moved to the depth dimension.
Input tensor has shape [N,C,H,W], where N is the batch axis, C is the channel or depth,
H is the height and W is the width.
Output node produces a tensor with shape:
[N, C * :code:`block_size` * :code:`block_size`, H / :code:`block_size`, W / :code:`block_size`]
:param node: The node with input tensor data.
:param block_size: The size of the block of value to be moved.
:param name: Optional output node name.
:return: The new node performing an DepthToSpace operation on its input tensor.
"""
return DepthToSpace(node, block_size)
@nameable_op @nameable_op
def select(selection_node, input_node1, input_node2, name=None): def select(selection_node, input_node1, input_node2, name=None):
# type: (Node, Node, Node, str) -> Node # type: (Node, Node, Node, str) -> Node
......
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// 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.
//*****************************************************************************
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "ngraph/op/fused/depth_to_space.hpp"
#include "pyngraph/ops/fused/depth_to_space.hpp"
namespace py = pybind11;
void regclass_pyngraph_op_DepthToSpace(py::module m)
{
py::class_<ngraph::op::DepthToSpace, std::shared_ptr<ngraph::op::DepthToSpace>, ngraph::op::Op>
depthtospace(m, "DepthToSpace");
depthtospace.doc() = "ngraph.impl.op.DepthToSpace wraps ngraph::op::DepthToSpace";
depthtospace.def(py::init<const std::shared_ptr<ngraph::Node>&, std::size_t&>());
}
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// 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.
//*****************************************************************************
#pragma once
#include <pybind11/pybind11.h>
namespace py = pybind11;
void regclass_pyngraph_op_DepthToSpace(py::module m);
...@@ -47,6 +47,7 @@ void regmodule_pyngraph_op(py::module m_op) ...@@ -47,6 +47,7 @@ void regmodule_pyngraph_op(py::module m_op)
regclass_pyngraph_op_ConvolutionBackpropFilters(m_op); regclass_pyngraph_op_ConvolutionBackpropFilters(m_op);
regclass_pyngraph_op_Cos(m_op); regclass_pyngraph_op_Cos(m_op);
regclass_pyngraph_op_Cosh(m_op); regclass_pyngraph_op_Cosh(m_op);
regclass_pyngraph_op_DepthToSpace(m_op);
regclass_pyngraph_op_Divide(m_op); regclass_pyngraph_op_Divide(m_op);
regclass_pyngraph_op_Dot(m_op); regclass_pyngraph_op_Dot(m_op);
regclass_pyngraph_op_Elu(m_op); regclass_pyngraph_op_Elu(m_op);
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "pyngraph/ops/equal.hpp" #include "pyngraph/ops/equal.hpp"
#include "pyngraph/ops/exp.hpp" #include "pyngraph/ops/exp.hpp"
#include "pyngraph/ops/floor.hpp" #include "pyngraph/ops/floor.hpp"
#include "pyngraph/ops/fused/depth_to_space.hpp"
#include "pyngraph/ops/get_output_element.hpp" #include "pyngraph/ops/get_output_element.hpp"
#include "pyngraph/ops/greater.hpp" #include "pyngraph/ops/greater.hpp"
#include "pyngraph/ops/greater_eq.hpp" #include "pyngraph/ops/greater_eq.hpp"
......
...@@ -177,6 +177,7 @@ sources = [ ...@@ -177,6 +177,7 @@ sources = [
'pyngraph/ops/cos.cpp', 'pyngraph/ops/cos.cpp',
'pyngraph/ops/cosh.cpp', 'pyngraph/ops/cosh.cpp',
'pyngraph/ops/ceiling.cpp', 'pyngraph/ops/ceiling.cpp',
'pyngraph/ops/fused/depth_to_space.cpp',
'pyngraph/ops/divide.cpp', 'pyngraph/ops/divide.cpp',
'pyngraph/ops/dot.cpp', 'pyngraph/ops/dot.cpp',
'pyngraph/ops/elu.cpp', 'pyngraph/ops/elu.cpp',
......
...@@ -67,3 +67,30 @@ def test_elu_operator_with_scalar(): ...@@ -67,3 +67,30 @@ def test_elu_operator_with_scalar():
result = computation(data_value) result = computation(data_value)
expected = np.array([[-2.9797862, 1.], [-2.5939941, 3.]], dtype=np.float32) expected = np.array([[-2.9797862, 1.], [-2.5939941, 3.]], dtype=np.float32)
assert np.allclose(result, expected) assert np.allclose(result, expected)
def test_depth_to_space():
runtime = get_runtime()
data_value = np.array([[[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]], dtype=np.float32)
block_size = np.float32(2)
data_shape = [1, 4, 2, 3]
parameter_data = ng.parameter(data_shape, name='Data', dtype=np.float32)
model = ng.depth_to_space(parameter_data, block_size)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array([[[[0, 6, 1, 7, 2, 8],
[12, 18, 13, 19, 14, 20],
[3, 9, 4, 10, 5, 11],
[15, 21, 16, 22, 17, 23]]]], dtype=np.float32)
assert np.allclose(result, expected)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment