wang-xinyu / tensorrtx

Implementation of popular deep learning networks with TensorRT network definition API
MIT License
7.04k stars 1.78k forks source link

当我试图将一张图片分割之后填入batch再进行推理时报错 #1558

Closed extreme99520-l closed 4 months ago

extreme99520-l commented 4 months ago
    std::vector<cv::Mat> preprocess_image_regions(const std::string& image_path, int rows = 3, int cols = 3) {
    cv::Mat img = cv::imread(image_path);
    int height = img.rows;
    int width = img.cols;
    int region_width = width / cols;
    int region_height = height / rows;
    std::vector<cv::Mat> regions;

    for(int i = 0; i < rows; ++i) {
        for(int j = 0; j < cols; ++j) {
            int x_start = j * region_width;
            int y_start = i * region_height;
            int x_end = (j == cols - 1) ? width : (j + 1) * region_width;
            int y_end = (i == rows - 1) ? height : (i + 1) * region_height;
            cv::Rect roi(x_start, y_start, x_end - x_start, y_end - y_start);
            cv::Mat region = img(roi).clone();
            regions.push_back(region);
        }
    }
    return regions;
    }
    我试图使用这个函数对图片进行分割,然后在主函数  batch predict部分做了如下修改。
for (size_t i = 0; i < file_names.size(); i += kBatchSize) {
    // Get a batch of images

    std::vector<cv::Mat> img_batch;
    std::vector<std::string> img_name_batch;
    for (size_t j = i; j < i + kBatchSize && j < file_names.size(); j++) {
        //cv::Mat img = cv::imread(img_dir + "/" + file_names[j]);
        std::vector<cv::Mat> regions = preprocess_image_regions(img_dir + "/" + file_names[j], 2, 2); 

        for(size_t k = 0; k < regions.size(); ++k) {
            img_batch.push_back(regions[k]);
            img_name_batch.push_back(file_names[j]+ "_" + std::to_string(k));
        }

    }
    当 rows=1,cols = 1时可以正常推理:

微信图片_20240717110022

    当参数设置为>1时,就会报错:

image

    请问我应该修改哪一部分来适应这个变动?或者说是否支持这个改动。如果将图片分割存储后再读取理论上应该是可行的,但那样耗时肯定会更久。

求大佬答疑,万分感谢!!!