-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchitecture_cnn_periodic.py
255 lines (219 loc) · 10 KB
/
architecture_cnn_periodic.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
import keras.layers
import tensorflow as tf
import tensorflow.keras.backend as K
import numpy as np
from tensorflow.keras.layers import Add
from tensorflow.keras.backend import sum
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
def tf_r2(y_true, y_pred):
SS_res = K.sum(K.square(y_true - y_pred))
SS_tot = K.sum(K.square(y_true-K.mean(y_true)))
return 1 - SS_res/(SS_tot + K.epsilon())
def periodic_padding_flexible(tensor, axis, padding=1):
"""
add periodic padding to a tensor for specified axis
tensor: input tensor
axis: on or multiple axis to pad along, int or tuple
padding: number of cells to pad, int or tuple
return: padded tensor
"""
if isinstance(axis,int):
axis = (axis,)
if isinstance(padding,int):
padding = (padding,)
ndim = len(tensor.shape)
for ax,p in zip(axis,padding):
# create a slice object that selects everything from all axes,
# except only 0:p for the specified for right, and -p: for left
ind_right = [slice(-p,None) if i == ax else slice(None) for i in range(ndim)]
ind_left = [slice(0, p) if i == ax else slice(None) for i in range(ndim)]
right = tensor[ind_right]
left = tensor[ind_left]
middle = tensor
tensor = tf.concat([right,middle,left], axis=ax)
return tensor
class LogLearningRateScheduler(tf.keras.callbacks.LearningRateScheduler):
"""
Make learning rate schedule function for log reduction.
Args:
lr_start (float, optional): Learning rate to start with. The default is 1e-3.
lr_stop (float, optional): Final learning rate at the end of epo. The default is 1e-5.
epochs (int, optional): Total number of epochs to reduce learning rate towards. The default is 100.
epomin (int, optional): Minimum number of epochs at beginning to leave learning rate constant. The default is 10.
Example:
model.fit(callbacks=[LogLearningRateScheduler()])
"""
def __init__(self, lr_start=1e-3, lr_stop=1e-5, epochs=100, epomin=10, verbose=0):
self.lr_start = lr_start
self.lr_stop = lr_stop
self.epochs = epochs
self.epomin = epomin
super(LogLearningRateScheduler, self).__init__(schedule=self.schedule_epoch_lr, verbose=verbose)
def schedule_epoch_lr(self, epoch, lr):
if epoch < self.epomin:
out = self.lr_start
else:
out = np.exp(
float(
np.log(self.lr_start) - (np.log(self.lr_start) - np.log(self.lr_stop)) /
(self.epochs - self.epomin) * (epoch - self.epomin)
)
)
print('lr scheduler', epoch, out)
return float(out)
def get_config(self):
config = super(LogLearningRateScheduler, self).get_config()
config.update({"lr_start": self.lr_start, "lr_stop": self.lr_stop, "epochs": self.epochs, "epomin": self.epomin})
return config
class Conv(tf.keras.layers.Layer):
def __init__(self, input_shape, pool_size=2, strides=2, **kwargs):
super(Conv, self).__init__()
self.layer_conv = []
self.layer_conv += [
tf.keras.layers.Conv2D(
filters=kwargs['n_filters'],
kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']),
padding='same',
input_shape=input_shape,
name="Conf2D_%i"%(0)),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D(pool_size,
strides,
padding='same')]
for i in range(kwargs['n_conv_steps']-1):
self.layer_conv += [
tf.keras.layers.Conv2D(
filters=kwargs['n_filters'],
kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']),
padding='same',
name="Conf2D_%i"%(i+1)),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D(pool_size,
strides,
padding='same')]
def call(self, x):
for i, l in enumerate(self.layer_conv):
x = l(x)
print(f'cnn layer: {l.name} {x.shape}')
print("Final layer")
return x
class ConvPeriodicPadding(tf.keras.layers.Layer):
def __init__(self, input_shape, pool_size=2, strides=2, **kwargs):
super(ConvPeriodicPadding, self).__init__()
self.kernel_size = kwargs['kernel_size']
self.layer_conv = []
self.layer_conv += [
tf.keras.layers.Conv2D(
filters=kwargs['n_filters'],
kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']),
padding='valid',
input_shape=input_shape,
name="Conf2D_%i"%(0)),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D(pool_size=pool_size,
strides=strides,
padding='valid')
# tf.keras.layers.AveragePooling2D(pool_size=pool_size,
# strides=strides,
# padding='valid')
]
for i in range(kwargs['n_conv_steps']-1):
self.layer_conv += [
tf.keras.layers.Conv2D(
filters=kwargs['n_filters'],
kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']),
padding='valid',
name="Conf2D_%i"%(i+1)),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D(pool_size=pool_size,
strides=strides,
padding='valid')
# tf.keras.layers.AveragePooling2D(pool_size=pool_size,
# strides=strides,
# padding='valid')
]
def call(self, x):
print("\n\n ### Start NN")
intermediate_sum = []
for i, l in enumerate(self.layer_conv):
if i%3==0: # conv layer
print(" ### Conv layer")
padding_required = (self.kernel_size - 1) // 2
x = periodic_padding_flexible(x, axis=(1,2), padding=(padding_required,padding_required))
print(f'cnn layer: periodic padding for conf {x.shape}, axis: (1,2), padding: ({padding_required},{padding_required})')
x = l(x)
print(f'cnn layer: {l.name} {x.shape}')
elif i%3==1: # act layer
print(" ### Act layer")
x = l(x)
print(f'cnn layer: {l.name} {x.shape}')
elif i%3==2: # pooling layer
print(" ### Pool layer")
x = periodic_padding_flexible(x, axis=1, padding=1)
print(f'cnn layer: periodic padding for pool {x.shape}, axis: (1), padding: (1)')
x = l(x)
print(f'cnn layer: {l.name} {x.shape}')
intermediate_sum.append(x)
#print(f'Sum list after pooling layer:{intermediate_sum.shape}')
return(x), (intermediate_sum)
#return intermediate_sum
class CustomSum(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(CustomSum, self).__init__()
self.customsum = tf.keras.layers.Conv1D(
input_shape=np.zeros((1, 5, 12, 117)),
kernel_size=kwargs['kernel_size_customsum'],
filters=kwargs['n_filters_customsum'])
def call(self, x):
x = self.customsum(x)
return x
class FC(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(FC, self).__init__()
self.flatten = tf.keras.layers.Flatten()
self.fc1 = tf.keras.layers.Dense(units=kwargs['dense_size'], activation='relu')
self.dropout = tf.keras.layers.Dropout(rate=kwargs['dropout'],)
self.fc2 = tf.keras.layers.Dense(2, activation='linear')
def call(self, x):
x = self.flatten(x)
x = self.fc1(x)
x = self.dropout(x)
x = self.fc2(x)
return x
class Cnnmodel_Shift_Flip(tf.keras.Model):
def __init__(self, input_shape, model_kwargs):
super(Cnnmodel_Shift_Flip, self).__init__()
#print(f'input shape {input_shape}')
self.conv = ConvPeriodicPadding(input_shape=input_shape, pool_size=2, strides=2, **model_kwargs)
self.customsum = CustomSum(**model_kwargs)
self.fc = FC(**model_kwargs)
def call(self, x):
x2, intermediate_states = self.conv(x)
x2_flip, intermediate_states_flip = self.conv(tf.image.flip_up_down(x))
print("shape before summation: ", x2.shape)
x2 = self.customsum(sum(x2, axis=-2))
print("shape after summation and Conv1D: ", x2.shape)
x2_flip = self.customsum(sum(x2_flip, axis=-2))
summed = []
summed_flip = []
for s in intermediate_states:
print("shape of intermediate state:", s.shape)
print("shape of state after sum:", (sum(s, axis=-2)).shape)
summed.append(self.customsum(sum(s, axis=-2)))
print("length of list after summation along y-axis: ", len(summed))
for f in intermediate_states_flip:
print("shape of flip's intermediate state:", f.shape)
print("shape of flip's state after sum:", (sum(f, axis=-2)).shape)
summed_flip.append(self.customsum(sum(f, axis=-2)))
x3 = tf.concat(summed, axis=-2) #shape:(None,119,8)
x3_flip = tf.concat(summed_flip, axis=-2)
print("shape after concatenate / flip and non-flip: ", x3.shape, x3_flip.shape)
x3_add = Add()([x3, x3_flip])
print("shape after Add:", x3_add.shape)
outputs = self.fc(x3_add)
print(" ### End NN\n\n")
print(f'shape of outputs:{outputs.shape}')
print(f'shape of x2:{x2.shape}')
print(f'shape of x3:{x3.shape}')
return outputs