1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
9 #include "ie_format_parser.h"
10 #include "xml_parse_utils.h"
11 #include "range_iterator.hpp"
12 #include "details/caseless.hpp"
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;
23 THROW_IE_EXCEPTION << "missing <" << InferenceEngine::details::dumpVec(tags)
24 << "> Tags at offset :" << node.offset_debug();
25 return pugi::xml_node();
28 using namespace XMLParseUtils;
30 namespace InferenceEngine {
33 class LayerCreator : public BaseCreator {
35 explicit LayerCreator(const std::string& type) : BaseCreator(type) {}
37 CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override {
38 auto res = std::make_shared<LT>(layerParsePrms.prms);
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"};
51 layerChild[res->name] = {"data", tolower(res->type) + "_data", tolower(res->type)};
54 pugi::xml_node dn = GetChild(node, layerChild[res->name], false);
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());
63 if (std::is_same<LT, CropLayer>::value) {
64 auto crop_res = std::dynamic_pointer_cast<CropLayer>(res);
66 THROW_IE_EXCEPTION << "Crop layer is nullptr";
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) + ",";
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);
87 std::map <std::string, std::vector<std::string>> layerChild;
90 class ActivationLayerCreator : public BaseCreator {
92 explicit ActivationLayerCreator(const std::string& type) : BaseCreator(type) {}
93 CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override;
96 class TILayerCreator : public BaseCreator {
98 explicit TILayerCreator(const std::string& type) : BaseCreator(type) {}
99 CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override;
101 } // namespace details
102 } // namespace InferenceEngine
104 /***********************************************************************************/
105 /******* End of Layer Parsers ******************************************************/
106 /***********************************************************************************/