Logo ROOT  
Reference Guide
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("python 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:68
void Generate(bool useSession=true, bool useWeightFile=true)
Definition: RModel.cxx:173
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition: RModel.cxx:89
void OutputGenerated(std::string filename="")
Definition: RModel.cxx:516
const std::vector< size_t > & GetTensorShape(std::string name)
Definition: RModel.cxx:47
static void PyInitialize()
Initialize Python interpreter.
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:31
Basic string class.
Definition: TString.h:136
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:656
RModel Parse(std::string filename)
Parser function for translatng Keras .h5 model into a RModel object.
std::string ConvertTypeToString(ETensorType type)
auto * m
Definition: textangle.C:8