from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Lambda, Dense, Flatten
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow import keras

def global_average_pooling(x):
    return keras.backend.mean(x, axis = (1, 2))

def global_average_pooling_shape(input_shape):
     return [input_shape[0], input_shape[3]]

def cnn_examine():
#add model layers 
    inputs = keras.Input(shape=(20, 200, 1))
    x = Conv2D(16, (5, 5), data_format='channels_last', input_shape=(20, 200, 1), activation='relu')(inputs)
    x = Conv2D(16, (3, 3), activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2))(x)
    x = Conv2D(16, (1, 3), activation='relu')(x)
    x = Conv2D(32, (1, 3), activation='relu', name='final_conv')(x)
    x = Flatten()(x)
    x = Dense(32, activation='relu', name='dense1')(x)
    output = Dense(units=2, activation='sigmoid', name='class')(x)

    return Model(inputs, output)

def cnn_global():
#add model layers 
    inputs = keras.Input(shape=(20, 200, 1))
    x = Conv2D(16, (5, 5), data_format='channels_last', input_shape=(20, 200, 1), activation='relu')(inputs)
    x = Conv2D(16, (3, 3), activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2))(x)
    x = Conv2D(16, (1, 3), activation='relu')(x)
    x = Conv2D(32, (1, 3), activation='relu', name='final_conv')(x)
# Add the global pooling layer, required for activation maps
    x = GlobalAveragePooling2D(name='global_average_pooling2d')(x)

    output = Dense(units=2, activation='sigmoid', name='dense')(x)
    
    return Model(inputs, output)
