Exemplo n.º 1
0
def load_model(filename: str):
    """Loads the model architecture and weights as a usable Model.
    Requires '<filename>_model.json' and '<filename>_weights.h5'"""

    with open(filename + '_model.json', 'r') as fobj:
        model = model_from_json(fobj.read())

    model.load_weights(filename + '_weights.h5')
    return model
Exemplo n.º 2
0
def load_model():
    # import model
    json_model = open('model/model.json', 'r')
    load_model_json = json_model.read()
    json_model.close()
    loaded_model = model_from_json(load_model_json)

    loaded_model.load_weights('model/model.h5')
    return loaded_model
Exemplo n.º 3
0
print('test after loading model...')

print(model.predict(x_test[:2]))

print('test accuracy :', model.evaluate(x_test, y_test))

plt.scatter(x_test, model.predict(x_test), c='r')

plt.scatter(x_test, y_test, c='b')

plt.show()

#another way to save and load

# only save the weights of model , the structure of model is not saved

model.save_weights('my_model_weights.h5')

# to bulid a new model whose structure is same as before ....

new_model.load_weights('my_model_weights.h5')

# a method to save model structure only ,well-trained weights is not saved

from keras import model_from_json

json_string = model.to_json()

new_mode = model_from_json(json_string)
Exemplo n.º 4
0
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")

# imports

from keras import model_from_json

# opening and store file in a variable

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()

# use Keras model_from_json to make a loaded model

loaded_model = model_from_json(loaded_model_json)

# load weights into new model

loaded_model.load_weights("model.h5")
print("Loaded Model from disk")

# compile and evaluate loaded model

loaded_model.compile(loss='categorical_crossentropy',
                     optimizer='adam',
                     metrics=['accuracy'])