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

Function extractBatchNormTrainedData

software/xtract/caffe_network_parser.cpp:662–742  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

660}
661
662void extractBatchNormTrainedData(XGraph* graph, const string& layerName, const caffe::NetParameter* Net,
663 const map<string, int>* layerIndex)
664{
665 map<string, int>::const_iterator modelLayer_it = layerIndex->find(layerName);
666 if(modelLayer_it == layerIndex->end())
667 {
668 cerr << "[EP030] Layer " << layerName << " is not found in the caffemodel file. " << endl;
669 exit(-1);
670 }
671
672 // Get the layer from caffemodel
673 int loc = modelLayer_it->second;
674 const caffe::LayerParameter& binLayer = Net->layer(loc);
675 XLayer* tmpXlayer = graph->layers[layerName];
676 string txtFileName;
677
678 // Extract MWA
679 cerr << "[IG001] Extracting " << tmpXlayer->name << " weights ... " << endl;
680 vector<float> mwa = extractBlobToVector(binLayer.blobs(2));
681 float scaling_factor = mwa[0] == 0 ? 0 : 1.0f/mwa[0];
682
683 // Extract the mean, do trimming & rounding before saving to TXT file
684 vector<int> weightsShape = getBlobDim(binLayer.blobs(0));
685 int channels = tmpXlayer->topShape.at(0).at(1); // Number of feature maps
686 ASSERT( (weightsShape.at(0) == channels), EP056,
687 "BatchNorm Layer: " << tmpXlayer->name << "mismatch in Mean shape : "
688 << TensorDimToString(weightsShape) << " != " << channels)
689 vector<float> weights = extractBlobToVector(binLayer.blobs(0));
690 for(int i=0; i<weights.size(); ++i)
691 weights[i] *= scaling_factor;
692 tmpXlayer->batchnorm_params->mean.push_back(weights);
693 tmpXlayer->batchnorm_params->meanDim.push_back(weightsShape);
694
695#if DEBUG_WEIGHT_EXTRACTION
696 string tmpName(tmpXlayer->name);
697 replace(tmpName.begin(), tmpName.end(), '/', '_');
698 txtFileName = graph->saveDir + tmpName + "_mean";
699 tmpXlayer->batchnorm_params->meanPath.push_back(txtFileName);
700 int sizeInBytes = getSize(weightsShape) * sizeof(float);
701 cerr << "[IG001] Saving " << txtFileName << " (" << humanReadableSize(sizeInBytes) << ")" << "\t";
702 if(sizeInBytes > (14*1024*1024))
703 {
704 cerr << "Parsing large data, this may take a while ...";
705 }
706 cerr << endl;
707 SAVEDATA(weights, txtFileName);
708#endif
709
710 // Extract the variance, do trimming & rounding before saving to TXT file
711 vector<int> biasShape = getBlobDim(binLayer.blobs(1));
712 ASSERT( (biasShape.at(0) == channels), EP056,
713 "BatchNorm Layer: " << tmpXlayer->name << "mismatch in variance shape : "
714 << TensorDimToString(biasShape) << " != " << channels)
715 vector<float> bias = extractBlobToVector(binLayer.blobs(1));
716 for(int i=0; i<bias.size(); ++i)
717 bias[i] *= scaling_factor;
718 tmpXlayer->batchnorm_params->variance.push_back(bias);
719 tmpXlayer->batchnorm_params->varianceDim.push_back(biasShape);

Callers 1

ExtractTrainedParametersFunction · 0.85

Calls 12

extractBlobToVectorFunction · 0.85
getBlobDimFunction · 0.85
TensorDimToStringFunction · 0.85
getSizeFunction · 0.85
humanReadableSizeFunction · 0.85
findMethod · 0.45
endMethod · 0.45
atMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45
beginMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected