# Inference with TMVA::Reader is very slow when using DNN

**URL:** <https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424>\
**Category:** TMVA\
**Created:** [June 15, 2022, 7:59pm UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424 "2022-06-15T19:59:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebachman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/5e9695/32.png) [@ebachman](https://root-forum.cern.ch/u/ebachman)\
**Post date:** [June 15, 2022, 7:59pm UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/1 "2022-06-15T19:59:12Z")

</div>

Dear TMVA experts,

I trained both a BDT and a DNN (using the PyKeras method) for binary classification with the TMVA::Factory class. The time it took for training and evaluation was reasonable for both methods (a few minutes for training and ~5-10 seconds for the evaluation).  
However when using them in our analysis with the TMVA::Reader class, inference using the DNN seems to be much slower than with the BDT: running the analysis on a subset of the MC-samples takes 3 minutes when using the BDT but ~3.5h when using the DNN. Most of that time is spend running the `EvaluateMVA` method of the TMVA::Reader object.

Is that to be expected or are we doing something wrong? We are using Root 6.24 on Red Hat Enterprise Linux 7.9

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [June 16, 2022, 8:07am UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/2 "2022-06-16T08:07:48Z")

</div>

@moneta could you have a look here?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 16, 2022, 8:32am UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/3 "2022-06-16T08:32:23Z")

</div>

Hi,  
This is expected because you are evaluating a single event with the TMVA Reader class, and you need to call a Python function for every event.  
If you instead you will use the native TMVA DNN it will be fast, since it is all coded in C++.  
The solution is to use the new TMVA SOFIE for evaluating the model. If you have only a dense network you can directly use as input the Keras h5 file, otherwise you need to export your model to ONNX using for example `tf2onnx`.  
See the SOFIE tutorial, [TMVA\_SOFIE\_Keras.C](https://root.cern.ch/doc/master/TMVA __SOFIE__ Keras_8C.html) or [TMVA\_SOFIE\_Keras\_HiggsModel.C](https://root.cern.ch/doc/master/TMVA __SOFIE__ Keras__HiggsModel_8C_source.html) to generate the code for evaluating the model and [TMVA\_SOFIE\_RDataFrame.C](https://root.cern.ch/doc/master/TMVA__SOFIE__RDataFrame_8C.html) to evaluate the model with the RDataFrame.

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![ebachman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/5e9695/32.png) [@ebachman](https://root-forum.cern.ch/u/ebachman)\
**Post date:** [June 21, 2022, 12:02pm UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/5 "2022-06-21T12:02:38Z")

</div>

Hi,  
thank you for your help.  
If I understand correctly, `SOFIE::PyKeras::Parse` currently does not support BatchNormalization and Dropout layers, which we unfortunatelly both need. Therefore I tried first converting the model to ONNX (which worked fine) and then use `SOFIE::RModelParser_ONNX` to parse it. However it seems like our model uses the operator `Mul` which also is not yet supported.

We used the following Python code to generate the Keras model:

```auto
model = keras.models.Sequential()
model.add(keras.layers.BatchNormalization(input_shape=(nVars,)))
for i in range(nHiddenLayers):
    model.add(keras.layers.Dense(nNodesPerLayer, kernel_initializer="he_normal"))
    model.add(keras.layers.LeakyReLU(alpha=0.1))
    model.add(keras.layers.BatchNormalization())
    model.add(keras.layers.Dropout(Dropout))
    model.add(keras.layers.Dense(2,activation="softmax", kernel_initializer="glorot_uniform"))

```

Is there an easy way to check which part of our model results in the need for the `Mul`-operator so that we can see if we can work around that? Thank you in advance!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 21, 2022, 2:55pm UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/6 "2022-06-21T14:55:15Z")

</div>

Hi,  
I think the `Mul` operator comes from the BatchNormalization layer. I have seen already ONNX models obtained from Keras that instead of the BatchNormalization is saved as a `Mul` and an `Add` operator.  
The `Mul` operator is going to be supported soon, we will have a PR in the master for this in the next days. Sometimes you can also simplify the obtained ONNX model using the onyx-simplifier tool , see [GitHub - daquexian/onnx-simplifier: Simplify your onnx model](https://github.com/daquexian/onnx-simplifier).  
If you share a link to your obtained ONNX model, I can have a look at it

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![ebachman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/5e9695/32.png) [@ebachman](https://root-forum.cern.ch/u/ebachman)\
**Post date:** [June 21, 2022, 5:24pm UTC](https://root-forum.cern.ch/t/inference-with-tmva-reader-is-very-slow-when-using-dnn/50424/7 "2022-06-21T17:24:41Z")

</div>

Hi,  
you are right. I looked at the ONNX graph using [https://netron.app/](https://netron.app/) and indeed, the BatchNormalization layer is expressed using `Add` and `Mul`. Unfortunatelly the simplification did not change that. I will send you a PM with a link to the model.

Best regards

Erik
