-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model_Implementations.py
executable file
·223 lines (147 loc) · 7.69 KB
/
Model_Implementations.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Full implementation of all methods of Abstract class "Model"
"""
import os
import tensorflow as tf
# tf.compat.v1.disable_eager_execution()
tf.enable_eager_execution()
tf.config.experimental_run_functions_eagerly(True)
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import numpy as np
from tensorflow.keras.layers import AveragePooling2D, BatchNormalization, Dropout, Multiply, Lambda, Input, Dense, Conv2D, MaxPooling2D, Flatten, Activation, UpSampling2D, Concatenate, GaussianNoise,Reshape
from tensorflow.keras.utils import plot_model
from tensorflow.keras import metrics, regularizers, optimizers
from tensorflow.keras.models import Model as KerasModel
from Model import Model
import numpy as np
from tensorflow.keras import losses, metrics
from ClassBlender import ClassBlender
from DataAugmenter import DataAugmenter
from sbp_lwta_con2d_layer import SB_Conv2d
from sbp_lwta_dense_layer import SB_Layer
from lwta_conv2d_activation import LWTA_Conv2D_Activation
from lwta_dense_activation import LWTA_Dense_Activation
#Full architectural definition for all "baseline" models used in the paper
def defineModelBaseline(self):
outputs=[]
self.penultimate = []
self.penultimate2 = []
x = self.input
x = GaussianNoise(self.params_dict['noise_stddev'], input_shape=self.params_dict['inp_shape'])(x)
if (self.TRAIN_FLAG==1):
if self.params_dict['DATA_AUGMENTATION_FLAG']>0:
x = DataAugmenter(self.params_dict['batch_size'])(x)
x = ClassBlender(self.params_dict['blend_factor'], self.params_dict['batch_size'])(x)
x = Lambda(lambda x: tf.clip_by_value(x,-0.5,0.5))(x)
#for some reason the tensor has None dimensions. Since they do not change we are going to put them back
# x.set_shape([x.shape[0], self.params_dict['inp_shape'][0],self.params_dict['inp_shape'][1],x.shape[2]])
# for CIFAR the last dimension is 3. But for MNIST it should be 1.
# x.set_shape([x.shape[0], 32,32,3])
# the same function but for MNIST
x.set_shape([x.shape[0],28,28,1])
for rep in np.arange(self.params_dict['model_rep']):
x = Conv2D(self.params_dict['num_filters_std'][0], (5,5), activation='linear', padding='same', kernel_regularizer=regularizers.l2(self.params_dict['weight_decay']))(x)
x,_ = LWTA_Conv2D_Activation()(x)
if self.params_dict['BATCH_NORMALIZATION_FLAG']>0:
x = BatchNormalization()(x)
x = Conv2D(self.params_dict['num_filters_std'][0], (3,3), strides=(2,2), activation='linear', padding='same')(x)
x,_ = LWTA_Conv2D_Activation()(x)
for rep in np.arange(self.params_dict['model_rep']):
x = Conv2D(self.params_dict['num_filters_std'][1], (3, 3), activation='linear', padding='same', kernel_regularizer=regularizers.l2(self.params_dict['weight_decay']))(x)
x,_ = LWTA_Conv2D_Activation()(x)
if self.params_dict['BATCH_NORMALIZATION_FLAG']>0:
x = BatchNormalization()(x)
x = Conv2D(self.params_dict['num_filters_std'][1], (3,3), strides=(2,2), activation='linear', padding='same')(x)
x,_ = LWTA_Conv2D_Activation()(x)
for rep in np.arange(self.params_dict['model_rep']):
x = Conv2D(self.params_dict['num_filters_std'][2], (3, 3), activation='linear', padding='same', kernel_regularizer=regularizers.l2(self.params_dict['weight_decay']))(x)
x,_ = LWTA_Conv2D_Activation()(x)
if self.params_dict['BATCH_NORMALIZATION_FLAG']>0:
x = BatchNormalization()(x)
x_ = Conv2D(self.params_dict['num_filters_std'][2], (3,3), strides=(2,2), activation='linear', padding='same')(x)
x_,_ = LWTA_Conv2D_Activation()(x_)
x_ = Flatten()(x_)
# x_ = tf.contrib.layers.flatten(x_)
x_ = Dense(128, activation='linear')(x_)
x_,_ = LWTA_Dense_Activation()(x_)
x_ = Dense(64, activation='linear')(x_)
x_,_ = LWTA_Dense_Activation()(x_)
x0 = Dense(64, activation='linear')(x_)
x0,_ = LWTA_Dense_Activation()(x_)
# x1 = Dense(self.params_dict['M'].shape[1], activation='linear', kernel_regularizer=regularizers.l2(0.0))(x0)
x1 = SB_Layer(K=int(self.params_dict['M'].shape[1]//2),U=2,activation='none',sbp=True)(x0)
outputs = [x1]
self.model = KerasModel(inputs=self.input, outputs=outputs)
print(self.model.summary())
# plot_model(self.model, to_file=self.params_dict['model_path'] + '/' + self.params_dict['name'] + '.png')
return outputs
class Model_Softmax_Baseline(Model):
def __init__(self, data_dict, params_dict):
super(Model_Softmax_Baseline, self).__init__(data_dict, params_dict)
def defineModel(self):
return defineModelBaseline(self)
def defineLoss(self, idx):
def loss_fn(y_true, y_pred):
loss = tf.keras.backend.categorical_crossentropy(y_true, y_pred, from_logits=True)
return loss
return loss_fn
def defineMetric(self):
return [metrics.categorical_accuracy]
class Model_Logistic_Baseline(Model):
def __init__(self, data_dict, params_dict):
super(Model_Logistic_Baseline, self).__init__(data_dict, params_dict)
def defineModel(self):
return defineModelBaseline(self)
def defineLoss(self, idx):
def loss_fn(y_true, y_pred):
loss = tf.keras.backend.binary_crossentropy(y_true, y_pred, from_logits=True)
return loss
return loss_fn
def defineMetric(self):
def sigmoid_pred(y_true, y_pred):
corr = tf.to_float((y_pred*(2*y_true-1))>0)
return tf.reduce_mean(corr)
return [sigmoid_pred]
class Model_Tanh_Baseline(Model):
def __init__(self, data_dict, params_dict):
super(Model_Tanh_Baseline, self).__init__(data_dict, params_dict)
def defineModel(self):
return defineModelBaseline(self)
def defineLoss(self, idx):
def hinge_loss(y_true, y_pred):
loss = tf.reduce_mean(tf.maximum(1.0-y_true*y_pred, 0))
return loss
return hinge_loss
def defineMetric(self):
def tanh_pred(y_true, y_pred):
corr = tf.to_float((y_pred*y_true)>0)
return tf.reduce_mean(corr)
return [tanh_pred]
class Model_Logistic_Ensemble(Model):
def __init__(self, data_dict, params_dict):
super(Model_Logistic_Ensemble, self).__init__(data_dict, params_dict)
def defineLoss(self, idx):
def loss_fn(y_true, y_pred):
loss = tf.keras.backend.binary_crossentropy(y_true, y_pred, from_logits=True)
return loss
return loss_fn
def defineMetric(self):
def sigmoid_pred(y_true, y_pred):
corr = tf.to_float((y_pred*(2*y_true-1))>0)
return tf.reduce_mean(corr)
return [sigmoid_pred]
class Model_Tanh_Ensemble(Model):
def __init__(self, data_dict, params_dict):
super(Model_Tanh_Ensemble, self).__init__(data_dict, params_dict)
def defineLoss(self, idx):
def hinge_loss(y_true, y_pred):
loss = tf.reduce_mean(tf.maximum(1.0-y_true*y_pred, 0))
return loss
return hinge_loss
def defineMetric(self):
def hinge_pred(y_true, y_pred):
corr = tf.to_float((y_pred*y_true)>0)
return tf.reduce_mean(corr)
return [hinge_pred]