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