# TMVA read image data for application of a CNN model

**URL:** <https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181>\
**Category:** TMVA\
**Tags:** rdataframe, tmva, root\
**Created:** [March 22, 2023, 7:15pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181 "2023-03-22T19:15:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 22, 2023, 7:15pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/1 "2023-03-22T19:15:08Z")

</div>

Hi!

I am starting to learn how to use TMVA. The input I need to use in my analysis is an image so I need to work with CNN. I have run the example on the TMVA tutorial TMVA\_CNN\_Classification.C. In this CNN example the input data is of type:

```auto
   std::vector<float> *px1 = &x1;
   std::vector<float> *px2 = &x2;
   bkg.Branch("vars", "std::vector<float>", &px1);
   sgn.Branch("vars", "std::vector<float>", &px2);

```

I want to run an application using this CNN example model with the same input data for learning how to perform an application with a CNN model. But I don’t know how to read the image data (std::vector \*px1) on the application step, since there is no AddVariablesArray() on TMVA::Reader.  
I also tried using `TMVA::Experimental::RReader` following this example [ROOT: tutorials/tmva/tmva003\_RReader.C File Reference](https://root.cern/doc/master/tmva003__RReader_8C.html) as follows:

```auto
using namespace TMVA::Experimental;
void TMVA_CNN_ClassificationApplication()
{
   const std::string filename = "images_data_16x16.root";

   // Next, we load the model from the TMVA XML file.
   RReader model("dataset/weights/TMVA_CNN_Classification_TMVA_CNN_GPU.weights.xml");
.
   auto variables = model.GetVariableNames();
   cout<<"Variables names: "<<variables[0]<<endl; // This is not giving the right name, just prints "[0]"

   auto make_histo = [&](const std::string &treename) {
      ROOT::RDataFrame df(treename, filename);
      auto df2 = df.Define("y", Compute<1, float>(model), {"vars"});
      return df2.Histo1D({treename.c_str(), ";CNN score;N_{Events}", 100, -0.1, 1.1}, "y");
   };
   auto sig = make_histo("sig_tree");
   auto bkg = make_histo("bkg_tree");
}

```

But I get the following error:  
root [0]  
Processing TMVA\_CNN\_ClassificationApplication.C…  
Variables names: [0]  
Error in TTreeReaderValueBase::CreateProxy(): The branch vars contains data of type vector. It cannot be accessed by a TTreeReaderValue  
terminate called after throwing an instance of ‘std::runtime\_error’  
what(): An error was encountered while processing the data. TTreeReader status code is: 6

If I change `Compute<1, float>(model)` by `Compute<1, vector<float>>(model)` it compiles but just returns ‘0’.

I appreciate any guidance on how to make an Application from a CNN model that used for training data trees with a single branch of a std::vector of size nh x nw containing the image data.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 23, 2023, 10:04am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/2 "2023-03-23T10:04:41Z")

</div>

I guess @moneta can help.

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 24, 2023, 10:16pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/3 "2023-03-24T22:16:27Z")

</div>

Thanks, @couet and @moneta. I have modified my post to give more details about the problem I am facing. Many thanks for your help.

---

<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:** [March 27, 2023, 9:15am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/4 "2023-03-27T09:15:55Z")

</div>

Hi,  
For the `RReader` class one needs to modify the `Compute` functor passed to `RDataFrame` to handle vector data. Attached you find the example code, defining a new functor to use with `RRdataFrame`.

I also attach the example code for using the Reader class in this case. Since `AddVariableArray` is missing one needs to declare each single vector element, as shown in the attached code. I will add this missing function in the next release.

Best Regards

Lorenzo

[TMVA\_CNN\_RReader.C](https://root-forum.cern.ch/uploads/short-url/gVjUtYzQnsAo74rAEkVXFAdRnk7.C) (908 Bytes)  
[TMVA\_CNN\_ClassificationApplication.C](https://root-forum.cern.ch/uploads/short-url/o0sdeFTMnFnfXNOEekCqQXgQVm6.C) (8.1 KB)

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 27, 2023, 8:48pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/5 "2023-03-27T20:48:47Z")

</div>

Dear Lorenzo,

Many thanks for your reply and the examples on how to read the image data on the CNN application step. I ran the tutorial TMVA\_CNN\_Classification.C on the GPU and then use the same signal tree to perform the application following the examples you provided. But the output of the CNN response is very different from the one obtained at the CNN classification step. The BDT and DNN\_GPU response do agree with their distributions at classification but I cannot get a similar behavior for the CNN\_GPU even if I use the same signal tree used for training. This happens with both Reader and RReader.  
I am using ROOT 6.27/01 and I have added the plots and the code for reference. Can you please help me understand what is happening? I appreciate your help very much!

- From Classification

![image](https://root-forum.cern.ch/uploads/default/original/3X/d/5/d5c4cf73d7ad611f9883321cc6683f1b78905434.png)

- From Application using the same signal tree used at training.

![image](https://root-forum.cern.ch/uploads/default/original/3X/a/a/aade8d9d9073320685b981ce0a256ccb970e83d5.png)  
Note: Repeating this analysis regenerating the data and redoing the training sometimes gives a single peak at 1 and other times at 0 or in both values as in the case shown in the image.  
[TMVA\_CNN\_RReader.C](https://root-forum.cern.ch/uploads/short-url/mihjPm5BXzBLdizPKboVVqGzZlw.C) (878 Bytes)  
[TMVA\_CNN\_ClassificationApplication.C](https://root-forum.cern.ch/uploads/short-url/4Od9bsNNop5gdA9R9o8x6lgljUL.C) (8.2 KB)  
[TMVA\_CNN\_Classification.C](https://root-forum.cern.ch/uploads/short-url/8QTzisSbEC1isuQxZj6VswIX6vR.C) (17.7 KB)

---

<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:** [March 28, 2023, 8:57am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/6 "2023-03-28T08:57:19Z")

</div>

This is strange, can you please attach also the saved xml files

Thanks

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 28, 2023, 1:16pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/7 "2023-03-28T13:16:14Z")

</div>

Dear Lorenzo,

Thank you so much for looking into this. Here are the files. The weights in the file `dataset` gave me a peak at 0 and 1 as in the above image for the CNN\_GPU. Reruning the analysis also gives sometimes just one peak at 0 (`dataset_0`) and other times one peak at 1 (`dataset_1`) for the CNN\_GPU.

[dataset.zip](https://root-forum.cern.ch/uploads/short-url/6ZwoNbHBy7cohngXEgD1KFbF8FQ.zip) (1.4 MB)  
[dataset\_1.zip](https://root-forum.cern.ch/uploads/short-url/7h8JEEEz0cLFH9AZGzc1bcpJMdj.zip) (1.5 MB)  
[dataset\_0.zip](https://root-forum.cern.ch/uploads/short-url/tJr7yAcTmQiXx7uIo1T0qo64GXu.zip) (1.4 MB)

Lilina

---

<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:** [March 28, 2023, 1:25pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/8 "2023-03-28T13:25:34Z")

</div>

Thank you for the files, I will look at them later today.  
If I have understood well when running the training, you get the nice plot above using the TMVA GUI, correct ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 28, 2023, 11:46pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/9 "2023-03-28T23:46:27Z")

</div>

Thanks a lot! Yes the first plot that shows both signal and background is from the TMVA GUI obtained at training with TMVA\_CNN\_Classification.C. The second one is obtained at the application stage with either Reader or RReader using the same signal image data tree used for training.

Lilina

---

<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:** [March 29, 2023, 6:52am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/10 "2023-03-29T06:52:37Z")

</div>

Hello Lilina,  
Which input data are you using ? The same used by the TMVA tutorial that is present in the ROOT repository?

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 29, 2023, 12:04pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/11 "2023-03-29T12:04:45Z")

</div>

I am using the data created by `MakeImagesTree(5000, 16, 16)` function of the TMVA\_CNN\_Classification.C.  
I put the data created with this function on this drive:  
[https://drive.google.com/drive/folders/1NConjXFlacXWIrX9h16cy9Gi-7Nlw\_fj?usp=sharing](https://drive.google.com/drive/folders/1NConjXFlacXWIrX9h16cy9Gi-7Nlw_fj?usp=sharing)

Training on `images_data_16x16.root` gave the weights in the folder `dataset`  
Training on `images_data_16x16_0.root` gave the weights in the folder `dataset_0`  
Training on `images_data_16x16_1.root` gave the weights in the folder `dataset_1`

Thank you so much!

Lilina

---

<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:** [March 29, 2023, 3:27pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/12 "2023-03-29T15:27:30Z")

</div>

Hello Lilina,

Thanks for sharing the file, I can reproduce your problem in the Reader when reading your XML weight files, but I could not when running the training myself on the data you have provided.  
I suspect in some cases the CNN did not train well. Can you please share for one of this cases (dataset\_0 or dataset\_1) the full log printout you have during training and also the TMVA output root file that is used by the TMVAGUI (it should be called `TMVA_CNN_ClassificationOutput.root`) ?  
The Training history is available using the TMVAGUI (item(8) ). It should show if the validation error has correctly decreased to a reasonable value.

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 29, 2023, 5:53pm UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/13 "2023-03-29T17:53:44Z")

</div>

Dear Lorenzo,

Many thanks for your help. Here is the information for `dataset_1`:

- TMVA output root file  
[https://drive.google.com/drive/folders/1NConjXFlacXWIrX9h16cy9Gi-7Nlw\_fj?usp=sharing](https://drive.google.com/drive/folders/1NConjXFlacXWIrX9h16cy9Gi-7Nlw_fj?usp=sharing)
- Training history TMVAGUI (item(8))  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/3/639f702fbc7080af23b3dab040dd5245455d4675.png)
- Training full log printout  
[training\_printout.txt](https://root-forum.cern.ch/uploads/short-url/nh4duuxkJccthSQobe3Sb7xdFa9.txt) (46.9 KB)

---

<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:** [March 30, 2023, 10:19am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/14 "2023-03-30T10:19:56Z")

</div>

Hello Lilian,

Thank you very much for all the files and information. Looking at the training results everything looks good. The training is fine.  
I can reproduce your problem when running the training , only using your input data, on the GPU. Running on a CPU works fine. I will need some time to investigate further this problem to find the cause. For the time being, I would suggest you to use as a workaround the `CPU` architecture. You need to change in `TMVA_CNN_Classification.C`, around line 320 to have as ddnOptions `"Architecture=CPU"`.

Lorenzo

---

<div class="post-metadata">

**Author:** ![lilina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lilina/32/28468_2.png) [@lilina](https://root-forum.cern.ch/u/lilina)\
**Post date:** [March 31, 2023, 2:51am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/15 "2023-03-31T02:51:03Z")

</div>

Hi Lorenzo,

Thanks a lot for investigating this problem, I appreciate it.

Lilina

---

<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:** [March 31, 2023, 8:24am UTC](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/16 "2023-03-31T08:24:30Z")

</div>

Thanks to you for reporting this problem. I have found what is causing it, it is a bug in the Batch Normalisation layer that is not evaluated correctly on CPU when it has ben trained on GPU. I will provide a fix in the next days. In the mean time a issue is open, see [[tmva] Wrong result when evaluating a TMVA CNN a GPU trained model with BNORM layer on CPU · Issue #12589 · root-project/root · GitHub](https://github.com/root-project/root/issues/12589)

Best regards,

Lorenzo
