Skip to content

Instantly share code, notes, and snippets.

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