Libtorch c++ equivalent of 'torch.transforms.Compose' function? Please Help

Hye-Min_Moon: <p>I would like to convert image (array) to tensor for Deep learning model inference.</p>
<p>How do I convert to libtorch based C++  from the below code?</p>
<p>img_transforms = transforms.Compose([transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) ?</p>
<p>P.S<br>
I found the below example in online</p>
<p>Tensor CVMatToTensor(cv::Mat mat)<br>
{<br>
std::cout &lt;&lt; “converting cvmat to tensor\n”;<br>
cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);<br>
cv::Mat matFloat;<br>
mat.convertTo(matFloat, CV_32F, 1.0 / 255);<br>
auto size = matFloat.size();<br>
auto nChannels = matFloat.channels();<br>
auto tensor = torch::from_blob(matFloat.data, { 1, size.height, size.width, nChannels });<br>
return tensor.permute({ 0, 3, 1, 2 });<br>
}</p>
<p>but not sure how to implement the 'transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) to C++ using libtorch (or whatevert methods).</p>
<p>Thank You.</p>

ptrblck: <p>Something like this should work:</p>
<pre><code class="lang-auto">std::vector&lt;double&gt; norm_mean = {0.485, 0.456, 0.406};
std::vector&lt;double&gt; norm_std = {0.229, 0.224, 0.225};
input_tensor = torch::data::transforms::Normalize&lt;&gt;(norm_mean, norm_std)(input_tensor);
</code></pre>
<p>defined <a href="https://github.com/pytorch/pytorch/blob/ee44d73e5973ae31475a394e7b5dc0b43d4babca/torch/csrc/api/include/torch/data/transforms/tensor.h#L56-L74">here</a> or you could directly normalize the tensor via <code>input.sub(mean).div(stddev);</code> as seen in the <code>operator()</code> definition.</p>

Hye-Min_Moon: <p>Thank you very much for the help.<br>
I have one more question, I’m new to C++ and how to convert the below code for post-processing the output (Tensor type) of the deep learning model to C++?</p>
<p>code:<br>
output_ = output.cpu().detach().numpy()</p>
<p>Thank you.</p>

ptrblck: <p>You won’t be able to use <code>numpy</code> in <code>libtorch</code> as it’s a Python library so you would need to process the PyTorch tensor instead. I don’t know which operations are applies to <code>output_</code> but would assume you could use the PyTorch equivalent.</p>

Dirk10001: <p>That is not an answer to that question. You cannot do this an a GPU. Directly data.transforms… would work everywhere/everytime.</p>

Dirk10001: <pre><code class="lang-auto">std::vector&lt;float&gt; norm_mean = { 0.485, 0.456, 0.406 };
std::vector&lt;float&gt; norm_std = { 0.229, 0.224, 0.225 };

std::vector&lt;transforms_Compose&gt; transform{
        transforms_Resize(cv::Size(vm["size"].as&lt;size_t&gt;(), vm["size"].as&lt;size_t&gt;()), cv::INTER_LINEAR),  // {IH,IW,C} ===method{OW,OH}===&gt; {OH,OW,C}
        transforms_ToTensor(),                                                                            // Mat Image [0,255] or [0,65535] ===&gt; Tensor Image [0,1]
        transforms_Normalize( transforms::NormalizeImpl(norm_mean,norm_std))                                                                    // [0,1] ===&gt; [-1,1]
    };

transform.insert(transform.begin(), transforms_Grayscale(3));
    
</code></pre>

ptrblck: <p>Why would my code showing a transformation on a tensor not work on a GPU? Could you clarify this, please?</p>

Dirk10001: <p>The code in transform is done by the Dataloader, before training or interference.<br>
If you call it later:</p>
<pre><code class="lang-auto">while (dataloader(data)){
     
        image = std::get&lt;0&gt;(data).to(device);

        input_tensor = torch::data::transforms::Normalize&lt;&gt;(norm_mean, norm_std)(input_tensor);
</code></pre>
<p>std::vector norm-mean and norm_std are not on the GPU. It just rises an exception.</p>
<p>My anser is just the translation from Python. It works the same, same place in code.</p>