|
| 1 | +import keras.layers |
| 2 | +import tensorflow as tf |
| 3 | +import tensorflow.keras.backend as K |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from tensorflow.keras.layers import Add |
| 7 | +from tensorflow.keras.backend import sum |
| 8 | +from tensorflow.python.client import device_lib |
| 9 | +print(device_lib.list_local_devices()) |
| 10 | + |
| 11 | +def tf_r2(y_true, y_pred): |
| 12 | + SS_res = K.sum(K.square(y_true - y_pred)) |
| 13 | + SS_tot = K.sum(K.square(y_true-K.mean(y_true))) |
| 14 | + return 1 - SS_res/(SS_tot + K.epsilon()) |
| 15 | + |
| 16 | + |
| 17 | +def periodic_padding_flexible(tensor, axis, padding=1): |
| 18 | + """ |
| 19 | + add periodic padding to a tensor for specified axis |
| 20 | + tensor: input tensor |
| 21 | + axis: on or multiple axis to pad along, int or tuple |
| 22 | + padding: number of cells to pad, int or tuple |
| 23 | +
|
| 24 | + return: padded tensor |
| 25 | + """ |
| 26 | + if isinstance(axis,int): |
| 27 | + axis = (axis,) |
| 28 | + if isinstance(padding,int): |
| 29 | + padding = (padding,) |
| 30 | + |
| 31 | + ndim = len(tensor.shape) |
| 32 | + for ax,p in zip(axis,padding): |
| 33 | + # create a slice object that selects everything from all axes, |
| 34 | + # except only 0:p for the specified for right, and -p: for left |
| 35 | + |
| 36 | + ind_right = [slice(-p,None) if i == ax else slice(None) for i in range(ndim)] |
| 37 | + ind_left = [slice(0, p) if i == ax else slice(None) for i in range(ndim)] |
| 38 | + right = tensor[ind_right] |
| 39 | + left = tensor[ind_left] |
| 40 | + middle = tensor |
| 41 | + tensor = tf.concat([right,middle,left], axis=ax) |
| 42 | + |
| 43 | + return tensor |
| 44 | + |
| 45 | +class LogLearningRateScheduler(tf.keras.callbacks.LearningRateScheduler): |
| 46 | + """ |
| 47 | + Make learning rate schedule function for log reduction. |
| 48 | + Args: |
| 49 | + lr_start (float, optional): Learning rate to start with. The default is 1e-3. |
| 50 | + lr_stop (float, optional): Final learning rate at the end of epo. The default is 1e-5. |
| 51 | + epochs (int, optional): Total number of epochs to reduce learning rate towards. The default is 100. |
| 52 | + epomin (int, optional): Minimum number of epochs at beginning to leave learning rate constant. The default is 10. |
| 53 | + Example: |
| 54 | + model.fit(callbacks=[LogLearningRateScheduler()]) |
| 55 | + """ |
| 56 | + def __init__(self, lr_start=1e-3, lr_stop=1e-5, epochs=100, epomin=10, verbose=0): |
| 57 | + self.lr_start = lr_start |
| 58 | + self.lr_stop = lr_stop |
| 59 | + self.epochs = epochs |
| 60 | + self.epomin = epomin |
| 61 | + super(LogLearningRateScheduler, self).__init__(schedule=self.schedule_epoch_lr, verbose=verbose) |
| 62 | + |
| 63 | + def schedule_epoch_lr(self, epoch, lr): |
| 64 | + if epoch < self.epomin: |
| 65 | + out = self.lr_start |
| 66 | + else: |
| 67 | + out = np.exp( |
| 68 | + float( |
| 69 | + np.log(self.lr_start) - (np.log(self.lr_start) - np.log(self.lr_stop)) / |
| 70 | + (self.epochs - self.epomin) * (epoch - self.epomin) |
| 71 | + ) |
| 72 | + ) |
| 73 | + print('lr scheduler', epoch, out) |
| 74 | + return float(out) |
| 75 | + |
| 76 | + def get_config(self): |
| 77 | + config = super(LogLearningRateScheduler, self).get_config() |
| 78 | + config.update({"lr_start": self.lr_start, "lr_stop": self.lr_stop, "epochs": self.epochs, "epomin": self.epomin}) |
| 79 | + return config |
| 80 | + |
| 81 | + |
| 82 | +class Conv(tf.keras.layers.Layer): |
| 83 | + |
| 84 | + def __init__(self, input_shape, pool_size=2, strides=2, **kwargs): |
| 85 | + super(Conv, self).__init__() |
| 86 | + |
| 87 | + self.layer_conv = [] |
| 88 | + self.layer_conv += [ |
| 89 | + |
| 90 | + tf.keras.layers.Conv2D( |
| 91 | + filters=kwargs['n_filters'], |
| 92 | + kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']), |
| 93 | + padding='same', |
| 94 | + input_shape=input_shape, |
| 95 | + name="Conf2D_%i"%(0)), |
| 96 | + tf.keras.layers.ReLU(), |
| 97 | + |
| 98 | + tf.keras.layers.MaxPool2D(pool_size, |
| 99 | + strides, |
| 100 | + padding='same')] |
| 101 | + for i in range(kwargs['n_conv_steps']-1): |
| 102 | + |
| 103 | + self.layer_conv += [ |
| 104 | + tf.keras.layers.Conv2D( |
| 105 | + filters=kwargs['n_filters'], |
| 106 | + kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']), |
| 107 | + padding='same', |
| 108 | + name="Conf2D_%i"%(i+1)), |
| 109 | + tf.keras.layers.ReLU(), |
| 110 | + tf.keras.layers.MaxPool2D(pool_size, |
| 111 | + strides, |
| 112 | + padding='same')] |
| 113 | + |
| 114 | + def call(self, x): |
| 115 | + for i, l in enumerate(self.layer_conv): |
| 116 | + x = l(x) |
| 117 | + print(f'cnn layer: {l.name} {x.shape}') |
| 118 | + print("Final layer") |
| 119 | + return x |
| 120 | + |
| 121 | + |
| 122 | +class ConvPeriodicPadding(tf.keras.layers.Layer): |
| 123 | + |
| 124 | + def __init__(self, input_shape, pool_size=2, strides=2, **kwargs): |
| 125 | + super(ConvPeriodicPadding, self).__init__() |
| 126 | + |
| 127 | + self.layer_conv = [] |
| 128 | + self.layer_conv += [ |
| 129 | + tf.keras.layers.Conv2D( |
| 130 | + filters=kwargs['n_filters'], |
| 131 | + kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']), |
| 132 | + padding='valid', |
| 133 | + input_shape=input_shape, |
| 134 | + name="Conf2D_%i"%(0)), |
| 135 | + tf.keras.layers.ReLU(), |
| 136 | + tf.keras.layers.MaxPool2D(pool_size=pool_size, |
| 137 | + strides=strides, |
| 138 | + padding='valid') |
| 139 | + ] |
| 140 | + |
| 141 | + for i in range(kwargs['n_conv_steps']-1): |
| 142 | + |
| 143 | + self.layer_conv += [ |
| 144 | + tf.keras.layers.Conv2D( |
| 145 | + filters=kwargs['n_filters'], |
| 146 | + kernel_size=(kwargs['kernel_size'], kwargs['kernel_size']), |
| 147 | + padding='valid', |
| 148 | + name="Conf2D_%i"%(i+1)), |
| 149 | + tf.keras.layers.ReLU(), |
| 150 | + tf.keras.layers.MaxPool2D(pool_size=pool_size, |
| 151 | + strides=strides, |
| 152 | + padding='valid') |
| 153 | + ] |
| 154 | + |
| 155 | + def call(self, x): |
| 156 | + print("\n\n ### Start NN") |
| 157 | + intermediate_sum = [] |
| 158 | + for i, l in enumerate(self.layer_conv): |
| 159 | + if i%3==0: # conv layer |
| 160 | + print(" ### Conv layer") |
| 161 | + x = periodic_padding_flexible(x, axis=(1,2), padding=(3,3)) |
| 162 | + print(f'cnn layer: periodic padding for conf {x.shape}, axis: (1,2), padding: (3,3)') |
| 163 | + x = l(x) |
| 164 | + print(f'cnn layer: {l.name} {x.shape}') |
| 165 | + elif i%3==1: # act layer |
| 166 | + print(" ### Act layer") |
| 167 | + x = l(x) |
| 168 | + print(f'cnn layer: {l.name} {x.shape}') |
| 169 | + elif i%3==2: # pooling layer |
| 170 | + print(" ### Pool layer") |
| 171 | + x = periodic_padding_flexible(x, axis=1, padding=1) |
| 172 | + print(f'cnn layer: periodic padding for pool {x.shape}, axis: (1), padding: (1)') |
| 173 | + x = l(x) |
| 174 | + print(f'cnn layer: {l.name} {x.shape}') |
| 175 | + intermediate_sum.append(x) |
| 176 | + |
| 177 | + return(x), (intermediate_sum) |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +class CustomSum(tf.keras.layers.Layer): |
| 183 | + def __init__(self, **kwargs): |
| 184 | + super(CustomSum, self).__init__() |
| 185 | + self.customsum = tf.keras.layers.Conv1D( |
| 186 | + input_shape=np.zeros((1, 5, 12, 117)), |
| 187 | + kernel_size=kwargs['kernel_size_customsum'], |
| 188 | + filters=kwargs['n_filters_customsum']) |
| 189 | + def call(self, x): |
| 190 | + x = self.customsum(x) |
| 191 | + return x |
| 192 | + |
| 193 | + |
| 194 | +class FC(tf.keras.layers.Layer): |
| 195 | + def __init__(self, **kwargs): |
| 196 | + super(FC, self).__init__() |
| 197 | + self.flatten = tf.keras.layers.Flatten() |
| 198 | + self.fc1 = tf.keras.layers.Dense(units=kwargs['dense_size'], activation='relu') |
| 199 | + self.dropout = tf.keras.layers.Dropout(rate=kwargs['dropout'],) |
| 200 | + self.fc2 = tf.keras.layers.Dense(2, activation='linear') |
| 201 | + |
| 202 | + def call(self, x): |
| 203 | + x = self.flatten(x) |
| 204 | + x = self.fc1(x) |
| 205 | + x = self.dropout(x) |
| 206 | + x = self.fc2(x) |
| 207 | + return x |
| 208 | + |
| 209 | +class Cnnmodel_Shift_Flip(tf.keras.Model): |
| 210 | + def __init__(self, input_shape, model_kwargs): |
| 211 | + super(Cnnmodel_Shift_Flip, self).__init__() |
| 212 | + #print(f'input shape {input_shape}') |
| 213 | + self.conv = ConvPeriodicPadding(input_shape=input_shape, pool_size=2, strides=2, **model_kwargs) |
| 214 | + self.customsum = CustomSum(**model_kwargs) |
| 215 | + self.fc = FC(**model_kwargs) |
| 216 | + |
| 217 | + def call(self, x): |
| 218 | + x2, intermediate_states = self.conv(x) |
| 219 | + x2_flip, intermediate_states_flip = self.conv(tf.image.flip_up_down(x)) |
| 220 | + print("shape before summation: ", x2.shape) |
| 221 | + x2 = self.customsum(sum(x2, axis=-2)) |
| 222 | + print("shape after summation and Conv1D: ", x2.shape) |
| 223 | + x2_flip = self.customsum(sum(x2_flip, axis=-2)) |
| 224 | + |
| 225 | + summed = [] |
| 226 | + summed_flip = [] |
| 227 | + for s in intermediate_states: |
| 228 | + print("shape of intermediate state:", s.shape) |
| 229 | + print("shape of state after sum:", (sum(s, axis=-2)).shape) |
| 230 | + summed.append(self.customsum(sum(s, axis=-2))) |
| 231 | + print("length of list after summation along y-axis: ", len(summed)) |
| 232 | + for f in intermediate_states_flip: |
| 233 | + print("shape of flip's intermediate state:", f.shape) |
| 234 | + print("shape of flip's state after sum:", (sum(f, axis=-2)).shape) |
| 235 | + summed_flip.append(self.customsum(sum(f, axis=-2))) |
| 236 | + |
| 237 | + x3 = tf.concat(summed, axis=-2) |
| 238 | + x3_flip = tf.concat(summed_flip, axis=-2) |
| 239 | + print("shape after concatenate / flip and non-flip: ", x3.shape, x3_flip.shape) |
| 240 | + x3_add = Add()([x3, x3_flip]) |
| 241 | + print("shape after Add:", x3_add.shape) |
| 242 | + outputs = self.fc(x3_add) |
| 243 | + print(" ### End NN\n\n") |
| 244 | + print(f'shape of outputs:{outputs.shape}') |
| 245 | + print(f'shape of x2:{x2.shape}') |
| 246 | + print(f'shape of x3:{x3.shape}') |
| 247 | + return outputs |
0 commit comments