Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_Keras.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tmva
3/// \notebook -nodraw
4/// This macro provides a simple example for the parsing of Keras .h5 file
5/// into RModel object and further generating the .hxx header files for inference.
6///
7/// \macro_code
8/// \macro_output
9/// \author Sanjiban Sengupta
10
11using namespace TMVA::Experimental;
12
13TString pythonSrc = "\
14import os\n\
15os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n\
16\n\
17import numpy as np\n\
18from tensorflow.keras.models import Model\n\
19from tensorflow.keras.layers import Input,Dense,Activation,ReLU\n\
20from tensorflow.keras.optimizers import SGD\n\
21\n\
22input=Input(shape=(64,),batch_size=4)\n\
23x=Dense(32)(input)\n\
24x=Activation('relu')(x)\n\
25x=Dense(16,activation='relu')(x)\n\
26x=Dense(8,activation='relu')(x)\n\
27x=Dense(4)(x)\n\
28output=ReLU()(x)\n\
29model=Model(inputs=input,outputs=output)\n\
30\n\
31randomGenerator=np.random.RandomState(0)\n\
32x_train=randomGenerator.rand(4,64)\n\
33y_train=randomGenerator.rand(4,4)\n\
34\n\
35model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))\n\
36model.fit(x_train, y_train, epochs=5, batch_size=4)\n\
37model.save('KerasModel.h5')\n";
38
39
40void TMVA_SOFIE_Keras(){
41
42 //Running the Python script to generate Keras .h5 file
44
45 TMacro m;
46 m.AddLine(pythonSrc);
47 m.SaveSource("make_keras_model.py");
48 gSystem->Exec(TMVA::Python_Executable() + " make_keras_model.py");
49
50 //Parsing the saved Keras .h5 file into RModel object
51 SOFIE::RModel model = SOFIE::PyKeras::Parse("KerasModel.h5");
52
53
54 //Generating inference code
55 model.Generate();
56 model.OutputGenerated("KerasModel.hxx");
57
58 //Printing required input tensors
59 std::cout<<"\n\n";
61
62 //Printing initialized tensors (weights)
63 std::cout<<"\n\n";
65
66 //Printing intermediate tensors
67 std::cout<<"\n\n";
69
70 //Checking if tensor already exist in model
71 std::cout<<"\n\nTensor \"dense2bias0\" already exist: "<<std::boolalpha<<model.CheckIfTensorAlreadyExist("dense2bias0")<<"\n\n";
72 std::vector<size_t> tensorShape = model.GetTensorShape("dense2bias0");
73 std::cout<<"Shape of tensor \"dense2bias0\": ";
74 for(auto& it:tensorShape){
75 std::cout<<it<<",";
76 }
77 std::cout<<"\n\nData type of tensor \"dense2bias0\": ";
78 SOFIE::ETensorType tensorType = model.GetTensorType("dense2bias0");
79 std::cout<<SOFIE::ConvertTypeToString(tensorType);
80
81 //Printing generated inference code
82 std::cout<<"\n\n";
83 model.PrintGenerated();
84}
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:70
void Generate(bool useSession=true, bool useWeightFile=true)
Definition RModel.cxx:175
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:91
void OutputGenerated(std::string filename="")
Definition RModel.cxx:525
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:49
static void PyInitialize()
Initialize Python interpreter.
Class supporting a collection of lines with C++ code.
Definition TMacro.h:31
virtual TObjString * AddLine(const char *text)
Add line with text in the list of lines of this macro.
Definition TMacro.cxx:141
Basic string class.
Definition TString.h:136
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:656
TString Python_Executable()
Function to find current Python executable used by ROOT If Python2 is installed return "python" Inste...
auto * m
Definition textangle.C:8