GenericDict c++

username: nerc
cooked: <p>hello,<br>
I’m having troubles to make my inference work in c++.<br>
I have exported my model from python and I import it in my c++ program and run the forward pass on a image tensor, so far everything is ok but at the output of my network a get a genericDict that I dont know how to exploit it, I have tried all I find on google on GenericDict, but none solves my problem.<br>
auto output = module.forward(inputs);</p>
<p>when I std::cout &lt;&lt; output I get<br>
{instance_seg_logits: (1,1,.,.) =<br>
Columns 1 to 6  9.2045e-01  9.7627e-01  9.4216e-01  9.7992e-01  9.4655e-01  9.8702e-01<br>
9.5792e-01  9.1308e-01  9.7300e-01  9.4184e-01  9.8030e-01  9.4778e-01<br>
9.5051e-01  9.8474e-01  9.7509e-01  9.8799e-01  9.7339e-01  9.9073e-01<br>
…<br>
…<br>
…<br>
[ CPUFloatType{1,3,256,512} ], binary_seg_pred: (1,1,.,.) =<br>
Columns 1 to 26  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0<br>
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0<br>
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0<br>
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0<br>
…<br>
…<br>
…<br>
[ CPULongType{1,1,256,512} ], binary_seg_logits: (1,1,.,.) =<br>
Columns 1 to 9  3.9905  3.0078  3.4031  2.7997  3.7476  2.6725  3.2712  2.7263  3.5508<br>
3.0313  3.6712  3.0079  3.5307  2.6315  3.3999  2.6493  3.2987  2.3360<br>
3.9653  3.3707  3.6795  3.0346  3.6641  2.8090  3.4282  2.8890  3.5552<br>
3.0351  3.1473  2.9095  3.1672  2.6434  3.0787  2.3649  2.7400  2.4020<br>
3.8545  3.3851  3.0482  3.0095  3.4703  2.9230  3.0951  3.0093  3.2929<br>
2.9893  3.3971  2.6363  3.2858  2.1782  3.0538  2.3728  3.1791  2.0665<br>
…<br>
… ]}</p>
<p>what I’m interested in is to get the different fields of this GenericDict, for instance the “binary_seg_pred”…<br>
could someone please let me know how can I exploit GenericDicts?</p>
<p>thanks in advance for your support,<br>
Nerc</p>

username: ptrblck
cooked: <p>You could <code>dict.at(key)</code> to access a specific <code>key</code> in the returned <code>Dict</code> or alternatively these approaches should also work:</p>
<ul>
<li>
<code>Dict&lt;key, value&gt;::iterator found = dict.find(key)</code> then compare it against <code>dict.end()</code> to make sure the <code>key</code> was found and access it via <code>found-&gt;key()</code> and <code>found-&gt;value()</code>.</li>
<li>iterate the <code>Dict</code> via <code>for (Dict&lt;key, value&gt;::iterator iter = dict.begin(); iter != dict.end(); ++iter)</code> and use <code>iter-&gt;value()</code> inside the loop.</li>
</ul>

username: nerc
cooked: <p>Thanks for your prompt response.<br>
the solution suggested does not work and doesnt compile:<br>
I get the following compile error: " ‘struct c10::IValue’ has no member named ‘at’ "<br>
if I try transform output using output.toList() or output.toTuple(), it does compile but I get a “core dumped” with a c10::Error, and the message is “EXpected GenericList but got GenericDict”<br>
erminate called after throwing an instance of ‘c10::Error’<br>
what():  isList()INTERNAL ASSERT FAILED at “…/libtorch/include/ATen/core/ivalue_inl.h”:1376, please report a bug to PyTorch.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="nerc" data-post="3" data-topic="178178">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/919ad9/48.png" class="avatar"> nerc:</div>
<blockquote>
<p>I get the following compile error: " ‘struct c10::IValue’ has no member named ‘at’ "</p>
</blockquote>
</aside>
<p>It seems you are not working with a <code>Dict</code> as previously stated so could you explain how your data refers to:</p>
<aside class="quote no-group" data-username="nerc" data-post="1" data-topic="178178">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/919ad9/48.png" class="avatar"> nerc:</div>
<blockquote>
<p>what I’m interested in is to get the different fields of this GenericDict, for instance the “binary_seg_pred”…<br>
could someone please let me know how can I exploit GenericDicts?</p>
</blockquote>
</aside>

username: nerc
cooked: <p>I’m using a network which has been trained in python and I have exported it in c++  Converting to Torch Script via Annotation and serializing via traced_script_module.save(“my_neural_network.pt”).<br>
I’m loading this pt in c++ and use the forward path but then I’m able to “cout” the output of the forward pass but I dont know how to get the different fields of the output which includes the image I’m interested in.</p>

username: nerc
cooked: <p>problem solved, I first had to convert the output of the forward pass of my network to genericDict, using the method toGenericDict() and the extract the wanted field using at(“key”)<br>
thanks.</p>