Release 18.05.01
[platform/upstream/armnn.git] / src / armnn / layers / Pooling2dLayer.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "Pooling2dLayer.hpp"
6
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <backends/WorkloadData.hpp>
11 #include <backends/WorkloadFactory.hpp>
12
13 namespace armnn
14 {
15
16 Pooling2dLayer::Pooling2dLayer(const Pooling2dDescriptor& param, const char* name)
17     : LayerWithParameters(1, 1, LayerType::Pooling2d, param, name)
18 {
19 }
20
21 std::unique_ptr<IWorkload> Pooling2dLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
22 {
23     Pooling2dQueueDescriptor descriptor;
24     return factory.CreatePooling2d(descriptor, PrepInfoAndDesc(descriptor, graph));
25 }
26
27 Pooling2dLayer* Pooling2dLayer::Clone(Graph& graph) const
28 {
29     return CloneBase<Pooling2dLayer>(graph, m_Param, GetName());
30 }
31
32 void Pooling2dLayer::ValidateTensorShapesFromInputs()
33 {
34     ConditionalThrow<LayerValidationException>(GetInputSlot(0).GetConnection() != nullptr,
35                      "Pooling2dLayer: InputSlot must be connected to an OutputSlot");
36     ConditionalThrow<LayerValidationException>(GetInputSlot(0).GetConnection()->IsTensorInfoSet(),
37                      "Pooling2dLayer: TensorInfo must be set on connected InputSlot.");
38
39     IOutputSlot* input = GetInputSlot(0).GetConnection();
40     const TensorShape& inputShape = input->GetTensorInfo().GetShape();
41
42     // If we support multiple batch dimensions in the future, then this assert will need to change.
43     BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Pooling2dLayer will always have 4D input.");
44
45
46     unsigned int inWidth = inputShape[3];
47     unsigned int inHeight = inputShape[2];
48     unsigned int inChannels = inputShape[1];
49     unsigned int inBatchSize = inputShape[0];
50
51     bool isGlobalPooling = (m_Param.m_StrideX==0 && m_Param.m_StrideY==0);
52     unsigned int outWidth = 1;
53     unsigned int outHeight = 1;
54     if (!isGlobalPooling)
55     {
56         BOOST_ASSERT_MSG(m_Param.m_StrideX!=0 && m_Param.m_StrideY!=0,
57                          "Stride can only be zero when performing global pooling");
58
59         auto CalcSize = [](auto inSize, auto lowPad, auto highPad, auto poolSize, auto stride, auto padMethod,
60                            auto outputShapeRounding)
61             {
62                 unsigned int readSize = inSize + lowPad + highPad - poolSize;
63                 float div = static_cast<float>(readSize) / static_cast<float>(stride);
64
65                 unsigned int size = 0;
66                 switch (outputShapeRounding)
67                 {
68                     case OutputShapeRounding::Ceiling:
69                         size = static_cast<unsigned int>(ceil(div)) + 1;
70                         break;
71                     case OutputShapeRounding ::Floor:
72                         size = static_cast<unsigned int>(floor(div)) + 1;
73                         break;
74                     default:
75                         BOOST_ASSERT_MSG(false, "Unsupported Output Shape Rounding");
76                 }
77
78                 // Make sure that border operations will start from inside the input and not the padded area
79                 // This is what both Caffe and CL does...
80                 if ((size - 1)*stride >= inSize + lowPad)
81                 {
82                     --size;
83                 }
84
85                 return size;
86             };
87
88         outWidth = CalcSize(inWidth, m_Param.m_PadLeft, m_Param.m_PadRight, m_Param.m_PoolWidth, m_Param.m_StrideX,
89                             m_Param.m_PaddingMethod, m_Param.m_OutputShapeRounding);
90         outHeight= CalcSize(inHeight, m_Param.m_PadTop, m_Param.m_PadBottom, m_Param.m_PoolHeight, m_Param.m_StrideY,
91                             m_Param.m_PaddingMethod, m_Param.m_OutputShapeRounding);
92
93
94     }
95     unsigned int outChannels = inChannels;
96     unsigned int outBatchSize = inBatchSize;
97
98     TensorShape shapeOut({outBatchSize, outChannels, outHeight, outWidth});
99
100     ConditionalThrowIfNotEqual<LayerValidationException>(
101         "Pooling2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
102         GetOutputSlot(0).GetTensorInfo().GetShape(),
103         shapeOut);
104 }
105
106 } // namespace armnn