| 361 | } |
| 362 | |
| 363 | void extractConvolutionTrainedData(XGraph* graph, const string& layerName, const caffe::NetParameter* Net, |
| 364 | const map<string, int>* layerIndex) |
| 365 | { |
| 366 | map<string, int>::const_iterator modelLayer_it = layerIndex->find(layerName); |
| 367 | if(modelLayer_it == layerIndex->end()) |
| 368 | { |
| 369 | cerr << "[EP030] Layer " << layerName << " is not found in the caffemodel file. " << endl; |
| 370 | exit(-1); |
| 371 | } |
| 372 | |
| 373 | // Get the layer from caffemodel |
| 374 | int loc = modelLayer_it->second; |
| 375 | const caffe::LayerParameter& binLayer = Net->layer(loc); |
| 376 | XLayer* tmpXlayer = graph->layers[layerName]; |
| 377 | string txtFileName; |
| 378 | |
| 379 | // Extract the weights, do trimming & rounding before saving to TXT file |
| 380 | cerr << "[IG001] Extracting " << tmpXlayer->name << " weights ... " << endl; |
| 381 | vector<int> weightsShape = getBlobDim(binLayer.blobs(0)); |
| 382 | ELOG( ((weightsShape.at(0) != tmpXlayer->conv_params->M) || |
| 383 | (weightsShape.at(1) != (tmpXlayer->conv_params->N/tmpXlayer->conv_params->group)) || |
| 384 | (weightsShape.at(2) != tmpXlayer->conv_params->filter_h) || |
| 385 | (weightsShape.at(3) != tmpXlayer->conv_params->filter_w)), |
| 386 | EP056, |
| 387 | "Convolution Layer: " << tmpXlayer->name << " - mismatch in filter shape. " |
| 388 | << TensorDimToString(weightsShape) << " v/s " << tmpXlayer->conv_params->filterDimToString() |
| 389 | ) |
| 390 | |
| 391 | vector<float> weights = extractBlobToVector(binLayer.blobs(0)); |
| 392 | tmpXlayer->conv_params->weights.push_back(weights); |
| 393 | tmpXlayer->conv_params->weightsDim.push_back(getBlobDim(binLayer.blobs(0))); |
| 394 | |
| 395 | #if DEBUG_WEIGHT_EXTRACTION |
| 396 | string tmpName(tmpXlayer->name); |
| 397 | replace(tmpName.begin(), tmpName.end(), '/', '_'); |
| 398 | txtFileName = graph->saveDir + tmpName + "_weights"; |
| 399 | tmpXlayer->conv_params->weightsPath.push_back(txtFileName); |
| 400 | int sizeInBytes = getSize(weightsShape) * sizeof(float); |
| 401 | cerr << "[IG001] Saving " << txtFileName << " (" << humanReadableSize(sizeInBytes) << ")" << "\t"; |
| 402 | if(sizeInBytes > (14*1024*1024)) |
| 403 | { |
| 404 | cerr << "Parsing large data, this may take a while ..."; |
| 405 | } |
| 406 | cerr << endl; |
| 407 | SAVEDATA(weights, txtFileName); |
| 408 | #endif |
| 409 | |
| 410 | // Extract the bias if bias is present, else save a vector filled with zeros |
| 411 | cerr << "[IG001] Extracting " << tmpXlayer->name << " bias ... " << endl; |
| 412 | vector<int> biasShape; |
| 413 | vector<float> bias; |
| 414 | if(tmpXlayer->conv_params->has_bias) |
| 415 | { |
| 416 | biasShape = getBlobDim(binLayer.blobs(1)); |
| 417 | ELOG( (biasShape.at(0) != tmpXlayer->conv_params->M) , EP031, |
| 418 | "Deconv Layer: " << tmpXlayer->name << " - mismatch in bias shape. " |
| 419 | << TensorDimToString(biasShape) << " v/s " << tmpXlayer->conv_params->M ) |
| 420 |
no test coverage detected