如果有5张RGB图像,可以按照下面的方式创建和添加至缓冲区buffers中:
const int numImages = 5; // 图像的数量
const int channels = 3; // 图像的通道数(RGB图像为3)
const int imageWidth = ...; // 图像的宽度
const int imageHeight = ...; // 图像的高度
// 确定输入绑定的索引值
const int inputIndex = ...; // 输入绑定的索引值
// 分配内存并为每张图像创建输入缓冲区
size_t bufferSize = imageWidth * imageHeight * channels * sizeof(float);
for (int i = 0; i < numImages; ++i) {
cudaMalloc(&buffers[inputIndex + i], bufferSize);
}
inputIndex是输入绑定的索引值,可以使用engine->getNbBindings() 来获取模型输入输出的总绑定数。
nvinfer1::ICudaEngine* engine = ...; // 获取到的TensorRT引擎
const int numBindings = engine->getNbBindings();
std::cout << "Total number of bindings: " << numBindings << std::endl;