Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in loading the model with load_weights in Keras

I trained a model in keras (regression) on a Linux platform and saved the model with a model.save_weights("kwhFinal.h5")

And then I was hoping to take my complete saved model to Python 3.6 on my Windows 10 laptop and just use it with IDLE:

from keras.models import load_model

# load weights into new model
loaded_model.load_weights("kwhFinal.h5")
print("Loaded model from disk")

Except I am running into this read only mode ValueError with Keras. Thru pip I installed Keras & Tensorflow on my Windows 10 laptop and researching more online it seems like this other SO post about the same issue, the answer states:

You have to set and define the architecture of your model and then use model.load_weights

But I don't understand this enough to recreate the code from the answer (link to git gist). This is my Keras script below that I ran on a Linux OS to create the model. Can someone give me a tip on how define architecture so I can use this model to predict on my Windows 10 laptop?

#https://machinelearningmastery.com/custom-metrics-deep-learning-keras-python/
#https://machinelearningmastery.com/save-load-keras-deep-learning-models/
#https://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras import backend
from keras.models import model_from_json
import os



def rmse(y_true, y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))

# load dataset
dataset = pd.read_csv("joinedRuntime2.csv", index_col='Date', parse_dates=True)

print(dataset.shape)
print(dataset.dtypes)
print(dataset.columns)

# shuffle dataset
df = dataset.sample(frac=1.0)

# split into input (X) and output (Y) variables
X = df.drop(['kWh'],1)
Y = df['kWh']

offset = int(X.shape[0] * 0.7)
X_train, Y_train = X[:offset], Y[:offset]
X_test, Y_test = X[offset:], Y[offset:]


model = Sequential()
model.add(Dense(60, input_dim=7, kernel_initializer='normal', activation='relu'))
model.add(Dense(55, kernel_initializer='normal', activation='relu'))
model.add(Dense(50, kernel_initializer='normal', activation='relu'))
model.add(Dense(45, kernel_initializer='normal', activation='relu'))
model.add(Dense(30, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.summary()

model.compile(loss='mse', optimizer='adam', metrics=[rmse])

# train model
history = model.fit(X_train, Y_train, epochs=5, batch_size=1,  verbose=2)

# plot metrics
plt.plot(history.history['rmse'])
plt.title("kWh RSME Vs Epoch")
plt.show()

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("kwhFinal.h5")
print("[INFO] Saved model to disk")

On machine learning mastery they demonstrate also saving YML & Json but I am unsure if this will help to define model architecture...

like image 370
bbartling Avatar asked Mar 07 '19 17:03

bbartling


2 Answers

You are saving the weights, not the whole model. A Model is more than just the weights, including architecture, losses, metrics and etc.

You have two solutions:

1) Go with saving the weights: in this case, in time of model loading, you will need to recreate your model, load the weight and then compile the model. Your code should be something like this:

model = Sequential()
model.add(Dense(60, input_dim=7, kernel_initializer='normal', activation='relu'))
model.add(Dense(55, kernel_initializer='normal', activation='relu'))
model.add(Dense(50, kernel_initializer='normal', activation='relu'))
model.add(Dense(45, kernel_initializer='normal', activation='relu'))
model.add(Dense(30, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.load_weights("kwhFinal.h5")
model.compile(loss='mse', optimizer='adam', metrics=[rmse])

2) Save the whole model by this command:

model.save("kwhFinal.h5")

And during the loading use this command for having your model loaded:

from keras.models import load_model
model=load_model("kwhFinal.h5")
like image 114
alift Avatar answered Nov 12 '22 20:11

alift


Save the model as:

model.save("kwhFinal.h5")

While loading model, you need to add the custom metric function you defined.

model=load_model("kwhFinal.h5",custom_objects={'rmse': rmse})
like image 31
phoenixio Avatar answered Nov 12 '22 22:11

phoenixio