-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_io.py
29 lines (23 loc) · 1.03 KB
/
model_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from keras.models import model_from_json
import json
class ModelIO(object):
def save(self, model, structure_file_name="saved/model.json", weights_file_name="saved/model.h5"):
model_json = model.to_json()
with open(structure_file_name, "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(weights_file_name)
print("Saved model to disk")
def load(self, structure_file_name="saved/model.json", weights_file_name="saved/model.h5"):
# load json and create model
json_file = open(structure_file_name, '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(weights_file_name)
print("Loaded model from disk")
return loaded_model
def saveObject(self, data, filename='hyperparameter.json'):
with open(filename, 'w') as outfile:
json.dump(data, outfile)