1
|
|
|
""" |
2
|
|
|
Summary: |
3
|
|
|
Functions to save and store a model. The current keras |
4
|
|
|
function to do this does not work in python3. Therefore, we |
5
|
|
|
implemented our own functions until the keras functionality has matured. |
6
|
|
|
Example function calls in 'Tutorial mcfly on PAMAP2.ipynb' |
7
|
|
|
""" |
8
|
|
|
from keras.models import model_from_json |
9
|
|
|
import json |
10
|
|
|
import numpy as np |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def savemodel(model, filepath, modelname): |
14
|
|
|
""" Save model to json file and weights to npy file |
15
|
|
|
|
16
|
|
|
Parameters |
17
|
|
|
---------- |
18
|
|
|
model : Keras object |
19
|
|
|
model to save |
20
|
|
|
filepath : str |
21
|
|
|
directory where the data will be stored |
22
|
|
|
modelname : str |
23
|
|
|
name of the model to be used in the filename |
24
|
|
|
|
25
|
|
|
Returns |
26
|
|
|
---------- |
27
|
|
|
json_path : str |
28
|
|
|
Path to json file with architecture |
29
|
|
|
numpy_path : str |
30
|
|
|
Path to npy file with weights |
31
|
|
|
""" |
32
|
|
|
json_string = model.to_json() # save architecture to json string |
33
|
|
|
json_path = filepath + modelname + '_architecture.json' |
34
|
|
|
with open(json_path, 'w') as outfile: |
35
|
|
|
json.dump(json_string, outfile, sort_keys=True, indent=4, |
36
|
|
|
ensure_ascii=False) |
37
|
|
|
wweights = model.get_weights() # get weight from model |
38
|
|
|
numpy_path = filepath + modelname + '_weights' |
39
|
|
|
np.save(numpy_path, |
40
|
|
|
wweights) # save weights in npy file |
41
|
|
|
return json_path, numpy_path |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def loadmodel(filepath, modelname): |
45
|
|
|
""" Load model + weights from json + npy file, respectively |
46
|
|
|
|
47
|
|
|
Parameters |
48
|
|
|
---------- |
49
|
|
|
filepath : str |
50
|
|
|
directory where the data will be stored |
51
|
|
|
modelname : str |
52
|
|
|
name of the model to be used in the filename |
53
|
|
|
|
54
|
|
|
Returns |
55
|
|
|
---------- |
56
|
|
|
model_repro : Keras object |
57
|
|
|
reproduced model |
58
|
|
|
""" |
59
|
|
|
with open(filepath + modelname + '_architecture.json', 'r') as outfile: |
60
|
|
|
json_string_loaded = json.load(outfile) |
61
|
|
|
model_repro = model_from_json(json_string_loaded) |
62
|
|
|
# wweights2 = model_repro.get_weights() |
63
|
|
|
# extracting the weights would give us the untrained/default weights |
64
|
|
|
wweights_recovered = np.load( |
65
|
|
|
filepath + modelname + '_weights.npy') # load the original weights |
66
|
|
|
model_repro.set_weights(wweights_recovered) # now set the weights |
67
|
|
|
return model_repro |
68
|
|
|
|
69
|
|
|
# If we would use standard Keras function, which stores model and weights |
70
|
|
|
# in HDF5 format it would look like code below. However, we did not use this |
71
|
|
|
# because |
72
|
|
|
# https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model |
73
|
|
|
# it is not compatible with default Keras version in python3. |
74
|
|
|
# from keras.models import load_model |
75
|
|
|
# import h5py |
76
|
|
|
# modelh5=models[0] |
77
|
|
|
# modelh5.save(resultpath+'mymodel.h5') |
78
|
|
|
# del modelh5 |
79
|
|
|
# modelh5 = load_model(resultpath+'mymodel.h5') |
80
|
|
|
|