| 742 | } |
| 743 | |
| 744 | void extractScaleTrainedData(XGraph* graph, const string& layerName, const caffe::NetParameter* Net, |
| 745 | const map<string, int>* layerIndex) |
| 746 | { |
| 747 | map<string, int>::const_iterator modelLayer_it = layerIndex->find(layerName); |
| 748 | if(modelLayer_it == layerIndex->end()) |
| 749 | { |
| 750 | cerr << "[EP035] Layer " << layerName << " is not found in the caffemodel file. " << endl; |
| 751 | exit(-1); |
| 752 | } |
| 753 | |
| 754 | // Get the layer from caffemodel |
| 755 | int loc = modelLayer_it->second; |
| 756 | const caffe::LayerParameter& binLayer = Net->layer(loc); |
| 757 | XLayer* tmpXlayer = graph->layers[layerName]; |
| 758 | string txtFileName; |
| 759 | |
| 760 | // Extract gamma |
| 761 | cerr << "[IG001] Extracting " << tmpXlayer->name << " weights ... " << endl; |
| 762 | |
| 763 | // Check if filter shape is matching |
| 764 | vector<int> weightsShape = getBlobDim(binLayer.blobs(0)); |
| 765 | int channels = tmpXlayer->topShape.at(0).at(1); // Number of feature maps |
| 766 | ASSERT( (weightsShape.at(0) == channels), EP057, |
| 767 | "Scale Layer: " << tmpXlayer->name << "mismatch in Scale shape : " |
| 768 | << TensorDimToString(weightsShape) << " != " << channels) |
| 769 | vector<float> weights = extractBlobToVector(binLayer.blobs(0)); |
| 770 | tmpXlayer->scale_params->gamma.push_back(weights); |
| 771 | tmpXlayer->scale_params->gammaDim.push_back(weightsShape); |
| 772 | |
| 773 | #if DEBUG_WEIGHT_EXTRACTION |
| 774 | string tmpName(tmpXlayer->name); |
| 775 | replace(tmpName.begin(), tmpName.end(), '/', '_'); |
| 776 | txtFileName = graph->saveDir + tmpName + "_gamma"; |
| 777 | tmpXlayer->scale_params->gammaPath.push_back(txtFileName); |
| 778 | int sizeInBytes = getSize(weightsShape) * sizeof(float); |
| 779 | cerr << "[IG001] Saving " << txtFileName << " (" << humanReadableSize(sizeInBytes) << ")" << "\t"; |
| 780 | if(sizeInBytes > (14*1024*1024)) |
| 781 | { |
| 782 | cerr << "Parsing large data, this may take a while ..."; |
| 783 | } |
| 784 | cerr << endl; |
| 785 | SAVEDATA(weights, txtFileName); |
| 786 | #endif |
| 787 | |
| 788 | // Extract the beta if beta is present, else save a vector filled with zeros |
| 789 | vector<float> bias; |
| 790 | vector<int> biasShape; |
| 791 | |
| 792 | if(tmpXlayer->scale_params->has_bias) |
| 793 | { |
| 794 | // Check if bias shape is matching |
| 795 | vector<int> biasShape = getBlobDim(binLayer.blobs(1)); |
| 796 | ASSERT( (biasShape.at(0) == channels), EP060, |
| 797 | "Scale Layer: " << tmpXlayer->name << "mismatch in Bias shape : " |
| 798 | << TensorDimToString(weightsShape) << " != " << channels) |
| 799 | |
| 800 | bias = extractBlobToVector(binLayer.blobs(1)); |
| 801 | } |
no test coverage detected