I want to use the bach size of image to foward with libtorch, how should i do?

username: xinsuinizhuan
cooked: <p>single image forward:</p>
<p>std::vector<a>std::vector</a><br>
model4_dec::Run(const cv::Mat&amp; img, float conf_threshold, float iou_threshold) {<br>
torch::NoGradGuard no_grad;<br>
std::cout &lt;&lt; “----------New Frame----------” &lt;&lt; std::endl;</p>
<pre><code class="lang-auto">// TODO: check_img_size()

/*** Pre-process ***/

auto start = std::chrono::high_resolution_clock::now();

// keep the original image for visualization purpose
cv::Mat img_input = img.clone();

std::vector&lt;float&gt; pad_info;
pad_info = LetterboxImage(img_input, img_input, cv::Size(INPUT_W, INPUT_H));
const float pad_w = pad_info[0];
const float pad_h = pad_info[1];
const float scale = pad_info[2];

cv::cvtColor(img_input, img_input, cv::COLOR_BGR2RGB);  // BGR -&gt; RGB
img_input.convertTo(img_input, CV_32FC3, 1.0f / 255.0f);  // normalization 1/255
auto tensor_img = torch::from_blob(img_input.data, { 1, img_input.rows, img_input.cols, img_input.channels() }).to(device_);

tensor_img = tensor_img.permute({ 0, 3, 1, 2 }).contiguous();  // BHWC -&gt; BCHW (Batch, Channel, Height, Width)

if (half_) {
	tensor_img = tensor_img.to(torch::kHalf);
}

std::vector&lt;torch::jit::IValue&gt; inputs;
inputs.emplace_back(tensor_img);

auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(end - start);
// It should be known that it takes longer time at first time
std::cout &lt;&lt; "pre-process takes : " &lt;&lt; duration.count() &lt;&lt; " ms" &lt;&lt; std::endl;

/*** Inference ***/
// TODO: add synchronize point
start = std::chrono::high_resolution_clock::now();

// inference
torch::jit::IValue output = module_.forward(inputs);

end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(end - start);
// It should be known that it takes longer time at first time
std::cout &lt;&lt; "inference takes : " &lt;&lt; duration.count() &lt;&lt; " ms" &lt;&lt; std::endl;

/*** Post-process ***/

start = std::chrono::high_resolution_clock::now();
auto detections = output.toTuple()-&gt;elements()[0].toTensor();

// result: n * 7
// batch index(0), top-left x/y (1,2), bottom-right x/y (3,4), score(5), class id(6)
auto result = PostProcessing(detections, pad_w, pad_h, scale, img.size(), conf_threshold, iou_threshold);

end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(end - start);
// It should be known that it takes longer time at first time
std::cout &lt;&lt; "post-process takes : " &lt;&lt; duration.count() &lt;&lt; " ms" &lt;&lt; std::endl;

return result;
</code></pre>
<p>}</p>
<p>but i want to use bach of image to forward:<br>
std::vector<a>std::vector</a> model4_dec::tensor_run(std::vectorcv::Mat&amp; _vecimg, float conf_threshold, float iou_threshold)<br>
{<br>
torch::NoGradGuard no_grad;</p>
<pre><code class="lang-auto">float scale = 1;// std::min(out_w / in_w, out_h / in_h);
int imgwidth = INPUT_W;
int imgheight = INPUT_H;
static float data[BATCH_SIZE * 3 * INPUT_H * INPUT_W];
for (int b = 0; b &lt; _vecimg.size(); b++)
{
	// keep the original image for visualization purpose
	cv::Mat img_input = _vecimg[b].clone();
	imgwidth = img_input.cols;
	imgheight = img_input.rows;
	scale = std::min(INPUT_W / img_input.cols, INPUT_H / img_input.rows);
	if (img_input.empty()) continue;
	cv::Mat pr_img = preprocess_img(img_input, INPUT_W, INPUT_H); // letterbox BGR to RGB
	pr_img.convertTo(pr_img, CV_32FC3, 1.0f / 255.0f);  // normalization 1/255
	int i = 0;
	for (int row = 0; row &lt; INPUT_H; ++row) {
		uchar* uc_pixel = pr_img.data + row * pr_img.step;
		for (int col = 0; col &lt; INPUT_W; ++col) {
			data[b * 3 * INPUT_H * INPUT_W + i] = (float)uc_pixel[1] / 255.0;
			data[b * 3 * INPUT_H * INPUT_W + i + INPUT_H * INPUT_W] = (float)uc_pixel[1] / 255.0;
			data[b * 3 * INPUT_H * INPUT_W + i + 2 * INPUT_H * INPUT_W] = (float)uc_pixel[0] / 255.0;
			uc_pixel += 3;
			++i;
		}
	}
}

auto tensor_img = torch::from_blob(data, { BATCH_SIZE, INPUT_H, INPUT_W, 3 }).to(device_);
tensor_img = tensor_img.permute({ 0, 3, 1, 2 }).contiguous();  // BHWC -&gt; BCHW (Batch, Channel, Height, Width)

if (half_) {
	tensor_img = tensor_img.to(torch::kHalf);
}

std::vector&lt;torch::jit::IValue&gt; inputs;
inputs.emplace_back(tensor_img);

// inference
torch::jit::IValue output = module_.forward(inputs);


/*** Post-process ***/
auto detections = output.toTuple()-&gt;elements()[0].toTensor();

// result: n * 7
// batch index(0), top-left x/y (1,2), bottom-right x/y (3,4), score(5), class id(6)
auto result = PostProcessing(detections, 0, 0, scale, cv::Size(imgwidth, imgheight), conf_threshold, iou_threshold);

return result;
</code></pre>
<p>}</p>

username: ptrblck
cooked: <p>If you’ve already stored the <code>cv::Mat</code>s in a <code>std::vector</code>, you could append the <code>tensor_img</code>s into <code>inputs</code> and pass this batch of tensors to the model.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>