Loading a keras model in ROOT


ROOT Version: 6.24/08
Platform: CentOS Linux release 7.9.2009


Dear experts,

After training and saving a keras model using a python script, is it possible to load it in ROOT to make C++ functions? I am not sure how to find documentation on this or if TMVA covers it somewhere.
In my case, it would be to use that model in the TTree Project() method. (It would save a lot of time and avoid some for loops).

Thank you for your time.

I gues @moneta can help

Hi,

Given a Keras model you can create using SOFIE and create a C++ code that you can directly use in your code. See the tutorial TMVA_SOFIE_Keras_HiggsModel.C to generate the code given a Keras model and the tutorial TMVA_SOFIE_RDataFrame.C to integrate the generated code in RDataFrame.
If you have further questions please let me know

Lorenzo

1 Like

Hi,

Thank you for your answer Lorenzo, this looks exactly like what I have been looking for!
Do you know of a version of root that has been built with -Dtmva-sofie=ON on cvmfs?

Cheers,
Theo

Hi,
I see for example that /cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib contains libROOTTMVASofie.so and libPyMVA.so needed for generating the code from a Keras model.

Lorenzo

1 Like

Thank you very much! When I run the setup script to use that root build, root does recognise the SOFIE:: namespace and I can get an output.

I do also get this error:

 W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /cvmfs/sft.cern.ch/lcg/releases/MCGenerators/thepeg/2.2.3-22dc4/x86_64-centos7-gcc11-opt/lib/ThePEG:/cvmfs/sft.cern.ch/lcg/releases/MCGenerators/herwig++/7.2.3-c9708/x86_64-centos7-gcc11-opt/lib/Herwig:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/jaxlib/mlir/_mlir_libs:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/torch/lib:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/onnxruntime/capi/:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/contrib/tensor_forest:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/python/framework:/cvmfs/sft.cern.ch/lcg/releases/java/8u362-88cd4/x86_64-centos7-gcc11-opt/jre/lib/amd64:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib64:/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.3.0-ad0f5/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.3.0-ad0f5/x86_64-centos7/lib64:/cvmfs/sft.cern.ch/lcg/releases/binutils/2.37-355ed/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/MCGenerators/thepeg/2.2.3-88592/x86_64-centos7-gcc11-opt/lib/ThePEG:/cvmfs/sft.cern.ch/lcg/releases/MCGenerators/herwig++/7.2.3-35f7a/x86_64-centos7-gcc11-opt/lib/Herwig:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/jaxlib/mlir/_mlir_libs:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/torch/lib:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/onnxruntime/capi/:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/contrib/tensor_forest:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/python/framework:/cvmfs/sft.cern.ch/lcg/releases/java/8u312-80070/x86_64-centos7-gcc11-opt/jre/lib/amd64:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib64:/cvmfs/sft.cern.ch/lcg/views/LCG_102a/x86_64-centos7-gcc11-opt/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.3.0-ad0f5/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.3.0-ad0f5/x86_64-centos7/lib64:/cvmfs/sft.cern.ch/lcg/releases/binutils/2.37-355ed/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/MCGenerators/thepeg/2.2.3-18d03/x86_64-centos7-gcc11-opt/lib/ThePEG:/cvmfs/sft.cern.ch/lcg/releases/MCGenerators/herwig++/7.2.3-94809/x86_64-centos7-gcc11-opt/lib/Herwig:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/jaxlib/mlir/_mlir_libs:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/torch/lib:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/onnxruntime/capi/:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/contrib/tensor_forest:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/tensorflow/python/framework:/cvmfs/sft.cern.ch/lcg/releases/java/8u312-80070/x86_64-centos7-gcc11-opt/jre/lib/amd64:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib64:/cvmfs/sft.cern.ch/lcg/views/LCG_102/x86_64-centos7-gcc11-opt/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.2.0-8a51a/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/gcc/11.2.0-8a51a/x86_64-centos7/lib64:/cvmfs/sft.cern.ch/lcg/releases/binutils/2.37-355ed/x86_64-centos7/lib:/cvmfs/sft.cern.ch/lcg/releases/R/4.1.2-234e4/x86_64-centos7-gcc11-opt/lib64/R/library/readr/rcon:/cvmfs/sft.cern.ch/lcg/releases/R/4.1.2-34737/x86_64-centos7-gcc11-opt/lib64/R/library/readr/rcon:/cvmfs/sft.cern.ch/lcg/releases/R/4.1.2-dbb9a/x86_64-centos7-gcc11-opt/lib64/R/library/readr/rcon
2023-06-02 18:08:20.391489: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Keras Version: 2.8.0
2023-06-02 18:08:49.789096: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2023-06-02 18:08:49.789132: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (lxplus783.cern.ch): /proc/driver/nvidia/version does not exist
2023-06-02 18:08:49.789981: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-06-02 18:08:49.873358: W tensorflow/core/util/tensor_slice_reader.cc:96] Could not open sig_back_model: DATA_LOSS: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
Model has not a defined batch size, assume is 1 - input shape for tensor dense_input : { 1 , 5 }
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace: __pos (which is 18446744073709551615) > this->size() (which is 17)

I am not sure why this error occurs, it seems to assume I want to run something with a GPU using cuda eventhough my network is already trained and only uses CPU.
Would the output still be useable? I’ll have to try it out.

Cheers,
Theo

I think you can ignore this error from Tensorflow. It is trying always to use Cuda, if available, but if it does not succeeded uses CPU.
There is however another error parsing the model, I think, giving an std::out_of_range. Can you maybe share the model so I can have a look ?

Lorenzo

That makes sense.
I would gladly share the model but it looks like the forum does not like binary files. Is there a nice way I can share it with you? It’s a very simple model and maybe I could also just give you the source files used to generate the binary.

This is what is generated by the macro:
sig_back_model.cc (14.6 KB)

Thank you !
Theo

Hello,

Thank you very much for the help Lorenzo @moneta! I have tested both the source filed outputted by the Sofie parser SOFIE::PyKeras::Parse and the output given by the RSofieReader object, used as found in: ROOT: tutorials/tmva/TMVA_SOFIE_RSofieReader.C File Reference. Both agree with the output of the keras model when testing seperately in python. Using either method, the keras model is accessible in c++ straightfowardly.

Thank you again for your time and the help.

Cheers,
Theo