forked from mwdhont/SimCLRv1-keras-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimCLR.py
291 lines (255 loc) · 8.66 KB
/
SimCLR.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from tensorflow.keras.layers import Input, Flatten, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import backend as K
from tensorflow.keras.regularizers import l1
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import (
ModelCheckpoint,
EarlyStopping,
ReduceLROnPlateau,
)
from keras.utils.layer_utils import count_params
from datetime import datetime
from SoftmaxCosineSim import SoftmaxCosineSim
from Classifier import Classifier
from swish import swish
class SimCLR:
"""
SimCLR-class contains among others a SimCLR keras-model
The SimCLR_model has
- (2 * batch_size) inputs of shape = (feat_dim)
- base_model which is stored independently to evaluate its feature quality
- flatten_layer
- projection_head
- 1 output = matrix of shape (batch_size x 4.batch_size)
"""
def __init__(
self,
base_model,
input_shape,
batch_size,
feat_dim,
feat_dims_ph,
num_of_unfrozen_layers,
ph_regul=0.005,
lr=1e-4,
loss="categorical_crossentropy",
save_path="models/trashnet",
r=1,
):
self.base_model = base_model
self.input_sh = input_shape
self.batch_size = batch_size
self.feat_dim = feat_dim
self.feat_dims_ph = feat_dims_ph
self.num_layers_ph = len(feat_dims_ph)
self.num_of_unfrozen_layers = num_of_unfrozen_layers
self.ph_regul = ph_regul
self.lr = lr
self.optimizer = Adam(lr, amsgrad=True)
self.loss = loss
self.save_path = save_path
self.r = r
# Different layers around base_model
self.flatten_layer = Flatten()
self.soft_cos_sim = SoftmaxCosineSim(
batch_size=self.batch_size, feat_dim=self.feat_dim
)
# Projection head
self.ph_l = []
for j in range(self.num_layers_ph):
if j < self.num_layers_ph - 1:
self.ph_l.append(
Dense(
feat_dims_ph[j],
activation="swish",
kernel_regularizer=l1(ph_regul),
)
)
else:
self.ph_l.append(
Dense(feat_dims_ph[j], kernel_regularizer=l1(ph_regul))
)
self.SimCLR_model = self.build_model()
def build_model(self):
""" Building SimCLR_model
"""
for layer in self.base_model.layers[: -self.num_of_unfrozen_layers]:
layer.trainable = False
for layer in self.base_model.layers[-self.num_of_unfrozen_layers :]:
layer.trainable = True
i = [] # Inputs (# = 2 x batch_size)
f_x = [] # Output base_model
h = [] # Flattened feature representation
g = [] # Projection head
for j in range(self.num_layers_ph):
g.append([])
# Getting learnable building blocks
base_model = self.base_model
ph_l = []
for j in range(self.num_layers_ph):
ph_l.append(self.ph_l[j])
for index in range(2 * self.batch_size):
i.append(Input(shape=self.input_sh))
f_x.append(base_model(i[index]))
h.append(self.flatten_layer(f_x[index]))
for j in range(self.num_layers_ph):
if j == 0:
g[j].append(ph_l[j](h[index]))
else:
g[j].append(ph_l[j](g[j - 1][index]))
o = self.soft_cos_sim(g[-1]) # Output = Last layer of projection head
# Combine model and compile
SimCLR_model = Model(inputs=i, outputs=o)
SimCLR_model.compile(optimizer=self.optimizer, loss=self.loss)
return SimCLR_model
def train(self, data_train, data_val, epochs=10, pr=True):
""" Training the SimCLR model and saving best model with time stamp
Transfers adapted weights to base_model
"""
# Callbacks
checkpoint, earlyStopping, reduce_lr = self.get_callbacks()
# Fit
SimCLR_model = self.SimCLR_model
SimCLR_model.fit(
data_train,
epochs=epochs,
verbose=1,
validation_data=data_val,
callbacks=[checkpoint, earlyStopping, reduce_lr],
)
# Print number of trainable weights
if pr:
self.print_weights()
# Save
self.save_base_model()
def unfreeze_and_train(
self,
data_train,
data_val,
num_of_unfrozen_layers,
r,
lr=1e-4,
epochs=10,
pr=True,
):
""" Changes number of unfrozen layers in the base model and rebuilds it
Training the SimCLR model and saving best model with time stamp
Transfers adapted weights to base_model
"""
# Update parameters
self.num_of_unfrozen_layers = num_of_unfrozen_layers
self.r = r
if self.lr != lr:
self.change_lr(lr)
# (Un)freeze layers of base_model
self.SimCLR_model = self.build_model()
# Print number of trainable weights
if pr:
self.print_weights()
# Train
self.train(data_train, data_val, epochs)
def predict(self, data):
""" SimCLR prediction
"""
return self.SimCLR_model.predict(data)
def save_base_model(self):
""" Save base_model with time stamp
"""
self.base_model.save(
self.save_path
+ "/base_model/base_model_round_"
+ str(self.r)
+ ".h5"
)
def change_lr(self, lr):
""" Changing learning rate of SimCLR_model
"""
self.lr = lr
K.set_value(self.SimCLR_model.optimizer.learning_rate, self.lr)
def get_callbacks(self):
""" Returns callbacks used while training
"""
# Time stamp for checkpoint
now = datetime.now()
dt_string = now.strftime("_%m_%d_%Hh_%M")
checkpoint = ModelCheckpoint(
self.save_path + "/SimCLR/SimCLR" + dt_string + ".h5",
monitor="val_loss",
verbose=1,
save_best_only=True,
save_weights_only=False,
mode="auto",
)
earlyStopping = EarlyStopping(
monitor="val_loss",
patience=10,
verbose=0,
mode="auto",
restore_best_weights=True,
)
reduce_lr = ReduceLROnPlateau(
monitor="val_loss", patience=5, verbose=1, factor=0.5,
)
return checkpoint, earlyStopping, reduce_lr
def print_weights(self):
""" Function to print (non)-learnable weights
Helps checking unfreezing process
"""
trainable_count = count_params(self.SimCLR_model.trainable_weights)
non_trainable_count = count_params(
self.SimCLR_model.non_trainable_weights
)
print(f"trainable parameters: {round(trainable_count/1e6,2)} M.")
print(
f"non-trainable parameters: {round(non_trainable_count/1e6,2)} M."
)
def train_NL_and_evaluate(
self,
dfs,
batch_size,
params_generator,
fraction,
class_labels,
reg_dense=0.005,
reg_out=0.005,
nums_of_unfrozen_layers=[5, 5, 6, 7],
lrs=[1e-3, 1e-4, 5e-5, 1e-5],
epochs=[5, 5, 20, 25],
verbose_epoch=0,
verbose_cycle=1,
):
""" Trains and evaluates a nonlinear classifier on top of the base_model
"""
results = {"acc": 0}
for i in range(5):
if verbose_cycle:
print(f"Learning attempt {i+1}")
classifier = Classifier(
base_model=self.base_model,
num_classes=params_generator["num_classes"],
reg_dense=reg_dense,
reg_out=reg_out,
)
data_train, data_val, data_test = classifier.get_generators(
dfs, fraction, batch_size, params_generator
)
classifier.train(
data_train,
data_val,
fraction,
nums_of_unfrozen_layers,
lrs,
epochs,
verbose_epoch,
verbose_cycle,
)
acc, report = classifier.evaluate_on_test(
dfs["test"], data_test, class_labels
)
if results["acc"] < acc:
results["acc"] = acc
results["report"] = report
results["attempt"] = i + 1
print("Best result from attempt", str(results["attempt"]))
print(results["report"])