-
Notifications
You must be signed in to change notification settings - Fork 40
/
modeltools.py
126 lines (82 loc) · 3.33 KB
/
modeltools.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
from DeepJetCore.customObjects import *
custom_objs = get_custom_objects()
def getLayer(model, name):
for layer in model.layers:
if layer.name == name:
return layer
def printLayerInfosAndWeights(model, noweights=False):
for layer in model.layers:
g=layer.get_config()
h=layer.get_weights()
print (g)
if noweights: continue
print (h)
def fixLayersContaining(m, fixOnlyContaining, invert=False):
import collections.abc
if isinstance(fixOnlyContaining, collections.abc.Sequence) and not isinstance(fixOnlyContaining, str):
isseq=True
else:
isseq=False
if not isseq:
fixOnlyContaining=[fixOnlyContaining]
if invert:
for layidx in range(len(m.layers)):
m.get_layer(index=layidx).trainable=False
for layidx in range(len(m.layers)):
for ident in fixOnlyContaining:
if len(ident) and ident in m.get_layer(index=layidx).name:
m.get_layer(index=layidx).trainable=True
else:
for layidx in range(len(m.layers)):
for ident in fixOnlyContaining:
if len(ident) and ident in m.get_layer(index=layidx).name:
m.get_layer(index=layidx).trainable=False
return m
def set_trainable(m, patterns, value):
if isinstance(patterns, basestring):
patterns = [patterns]
for layidx in range(len(m.layers)):
name = m.get_layer(index=layidx).name
if any(i in name for i in patterns):
m.get_layer(index=layidx).trainable = value
return m
def setAllTrainable(m, val=True):
for layidx in range(len(m.layers)):
m.get_layer(index=layidx).trainable = val
return m
def loadModelAndFixLayers(filename,fixOnlyContaining):
#import keras
from keras.models import load_model
m=load_model(filename, custom_objects=custom_objs)
fixLayersContaining(m, fixOnlyContaining)
return m
def load_model(filename):
from keras.models import load_model
model=load_model(filename, custom_objects=custom_objs)
return model
def apply_weights_where_possible(target_model, weight_model):
for layer_a in target_model.layers:
for layer_b in weight_model.layers:
if layer_a.name == layer_b.name:
try:
layer_a.set_weights(layer_b.get_weights())
print('using weights from ', layer_a.name)
except:
print('unable to copy weights for layer ', layer_a.name)
#print(layer_a.weights,'\n',layer_b.weights)
return target_model
################# wrappers for keras models in DJC
import tensorflow as tf
class DJCKerasModel(tf.keras.models.Model):
'''
Base class to implement automatic shape feeding as in DJC
Interfaces smoothly with training_base
'''
def __init__(self,*args,**kwargs):
super(DJCKerasModel, self).__init__(*args,dynamic=False,**kwargs)
self.keras_input_shapes=None
self._is_djc_keras_model = True
def setInputShape(self,keras_inputs):
self.keras_input_shapes=[i.shape for i in keras_inputs]
def build(self,input_shapes):
super(DJCKerasModel,self).build(self.keras_input_shapes)