Spaces:
Runtime error
Runtime error
File size: 9,951 Bytes
9cebadf 2b8a8b8 9cebadf 2b8a8b8 |
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 |
import pickle
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy, Reduction
from tensorflow.keras.layers import Input, Resizing, Conv2D, BatchNormalization, Multiply, Lambda, Concatenate
import tensorflow.keras.backend as K
EPOCHS = 10
TRAINING_DTYPE = tf.float16
SPLIT_SIZE = 256
BATCH_SIZE = 24
LEARNING_RATE = 5e-3
RESIZING_FILTER = 'bilinear'
############################################################
def mask_voas_cnn_model(l_rate = LEARNING_RATE):
x_in = Input(shape=(360, SPLIT_SIZE, 1))
x = Resizing(90, int(SPLIT_SIZE/2), RESIZING_FILTER,
name="downscale")(x_in)
x = BatchNormalization()(x)
x = Conv2D(filters=32, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=32, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=16, kernel_size=(70, 3), padding="same",
activation="relu", name="conv_harm_1")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=16, kernel_size=(70, 3), padding="same",
activation="relu", name="conv_harm_2")(x)
x = BatchNormalization()(x)
## "masking" original input with trained data
x = Resizing(360, SPLIT_SIZE, RESIZING_FILTER,
name="upscale")(x)
x = Multiply(name="multiply_mask")([x, x_in])
## start four branches now
## branch 1
x1a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1a")(x)
x1a = BatchNormalization()(x1a)
x1b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1b")(x1a)
## branch 2
x2a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2a")(x)
x2a = BatchNormalization()(x2a)
x2b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2b")(x2a)
## branch 3
x3a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv3a")(x)
x3a = BatchNormalization()(x3a)
x3b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv3b")(x3a)
x4a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv4a")(x)
x4a = BatchNormalization()(x4a)
x4b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv4b"
)(x4a)
y1 = Conv2D(filters=1, kernel_size=1, name='conv_soprano',
padding='same', activation='sigmoid')(x1b)
y1 = tf.squeeze(y1, axis=-1, name='sop')
y2 = Conv2D(filters=1, kernel_size=1, name='conv_alto',
padding='same', activation='sigmoid')(x2b)
y2 = tf.squeeze(y2, axis=-1, name='alt')
y3 = Conv2D(filters=1, kernel_size=1, name='conv_tenor',
padding='same', activation='sigmoid')(x3b)
y3 = tf.squeeze(y3, axis=-1, name='ten')
y4 = Conv2D(filters=1, kernel_size=1, name='conv_bass',
padding='same', activation='sigmoid')(x4b)
y4 = tf.squeeze(y4, axis=-1, name='bas')
out = [y1, y2, y3, y4]
model = Model(inputs=x_in, outputs=out, name='MaskVoasCNN')
model.compile(optimizer=Adam(learning_rate=l_rate),
loss=BinaryCrossentropy(reduction=Reduction.SUM_OVER_BATCH_SIZE))
model.load_weights('./Checkpoints/mask_voas.keras')
return model
############################################################
def mask_voas_cnn_v2_model(l_rate = LEARNING_RATE):
x_in = Input(shape=(360, SPLIT_SIZE, 1))
x = Resizing(90, int(SPLIT_SIZE/2), RESIZING_FILTER,
name="downscale")(x_in)
x = BatchNormalization()(x)
x = Conv2D(filters=32, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=32, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=16, kernel_size=(48, 3), padding="same",
activation="relu", name="conv_harm_1")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=16, kernel_size=(48, 3), padding="same",
activation="relu", name="conv_harm_2")(x)
x = BatchNormalization()(x)
x = Conv2D(filters=16, kernel_size=1, padding="same",
activation="sigmoid", name="conv_sigmoid_before_mask")(x)
## "masking" original input with trained data
x = Resizing(360, SPLIT_SIZE, RESIZING_FILTER,
name="upscale")(x)
x = Multiply(name="multiply_mask")([x, x_in])
x = BatchNormalization()(x)
## start four branches now
## branch 1
x1a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1a")(x)
x1a = BatchNormalization()(x1a)
x1b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv1b")(x1a)
## branch 2
x2a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2a")(x)
x2a = BatchNormalization()(x2a)
x2b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv2b")(x2a)
## branch 3
x3a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv3a")(x)
x3a = BatchNormalization()(x3a)
x3b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv3b")(x3a)
x4a = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv4a")(x)
x4a = BatchNormalization()(x4a)
x4b = Conv2D(filters=16, kernel_size=(3, 3), padding="same",
activation="relu", name="conv4b"
)(x4a)
y1 = Conv2D(filters=1, kernel_size=1, name='conv_soprano',
padding='same', activation='sigmoid')(x1b)
y1 = tf.squeeze(y1, axis=-1, name='sop')
y2 = Conv2D(filters=1, kernel_size=1, name='conv_alto',
padding='same', activation='sigmoid')(x2b)
y2 = tf.squeeze(y2, axis=-1, name='alt')
y3 = Conv2D(filters=1, kernel_size=1, name='conv_tenor',
padding='same', activation='sigmoid')(x3b)
y3 = tf.squeeze(y3, axis=-1, name='ten')
y4 = Conv2D(filters=1, kernel_size=1, name='conv_bass',
padding='same', activation='sigmoid')(x4b)
y4 = tf.squeeze(y4, axis=-1, name='bas')
out = [y1, y2, y3, y4]
model = Model(inputs=x_in, outputs=out, name='MaskVoasCNNv2')
model.compile(optimizer=Adam(learning_rate=l_rate),
loss=BinaryCrossentropy(reduction=Reduction.SUM_OVER_BATCH_SIZE))
model.load_weights('./Checkpoints/mask_voas_v2.keras')
return model
############################################################
def __base_model(input, let):
b1 = BatchNormalization()(input)
# conv1
y1 = Conv2D(16, (5, 5), padding='same', activation='relu', name='conv1{}'.format(let))(b1)
y1a = BatchNormalization()(y1)
# conv2
y2 = Conv2D(32, (5, 5), padding='same', activation='relu', name='conv2{}'.format(let))(y1a)
y2a = BatchNormalization()(y2)
# conv3
y3 = Conv2D(32, (5, 5), padding='same', activation='relu', name='conv3{}'.format(let))(y2a)
y3a = BatchNormalization()(y3)
# conv4 layer
y4 = Conv2D(32, (5, 5), padding='same', activation='relu', name='conv4{}'.format(let))(y3a)
y4a = BatchNormalization()(y4)
# conv5 layer, harm1
y5 = Conv2D(32, (70, 3), padding='same', activation='relu', name='harm1{}'.format(let))(y4a)
y5a = BatchNormalization()(y5)
# conv6 layer, harm2
y6 = Conv2D(32, (70, 3), padding='same', activation='relu', name='harm2{}'.format(let))(y5a)
y6a = BatchNormalization()(y6)
return y6a, input
def late_deep_cnn_model():
'''Late/Deep
'''
input_shape_1 = (None, None, 5) # HCQT input shape
input_shape_2 = (None, None, 5) # phase differentials input shape
inputs1 = Input(shape=input_shape_1)
inputs2 = Input(shape=input_shape_2)
y6a, _ = __base_model(inputs1, 'a')
y6b, _ = __base_model(inputs2, 'b')
# concatenate features
y6c = Concatenate()([y6a, y6b])
# conv7 layer
y7 = Conv2D(64, (3, 3), padding='same', activation='relu', name='conv7')(y6c)
y7a = BatchNormalization()(y7)
# conv8 layer
y8 = Conv2D(64, (3, 3), padding='same', activation='relu', name='conv8')(y7a)
y8a = BatchNormalization()(y8)
y9 = Conv2D(8, (360, 1), padding='same', activation='relu', name='distribution')(y8a)
y9a = BatchNormalization()(y9)
y10 = Conv2D(1, (1, 1), padding='same', activation='sigmoid', name='squishy')(y9a)
predictions = Lambda(lambda x: K.squeeze(x, axis=3))(y10)
model = Model(inputs=[inputs1, inputs2], outputs=predictions)
model.compile(
loss=__bkld, metrics=['mse', __soft_binary_accuracy],
optimizer='adam'
)
model.load_weights('./Checkpoints/exp3multif0.h5')
return model
############################################################
def __bkld(y_true, y_pred):
"""Brian's KL Divergence implementation
"""
y_true = K.clip(y_true, K.epsilon(), 1.0 - K.epsilon())
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return K.mean(K.mean(
-1.0*y_true* K.log(y_pred) - (1.0 - y_true) * K.log(1.0 - y_pred),
axis=-1), axis=-1)
############################################################
def __soft_binary_accuracy(y_true, y_pred):
"""Binary accuracy that works when inputs are probabilities
"""
return K.mean(K.mean(
K.equal(K.round(y_true), K.round(y_pred)), axis=-1), axis=-1)
############################################################ |