Referring to this thread, we are loading .xml files generated from TMVA using RReader and RDataFrame for our analysis. While it works fine for BDT, we are having trouble loading a neural network model trained with PyTorch.
Basically, I followed this TMVA PyTorch tutorial to train our model and make the .xml file. When we try to load it with RReader, we get the following error.
root [0] TMVA::Experimental::RReader model("dataset/weights/TMVAClassification_PyTorch.weights.xml");
<FATAL> : Unknown method in map: PyTorch
***> abort program execution
Error in <TRint::HandleTermInput()>: std::runtime_error caught: FATAL error
What might be causing this, and how would we find a way to load the .xml file?
and welcome to the ROOT forum! @moneta can correct me if I’m wrong, but I think RReader covers BDTs while for NN inference you need SOFIE, see the tutorials here (in particular the TMVA_SOFIE_RDataFrame.C).
Thanks for the response. It seems like using SOFIE is the right place to start. Looking at the TMVA_SOFIE_RDataFrame.C tutorial, I see that I first need to generate a header file .hxx in order to load the data into RDataFrame with the trained model.
Since I am using PyTorch, I’ve also referred to this tutorial, TMVA_SOFIE_Pytorch.C, in order to generate the .hxx. I ran the example in SWAN, and this is the error message that I get.
SOFIE::RModel model = SOFIE::PyTorch::Parse("PyTorchModel.pt",inputShapesSequential);
Torch Version: 1.11.0a0+gitbc2c6ed
Python error message:
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: _model_to_graph() got an unexpected keyword argument 'example_outputs'
When I try to do the same with my .pt model in my environment (ROOT version 6.26/04), I still get the identical error message.
root [3] TMVA::Experimental::SOFIE::RModel model = TMVA::Experimental::SOFIE::PyTorch::Parse("trainedModelClassification.pt", inputShapesSequential);
Torch Version: 1.11.0a0+gitbc2c6ed
Python error message:
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: _model_to_graph() got an unexpected keyword argument 'example_outputs'
Error in <TRint::HandleTermInput()>: std::runtime_error caught:
Failed to run python code: graph=_model_to_graph(model,dummyInputs,example_outputs=output)
Hi,
Can you please post your PyTorch model (the .pt file) and I can have look. It is possible we need to convert to ONNX, since the support for parsing from native PyTorch is limited to few operators.
Afterwards you can use generate the header file to be used with RDataFrame in C++.
You can use these lines of code to generate the header file (from tutorial TMVA_SOFIE_ONNX.C) and “.dat” file containing the weights:
You can also do all in Python (parsing ONNX file and compile header on the file) as shown in this tutorial, TMVA_SOFIE_RDataFrame.py.
There is only one issue, you would need to use the master version or one of the latest nightly builds, since one operator in your model (SoftMax) is not part of 6.26.