[英]model.fit() generates ERROR: ValueError: All input arrays (x) should have the same number of samples
[英]Keras model.fit ValueError: Input arrays should have the same number of samples as target arrays
我正在嘗試將通過運行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個子目錄。 我的兩個假設是:
任何正確方向的指導將不勝感激!
這是代碼:
# 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])
在監督學習輸入采樣(的數目的情況下, X
)必須樣品(輸出(標簽)的數量匹配Y
)。
例如:如果我們要適合(學習)NN以識別手寫數字,並且將10.000張圖像( X
)輸入到模型中,那么我們還應該傳遞10.000張標簽( Y
)。
在您的情況下,這些數字不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.