Completed
Push — master ( 663a17...363033 )
by Christiaan
25s queued 10s
created

loadmodel()   B

Complexity

Conditions 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.5185

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
dl 0
loc 24
ccs 1
cts 7
cp 0.1429
crap 4.5185
rs 8.9713
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 1
from keras.models import model_from_json
9 1
import json
10 1
import numpy as np
11 1
import os
12
13
14 1
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 1
    json_string = model.to_json()  # save architecture to json string
34 1
    json_path = os.path.join(filepath, modelname + '_architecture.json')
35 1
    with open(json_path, 'w') as outfile:
36 1
        json.dump(json_string, outfile, sort_keys=True, indent=4,
37
                  ensure_ascii=False)
38 1
    wweights = model.get_weights()  # get weight from model
39 1
    numpy_path = os.path.join(filepath, modelname + '_weights')
40 1
    np.save(numpy_path,
41
            wweights)  # save weights in npy file
42 1
    return json_path, numpy_path
43
44
45 1
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:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (88/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (88/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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