Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / shape_infer / built-in / ie_flatten_shape_infer.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <description_buffer.hpp>
8 #include "ie_built_in_impl.hpp"
9 #include <ie_layers.h>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 #include <debug.h>
15 #include <functional>
16
17 namespace InferenceEngine {
18 namespace ShapeInfer {
19
20 /**
21  *@brief Implementation of Shape inference for Reshape layer
22  */
23 class FlattenShapeProp : public BuiltInShapeInferImpl {
24 public:
25     explicit FlattenShapeProp(const std::string &type) : BuiltInShapeInferImpl(type) {}
26
27     void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs,
28                          const std::map<std::string, std::string>& params,
29                          const std::map<std::string, Blob::Ptr>& blobs,
30                          std::vector<SizeVector>& outShapes) override {
31         LayerParams lp{};
32         ReshapeLayer reshapeLayer(lp);
33         reshapeLayer.params = params;
34         reshapeLayer.type = _type;
35         validate(&reshapeLayer, inBlobs, params, blobs);
36
37         auto inputShape = inShapes[0];
38         size_t inputShapeTotal = std::accumulate(inputShape.begin(), inputShape.end(), 1lu, std::multiplies<size_t>());
39         SizeVector outShape;
40
41         int numAxes = reshapeLayer.num_axes;
42         int axis = reshapeLayer.axis;
43         size_t notFlatten = 1;
44         if (numAxes == -1 && axis == 0) {
45             outShape = {inputShapeTotal};
46         } else {
47             if (axis > 0) {
48                 for (int i = 0; i < axis; i++) {
49                     notFlatten *= inputShape[i];
50                     outShape.push_back(inputShape[i]);
51                 }
52             }
53             outShape.push_back(1);
54             if (numAxes > 0) {
55                 for (int i = numAxes + 1; i < inputShape.size(); i++) {
56                     notFlatten *= inputShape[i];
57                     outShape.push_back(inputShape[i]);
58                 }
59             }
60             outShape[axis] = inputShapeTotal / notFlatten;
61         }
62
63         outShapes.emplace_back(outShape);
64     }
65 };
66
67 }  // namespace ShapeInfer
68 }  // namespace InferenceEngine