Publishing R3
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / v2_layer_parsers.h
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #pragma once
7
8 #include <debug.h>
9 #include <memory>
10 #include "v2_format_parser.h"
11 #include "xml_parse_utils.h"
12 #include "range_iterator.hpp"
13 #include "caseless.hpp"
14 #include <vector>
15 #include <string>
16 #include <map>
17
18 inline pugi::xml_node GetChild(const pugi::xml_node& node, std::vector<std::string> tags, bool failIfMissing = true) {
19     for (auto tag : tags) {
20         pugi::xml_node dn = node.child(tag.c_str());
21         if (!dn.empty()) return dn;
22     }
23     if (failIfMissing)
24         THROW_IE_EXCEPTION << "missing <" << InferenceEngine::details::dumpVec(tags)
25                            << "> Tags at offset :" << node.offset_debug();
26     return pugi::xml_node();
27 }
28
29 using namespace XMLParseUtils;
30
31 namespace InferenceEngine {
32 namespace details {
33 template<class LT>
34 class V2LayerCreator : public BaseCreator {
35 public:
36     explicit V2LayerCreator(const std::string& type) : BaseCreator(type) {}
37
38     CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override {
39         auto res = std::make_shared<LT>(layerParsePrms.prms);
40
41         if (std::is_same<LT, FullyConnectedLayer>::value) {
42             layerChild[res->name] = {"fc", "fc_data", "data"};
43         } else if (std::is_same<LT, NormLayer>::value) {
44             layerChild[res->name] = {"lrn", "norm", "norm_data", "data"};
45         } else if (std::is_same<LT, CropLayer>::value) {
46             layerChild[res->name] = {"crop", "crop-data", "data"};
47         } else if (std::is_same<LT, BatchNormalizationLayer>::value) {
48             layerChild[res->name] = {"batch_norm", "batch_norm_data", "data"};
49         } else if ((std::is_same<LT, EltwiseLayer>::value)) {
50             layerChild[res->name] = {"elementwise", "elementwise_data", "data"};
51         } else {
52             layerChild[res->name] = {"data", tolower(res->type) + "_data", tolower(res->type)};
53         }
54
55         pugi::xml_node dn = GetChild(node, layerChild[res->name], false);
56
57         if (!dn.empty()) {
58             if (dn.child("crop").empty()) {
59                 for (auto ait = dn.attributes_begin(); ait != dn.attributes_end(); ++ait) {
60                     pugi::xml_attribute attr = *ait;
61                     if (attr.name() == dn.attribute("region").name()) {
62                         bool isSame = std::equal(null_terminated_string(attr.value()),
63                                                  null_terminated_string_end(),
64                                                  null_terminated_string("same"),
65                                                  [](char c1, char c2) {
66                                                      return std::tolower(c1) == c2;
67                                                  });
68                         bool var = attr.empty() || !isSame;
69
70                         res->params[attr.name()] = var == 0 ? "false" : "true";
71                     } else {
72                         res->params[attr.name()] = attr.value();
73                     }
74                 }
75             } else {
76                 if (std::is_same<LT, CropLayer>::value) {
77                     auto crop_res = std::dynamic_pointer_cast<CropLayer>(res);
78                     if (!crop_res) {
79                         THROW_IE_EXCEPTION << "Crop layer is nullptr";
80                     }
81                     std::string axisStr, offsetStr, dimStr;
82                     FOREACH_CHILD(_cn, dn, "crop") {
83                         int axis = GetIntAttr(_cn, "axis", 0);
84                         crop_res->axis.push_back(axis);
85                         axisStr +=  std::to_string(axis) + ",";
86                         int offset = GetIntAttr(_cn, "offset", 0);
87                         crop_res->offset.push_back(offset);
88                         offsetStr +=  std::to_string(offset) + ",";
89                     }
90                     if (!axisStr.empty() && !offsetStr.empty() && !dimStr.empty()) {
91                         res->params["axis"] = axisStr.substr(0, axisStr.size() - 1);
92                         res->params["offset"] = offsetStr.substr(0, offsetStr.size() - 1);
93                     }
94                 }
95             }
96         }
97         return res;
98     }
99
100     std::map <std::string, std::vector<std::string>> layerChild;
101 };
102
103 class ActivationLayerCreator : public BaseCreator {
104  public:
105     explicit ActivationLayerCreator(const std::string& type) : BaseCreator(type) {}
106     CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override;
107 };
108 }  // namespace details
109 }  // namespace InferenceEngine
110
111 /***********************************************************************************/
112 /******* End of Layer Parsers ******************************************************/
113 /***********************************************************************************/