How to fit input and output data into Siamese Network using Keras?

感情迁移 提交于 2021-01-07 02:53:56

问题


I am trying to implement a face recognition Siamese Network using the Labelled Faces in the Wild (LFW Dataset in Kaggle).

The training data image pairs is stored in the format of :

ndarray[ndarray[image1,image2],ndarray[image1,image2]...] and so on. The images are RGB channelled with size of 224*224.

There are 2200 training pairs with 1100 match image pairs and 1100 mismatch image pairs. Also, there are 1000 test pairs with 500 match image pairs and 500 mismatch image pairs.

I have designed the Siamese network with VGG-16 architecture. The model summary is as follows:

However, when I try to fit the model for the data, I get this error:

The code for the Network is:

from keras.layers import Input,Lambda
from keras import backend as K
from keras.models import Model
from keras.regularizers import l2

IMG_SHAPE=(224,224,3)
BATCH_SIZE=16
EPOCHS=32

def return_siamese_net():

  left_input=Input(IMG_SHAPE)
  right_input=Input(IMG_SHAPE)

  model=Sequential(name="VGG-16")

  #First Layer

  model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',padding='same',input_shape=IMG_SHAPE,kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  #Second Layer
  model.add(Conv2D(filters=128,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=128,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  #Third Layer
  model.add(Conv2D(filters=256,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=256,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=256,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  #Fourth Layer
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  #Fifth Layer
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(Conv2D(filters=512,kernel_size=(3,3),activation='relu',padding='same',kernel_initializer='glorot_uniform',kernel_regularizer=l2(1e-4)))
  model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

  #Sixth Layer
  model.add(Flatten())
  model.add(Dense(4096, activation='relu'))

  encoded_l=model(left_input)
  encoded_r=model(right_input)

  lambda_layer= Lambda(lambda tensors:K.abs(tensors[0]-tensors[1]))
  L1_distance = lambda_layer([encoded_l, encoded_r])
  prediction = Dense(1,activation='sigmoid')(L1_distance)
  siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)
  
  return siamese_net


from keras.optimizers import SGD,RMSprop,Adam

optimizer=Adam(lr=0.01)
model.compile(loss='binary_crossentropy',metrics=['accuracy'],optimizer=optimizer)

In the below snippet, train_nparr_pairs has 2200 match and mismatch images and test_nparr_pairs has 1000 match and mismatch images. train_labels and test_labels have 0 and 1 based on positive pair and negative pair .

history = model.fit([train_nparr_pairs[:, 0], train_nparr_pairs[:, 1]], train_labels,validation_data=([test_nparr_pairs[:, 0], test_nparr_pairs[:, 1]], test_labels),batch_size=BATCH_SIZE, epochs=EPOCHS)

Is there anything that I am missing here?

来源:https://stackoverflow.com/questions/65536939/how-to-fit-input-and-output-data-into-siamese-network-using-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!