1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
10 #include "v2_format_parser.h"
11 #include "xml_parse_utils.h"
12 #include "range_iterator.hpp"
13 #include "caseless.hpp"
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;
24 THROW_IE_EXCEPTION << "missing <" << InferenceEngine::details::dumpVec(tags)
25 << "> Tags at offset :" << node.offset_debug();
26 return pugi::xml_node();
29 using namespace XMLParseUtils;
31 namespace InferenceEngine {
34 class V2LayerCreator : public BaseCreator {
36 explicit V2LayerCreator(const std::string& type) : BaseCreator(type) {}
38 CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override {
39 auto res = std::make_shared<LT>(layerParsePrms.prms);
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"};
52 layerChild[res->name] = {"data", tolower(res->type) + "_data", tolower(res->type)};
55 pugi::xml_node dn = GetChild(node, layerChild[res->name], false);
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;
68 bool var = attr.empty() || !isSame;
70 res->params[attr.name()] = var == 0 ? "false" : "true";
72 res->params[attr.name()] = attr.value();
76 if (std::is_same<LT, CropLayer>::value) {
77 auto crop_res = std::dynamic_pointer_cast<CropLayer>(res);
79 THROW_IE_EXCEPTION << "Crop layer is nullptr";
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) + ",";
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);
100 std::map <std::string, std::vector<std::string>> layerChild;
103 class ActivationLayerCreator : public BaseCreator {
105 explicit ActivationLayerCreator(const std::string& type) : BaseCreator(type) {}
106 CNNLayer::Ptr CreateLayer(pugi::xml_node& node, LayerParseParameters& layerParsePrms) override;
108 } // namespace details
109 } // namespace InferenceEngine
111 /***********************************************************************************/
112 /******* End of Layer Parsers ******************************************************/
113 /***********************************************************************************/