Keras model.fit ValueError:输入数组应具有与目标数组相同的样本数

时间:2018-04-28 21:54:00

标签: python machine-learning keras

我正在尝试将从运行resnet50获得的bottleneck_features加载到顶层模型中。我在resnet上运行了predict_generator,并将生成的bottleneck_features保存到npy文件中。由于以下错误,我无法适应我创建的模型:

    Traceback (most recent call last):
  File "Labeled_Image_Recognition.py", line 119, in <module>
    callbacks=[checkpointer])
  File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/models.py", line 963, in fit
    validation_steps=validation_steps)
  File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1630, in fit
    batch_size=batch_size)
  File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1490, in _standardize_user_data
    _check_array_lengths(x, y, sample_weights)
  File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 220, 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 940286 input samples and 14951 target samples.

我不确定这意味着什么。我的火车目录中有940286个总图像,这些图像被分成14951个总数。我的两个假设是:

  1. 我可能没有正确格式化train_data和train_labels。
  2. 我错误地设置了模型
  3. 非常感谢任何正确方向的指导!

    以下是代码:

    # Constants
    num_train_dirs = 14951 #This is the total amount of classes I have
    num_valid_dirs = 13168 
    
    def load_labels(path):
        targets = os.listdir(path)
        labels = np_utils.to_categorical(targets, len(targets))
        return labels
    
    def create_model(train_data):
        model = Sequential()
        model.add(Flatten(input_shape=train_data.shape[1:]))
        model.add(Dense(num_train_dirs, activation='relu'))
        model.add(Dropout(0.2))
        model.add(Dense(num_train_dirs, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
        return model    
    
    train_data = np.load(open('bottleneck_features/bottleneck_features_train.npy', 'rb'))
    train_labels = load_labels(raid_train_dir)
    
    valid_data = np.load(open('bottleneck_features/bottleneck_features_valid.npy', 'rb'))
    valid_labels = train_labels
    
    model = create_model(train_data)
    model.summary()
    
    checkpointer = ModelCheckpoint(filepath='weights/first_try.hdf5', verbose=1, save_best_only=True)
    
    print("Fitting model...")
    
    model.fit(train_data, train_labels,
         epochs=50,
         batch_size=100,
         verbose=1,
         validation_data=(valid_data, valid_labels),
         callbacks=[checkpointer])
    

1 个答案:

答案 0 :(得分:0)

在监督学习的情况下,输入样本(X)的数量必须与输出(标签)样本的数量(Y)匹配。

例如:如果我们想要(学习)NN来识别手写数字并且我们将10.000个图像(X)提供给我们的模型,那么我们也应该传递10.000个标签(Y

在您的情况下,这些数字不匹配。