tensorflow 模型批处理参数tensor快速赋值参考
作者:互联网
批处理调用模型的时候,如果逐像素给tensor对象数据部分赋值的话,效率是很低的,尤其是对于一些图片数据,所以数据块直接拷贝可以大大提高效率,
先取得数据指针:
output_tensor->flat<float>().data();
再直接对象拷贝好了
std::copy(p, p + arrSize, imgTensorFlat + t * arrSize);
void CVMat_to_Tensor_Batch_BLOB(std::vector<Mat> imgs, Tensor* output_tensor, int input_rows, int input_cols) { float *imgTensorFlat = output_tensor->flat<float>().data(); int arrSize = input_rows * input_cols * 3; auto output = output_tensor->shaped<float, 4>({ (long long)imgs.size(), input_rows, input_cols, 3 }); for (int t = 0; t < imgs.size(); t++) { Tensor tensorT(DT_FLOAT, TensorShape({ 1,input_rows,input_cols,3 })); int imgRow = imgs[t].rows; int imgCol = imgs[t].cols; float fScale = std::min(input_cols*1.0/ imgCol, input_rows*1.0/ imgRow); int newRow = imgRow * fScale; int newCol = imgCol * fScale; Mat oriImg; resize(imgs[t], oriImg, cv::Size(newCol, newRow), cv::INTER_LINEAR); cv::Mat tmp; int dw = (input_cols - newCol) / 2; int dh = (input_rows - newRow) / 2; copyMakeBorder(oriImg, tmp, dh, input_rows - newRow-dh,dw, input_cols - newCol-dw, BORDER_CONSTANT, Scalar(128.0, 128.0, 128.0)); tmp.convertTo(tmp, CV_32FC3); tmp = tmp/255.0; float* p = tmp.ptr<float>(); std::copy(p, p + arrSize, imgTensorFlat + t * arrSize); } return; }
标签:tmp,rows,tensor,批处理,cols,int,input,tensorflow 来源: https://www.cnblogs.com/jjqnh/p/16189691.html