Value Error: Input arrays should have the same number of samples as target arrays. Found 166 input samples and 4 target samples

Viewed 3282

I'm working with Keras DL library to classify the dataset of images. I've encountered an error while I was trying to train the model.

The dataset I am working on doesn't have a huge amount of data, so an example the training set contains 166 images. I am not sure about the error but I think somehow I must change the shape of the label set to fix it. Here is the code:

import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D


DIR = '/home/.../'
IMG_H = 256
IMG_W = 256
IMG_CH = 1


loadFile = DIR + 'Img.npz'
X = np.load(loadFile)
trainImgSet = X['trainImgSet']
trainLabelSet = X['trainLabelSet']
testImgSet = X['testImgSet']

print('Shape of trainImgSet: {}'.format(trainImgSet.shape))
print('Shape of trainLabelSet: {}'.format(trainLabelSet))
#print('Shape of testImgSet:{}'.format(testImgSet))


model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(256, (3, 3), input_shape = (IMG_H, IMG_W, IMG_CH)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Conv2D(256, (3, 3)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(64))

model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.Activation('sigmoid'))

model.compile(loss='binary_crossentropy', 
                optimizer='adam',
                metrics=['accuracy'])

model.summary()

#train the CNN
model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)



Here is the error:
Traceback (most recent call last):
  File "/home/Code/DeepCl.py", line 49, in <module>
    model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)
  File "anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
    validation_split=validation_split)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1169, in _standardize_weights
    training_utils.check_array_lengths(x, y, sample_weights)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 426, in check_array_lengths
    'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 166 input samples and 4 target samples.
2 Answers

Here,

  • The number of training samples is not equal to the number of labels.

  • There are 144 training samples but only 4 labels for them.

  • The shapes of the training and testing data must have equal number of samples.

  • Ex. training data has shape ( 100 , 256 , 256 , 1 ). Testing data should have the shape ( 100 , 1 ).

This happened to me when I called:

X_train, y_train, X_test, y_test = sklearn.model_selection.train_test_split(...)

instead of

X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(...)

(Notice the 2nd and 3rd variables are swopped on the LHS.)

It resulted in me having data and labels with unequal length:

i.e. len(X_train) != len(y_train) and len(X_test) != len(y_test).

Related