TMVA Reader With PyTorch CNN

Hi, ROOT experts,

I have two questions, there’s this CNN tutorial:
https://root.cern/doc/master/TMVA__CNN__Classification_8C.html
It also has a Python version:
https://root.cern/doc/master/TMVA__CNN__Classification_8py.html

And I found that there’s an example applying the reader to the trained CNN with respect to the above tutorial in this post below:

I was trying to recreate a Python version of the reader application but I failed because in the CNN classification tutorial the variables were saved in a vector for each entry and then were given to the TMVA methods by using:

loader.AddVariablesArray("vars", imgSize);

However, in the reader application example each bin is added to the TMVA reader as a variable:

std::vector<float> vars(16*16);
   for (unsigned int i = 0; i < vars.size(); i++) {
      std::string varName = "vars";
      reader->AddVariable(varName.c_str(), &vars[i]);
   }

This line below is used in C++ to reconcile the difference:

std::copy(userVars->begin(), userVars->end(), vars.begin());

But I couldn’t make it work in Python, is there a way to do it in Python? (First question)

My Second question is how to use the reader to EvaluateMVA on a test sample by using a trained PyTorch CNN model? We see that there are the PyTorch CNN and the Keras CNN in the tutorial TMVA_CNN_Classification.C, but in the reader example we only see the three TMVA built-in methods TMVA_CNN, TMVA_DNN, and BDT are used, is there a way to also apply the reader while using PyTorch? (In either C++ or Python, or both)

Thank you!

Hello,

Thanks for the post. Let me add @moneta in the loop.

Cheers,
Danilo

Hi @Danilo
Thank you for tagging @moneta for me!

I am asking the second question because I am facing an issue when I run this line below in C++ (this is my actual second question):

TMVA::PyMethodBase::PyInitialize();
cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/martin/opt/anaconda3/envs/deep_learning/lib/libPyMVA.6.28.00.so, 0x0009): symbol not found in flat namespace (_PyCapsule_Type)
Error in <AutoloadLibraryMU>: Failed to load library /Users/martin/opt/anaconda3/envs/deep_learning/lib/libPyMVA.6.28.00.socling JIT session error: Failed to materialize symbols: { (main, { __ZN4TMVA12PyMethodBase12PyInitializeEv }) }

But it doesn’t have this issue when I use Python; however, without knowing how to get the CNN reader application example work in Python, I can’t really proceed to do anything with a trained PyTorch model (my first question).

So the difficulties I have are:
In C++ ROOT interface I’ve failed to load
TMVA::PyMethodBase::PyInitialize()
I can’t proceed to use a trained PyTorch model in this case.
While in Python I can load
TMVA.PyMethodBase.PyInitialize()
But I can’t reconcile the issue of connecting AddVariable() for each var (bin) for the reader and the “vars” saved in
vars = ROOT.std.vector["float"](ntot) for each entry with the use of AddVariableArray().

Update:
I’ve used a different way to install PyTorch so now I can use
TMVA::PyMethodBase::PyInitialize();
with no problem in ROOT, but then I’ve run into a different issue:

==> Start TMVAClassificationApplication
                         : Booking "PyTorch" of type "PyTorch" from s13/2_stages/dataLoader_s13/weights/TMVA_CNN_Classification_PyTorch.weights.xml.
<FATAL>                          : Unknown method in map: PyTorch
***> abort program execution
libc++abi: terminating with uncaught exception of type std::runtime_error: FATAL error

PyTorch still cannot be used when I am working in the ROOT interface. That’s why I really want to use the reader in Python; however, without having the function AddVariableArray() in the reader class, I can’t make the CNN work :smiling_face_with_tear:

Update 2:
I just fixed the issue and now I can use PyTorch in the C++ ROOT interface, so it means that everything is good now, I can use the reader for the PyTorch CNN in C++, but it still doesn’t work in Python without having the function AddVariableArray() in the reader class though.