| 660 | } |
| 661 | |
| 662 | void 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); |
no test coverage detected