Converting 1-d float vector to (C,H,W) tensor in C++

username: yichong96
cooked: <p>Given a 1-d array in python labelled <code>array</code>, I can convert the array into a tensor using the following code</p>
<pre><code class="lang-auto">tensor = torch.FloatTensor(array) # array with normalised pixel values
tensor = torch.reshape(tensor, (3, 320, 320))
model([tensor]) # run model inference
</code></pre>
<p>How would I convert the 1-d array into a tensor in c++ with (C,H,W) dims given that I have a pointer to the base array address ?</p>
<p><strong>P.S</strong><br>
I have tried <code>at::Tensor tensor = torch::from_blob(array, { 3, input_width, input_height }, at::kFloat);</code> however I believe the tensor output is different from that in python because the model inference in c++ shows differently.</p>

username: tom
cooked: <p><code>from_blob</code> is right, but you have to make sure</p>
<ol>
<li>that your array element datatype is indeed float</li>
<li>using from_blob requires you to keep the pointer <code>array</code> alive for the lifetime of the returned tensor. If that is a problem for you, you might try copying…</li>
<li>I’d probably print the converted tensor, too, just to be sure. I have stumbled over OpenCV giving me BGR in C++ while my model expected the RGB I got in Python and so.</li>
</ol>
<p>Best regards</p>
<p>Thomas</p>
<p>P.S.: Nowadays we use torch.tensor in Python instead of the constructors.</p>

username: yichong96
cooked: <p>Thank you for your help <a class="mention" href="/u/tom">@tom</a></p>

username: yichong96
cooked: <p><a class="mention" href="/u/tom">@tom</a> do you mind explaining what the phrase “without taking ownership of the original data.” means? from the torch::from_blob docmentation.</p>

username: tom
cooked: <aside class="quote no-group" data-username="yichong96" data-post="4" data-topic="126713">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yichong96/48/38982_2.png" class="avatar"> yichong96:</div>
<blockquote>
<p>do you mind explaining what the phrase “without taking ownership of the original data.” means? from the torch::from_blob docmentation.</p>
</blockquote>
</aside>
<p>This is what I meant with</p>
<blockquote>
<p>using from_blob requires you to keep the pointer <code>array</code> alive for the lifetime of the returned tensor.</p>
</blockquote>
<p>So you pass in a pointer to <code>from_blob</code> but PyTorch “not taking ownership” means that PyTorch doesn’t know where it comes from or tries to do anything with it. This means that you as the caller are responsible for the lifetime of the memory blob:</p>
<ul>
<li>making sure the memory is alive while PyTorch has references to the tensor (e.g. if you have a local <code>std::vector&lt;float&gt; v</code> and you use <code>from_blob</code> on its <code>v.data()</code>, you run into trouble if <code>v</code> goes out of scope while the tensor is still in use. (On the other hand, if you do copying with <code>.clone()</code> or computation, like <code>/255.</code>, and don’t use the original anymore, you are OK.</li>
<li>deallocating the memory eventually. PyTorch can help you by signalling when it is done with the tensor returned by <code>from_blob</code> if you pass in a <code>deleter</code> .</li>
</ul>
<p>Best regards</p>
<p>Thomas</p>