cyrusbehr / tensorrt-cpp-api

TensorRT C++ API Tutorial
MIT License
577 stars 72 forks source link

Segfault #4

Closed DJT777 closed 1 year ago

DJT777 commented 2 years ago

Hey Cyrus,

I'm working through your code to learn TensorRT.

I trained tiny Yolov4 model and ported it over to onnx with several different batch sizes. I made some modifications to the code to accommodate static batch sizes. For each model I changed the dimension calculations, depending on which model but each was consistent with the input dimensions.

However I am getting some segfaults on this block of code in engine.cpp


    for (size_t batch = 0; batch < inputFaceChips.size(); ++batch) {
        auto image = inputFaceChips[batch];

        // Preprocess code
        image.convertTo(image, CV_32FC3, 1.f / 255.f);
        cv::subtract(image, cv::Scalar(0.5f, 0.5f, 0.5f), image, cv::noArray(), -1);
        cv::divide(image, cv::Scalar(0.5f, 0.5f, 0.5f), image, 1, -1);

        // NHWC to NCHW conversion
        // NHWC: For each pixel, its 3 colors are stored together in RGB order.
        // For a 3 channel image, say RGB, pixels of the R channel are stored first, then the G channel and finally the B channel.
        // https://user-images.githubusercontent.com/20233731/85104458-3928a100-b23b-11ea-9e7e-95da726fef92.png
        int offset = dims.d[1] * dims.d[2] * dims.d[3] * batch;
        int r = 0 , g = 0, b = 0;
        for (int i = 0; i < dims.d[1] * dims.d[2] * dims.d[3]; ++i) {
            if (i % 3 == 0) {
//SEGFAULT HERE  hostDataBuffer[offset + r++] = *(reinterpret_cast<float*>(image.data) + i);
            } else if (i % 3 == 1) {
                hostDataBuffer[offset + g++ + dims.d[2]*dims.d[3]] = *(reinterpret_cast<float*>(image.data) + i);
            } else {
                hostDataBuffer[offset + b++ + dims.d[2]*dims.d[3]*2] = *(reinterpret_cast<float*>(image.data) + i);
            }
        }
    }

Here are the models I am using:

https://drive.google.com/drive/u/0/folders/14AEDjchvPF8Tp8PrFsRMg7q4wZG8KMNi

cyrusbehr commented 1 year ago

Hi @DJT777 , sorry it has taken me so long to get back to you. There are a few issues at play here.

1) You model has a static batch size, whereas my implementation originally only supported dynamic batch sizes. It now supports both, please set Options.doesSupportDynamicBatchSize appropriately.

2) Your model expects the input image to be of size (3, 512, 512). If you were running my sample code, you were feeding it images of size (3, 112, 112), so that is why it was crashing. I have written more robust code which will now return an error if you try to feed the model an incorrectly sizes image, and I have also specified where in the code you need to resize your image.