-
Notifications
You must be signed in to change notification settings - Fork 2
/
func_api_helpers.py
251 lines (198 loc) · 6.67 KB
/
func_api_helpers.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright 2015 Leon Sixt
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from keras.engine.topology import merge
from keras.layers.core import Activation
from keras.utils.layer_utils import layer_from_config
from contextlib import contextmanager
from collections import OrderedDict
import h5py
import json
@contextmanager
def trainable(model, trainable):
"""
Sets all layers in model to trainable and restores the state afterwards.
.. warning::
Be aware, that the keras ``Model.compile`` method is lazy.
You might want to call ``Model._make_train_function`` to force a compilation.
Args:
model: keras model
trainable (bool): set layer.traiable to this value
Example:
.. code:: python
model = Model(x, y)
with trainable(model, False):
# layers of model are now not trainable
# Do something
z = model(y)
[...]
# now the layers of `model` are trainable again
"""
trainables = []
for layer in model.layers:
trainables.append(layer.trainable)
layer.trainable = trainable
yield
for t, layer in zip(trainables, model.layers):
layer.trainable = t
def get_layer(keras_tensor):
"""
Returns the corresponding layer to a keras tensor.
"""
layer = keras_tensor._keras_history[0]
return layer
def sequential(layers, ns=None, trainable=True):
"""
The functional flexible counter part to the keras Sequential model.
Args:
layers (list): Can be a arbitrary nested list of layers.
The layers will be called sequentially. Can contain ``None``'s
ns (optional str): Namespace prefix of the layers
trainable (optional bool): set the layer's trainable attribute to this value.
Returns:
A function that takes a tensor as input, applies all the layers, and
returns the output tensor.
**Simple example:**
Call a list of layers.
.. code:: python
x = Input(shape=(32,))
y = sequential([
Dense(10),
LeakyReLU(0.4),
Dense(10, activation='sigmoid'),
])(x)
m = Model(x, y)
**Advanced example:**
Use a function to construct reoccuring blocks. The ``conv`` functions
returns a nested list of layers. This allows one to nicely combine and stack
different building blocks function.
.. code:: python
def conv(n, depth=2, f=3, activation='relu'):
layers = [
[
Convolution2D(n, f, f, border_mode='same'),
BatchNormalization(),
Activation(activation)
] for _ in range(depth)
]
return layers + [MaxPooling2D()]
x = Input(shape=(32,))
y = sequential([
conv(32),
conv(64),
conv(128),
Flatten(),
Dense(10, activation='sigmoid'),
])(x, ns='classifier')
m = Model(x, y)
"""
def flatten(xs):
for x in xs:
try:
for f in flatten(x):
if f is not None:
yield f
except TypeError:
if x is not None:
yield x
for i, l in enumerate(flatten(layers)):
if not hasattr(l, 'name'):
continue
if ns is not None:
if '.' not in l.name:
name = type(l).__name__.lower()
name = "{:02}_{}".format(i, name)
l.name = ns + '.' + name
l.trainable = trainable
def call(input):
x = input
for l in flatten(layers):
x = l(x)
return x
return call
def concat(tensors, axis=1, **kwargs):
"""
Wrapper around keras merge function.
Args:
tensors: list of keras tensors
axis: concat on this axis
kwargs: passed to the merge function
Returns:
The concatenated tensor
"""
if type(tensors) not in (list, tuple):
return tensors
elif len(tensors) == 1:
return tensors[0]
return merge(tensors, mode='concat', concat_axis=axis,
**kwargs)
def rename_layer(keras_tensor, name):
"""
Renames the layer of the ``keras_tensor``
"""
layer = get_layer(keras_tensor)
layer.name = name
def name_tensor(keras_tensor, name):
"""
Add a layer with this ``name`` that does nothing.
Usefull to mark a tensor.
"""
return Activation('linear', name=name)(keras_tensor)
def keras_copy(obj):
"""
Copies a keras object by using the ``get_config`` method.
"""
config = obj.get_config()
if 'name' in config:
del config['name']
return type(obj)(**config)
def predict_wrapper(func, names):
def wrapper(*args, **kwargs):
out = func(*args, **kwargs)
return OrderedDict(zip(names, out))
return wrapper
def save_model(model, fname, overwrite=False, attrs={}):
"""
Saves the weights and the config of ``model`` in the HDF5 file ``fname``.
The model config is saved as: ``f.attrs["model"] = model.to_json().encode('utf-8')``,
where ``f`` is the HDF5 file.
"""
assert 'layer_names' not in attrs
model.save_weights(fname, overwrite)
f = h5py.File(fname, 'r+')
f.attrs['model'] = model.to_json().encode('utf-8')
for k, v in attrs.items():
if type(v) == str:
v = v.encode('utf-8')
f.attrs[k] = v
f.close()
def load_model(fname, custom_objects={}):
"""
Loads the model and weights from ``fname``. Counterpart to :py:func:`save_model`.
"""
json_config = get_hdf5_attr(fname, 'model').decode('utf-8')
config = json.loads(json_config)
model = layer_from_config(config, custom_objects)
model.load_weights(fname)
return model
def get_hdf5_attr(fname, attr_name, default=None):
"""
Returns the toplevel attribute ``attr_name`` of the hdf5 file ``fname``.
If ``default`` is not None and the attribute is not present, then
``default`` is returned.
"""
with h5py.File(fname, 'r') as f:
if attr_name not in f.attrs and default is not None:
return default
else:
return f.attrs[attr_name]