值错误:输入数组应具有与目标数组相同的样本数。找到1600个输入样本和6400个目标样本

时间:2017-05-25 15:57:00

标签: python arrays numpy keras

我正在尝试进行8级分类。这是代码:

import keras
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras import applications
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('tf')
img_width, img_height = 48,48
top_model_weights_path = 'modelom.h5'
train_data_dir = 'chCdata1/train'
validation_data_dir = 'chCdata1/validation'
nb_train_samples = 6400
nb_validation_samples = 1600
epochs = 50
batch_size = 10
def save_bottlebeck_features():
   datagen = ImageDataGenerator(rescale=1. / 255)
   model = applications.VGG16(include_top=False, weights='imagenet', input_shape=(48,48,3))
   generator = datagen.flow_from_directory(
               train_data_dir,
               target_size=(img_width, img_height),
               batch_size=batch_size,
               class_mode='categorical',
               shuffle=False)
   bottleneck_features_train = model.predict_generator(
               generator, nb_train_samples // batch_size)
   np.save(open('bottleneck_features_train', 'wb'),bottleneck_features_train)

   generator = datagen.flow_from_directory(
               validation_data_dir,
               target_size=(img_width, img_height),
               batch_size=batch_size,
               class_mode='categorical',
               shuffle=False)
   bottleneck_features_validation = model.predict_generator(
               generator, nb_validation_samples // batch_size)
   np.save(open('bottleneck_features_validation', 'wb'),bottleneck_features_validation)

def train_top_model():
   train_data = np.load(open('bottleneck_features_train', 'rb'))
   train_labels = np.array([0] * (nb_train_samples // 8) + [1] * (nb_train_samples // 8) + [2] * (nb_train_samples // 8) + [3] * (nb_train_samples // 8) + [4] * (nb_train_samples // 8) + [5] * (nb_train_samples // 8) + [6] * (nb_train_samples // 8) + [7] * (nb_train_samples // 8))
   validation_data = np.load(open('bottleneck_features_validation', 'rb'))
   validation_labels = np.array([0] * (nb_train_samples // 8) + [1] * (nb_train_samples // 8) + [2] * (nb_train_samples // 8) + [3] * (nb_train_samples // 8) + [4] * (nb_train_samples // 8) + [5] * (nb_train_samples // 8) + [6] * (nb_train_samples // 8) + [7] * (nb_train_samples // 8))
   train_labels = keras.utils.to_categorical(train_labels, num_classes = 8)
   validation_labels = keras.utils.to_categorical(validation_labels, num_classes = 8)
   model = Sequential()
   model.add(Flatten(input_shape=train_data.shape[1:]))
   model.add(Dense(512, activation='relu'))
   model.add(Dropout(0.5))
   model.add(Dense(8, activation='softmax'))
   sgd = SGD(lr=1e-2, decay=0.00371, momentum=0.9, nesterov=False)
   model.compile(optimizer=sgd,
         loss='categorical_crossentropy', metrics=['accuracy'])
   model.fit(train_data, train_labels,
          epochs=epochs,
          batch_size=batch_size,
   validation_data=(validation_data, validation_labels))
   model.save_weights(top_model_weights_path)

save_bottlebeck_features()
train_top_model()

我在这里添加了完整的错误列表:

Traceback (most recent call last):

  File "<ipython-input-14-1d34826b5dd5>", line 1, in <module>
    runfile('C:/Users/rajaramans2/codes/untitled15.py', wdir='C:/Users/rajaramans2/codes')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/rajaramans2/codes/untitled15.py", line 71, in <module>
    train_top_model()

  File "C:/Users/rajaramans2/codes/untitled15.py", line 67, in train_top_model
    validation_data=(validation_data, validation_labels))

  File "C:\Anaconda3\lib\site-packages\keras\models.py", line 856, in fit
    initial_epoch=initial_epoch)

  File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1449, in fit
    batch_size=batch_size)

  File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1317, in _standardize_user_data
    _check_array_lengths(x, y, sample_weights)

  File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 235, 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 1600 input samples and 6400 target samples.

“ValueError:输入数组应具有与目标数组相同的样本数。发现1600个输入样本和6400个目标样本”。请帮助解决方案和对代码进行必要的修改。提前谢谢。

5 个答案:

答案 0 :(得分:8)

看起来X_train中的示例数量,即train_data与y_train中的示例数量(即train_labels)不匹配。你能仔细检查一下吗?并且,在将来,请附上完整的错误,因为它有助于调试问题。

答案 1 :(得分:2)

看起来您有1600个培训示例。并且您的8个类在样本中没有分开,因此您有一个8 x 1600 = 6400值的数组。

该数组必须是(1600,8)。即:1600个样本,8个可能的类。

现在您需要了解train_labels数组的组织方式。如果数组正确排序,也许一个简单的reshape((1600,8))就足够了。

如果没有,你必须自己组织1600个八个标签样本。

答案 2 :(得分:1)

这与len(X_train)无关!= len(y_train)。

将数据拆分为相等的大小,以进行训练和测试(验证)。确保输入数据大小均匀。如果不是,请尝试通过省略输入数据中的最后一个观测值来修剪数据。

train_test_split(X,y,test_size = 0.5,random_state = 42)

这对我有用。

答案 3 :(得分:0)

我知道您有一个答案,但对于其他旅客,请确保您的火车数据可被batch_size整除。

答案 4 :(得分:0)

这种情况下的问题就在这条线上

pids

肯定有更好的书写方式,因为现在validation_labels = np.array([0] * (nb_train_samples // 8) + [1] * (nb_train_samples // 8) + [2] * (nb_train_samples // 8) + [3] * (nb_train_samples // 8) + [4] * (nb_train_samples // 8) + [5] * (nb_train_samples // 8) + [6] * (nb_train_samples // 8) + [7] * (nb_train_samples // 8)) 的所有出现都应替换为nb_train_samples