CV-CUDA icon indicating copy to clipboard operation
CV-CUDA copied to clipboard

[QUESTION] How can I convert cv::Mat to TensorWrapData?

Open SineYuan opened this issue 2 years ago • 6 comments

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 ?

SineYuan avatar Mar 24 '23 14:03 SineYuan

cudaMallocAsync操作是异步的,cudaMemcpy之前是不是需要同步下,确保显存分配完成了

sunflowersnow avatar Mar 28 '23 13:03 sunflowersnow

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.

milesp-nvidia avatar Mar 29 '23 18:03 milesp-nvidia

从提供的代码上看上去,有可能是inBuf.strides[0]计算出来的内存字节数,和CalcRequirements计算出的inReqs的strides不一致,建议把inBuf和inReqs的strides属性打印出来看一下

monsterlyg avatar Apr 11 '23 06:04 monsterlyg

另外我之前实验时记得 CVCUDA上image tensor的内存是按照width \times 3,以32字节对齐的, 经过CVCUDA OP的操作后,width \times 3是32的整数倍

monsterlyg avatar Apr 11 '23 06:04 monsterlyg

We are currently looking into how to resolve this conversion issue between cv::Mat and nvcv::Tensor.

milesp-nvidia avatar May 10 '23 18:05 milesp-nvidia

any updates?

silaopi avatar Sep 06 '23 09:09 silaopi