Skip to content

Commit

Permalink
[tmva][pymva] Use .h5 file instead of .keras
Browse files Browse the repository at this point in the history
Go back to use .h5 files instead of .keras due to a problem in Keras loading .keras files on MacOS ARM (see https://github.com/keras-team/keras/issues/18278 )
  • Loading branch information
lmoneta committed Sep 14, 2023
1 parent 160d396 commit 8794972
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion math/mathcore/src/FitUtil.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ double FitUtil::EvaluateLogL(const IModelFunction &func, const UnBinData &data,
} else {
// use (-inf +inf)
data.Range().GetRange(&xmin[0],&xmax[0]);
// check if funcition is zero at +- inf
// check if function is zero at +- inf
if (func(xmin.data(), p) != 0 || func(xmax.data(), p) != 0) {
MATH_ERROR_MSG("FitUtil::EvaluateLogLikelihood","A range has not been set and the function is not zero at +/- inf");
return 0;
Expand Down
4 changes: 2 additions & 2 deletions tmva/pymva/test/EmitCustomModel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ y_train=randomGenerator.rand(1,4)\n\
\n\
model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))\n\
model.fit(x_train, y_train, epochs=10, batch_size=1)\n\
model.save('KerasModelForCustomOp.keras')\n";
model.save('KerasModelForCustomOp.h5')\n";

int main(){
Py_Initialize();
Expand All @@ -43,7 +43,7 @@ int main(){
PyRun_SimpleString(pythonSrc);

//Parsing the saved Keras .keras file into RModel object
RModel model = PyKeras::Parse("KerasModelForCustomOp.keras");
RModel model = PyKeras::Parse("KerasModelForCustomOp.h5");
model.Generate();

std::unique_ptr<ROperator> op;
Expand Down
20 changes: 10 additions & 10 deletions tmva/pymva/test/EmitFromKeras.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,54 @@ int main(){
PyRun_SimpleFile(fKerasModels, "generateKerasModels.py");

//Emitting header file for Keras Sequential Model
RModel modelSequential = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelSequential.keras");
RModel modelSequential = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelSequential.h5");
modelSequential.Generate();
modelSequential.OutputGenerated("KerasSequentialModel.hxx");

//Emitting header file for Keras Functional API Model
RModel modelFunctional = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelFunctional.keras");
RModel modelFunctional = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelFunctional.h5");
modelFunctional.Generate();
modelFunctional.OutputGenerated("KerasFunctionalModel.hxx");

//Emitting header file for Keras BatchNorm Model
RModel modelBatchNorm = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelBatchNorm.keras");
RModel modelBatchNorm = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelBatchNorm.h5");
modelBatchNorm.Generate();
modelBatchNorm.OutputGenerated("KerasBatchNormModel.hxx");

#if PY_MAJOR_VERSION >= 3 // parsing of convolutional models supported only for Python3
// Emitting header file for Keras Conv2D Model with valid padding
RModel modelConv2D_Valid = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConv2D_Valid.keras");
RModel modelConv2D_Valid = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConv2D_Valid.h5");
modelConv2D_Valid.Generate();
modelConv2D_Valid.OutputGenerated("KerasConv2D_Valid.hxx");

// Emitting header file for Keras Conv2D Model with valid padding
RModel modelConv2D_Same = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConv2D_Same.keras");
RModel modelConv2D_Same = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConv2D_Same.h5");
modelConv2D_Same.Generate();
modelConv2D_Same.OutputGenerated("KerasConv2D_Same.hxx");
#endif

//Emitting header file for Keras model with Reshape layer
RModel modelReshape = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelReshape.keras");
RModel modelReshape = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelReshape.h5");
modelReshape.Generate();
modelReshape.OutputGenerated("KerasReshapeModel.hxx");

//Emitting header file for Keras model with Concatenate layer
RModel modelConcatenate = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConcatenate.keras");
RModel modelConcatenate = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelConcatenate.h5");
modelConcatenate.Generate();
modelConcatenate.OutputGenerated("KerasConcatenateModel.hxx");

// Emitting header file for Keras Binary Op Model
RModel modelBinaryOp = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelBinaryOp.keras");
RModel modelBinaryOp = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelBinaryOp.h5");
modelBinaryOp.Generate();
modelBinaryOp.OutputGenerated("KerasBinaryOpModel.hxx");

//Emitting header file for Keras activation functions model
RModel modelActivations = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelActivations.keras");
RModel modelActivations = TMVA::Experimental::SOFIE::PyKeras::Parse("KerasModelActivations.h5");
modelActivations.Generate();
modelActivations.OutputGenerated("KerasActivationsModel.hxx");

// Emitting header file for Swish activation functions model
RModel modelSwish = TMVA::Experimental::SOFIE::PyKeras::Parse("swish_model.keras");
RModel modelSwish = TMVA::Experimental::SOFIE::PyKeras::Parse("swish_model.h5");
modelSwish.Generate();
modelSwish.OutputGenerated("KerasSwish_model.hxx");

Expand Down
20 changes: 10 additions & 10 deletions tmva/pymva/test/generateKerasModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def generateFunctionalModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=2)
model.save('KerasModelFunctional.keras')
model.save('KerasModelFunctional.h5')

def generateSequentialModel():
model=Sequential()
Expand All @@ -39,7 +39,7 @@ def generateSequentialModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=4)
model.save('KerasModelSequential.keras')
model.save('KerasModelSequential.h5')

def generateBatchNormModel():
model=Sequential()
Expand All @@ -53,7 +53,7 @@ def generateBatchNormModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=2)
model.save('KerasModelBatchNorm.keras')
model.save('KerasModelBatchNorm.h5')

def generateConv2DModel_ValidPadding():
model=Sequential()
Expand All @@ -65,7 +65,7 @@ def generateConv2DModel_ValidPadding():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=2)
model.save('KerasModelConv2D_Valid.keras')
model.save('KerasModelConv2D_Valid.h5')

def generateConv2DModel_SamePadding():
model=Sequential()
Expand All @@ -77,7 +77,7 @@ def generateConv2DModel_SamePadding():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=2)
model.save('KerasModelConv2D_Same.keras')
model.save('KerasModelConv2D_Same.h5')

def generateReshapeModel():
model = Sequential()
Expand All @@ -90,7 +90,7 @@ def generateReshapeModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=2)
model.save('KerasModelReshape.keras')
model.save('KerasModelReshape.h5')

def generateConcatModel():
input_1 = Input(shape=(2,))
Expand All @@ -107,7 +107,7 @@ def generateConcatModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit([x1_train,x2_train], y_train, epochs=10, batch_size=1)
model.save('KerasModelConcatenate.keras')
model.save('KerasModelConcatenate.h5')

def generateBinaryOpModel():
input1 = Input(shape=(2, ))
Expand All @@ -124,7 +124,7 @@ def generateBinaryOpModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit([x1_train,x2_train], y_train, epochs=10, batch_size=2)
model.save('KerasModelBinaryOp.keras')
model.save('KerasModelBinaryOp.h5')

def generateActivationModel():
input=Input(shape=(8,))
Expand All @@ -140,7 +140,7 @@ def generateActivationModel():

model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01))
model.fit(x_train, y_train, epochs=10, batch_size=1)
model.save('KerasModelActivations.keras')
model.save('KerasModelActivations.h5')

def generateSwishModel():
# Create the Keras model
Expand All @@ -153,7 +153,7 @@ def generateSwishModel():
optimizer='adam',
metrics=['accuracy'])
# Save the model as an.keras file
model.save('swish_model.keras')
model.save('swish_model.h5')


generateFunctionalModel()
Expand Down
25 changes: 13 additions & 12 deletions tmva/pymva/test/testPyKerasClassification.C
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ int testPyKerasClassification(){
TFile *input = TFile::Open(fname);

// Build model from python file
std::cout << "Generate keras model..." << std::endl;
UInt_t ret;
ret = gSystem->Exec("echo '"+pythonSrc+"' > generateKerasModelClassification.py");
if(ret!=0){
std::cout << "[ERROR] Failed to write python code to file" << std::endl;
return 1;
if (gSystem->AccessPathName("kerasModelClassification.h5")) {
std::cout << "Generate keras model..." << std::endl;
UInt_t ret;
ret = gSystem->Exec("echo '"+pythonSrc+"' > generateKerasModelClassification.py");
if(ret!=0){
std::cout << "[ERROR] Failed to write python code to file" << std::endl;
return 1;
}
ret = gSystem->Exec(TMVA::Python_Executable() + " generateKerasModelClassification.py");
if(ret!=0){
std::cout << "[ERROR] Failed to generate model using python" << std::endl;
return 1;
}
}
ret = gSystem->Exec(TMVA::Python_Executable() + " generateKerasModelClassification.py");
if(ret!=0){
std::cout << "[ERROR] Failed to generate model using python" << std::endl;
return 1;
}

// Setup PyMVA and factory
std::cout << "Setup TMVA..." << std::endl;
TMVA::PyMethodBase::PyInitialize();
Expand Down

0 comments on commit 8794972

Please sign in to comment.