Я пытаюсь решить проблему классификации 21 категории. Вот код:
# dimensions of our images.
img_width, img_height = 256, 256
top_model_weights_path = 'bottleneck_fc_model1.h5'
train_data_dir = 'data1/train1'
validation_data_dir = 'data1/validation1'
nb_train_samples = 1680
nb_validation_samples = 420
epochs = 10
batch_size = 16
def save_bottlebeck_features():
datagen = ImageDataGenerator(rescale=1. / 255)
# build the VGG16 network
model = applications.VGG16(include_top=False, weights='imagenet')
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
bottleneck_features_train = model.predict_generator(
generator, nb_train_samples // batch_size)
np.save('bottleneck_features_train1.npy',
bottleneck_features_train)
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
bottleneck_features_validation = model.predict_generator(
generator, nb_validation_samples // batch_size)
np.save('bottleneck_features_validation1.npy',
bottleneck_features_validation)
def train_top_model():
train_data = np.load('bottleneck_features_train1.npy')
train_labels = np.zeros((1680,21))
j = 0
i = 0
for j in range(0, 21):
train_labels[i:i+80, j] = 1
i = i+80
validation_data = np.load('bottleneck_features_validation1.npy')
validation_labels = np.zeros((420,21))
j = 0
i = 0
for j in range(0, 21):
validation_labels[i:i+20, j] = 1
i = i+20
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(21, activation='softmax'))
model.compile(optimizer='rmsprop',
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()
Я получаю сообщение об ошибке, в котором говорится, что у меня не совпадает число входных выборок проверки с целевыми выборками проверки:
File "<ipython-input-96-0da2181ac8b3>", line 1, in <module>
train_top_model()
File "<ipython-input-87-43a97663639c>", line 36, in train_top_model
validation_data=(validation_data, validation_labels))
File "C:\Users\Spencer\Anaconda3_2\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 972, in fit
batch_size=batch_size)
File "C:\Users\Spencer\Anaconda3_2\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 804, in _standardize_user_data
check_array_length_consistency(x, y, sample_weights)
File "C:\Users\Spencer\Anaconda3_2\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 237, in check_array_length_consistency
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 416 input samples and 420 target samples.
Я не знаю, почему он говорит, что у меня 416 входных выборок и 420 целевых выборок; У меня определенно есть 420 входных и 420 целевых образцов. Есть идеи, почему модель ведет себя так?