Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Last active May 6, 2019 04:35
Show Gist options
  • Save shubham0204/2a0e17e99680f777a6c1d875baad88c8 to your computer and use it in GitHub Desktop.
Save shubham0204/2a0e17e99680f777a6c1d875baad88c8 to your computer and use it in GitHub Desktop.
import tensorflow.keras.backend as K
input_shape = ( (DIMEN**2) * 3 , )
convolution_shape = ( DIMEN , DIMEN , 3 )
kernel_size_1 = ( 8 , 8 )
kernel_size_2 = ( 6 , 6 )
kernel_size_3 = ( 4 , 4 )
pool_size_1 = ( 6 , 6 )
pool_size_2 = ( 4 , 4 )
strides = 1
seq_conv_model = [
tf.keras.layers.Reshape( input_shape=input_shape , target_shape=convolution_shape),
tf.keras.layers.Conv2D( 32, kernel_size=kernel_size_1 , strides=strides ,activation='relu' ),
tf.keras.layers.MaxPooling2D(pool_size=pool_size_1, strides=strides ),
tf.keras.layers.Conv2D( 64, kernel_size=kernel_size_2 , strides=strides ,activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=pool_size_2 , strides=strides),
tf.keras.layers.Conv2D( 128, kernel_size=kernel_size_3 , strides=strides ,activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense( 3076 , activation=tf.keras.activations.sigmoid )
]
seq_model = tf.keras.Sequential( seq_conv_model )
input_x1 = tf.keras.layers.Input( shape=input_shape )
input_x2 = tf.keras.layers.Input( shape=input_shape )
output_x1 = seq_model( input_x1 )
output_x2 = seq_model( input_x2 )
distance_euclid = tf.keras.layers.Lambda( lambda tensors : K.abs( tensors[0] - tensors[1] ))( [output_x1 , output_x2] )
outputs = tf.keras.layers.Dense( 1 , activation=tf.keras.activations.sigmoid) ( distance_euclid )
model = tf.keras.models.Model( [ input_x1 , input_x2 ] , outputs )
model.compile( loss=tf.keras.losses.binary_crossentropy , optimizer=tf.keras.optimizers.Adam(lr=0.0001) , metrics=['accuracy'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment