[QUESTION] How can I convert cv::Mat to TensorWrapData?
What is your question?
How can I convert cv::Mat to TensorWrapData?
There is my code modified from samples/cropandresize, but it doesn't produce the right resize image.
auto imgMat = cv::imread(imagePath);
nvcv::TensorDataStridedCuda::Buffer inBuf;
inBuf.strides[3] = sizeof(uint8_t);
inBuf.strides[2] = imgMat.channels() * inBuf.strides[3];
inBuf.strides[1] = imgMat.cols * inBuf.strides[2];
inBuf.strides[0] = imgMat.rows * inBuf.strides[1];
CHECK_CUDA_ERROR(cudaMallocAsync(&inBuf.basePtr, batchSize * inBuf.strides[0], stream));
// tag: Tensor Requirements
// Calculate the requirements for the RGBI uint8_t Tensor which include
// pitch bytes, alignment, shape and tensor layout
nvcv::Tensor::Requirements inReqs
= nvcv::Tensor::CalcRequirements(1, {imgMat.cols, imgMat.rows}, nvcv::FMT_RGB8);
// Create a tensor buffer to store the data pointer and pitch bytes for each plane
nvcv::TensorDataStridedCuda inData(nvcv::TensorShape{inReqs.shape, inReqs.rank, inReqs.layout},
nvcv::DataType{inReqs.dtype}, inBuf);
// TensorWrapData allows for interoperation of external tensor representations with CVCUDA Tensor.
nvcv::TensorWrapData inTensor(inData);
// tag: Image Loading
// NvJpeg is used to load the images to create a batched input device buffer.
uint8_t *gpuInput = reinterpret_cast<uint8_t *>(inBuf.basePtr);
CHECK_CUDA_ERROR(cudaMemcpy(gpuInput, imgMat.data, inBuf.strides[0], cudaMemcpyHostToDevice));
can your guys tell me how to convert cv::Mat to TensorWrapData?
or some documents about the memory layout of the nvcv tensor ?
cudaMallocAsync操作是异步的,cudaMemcpy之前是不是需要同步下,确保显存分配完成了
We are considering host side Tensor support in NVCV-types. I am converting this issue to a feature request. There may be additional behavior needed to address this.
从提供的代码上看上去,有可能是inBuf.strides[0]计算出来的内存字节数,和CalcRequirements计算出的inReqs的strides不一致,建议把inBuf和inReqs的strides属性打印出来看一下
另外我之前实验时记得 CVCUDA上image tensor的内存是按照width \times 3,以32字节对齐的, 经过CVCUDA OP的操作后,width \times 3是32的整数倍
We are currently looking into how to resolve this conversion issue between cv::Mat and nvcv::Tensor.
any updates?