void blobFromProto(const caffe::BlobProto &pbBlob, cv::Mat &dstBlob) { MatShape shape; blobShapeFromProto(pbBlob, shape); dstBlob.create((int)shape.size(), &shape[0], CV_32F); if (pbBlob.data_size()) { // Single precision floats. CV_Assert(pbBlob.data_size() == (int)dstBlob.total()); CV_DbgAssert(pbBlob.GetDescriptor()->FindFieldByLowercaseName("data")->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT); Mat(dstBlob.dims, &dstBlob.size[0], CV_32F, (void*)pbBlob.data().data()).copyTo(dstBlob); } else { CV_Assert(pbBlob.has_raw_data()); const std::string& raw_data = pbBlob.raw_data(); if (pbBlob.raw_data_type() == caffe::FLOAT16) { // Half precision floats. CV_Assert(raw_data.size() / 2 == (int)dstBlob.total()); Mat halfs((int)shape.size(), &shape[0], CV_16SC1, (void*)raw_data.c_str()); convertFp16(halfs, dstBlob); } else if (pbBlob.raw_data_type() == caffe::FLOAT) { CV_Assert(raw_data.size() / 4 == (int)dstBlob.total()); Mat((int)shape.size(), &shape[0], CV_32FC1, (void*)raw_data.c_str()).copyTo(dstBlob); } else CV_Error(Error::StsNotImplemented, "Unexpected blob data type"); } }
void blobFromProto(const caffe::BlobProto &pbBlob, cv::Mat &dstBlob) { MatShape shape; blobShapeFromProto(pbBlob, shape); dstBlob.create((int)shape.size(), &shape[0], CV_32F); float *dstData = dstBlob.ptr<float>(); if (pbBlob.data_size()) { // Single precision floats. CV_Assert(pbBlob.data_size() == (int)dstBlob.total()); CV_DbgAssert(pbBlob.GetDescriptor()->FindFieldByLowercaseName("data")->cpp_type() == FieldDescriptor::CPPTYPE_FLOAT); for (int i = 0; i < pbBlob.data_size(); i++) dstData[i] = pbBlob.data(i); } else { // Half precision floats. CV_Assert(pbBlob.raw_data_type() == caffe::FLOAT16); std::string raw_data = pbBlob.raw_data(); CV_Assert(raw_data.size() / 2 == (int)dstBlob.total()); Mat halfs((int)shape.size(), &shape[0], CV_16SC1, (void*)raw_data.c_str()); convertFp16(halfs, dstBlob); } }
void blobShapeFromProto(const caffe::BlobProto &pbBlob, MatShape& shape) { shape.clear(); if (pbBlob.has_num() || pbBlob.has_channels() || pbBlob.has_height() || pbBlob.has_width()) { shape.push_back(pbBlob.num()); shape.push_back(pbBlob.channels()); shape.push_back(pbBlob.height()); shape.push_back(pbBlob.width()); } else if (pbBlob.has_shape()) { const caffe::BlobShape &_shape = pbBlob.shape(); for (int i = 0; i < _shape.dim_size(); i++) shape.push_back((int)_shape.dim(i)); } else shape.resize(1, 1); // Is a scalar. }