I am developing a Siamese Neural Network for Face Recognition. The images are RGB channeled with size 224*224. There are 2200 pairs of training images and 1000 pairs of test images.
During the training of this model, I got this error:
The shapes and model.fit code is given below:
#train_nparr_pairs.shape --> (2200,2,224,224,3)
#test_nparr_pairs.shape --> (1000,2,224,224,3)
#train_labels.shape --> (2200,)
#test_labels.shape --> (1000,)
#BATCH_SIZE 32
#EPOCHS 64
model.fit(np.asarray([train_nparr_pairs[:, 0], train_nparr_pairs[:, 1]]), train_labels[:],
validation_data=(np.asarray([test_nparr_pairs[:, 0], test_nparr_pairs[:, 1]]),
test_labels[:]),batch_size=BATCH_SIZE, epochs=EPOCHS)
Architecture of the Neural Network:
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 Conv-Pool 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 Conv-Pool 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 Conv-Pool 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 Conv-Pool 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 Conv-Pool 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)))
#Flatten 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
I do know that this is related to the shape of the train and test numpy arrays . I tried using expand dims and reshape to adjust the dimensions but still the error remains the same. Is there any ways to debug this error?
As the error suggest you are passing to the fit function an x
np.ndarray with 2 as first dimension and an y
np.ndarray with 2200 as first dimension.
It is hard to answer without knowing how you constructed the network model, however, let's suppose that your model takes two inputs corresponding to the two images with shape (224, 224, 3)
each, then you can pass to the fit function as x
argument (in case the model has multiple inputs):
You can't use a numpy array as in your code. Have a look a the tf documentation for more details about the fit method.
So, you can adjust your code as follows:
model.fit(x=[[train_nparr_pairs[:, 0], train_nparr_pairs[:, 1]]], y=train_labels[:],
validation_data=([[test_nparr_pairs[:, 0], test_nparr_pairs[:, 1]]], test_labels[:]),
batch_size=BATCH_SIZE, epochs=EPOCHS)