Tensor to Mat is inconsistent!

NickKao: <p>I have been stuck in a place for a few days, please help, thank you.</p>
<p>I need to convert tensor to mat<br>
Tenser.size()=[288, 384]<br>
Tenser.dtype()=float</p>
<p>However, when I compare the results before and after the conversion, the content of the comparison is inconsistent. May I be mistaken there?</p>
<pre><code class="lang-auto">   cout &lt;&lt; "score_text sizes=" &lt;&lt; score_text.sizes() &lt;&lt; "\n";//[288, 384]
   cout &lt;&lt; "score_text dtype=" &lt;&lt; score_text.dtype() &lt;&lt; "\n";  //float


	cv::Mat textmap(score_text.size(0), score_text.size(1), CV_32FC1);
	std::memcpy((void *)textmap.data, score_text.data_ptr(), sizeof(torch::kFloat32) * score_text.numel());//sizeof(torch::kFloat32)
	cout &lt;&lt; "textmap rows=" &lt;&lt; textmap.rows &lt;&lt; "\n"; //textmap rows = 288
	cout &lt;&lt; "textmap=" &lt;&lt; textmap.size() &lt;&lt; "\n";//textmap = [384 x 288]

	for (size_t row = 0; row &lt; textmap.rows; row++) {
		for (size_t col = 0; col &lt; textmap.cols; col++) {
			float textmap_value = textmap.ptr&lt;float&gt;(row)[col];
			float score_text_value = score_text[row][col].item&lt;float&gt;();
			if (textmap_value != score_text_value) {
				cout &lt;&lt; "textmap=" &lt;&lt; textmap_value &lt;&lt; ",score_text_value=" &lt;&lt; score_text_value &lt;&lt;  "\n";
			}
		}
	}

</code></pre>

NickKao: <p>It seems to be solved, and it seems to be replaced by the following…<br>
But why?</p>
<pre><code class="lang-auto">	cv::Mat textmap(cv::Size(score_text.size(1), score_text.size(0)), CV_32FC1, score_text.data&lt;float&gt;());
</code></pre>

Matej_Kompanek: <p>Have you tried using sizeof(float) instead of sizeof(torch::kFloat32) ?</p>
<pre><code class="lang-auto">		printf("%I64u\n", sizeof(torch::kFloat32)); // outputs 1
		printf("%I64u\n", sizeof(float)); // outputs 4
</code></pre>

NickKao: <p>Yes.<br>
I had tried it.<br>
Same error.<br>
Now<br>
cv::Mat textmap(cv::Size(score_text.size(1), score_text.size(0)), CV_32FC1, score_text.data());<br>
is my choice.</p>

Matej_Kompanek: <p>I used following code to verify:</p>
<pre><code class="lang-auto">	torch::Tensor score_text = torch::ones({ 5, 3 }); // tensor of ones to simulate input

	std::cout &lt;&lt; "score_text sizes=" &lt;&lt; score_text.sizes() &lt;&lt; std::endl;
	std::cout &lt;&lt; "score_text dtype=" &lt;&lt; score_text.dtype() &lt;&lt; std::endl;

	cv::Mat textmap(score_text.size(0), score_text.size(1), CV_32FC1);

	textmap.setTo(0); // zero memory to be able to check whether it was overwritten

	//std::memcpy((void *)textmap.data, score_text.data_ptr(), sizeof(torch::kFloat32) * score_text.numel());
	std::memcpy((void *)textmap.data, score_text.data_ptr(), sizeof(float) * score_text.numel());

	std::cout &lt;&lt; "textmap rows=" &lt;&lt; textmap.rows &lt;&lt; std::endl;
	std::cout &lt;&lt; "textmap=" &lt;&lt; textmap.size() &lt;&lt; std::endl;

	for (size_t row = 0; row &lt; textmap.rows; row++) 
	{
		for (size_t col = 0; col &lt; textmap.cols; col++) 
		{
			float textmap_value = textmap.ptr&lt;float&gt;(row)[col];
			float score_text_value = score_text[row][col].item&lt;float&gt;();

			if (textmap_value != score_text_value) 
			{
				std::cout &lt;&lt; "textmap=" &lt;&lt; textmap_value &lt;&lt; ",score_text_value=" &lt;&lt; score_text_value &lt;&lt; std::endl;
			}
		}
	}

</code></pre>
<p>Why this works -&gt;</p>
<pre><code class="lang-auto">cv::Mat textmap(cv::Size(score_text.size(1), score_text.size(0)), CV_32FC1, score_text.data_ptr())
</code></pre>
<p>OpenCV’s Mat correctly assumes length of data as rows * cols * sizeof(float)</p>

NickKao: <p>I try “sizeof(float)” again.<br>
You are right.<br>
Thanks.</p>