void extractLayerParams(const Message &msg, cv::dnn::LayerParams ¶ms, bool isInternal = false) { const Descriptor *msgDesc = msg.GetDescriptor(); const Reflection *msgRefl = msg.GetReflection(); for (int fieldId = 0; fieldId < msgDesc->field_count(); fieldId++) { const FieldDescriptor *fd = msgDesc->field(fieldId); if (!isInternal && !ends_with_param(fd->name())) continue; const google::protobuf::UnknownFieldSet& unknownFields = msgRefl->GetUnknownFields(msg); bool hasData = fd->is_required() || (fd->is_optional() && msgRefl->HasField(msg, fd)) || (fd->is_repeated() && msgRefl->FieldSize(msg, fd) > 0) || !unknownFields.empty(); if (!hasData) continue; extractCustomParams(unknownFields, params); if (fd->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { if (fd->is_repeated()) //Extract only first item! extractLayerParams(msgRefl->GetRepeatedMessage(msg, fd, 0), params, true); else extractLayerParams(msgRefl->GetMessage(msg, fd), params, true); } else { addParam(msg, fd, params); } } }
void populateNet(Net dstNet) { CV_TRACE_FUNCTION(); int layersSize = net.layer_size(); layerCounter.clear(); addedBlobs.clear(); addedBlobs.reserve(layersSize + 1); //setup input layer names std::vector<String> netInputs(net.input_size()); { for (int inNum = 0; inNum < net.input_size(); inNum++) { addedBlobs.push_back(BlobNote(net.input(inNum), 0, inNum)); netInputs[inNum] = net.input(inNum); } } for (int li = 0; li < layersSize; li++) { const caffe::LayerParameter &layer = net.layer(li); String name = layer.name(); String type = layer.type(); LayerParams layerParams; extractLayerParams(layer, layerParams); extractBinaryLayerParms(layer, layerParams); int repetitions = layerCounter[name]++; if (repetitions) name += String("_") + toString(repetitions); if (type == "Input") { for (int outNum = 0; outNum < layer.top_size(); outNum++) { addOutput(layer, 0, outNum); addedBlobs.back().outNum = netInputs.size(); netInputs.push_back(addedBlobs.back().name); } continue; } int id = dstNet.addLayer(name, type, layerParams); for (int inNum = 0; inNum < layer.bottom_size(); inNum++) addInput(layer.bottom(inNum), id, inNum, dstNet); for (int outNum = 0; outNum < layer.top_size(); outNum++) addOutput(layer, id, outNum); } dstNet.setInputsNames(netInputs); addedBlobs.clear(); }
void populateNet(Net dstNet) { CV_TRACE_FUNCTION(); int layersSize = net.layer_size(); layerCounter.clear(); addedBlobs.clear(); addedBlobs.reserve(layersSize + 1); //setup input layer names std::vector<String> netInputs(net.input_size()); { for (int inNum = 0; inNum < net.input_size(); inNum++) { addedBlobs.push_back(BlobNote(net.input(inNum), 0, inNum)); netInputs[inNum] = net.input(inNum); } } for (int li = 0; li < layersSize; li++) { const caffe::LayerParameter &layer = net.layer(li); String name = layer.name(); String type = layer.type(); LayerParams layerParams; extractLayerParams(layer, layerParams); extractBinaryLayerParams(layer, layerParams); int repetitions = layerCounter[name]++; if (repetitions) name += String("_") + toString(repetitions); if (type == "Input") { for (int outNum = 0; outNum < layer.top_size(); outNum++) { addOutput(layer, 0, outNum); addedBlobs.back().outNum = netInputs.size(); netInputs.push_back(addedBlobs.back().name); } continue; } else if (type == "BatchNorm") { if (!layerParams.get<bool>("use_global_stats", true)) { CV_Assert_N(layer.bottom_size() == 1, layer.top_size() == 1); LayerParams mvnParams; mvnParams.set("eps", layerParams.get<float>("eps", 1e-5)); std::string mvnName = name + "/mvn"; int repetitions = layerCounter[mvnName]++; if (repetitions) mvnName += String("_") + toString(repetitions); int mvnId = dstNet.addLayer(mvnName, "MVN", mvnParams); addInput(layer.bottom(0), mvnId, 0, dstNet); addOutput(layer, mvnId, 0); net.mutable_layer(li)->set_bottom(0, layer.top(0)); layerParams.blobs[0].setTo(0); // mean layerParams.blobs[1].setTo(1); // std } } else if ("ConvolutionDepthwise" == type) { type = "Convolution"; } int id = dstNet.addLayer(name, type, layerParams); for (int inNum = 0; inNum < layer.bottom_size(); inNum++) addInput(layer.bottom(inNum), id, inNum, dstNet); for (int outNum = 0; outNum < layer.top_size(); outNum++) addOutput(layer, id, outNum); } dstNet.setInputsNames(netInputs); addedBlobs.clear(); }