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
|
|
|
import os |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def savemodel(model, filepath, modelname): |
15
|
|
|
""" Save model to json file and weights to npy file |
16
|
|
|
|
17
|
|
|
Parameters |
18
|
|
|
---------- |
19
|
|
|
model : Keras object |
20
|
|
|
model to save |
21
|
|
|
filepath : str |
22
|
|
|
directory where the data will be stored |
23
|
|
|
modelname : str |
24
|
|
|
name of the model to be used in the filename |
25
|
|
|
|
26
|
|
|
Returns |
27
|
|
|
---------- |
28
|
|
|
json_path : str |
29
|
|
|
Path to json file with architecture |
30
|
|
|
numpy_path : str |
31
|
|
|
Path to npy file with weights |
32
|
|
|
""" |
33
|
|
|
json_string = model.to_json() # save architecture to json string |
34
|
|
|
json_path = os.path.join(filepath, modelname + '_architecture.json') |
35
|
|
|
with open(json_path, 'w') as outfile: |
36
|
|
|
json.dump(json_string, outfile, sort_keys=True, indent=4, |
37
|
|
|
ensure_ascii=False) |
38
|
|
|
wweights = model.get_weights() # get weight from model |
39
|
|
|
numpy_path = os.path.join(filepath, modelname + '_weights') |
40
|
|
|
np.save(numpy_path, |
41
|
|
|
wweights) # save weights in npy file |
42
|
|
|
return json_path, numpy_path |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def loadmodel(filepath, modelname): |
46
|
|
|
""" Load model + weights from json + npy file, respectively |
47
|
|
|
|
48
|
|
|
Parameters |
49
|
|
|
---------- |
50
|
|
|
filepath : str |
51
|
|
|
directory where the data will be stored |
52
|
|
|
modelname : str |
53
|
|
|
name of the model to be used in the filename |
54
|
|
|
|
55
|
|
|
Returns |
56
|
|
|
---------- |
57
|
|
|
model_repro : Keras object |
58
|
|
|
reproduced model |
59
|
|
|
""" |
60
|
|
|
with open(os.path.join(filepath, modelname + '_architecture.json'), 'r') as outfile: |
|
|
|
|
61
|
|
|
json_string_loaded = json.load(outfile) |
62
|
|
|
model_repro = model_from_json(json_string_loaded) |
63
|
|
|
# wweights2 = model_repro.get_weights() |
64
|
|
|
# extracting the weights would give us the untrained/default weights |
65
|
|
|
wweights_recovered = np.load( |
66
|
|
|
os.path.join(filepath, modelname + '_weights.npy')) # load the original weights |
|
|
|
|
67
|
|
|
model_repro.set_weights(wweights_recovered) # now set the weights |
68
|
|
|
return model_repro |
69
|
|
|
|
70
|
|
|
# If we would use standard Keras function, which stores model and weights |
71
|
|
|
# in HDF5 format it would look like code below. However, we did not use this |
72
|
|
|
# because |
73
|
|
|
# https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model |
74
|
|
|
# it is not compatible with default Keras version in python3. |
75
|
|
|
# from keras.models import load_model |
76
|
|
|
# import h5py |
77
|
|
|
# modelh5=models[0] |
78
|
|
|
# modelh5.save(resultpath+'mymodel.h5') |
79
|
|
|
# del modelh5 |
80
|
|
|
# modelh5 = load_model(resultpath+'mymodel.h5') |
81
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.