/
models.py
383 lines (311 loc) · 14.9 KB
/
models.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
import tensorflow.keras as keras
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Layer
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler, ModelCheckpoint
from pyclustering.cluster.kmedoids import kmedoids
from sklearn.cluster import KMeans
# RBF model main class.
class RBF(Layer):
def __init__(self, embedding_dim, n_keys_per_class, num_classes, kernel_type = "inverse", **kwargs):
self.output_dim = embedding_dim
self.initializer = keras.initializers.TruncatedNormal(mean=0.0, stddev=0.1, seed=None)
self.num_classes = num_classes
self.embedding_dim = embedding_dim
self.n_keys = n_keys_per_class*num_classes
self.n_keys_per_class = n_keys_per_class
self.kernel_type = kernel_type
super(RBF, self).__init__(**kwargs)
def build(self, input_shape):
self.keys = self.add_weight(name='keys',
shape=(self.n_keys_per_class, self.num_classes, self.embedding_dim),
initializer=self.initializer,
trainable=True)
super(RBF, self).build(input_shape)
def call(self, x):
keys2 = tf.reshape(self.keys, (-1, self.embedding_dim))
K = self.kernel(keys2, x)
inner_logits = tf.transpose(tf.reduce_sum(tf.reshape(K, (self.n_keys_per_class, self.num_classes, -1)), axis=0))
sum_inner_logits = tf.reduce_sum(inner_logits, axis=1)
output = inner_logits / tf.reshape(sum_inner_logits, (-1, 1))
return output
def get_keys(self):
return self.get_weights()[0]
def set_keys(self, keys):
self.set_weights([keys])
# Kernel Functions
def kernel(self, keys, x):
return {
'gauss': self.kernel_gauss(keys, x),
'inverse': self.kernel_inverse(keys, x)
}.get(self.kernel_type, self.kernel_inverse(keys, x))
def sq_distance(self, A, B):
''' Square Distance.
'''
row_norms_A = tf.reduce_sum(tf.square(A), axis=1)
row_norms_A = tf.reshape(row_norms_A, [-1, 1]) # Column vector.
row_norms_B = tf.reduce_sum(tf.square(B), axis=1)
row_norms_B = tf.reshape(row_norms_B, [1, -1]) # Row vector.
return row_norms_A - 2 * tf.matmul(A, tf.transpose(B)) + row_norms_B
def kernel_inverse(self, A,B):
''' Inverse Kernel.
'''
d = self.sq_distance(A,B)
o = tf.math.reciprocal(d+1)
return o
def kernel_cos(self, A,B):
''' Cosine Similarity.
'''
normalize_A = tf.nn.l2_normalize(A,1)
normalize_B = tf.nn.l2_normalize(B,1)
cossim = tf.matmul(normalize_B, tf.transpose(normalize_A))
return tf.transpose(cossim)
def kernel_gauss(self, A,B):
''' Gaussian Kernel.
'''
d = self.sq_distance(A,B)
o = tf.exp(-d/100)
return o
# Relaxed Similary / Soft Triple Loss model main class.
class RelaxedSimilarity(Layer):
def __init__(self, emb_size, n_centers, n_classes, gamma=0.1, **kwargs):
self.initializer = keras.initializers.TruncatedNormal(mean=0.0, stddev=0.1, seed=None)
self.emb_size = emb_size
self.gamma = gamma
self.n_centers = n_centers
self.n_classes = n_classes
super(RelaxedSimilarity, self).__init__(**kwargs)
def build(self, input_shape):
self.keys = self.add_weight(name='keys',
shape=(self.n_centers, self.emb_size, self.n_classes),
initializer=self.initializer,
trainable=True)
super(RelaxedSimilarity, self).build(input_shape)
def call(self, X):
X_n = tf.math.l2_normalize(X, axis=1)
W_n = tf.math.l2_normalize(self.keys, axis=1)
inner_logits = tf.einsum('ie,kec->ikc', X_n, W_n)
inner_SoftMax = tf.nn.softmax((1/self.gamma)*inner_logits, axis=1)
output = tf.reduce_sum( tf.multiply(inner_SoftMax, inner_logits), axis=1)
return output
# Loss Functions
def custom_loss(layer, sigma=0.01, custom=1):
''' Create a loss function that adds the MSE loss to the mean of all
squared activations of a specific layer. Also possible single
Cross Entropy according to the 'custom' parameter.
sigma -> Regularization parameter.
custom -> 1 if custom mode. 0 if normal Cross Entropy.
Returns a Loss Function.
'''
def loss(y_true,y_pred):
if(custom==1):
flatten_keys = keys2 = tf.reshape(layer.keys, (-1, layer.embedding_dim))
return keras.losses.categorical_crossentropy(y_true=y_true, y_pred=y_pred)+sigma*tf.reduce_sum(layer.kernel(flatten_keys, flatten_keys))# + sigma*tf.reduce_mean(layer.kernel(layer.keys, layer.keys) , axis=-1)
else:
return keras.losses.categorical_crossentropy(y_true=y_true, y_pred=y_pred)
return loss
def SoftTripleLoss(layer, lamb=5, delta=0.01):
''' Soft Triple Loss.
lambda -> Lambda value according to STL paper.
delta -> Delta Value according to STL paper.
Returns a Loss Function.
'''
def loss(y_true, y_pred):
s = lamb*(y_pred - delta*y_true)
outer_SoftMax = tf.nn.softmax(s)
soft_triple_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.multiply(outer_SoftMax, y_true), axis=1)))
return soft_triple_loss
return loss
# Model Training Functions
def sample_train(x_train, y_train, pct):
''' Sample an specific data percentage of the dataset.
x_train, y_train -> Train data.
pct -> Data Percentage.
'''
print("Train_pct=", pct)
n_train = x_train.shape[0]
idx = np.arange(n_train)
np.random.shuffle(idx)
train_samples = int(pct*n_train)
x_train_pct = x_train[idx][:train_samples]
y_train_pct = y_train[idx][:train_samples]
return x_train_pct, y_train_pct
def print_params(feature_extractor, embedding_dim, n_centers_per_class, n_classes, lr,
sigma, batch_size, epochs, dataset, input_shape, patience):
''' Print current parameters setup of the model.
'''
print( "embedding_dim = ", embedding_dim, "\n",
"n_centers_per_class = ", n_centers_per_class, "\n",
"n_classes = ", n_classes, "\n",
"batch_size = ", batch_size, "\n",
"lr = ", lr, "\n",
"epochs = ", epochs, "\n",
"sigma = ", sigma, "\n",
"n_output = ", n_classes, "\n",
"feature_extractor = ", feature_extractor, "\n",
"dataset = ", dataset, "\n",
"input_shape = ", input_shape, "\n",
"patience = ", patience)
def get_initial_weights(embedding, x, y, n_clusters, n_classes, embedding_dim,
init_method= "KMEANS", n_init=20, max_iter=200):
''' Initialize the weights of the model depending on the given initialization
method (K-means or K-Medoids).
'''
embedding_outputs = embedding.predict(x)
centers = np.zeros([n_clusters, n_classes, embedding_dim])
if init_method=="KMEANS":
for i in range(n_classes):
kmeans = KMeans(n_clusters=n_clusters, random_state=0, n_init= n_init, max_iter =max_iter)
x_class = embedding_outputs[np.where(np.argmax(y,1)==i)]
centers[:, i, :] = kmeans.fit(x_class).cluster_centers_
elif init_method =="KMEDOIDS":
for i in range(n_classes):
x_class = embedding_outputs[np.where(np.argmax(y,1)==i)]
initial_medoids = np.random.randint(low=0, high= x_class.shape[0], size=n_clusters)
cluster = kmedoids(x_class, initial_medoids)
centers_idx = cluster.process().get_medoids()
centers[:, i, :] = embedding_outputs[centers_idx]
else:
raise "Not implemented"
return centers
# Model Construction functions
def construct_models (feature_extractor, embedding_dim, n_centers_per_class,
n_classes, lr, sigma, kernel_type = "inverse"):
''' Creates an RBF Model.
feature_extractor -> Feature stractor used (CONVNET or RESNET).
embedding_dim -> Size of the output Embedding.
n_centers_per_class -> Number of Centers per class.
n_classes -> Number of Classes.
lr -> Learning Rate.
sigma -> Regularization parameter.
kerney_type -> Used kernel (Inverse, Cosine, Gauss).
Returns a training model.
'''
if feature_extractor == "RESNET":
conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(200, 200, 3))
input = layers.Input(shape=( 32,32,3,))
x=layers.UpSampling2D((2,2) )(input)
x=layers.UpSampling2D((2,2))(x)
x=layers.UpSampling2D((2,2))(x)
x=conv_base(x)
x=layers.Flatten()(x)
x=layers.BatchNormalization()(x)
x=layers.Dense(512, activation='relu')(x)
x=layers.Dropout(0.5)(x)
x=layers.BatchNormalization()(x)
x=layers.Dense(embedding_dim, activation='relu')(x)
x=layers.BatchNormalization()(x)
varkeys_output = RBF(embedding_dim, n_centers_per_class, n_classes, kernel_type)(x)
plain_output = layers.Activation('softmax')(layers.Dense(n_classes)(x))
softmax_model = Model(inputs=input, outputs=plain_output)
rbf_model = Model(inputs=input, outputs=varkeys_output)
embeddings = Model(inputs=input, outputs=x)
rbf_model.compile(loss=custom_loss(rbf_model.layers[-1], sigma, 1),
optimizer = optimizers.RMSprop(lr=lr),
metrics=['accuracy'])
softmax_model.compile(loss= keras.losses.categorical_crossentropy,
optimizer = optimizers.RMSprop(lr=lr),
metrics=['accuracy'])
else:
layers_dim=[32, 64, 512]
input = layers.Input(shape=( 32,32,3,))
x = layers.Conv2D(layers_dim[0], (3, 3), padding='same', input_shape=[32,32,3])(input)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[0], (3,3))(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[0], (3, 3))(x)
x = layers.Activation('relu')(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Dropout(0.25)(x)
x = layers.Conv2D(layers_dim[1], (3, 3), padding='same')(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[1], (3, 3))(x)
x = layers.Activation('relu')(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Dropout(0.25)(x)
x = layers.Flatten()(x)
x = layers.Dense(layers_dim[2])(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(embedding_dim)(x)
x = layers.Activation('relu')(x)
x = layers.BatchNormalization()(x)
varkeys_output = RBF(embedding_dim, n_centers_per_class, n_classes, kernel_type)(x)
plain_output = layers.Activation('softmax')(layers.Dense(n_classes)(x))
softmax_model = Model(inputs=input, outputs=plain_output)
rbf_model = Model(inputs=input, outputs=varkeys_output)
embeddings = Model(inputs=input, outputs=x)
rbf_model.compile(loss=custom_loss(rbf_model.layers[-1], sigma, 1),
optimizer = optimizers.RMSprop(lr=lr),
metrics=['accuracy'])
softmax_model.compile(loss= keras.losses.categorical_crossentropy,
optimizer = optimizers.RMSprop(lr=lr),
metrics=['accuracy'])
return rbf_model, softmax_model, embeddings
def construct_model_STL(feature_extractor, embedding_dim, n_centers_per_class,
n_classes, lr, gamma):
''' Creates a Soft Triple Loss Model.
feature_extractor -> Feature stractor used (CONVNET or RESNET).
embedding_dim -> Size of the output Embedding.
n_centers_per_class -> Number of Centers per class.
n_classes -> Number of Classes.
lr -> Learning Rate.
gamma -> Gamma parameter according to STL paper.
Returns a training model.
'''
if feature_extractor == "RESNET":
conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(200, 200, 3))
input = layers.Input(shape=( 32,32,3,))
x=layers.UpSampling2D((2,2) )(input)
x=layers.UpSampling2D((2,2))(x)
x=layers.UpSampling2D((2,2))(x)
x=conv_base(x)
x=layers.Flatten()(x)
x=layers.BatchNormalization()(x)
x=layers.Dense(512, activation='relu')(x)
x=layers.Dropout(0.5)(x)
x=layers.BatchNormalization()(x)
x=layers.Dense(embedding_dim, activation='relu')(x)
x=layers.BatchNormalization()(x)
x = RelaxedSimilarity(embedding_dim, n_centers_per_class, n_classes, gamma)(x)
output = layers.Softmax()(x)
model = Model(inputs=input, outputs=output)
model.compile(optimizer=optimizers.RMSprop(lr=lr), loss=SoftTripleLoss(model.layers[-2]), metrics=['acc'])
else:
layers_dim=[32, 64, 512]
input = layers.Input(shape=( 32,32,3,))
x = layers.Conv2D(layers_dim[0], (3, 3), padding='same', input_shape=[32,32,3])(input)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[0], (3,3))(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[0], (3, 3))(x)
x = layers.Activation('relu')(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Dropout(0.25)(x)
x = layers.Conv2D(layers_dim[1], (3, 3), padding='same')(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(layers_dim[1], (3, 3))(x)
x = layers.Activation('relu')(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Dropout(0.25)(x)
x = layers.Flatten()(x)
x = layers.Dense(layers_dim[2])(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(embedding_dim)(x)
x = layers.Activation('relu')(x)
x = layers.BatchNormalization()(x)
x = RelaxedSimilarity(embedding_dim, n_centers_per_class, n_classes, gamma)(x)
output = layers.Softmax()(x)
model = Model(inputs=input, outputs=output)
model.compile(optimizer=optimizers.RMSprop(lr=lr), loss=SoftTripleLoss(model.layers[-2]), metrics=['acc'])
return model