Unable to get the same values when converting from torch::Tensor to cv::Mat

username: chetan_patil
cooked: <p>I am trying to convert a Torch-tensor to OpenCV-Mat <code>int32</code></p>
<pre><code class="lang-auto">torch::Tensor A = torch::randint(0,30,{4,7},torch::TensorOptions().dtype(torch::kInt32));
cv::Mat cv_A(4,7, CV_32SC1);
std::memcpy((void*)cv_A.data, A.data_ptr(),sizeof(torch::kInt32)*A.numel());
</code></pre>
<p>When I print out the results, it shows</p>
<pre><code class="lang-auto"># Torch Tensor
 16  29   8  18   3  17  13
  4  17   0  26  22  21  26
 28  29  28  11  25  19  28
 11  25  13  11  23  20  28
[ CPUIntType{4,7} ]

and 
#OpenCV Mat
[16, 29, 8, 18, 3, 17, 13;
 0, 1, 4, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0]

</code></pre>
<p>The results will not be same in-case of <code>int-16</code>.</p>
<pre><code class="lang-auto">torch::Tensor A = torch::randint(0,30,{4,7},torch::TensorOptions().dtype(torch::kInt16)); 
cv::Mat cv_A(4,7, CV_16SC1);
std::memcpy((void*)cv_A.data, A.data_ptr(),sizeof(torch::kInt16)*A.numel());
</code></pre>
<p>When printed the results for <code>A</code> and <code>cv_A</code> for the above code, I get</p>
<pre><code class="lang-auto">#torch Tensor
 6   6  10   3  26  29   3
  2  24  21  10  27   3   8
 21   8   8  27  19  18  22
 16   2   5   3  28  11  16
[ CPUShortType{4,7} ]

#opencv Mat
[6, 6, 10, 3, 26, 29, 3;
 2, 24, 21, 10, 27, 3, 8;
 0, 0, 0, 0, 0, 0, -26544;
 28670, 21872, 0, 0, 0, 0, 0]

</code></pre>
<p>However, I get the same results when <code>uint8</code> is used.</p>
<pre><code class="lang-auto"> torch::Tensor A = torch::randint(0,30,{4,7},torch::TensorOptions().dtype(torch::kUInt8)); 
 cv::Mat cv_A(4,7, CV_8UC1);
 std::memcpy((void*)cv_A.data, A.data_ptr(),sizeof(torch::kUInt8)*A.numel());
</code></pre>
<pre><code class="lang-auto">#torch Tensor
20  21  22   5  10  21   1
  7   5  22  25  23  16   2
  8   3  15  20  17  16  27
 19  29  25   8   8  26  16
[ CPUByteType{4,7} ]

# OpenCV Mat
[ 20,  21,  22,   5,  10,  21,   1;
   7,   5,  22,  25,  23,  16,   2;
   8,   3,  15,  20,  17,  16,  27;
  19,  29,  25,   8,   8,  26,  16]

</code></pre>
<p>I am using the latest version of Libtorch ie 1.5.1<br>
Can I know what is the issue here ?<br>
Is there something wrong in the datatypes being used ?<br>
P.S I have referred most of the issues posted in the forum and couldn’t come up with a solution for this.</p>

username: chetan_patil
cooked: <p>Found a work-around for this from <a href="https://github.com/pytorch/pytorch/issues/12506" rel="nofollow noopener">here</a><br>
However, the tensor won’t be copied.</p>
<pre><code class="lang-auto">cv::Mat cv_A(4, 7, CV_32SC1, A.data_ptr&lt;int32_t&gt;());
</code></pre>
<p>in the case of int-32 datatype.</p>

username: tom
cooked: <aside class="quote no-group" data-username="chetan_patil" data-post="1" data-topic="91672">
<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/chetan_patil/48/25122_2.png" class="avatar"> chetan_patil:</div>
<blockquote>
<p><code>sizeof(torch::kInt32)</code></p>
</blockquote>
</aside>
<p>This isn’t what you think it is (it is the size of a value of the ScalarType type), you probably want <code>elementSize(torch::Int32)</code>.</p>
<p>Best regards</p>
<p>Thomas</p>