MCPcopy Create free account
hub / github.com/Xilinx/CHaiDNN / extractL2NormalizeTrainedData

Function extractL2NormalizeTrainedData

software/xtract/caffe_network_parser.cpp:606–660  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

604}
605
606void extractL2NormalizeTrainedData(XGraph* graph, const string& layerName, const caffe::NetParameter* Net,
607 const map<string, int>* layerIndex)
608{
609 map<string, int>::const_iterator modelLayer_it = layerIndex->find(layerName);
610 if(modelLayer_it == layerIndex->end())
611 {
612 cerr << "[EP037] Layer " << layerName << " is not found in the caffemodel file. " << endl;
613 exit(-1);
614 }
615
616 // Get the layer from caffemodel
617 int loc = modelLayer_it->second;
618 const caffe::LayerParameter& binLayer = Net->layer(loc);
619 XLayer* tmpXlayer = graph->layers[layerName];
620 string txtFileName;
621
622 // Extract the weights
623 cerr << "[IG001] Extracting " << tmpXlayer->name << " weights ... " << endl;
624 int channels = tmpXlayer->topShape.at(0).at(1); // Number of feature maps
625 caffe::BlobProto blob = binLayer.blobs(0);
626
627 // If it is just one value for all channels, replicate it #channels times
628 if(tmpXlayer->l2norm_params->channel_shared)
629 {
630 float val = blob.data(0);
631 tmpXlayer->l2norm_params->gamma.resize(channels, val); // Replicate same value #channels times
632 }
633 else
634 {
635 // Check if bias shape is matching
636 vector<int> gammaShape = getBlobDim(binLayer.blobs(0));
637 ELOG( (gammaShape.at(0) != channels) , EP038,
638 "L2 Normalization Layer: " << tmpXlayer->name << " - mismatch in gamma shape. "
639 << TensorDimToString(gammaShape) << " v/s " << channels );
640
641 tmpXlayer->l2norm_params->gamma.reserve(channels);
642 std::copy(blob.data().begin(), blob.data().end(), std::back_inserter(tmpXlayer->l2norm_params->gamma));
643 }
644
645#if DEBUG_WEIGHT_EXTRACTION
646 string tmpName(tmpXlayer->name);
647 replace(tmpName.begin(), tmpName.end(), '/', '_');
648 txtFileName = graph->saveDir + tmpName + "_weights";
649 tmpXlayer->l2norm_params->gammaFile = txtFileName;
650 int sizeInBytes = tmpXlayer->l2norm_params->gamma.size() * sizeof(float);
651 cerr << "[IG001] Saving " << txtFileName << " (" << humanReadableSize(sizeInBytes) << ")" << endl;
652 SAVEDATA(tmpXlayer->l2norm_params->gamma, txtFileName);
653#endif
654
655 // If th_l2n_gamma not provided set default values
656 if (tmpXlayer->quantization_scheme == "Xilinx" && tmpXlayer->th_l2n_gamma.empty()) {
657 tmpXlayer->th_params = tmpXlayer->l2norm_params->gamma;
658 }
659
660}
661
662void extractBatchNormTrainedData(XGraph* graph, const string& layerName, const caffe::NetParameter* Net,
663 const map<string, int>* layerIndex)

Callers 1

ExtractTrainedParametersFunction · 0.85

Calls 11

getBlobDimFunction · 0.85
TensorDimToStringFunction · 0.85
humanReadableSizeFunction · 0.85
findMethod · 0.45
endMethod · 0.45
atMethod · 0.45
dataMethod · 0.45
resizeMethod · 0.45
beginMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected