GenericDict c++

hello,
I’m having troubles to make my inference work in c++.
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.
auto output = module.forward(inputs);

when I std::cout << output I get
{instance_seg_logits: (1,1,.,.) =
Columns 1 to 6 9.2045e-01 9.7627e-01 9.4216e-01 9.7992e-01 9.4655e-01 9.8702e-01
9.5792e-01 9.1308e-01 9.7300e-01 9.4184e-01 9.8030e-01 9.4778e-01
9.5051e-01 9.8474e-01 9.7509e-01 9.8799e-01 9.7339e-01 9.9073e-01



[ CPUFloatType{1,3,256,512} ], binary_seg_pred: (1,1,.,.) =
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
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
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
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



[ CPULongType{1,1,256,512} ], binary_seg_logits: (1,1,.,.) =
Columns 1 to 9 3.9905 3.0078 3.4031 2.7997 3.7476 2.6725 3.2712 2.7263 3.5508
3.0313 3.6712 3.0079 3.5307 2.6315 3.3999 2.6493 3.2987 2.3360
3.9653 3.3707 3.6795 3.0346 3.6641 2.8090 3.4282 2.8890 3.5552
3.0351 3.1473 2.9095 3.1672 2.6434 3.0787 2.3649 2.7400 2.4020
3.8545 3.3851 3.0482 3.0095 3.4703 2.9230 3.0951 3.0093 3.2929
2.9893 3.3971 2.6363 3.2858 2.1782 3.0538 2.3728 3.1791 2.0665

… ]}

what I’m interested in is to get the different fields of this GenericDict, for instance the “binary_seg_pred”…
could someone please let me know how can I exploit GenericDicts?

thanks in advance for your support,
Nerc

You could dict.at(key) to access a specific key in the returned Dict or alternatively these approaches should also work:

  • Dict<key, value>::iterator found = dict.find(key) then compare it against dict.end() to make sure the key was found and access it via found->key() and found->value().
  • iterate the Dict via for (Dict<key, value>::iterator iter = dict.begin(); iter != dict.end(); ++iter) and use iter->value() inside the loop.

Thanks for your prompt response.
the solution suggested does not work and doesnt compile:
I get the following compile error: " ‘struct c10::IValue’ has no member named ‘at’ "
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”
erminate called after throwing an instance of ‘c10::Error’
what(): isList()INTERNAL ASSERT FAILED at “…/libtorch/include/ATen/core/ivalue_inl.h”:1376, please report a bug to PyTorch.

It seems you are not working with a Dict as previously stated so could you explain how your data refers to:

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”).
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.

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”)
thanks.