linear transformation with Siamese network

125 Views Asked by At

I have a Siamese neural network and I want to apply a linear transformation on the extracted image feature to reduce the dimensionality using PCA or autoencoders. how can I implement it after the flatten layer?

this is my code:

    input_a = Input(shape=(input_shape))
    input_b = Input(shape=(input_shape)) 

    # Convolutional Neural NetworK
    seq = Sequential()
    seq.add(Conv2D(32, (5,5), activation='relu',padding='same',input_shape=input_shape,
      kernel_initializer=initializers.RandomNormal(mean=0.0 ,stddev=0.1, seed=None),bias_initializer= initializers.Zeros()))
    seq.add(MaxPooling2D(pool_size=(2,2) ,strides=(2,2)))
    seq.add(Conv2D(64, (5,5), activation='relu',padding='same',
           kernel_initializer=initializers.RandomNormal(mean=0.0 ,stddev=0.1, seed=None),bias_initializer= initializers.Zeros()))
    seq.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))    
    seq.add(Flatten())
    

    processed_a = seq(input_a)
    processed_b = seq(input_b)
    #here i want to preform linear transformation

    L2_distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape,name='L2')([processed_a, processed_b])    
    a = Lambda(function,output_shape=eucl_dist_output_shape,name='out1')(L2_distance)
   

    model = Model([input_a, input_b],a)
1

There are 1 best solutions below

0
On

For adding a linear transformation layer at the end of a Siamese Neural Network, or better say its encoder, you can do the following two steps:

  1. You need to build a custom layer. You can use the following one and removing the bias term: https://keras.io/guides/making_new_layers_and_models_via_subclassing/#the-layer-class-the-combination-of-state-weights-and-some-computation
  2. You need to add that layer after seq.add(Flatten())