scientific-computing/FKB

Keras saved mode is not .h5 file

xushanthu-2014 opened this issue · 2 comments

Hi I am planning embedding an ML model trained by keras into a climate model which is written by fortran 90. I checked your package and found that it claimed that it is able to translate saved model to fortran script. But I found that my model is not saved by .h5 format, but a folder. I used

def get_model():
    # Create a simple model.
    inputs = keras.Input(shape=(32,))
    outputs = keras.layers.Dense(1)(inputs)
    model = keras.Model(inputs, outputs)
    model.compile(optimizer="adam", loss="mean_squared_error")
    return model


model = get_model()

# Train the model.
test_input = np.random.random((128, 32))
test_target = np.random.random((128, 1))
model.fit(test_input, test_target)

# Calling `save('my_model')` creates a SavedModel folder `my_model`.
model.save("my_model")

# It can be used to reconstruct the model identically.
reconstructed_model = keras.models.load_model("my_model")

so there is a folder named "my_model", and inside there are assets saved_model.pb variables. So can I still use your package? thanks!

The saved_model.pb refers to the protocol buffer file which contains the graph definition as well as the weights of the model which you can load with something fancy like tf.io.gfile.GFile('saved_model.pb' , 'rb') and tf.Graph() . Better would be to simply save your model in .h5 with model.save("my_model.h5") . Then you can use python convert_weights.py --weights_file path/to/my_model.h5 --output_file path/to/model_config.txt, and follow the rest of instructions.

Thanks for your reply! @aakash30jan! And one thing is that, could you please generate small examples for how to propagating the loaded ml model in Fortran? for example, if I have load the my_model.txt in my individual fortran program demo_example.F90 by call net % load('model_config.txt'), how can I use your mode mod_network to calculate the forward output using some arbitrary inputs? how do I compile your mode using fortran? I saw your example about using build/bin/./test_keras, but if I want to add your mode in my own program demo_example.F90, what should I do? thanks!