Keras: Save and Load Your Keras Deep Learning Model
Sumber: https://machinelearningmastery.com/save-load-keras-deep-learning-models/
Keras adalah library Python yang sederhana tapi powerful untuk deep learning.
Mengingat bahwa model deep learning dapat memakan waktu berjam-jam, berhari-hari, dan bahkan berminggu-minggu untuk di training, penting untuk mengetahui bagaimana cara menyimpan dan memuatnya dari disk.
Dalam tulisan ini, kita akan menemukan bagaimana kita dapat menyimpan model Keras ke file dan memuatnya lagi untuk membuat prediksi.
Setelah membaca tutorial ini, kita akan tahu:
- Bagaimana cara menyimpan weight model dan arsitektur model dalam file terpisah.
- Bagaimana cara menyimpan model architecture dalam format YAML dan JSON.
- Bagaimana cara menyimpan model weight dan architecture dalam sebuah file untuk penggunaan kemudian.
Tutorial Overview
Keras memisahkan masalah save arsitektur model dan save weight bobot model.
Weight model disimpan ke format HDF5. Ini adalah format grid yang ideal untuk menyimpan array angka multi-dimensi.
Struktur model dapat dijelaskan dan disimpan menggunakan dua format yang berbeda: JSON dan YAML.
Dalam tulisan ini kita akan melihat dua contoh untuk save dan load model ke file:
- Save Model ke JSON.
- Save Model ke YAML.
Setiap contoh juga akan menunjukkan teknik save dan load weight model ke file format HDF5.
Contoh-contoh akan menggunakan network sederhana yang di train menggunakan dilatih pada Pima Indians onset of diabetes binary classification dataset. Ini adalah dataset kecil yang berisi semua data numerik dan mudah digunakan. Kita dapat mengunduh dataset ini dan meletakkannya di direktori kerja kita dengan nama file "pima-indians-diabetes.csv" (download dari: https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv ).
Pastikan anda mempunyai versi Keras terakhir, dan library h5py. Library h5py bisa di instalasi melalui:
sudo pip install h5py
Save Model ke JSON
JSON adalah format file sederhana untuk menggambarkan data secara hierarkis.
Keras memberikan kemampuan untuk menggambarkan model apa pun menggunakan format JSON dengan fungsi to_json(). Ini dapat disimpan ke file dan kemudian dimuat melalui fungsi model_from_json () yang akan membuat model baru dari spesifikasi JSON.
Weight dapat di save langsung dari model menggunakan fungsi save_weights() dan kemudian di load menggunakan fungsi load_weights() .
Contoh di bawah ini melatih dan mengevaluasi model sederhana pada dataset Pima Indian. Model ini kemudian dikonversi ke format JSON dan ditulis ke model.json di direktori lokal. Weight network ditulis ke model.h5 di direktori lokal.
Model dan weight data diambil dari file yang disimpan dan model baru dibuat. Penting untuk mengkompilasi model yang dimuat sebelum digunakan. Ini agar prediksi yang dibuat dengan menggunakan model dapat menggunakan perhitungan efisien yang tepat dari backend Keras.
Model dievaluasi dengan cara yang sama akan mencetak skor evaluasi yang sama.
# MLP for Pima Indians Dataset Serialize to JSON and HDF5 from keras.models import Sequential from keras.layers import Dense from keras.models import model_from_json import numpy import os # fix random seed for reproducibility numpy.random.seed(7) # load pima indians dataset dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:8] Y = dataset[:,8] # create model model = Sequential() model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu')) model.add(Dense(8, kernel_initializer='uniform', activation='relu')) model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, epochs=150, batch_size=10, verbose=0) # evaluate the model scores = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) # serialize model to JSON 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") print("Saved model to disk") # later... # load json and create model json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("model.h5") print("Loaded model from disk") # evaluate loaded model on test data loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
Jalankan contoh akan memberikan output seperti,
acc: 78.78% Saved model to disk Loaded model from disk acc: 78.78%
JSON format dari model akan tampak sebagai berikut,
{ "class_name":"Sequential", "config":{ "name":"sequential_1", "layers":[ { "class_name":"Dense", "config":{ "name":"dense_1", "trainable":true, "batch_input_shape":[ null, 8 ], "dtype":"float32", "units":12, "activation":"relu", "use_bias":true, "kernel_initializer":{ "class_name":"RandomUniform", "config":{ "minval":-0.05, "maxval":0.05, "seed":null } }, "bias_initializer":{ "class_name":"Zeros", "config":{ } }, "kernel_regularizer":null, "bias_regularizer":null, "activity_regularizer":null, "kernel_constraint":null, "bias_constraint":null } }, { "class_name":"Dense", "config":{ "name":"dense_2", "trainable":true, "units":8, "activation":"relu", "use_bias":true, "kernel_initializer":{ "class_name":"RandomUniform", "config":{ "minval":-0.05, "maxval":0.05, "seed":null } }, "bias_initializer":{ "class_name":"Zeros", "config":{ } }, "kernel_regularizer":null, "bias_regularizer":null, "activity_regularizer":null, "kernel_constraint":null, "bias_constraint":null } }, { "class_name":"Dense", "config":{ "name":"dense_3", "trainable":true, "units":1, "activation":"sigmoid", "use_bias":true, "kernel_initializer":{ "class_name":"RandomUniform", "config":{ "minval":-0.05, "maxval":0.05, "seed":null } }, "bias_initializer":{ "class_name":"Zeros", "config":{ } }, "kernel_regularizer":null, "bias_regularizer":null, "activity_regularizer":null, "kernel_constraint":null, "bias_constraint":null } } ] }, "keras_version":"2.2.4", "backend":"tensorflow" }
Save Model ke YAML
Contoh ini hampir sama dengan contoh JSON di atas, kecuali format YAML digunakan untuk spesifikasi model.
Model ini dijelaskan menggunakan YAML, disimpan ke file model.yaml dan kemudian di load ke model baru melalui fungsi model_from_yaml(). Weight ditangani dengan cara yang sama seperti di atas dalam format HDF5 seperti model.h5.
# MLP for Pima Indians Dataset serialize to YAML and HDF5 from keras.models import Sequential from keras.layers import Dense from keras.models import model_from_yaml import numpy import os # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # load pima indians dataset dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:8] Y = dataset[:,8] # create model model = Sequential() model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu')) model.add(Dense(8, kernel_initializer='uniform', activation='relu')) model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, epochs=150, batch_size=10, verbose=0) # evaluate the model scores = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) # serialize model to YAML model_yaml = model.to_yaml() with open("model.yaml", "w") as yaml_file: yaml_file.write(model_yaml) # serialize weights to HDF5 model.save_weights("model.h5") print("Saved model to disk") # later... # load YAML and create model yaml_file = open('model.yaml', 'r') loaded_model_yaml = yaml_file.read() yaml_file.close() loaded_model = model_from_yaml(loaded_model_yaml) # load weights into new model loaded_model.load_weights("model.h5") print("Loaded model from disk") # evaluate loaded model on test data loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) score = loaded_model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
Jalankan contoh akan memberikan output seperti,
acc: 78.78% Saved model to disk Loaded model from disk acc: 78.78%
YAML format dari model akan tampak sebagai berikut,
backend: tensorflow class_name: Sequential config: layers: - class_name: Dense config: activation: relu activity_regularizer: null batch_input_shape: !!python/tuple [null, 8] bias_constraint: null bias_initializer: class_name: Zeros config: {} bias_regularizer: null dtype: float32 kernel_constraint: null kernel_initializer: class_name: RandomUniform config: {maxval: 0.05, minval: -0.05, seed: null} kernel_regularizer: null name: dense_1 trainable: true units: 12 use_bias: true - class_name: Dense config: activation: relu activity_regularizer: null bias_constraint: null bias_initializer: class_name: Zeros config: {} bias_regularizer: null kernel_constraint: null kernel_initializer: class_name: RandomUniform config: {maxval: 0.05, minval: -0.05, seed: null} kernel_regularizer: null name: dense_2 trainable: true units: 8 use_bias: true - class_name: Dense config: activation: sigmoid activity_regularizer: null bias_constraint: null bias_initializer: class_name: Zeros config: {} bias_regularizer: null kernel_constraint: null kernel_initializer: class_name: RandomUniform config: {maxval: 0.05, minval: -0.05, seed: null} kernel_regularizer: null name: dense_3 trainable: true units: 1 use_bias: true name: sequential_1 keras_version: 2.2.4
Save Model Weights dan Architecture Bersamaan
Keras juga mendukung interface yang lebih sederhana untuk menyimpan weight model dan arsitektur model bersamaan menjadi satu file H5.
Saving model dengan cara ini akan mencakup semua yang perlu kita ketahui tentang model, termasuk:
- Model weight.
- Model architecture.
- Model compilation details (loss & metrics).
- Model optimizer state.
Ini berarti bahwa kita dapat memuat dan menggunakan model secara langsung, tanpa harus melakukan kompilasi ulang seperti yang kita lakukan pada contoh di atas.
Catatan: ini adalah cara yang disukai untuk men-save dan me-load model Keras.
How to Save a Keras Model
You can save your model by calling the save() function on the model and specifying the filename.
The example below demonstrates this by first fitting a model, evaluating it and saving it to the file model.h5.
# MLP for Pima Indians Dataset saved to single file from numpy import loadtxt from keras.models import Sequential from keras.layers import Dense # load pima indians dataset dataset = loadtxt("pima-indians-diabetes.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:8] Y = dataset[:,8] # define model model = Sequential() model.add(Dense(12, input_dim=8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(1, activation='sigmoid')) # compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, epochs=150, batch_size=10, verbose=0) # evaluate the model scores = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) # save model and architecture to single file model.save("model.h5") print("Saved model to disk")
Running the example fits the model, summarizes the models performance on the training dataset and saves the model to file.
acc: 77.73% Saved model to disk
We can later load this model from file and use it.
How to Load a Keras Model
Your saved model can then be loaded later by calling the load_model() function and passing the filename. The function returns the model with the same architecture and weights.
In this case, we load the model, summarize the architecture and evaluate it on the same dataset to confirm the weights and architecture are the same.
# load and evaluate a saved model from numpy import loadtxt from keras.models import load_model # load model model = load_model('model.h5') # summarize model. model.summary() # load dataset dataset = loadtxt("pima-indians-diabetes.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:8] Y = dataset[:,8] # evaluate the model score = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
Running the example first loads the model, prints a summary of the model architecture then evaluates the loaded model on the same dataset.
The model achieves the same accuracy score which in this case is 77%.
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_1 (Dense) (None, 12) 108 _________________________________________________________________ dense_2 (Dense) (None, 8) 104 _________________________________________________________________ dense_3 (Dense) (None, 1) 9 ================================================================= Total params: 221 Trainable params: 221 Non-trainable params: 0 _________________________________________________________________ acc: 77.73%
Further Reading
How can I save a Keras model? in the Keras documentation. About Keras models in the Keras documentation.
Summary
In this post, you discovered how to serialize your Keras deep learning models.
You learned how you can save your trained models to files and later load them up and use them to make predictions.
You also learned that model weights are easily stored using HDF5 format and that the network structure can be saved in either JSON or YAML format.