Created
January 29, 2019 19:13
-
-
Save alidastgheib/64f4491e00c13dd04c0494eb579572a4 to your computer and use it in GitHub Desktop.
performing "DCT transform" using Lambda layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import keras.backend as K # which is Tensorflow | |
| def dct_layer_function(x_batch): | |
| output = K.zeros(x_batch.shape) | |
| batch_size = x_batch.shape[0] | |
| channel_size = x_batch.shape[-1] | |
| for idx_batch in range(batch_size): | |
| for idx_channel in range(channel_size): | |
| output[idx_batch, :, :, idx_channel] = K.spectral.dct( | |
| K.transpose( | |
| K.spectral.dct( | |
| x_batch[idx_batch, :, :, idx_channel]))) # a 2D DCT (a transpose in the end is optional) | |
| return output | |
| from keras.models import Model | |
| from keras import layers | |
| from keras.layers import Lambda | |
| input_of_net = layers.Input(shape=(27, 27, 3), name='input_of_net') | |
| x = layers.Conv2D(32, (3, 3), strides=(2, 2), kernel_initializer='glorot_normal', name='block1_conv1')(input_of_net) | |
| x = layers.BatchNormalization(name='block1_conv1_bn')(x) | |
| x = layers.Activation('relu', name='block1_conv1_act')(x) | |
| dct_layer = Lambda(function = dct_layer_function, output_shape=dct_layer_function_output_shape) | |
| x = dct_layer(x) | |
| x = layers.Conv2D(64, (3, 3), kernel_initializer='glorot_normal', name='block1_conv2')(x) | |
| x = layers.BatchNormalization(name='block1_conv2_bn')(x) | |
| x = layers.Activation('relu', name='block1_conv2_act')(x) | |
| x = layers.GlobalAveragePooling2D()(x) | |
| x = layers.Dense(2, activation = 'sigmoid')(x) | |
| model = Model(inputs = input_of_net, outputs = x) | |
| model.summary() | |
| ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 13, 13, 32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment