Publishing R5 content (#72)
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / shape_infer / built-in / ie_argmax_shape_infer.hpp
1 // Copyright (C) 2018 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
15 namespace InferenceEngine {
16 namespace ShapeInfer {
17
18 /**
19  *@brief Implementation of Shape inference for ArgMax layer
20  */
21 class ArgMaxShapeProp : public BuiltInShapeInferImpl {
22 public:
23     explicit ArgMaxShapeProp(const std::string& type) : BuiltInShapeInferImpl(type) {}
24
25     void inferShapesImpl(const std::vector<SizeVector>& inShapes,
26                          const std::map<std::string, std::string>& params,
27                          const std::map<std::string, Blob::Ptr>& blobs,
28                          std::vector<SizeVector>& outShapes) override {
29         LayerParams lp{};
30         CNNLayer cnnLayer(lp);
31         cnnLayer.params = params;
32         cnnLayer.type = _type;
33         validate(&cnnLayer, inShapes, params, blobs);
34         auto out_max_val = static_cast<size_t>(cnnLayer.GetParamAsInt("out_max_val", 0));
35         auto top_k = static_cast<size_t>(cnnLayer.GetParamAsInt("top_k", 0));
36         int axis = 0;
37         bool isValidAxis = true;
38         try {
39             axis = cnnLayer.GetParamAsInt("axis");
40         } catch(const details::InferenceEngineException &exception) {
41             isValidAxis = false;
42         }
43
44         auto firstInputShape = inShapes[0];
45         size_t num_top_axes = firstInputShape.size();
46         if (num_top_axes < 3) num_top_axes = 3;
47
48         SizeVector outputShape(num_top_axes, 1);
49         if (isValidAxis) {
50             if (axis < 0) {
51                 axis = static_cast<int>(firstInputShape.size() + axis);
52             }
53             outputShape = firstInputShape;
54             outputShape[axis] = top_k;
55         } else {
56             outputShape[0] = firstInputShape[0];
57             outputShape[2] = top_k;
58             if (out_max_val) {
59                 outputShape[1] = 2;
60             }
61         }
62         outShapes.push_back(outputShape);
63     }
64 };
65
66 }  // namespace ShapeInfer
67 }  // namespace InferenceEngine