我正在尝试解决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个目标样本。关于模型为何如此表现的任何想法?
答案 0 :(得分:1)
问题是 @RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> methodName()
{
// your buisness logic
return new ResponseEntity.noContent().build();
}
(16)无法将nb_validation_samples
(420)整除。这导致我的batch_size
调用按以下方式执行楼层划分:420 // 16 ==26。因此,样本总数等于16 * 26 == 416。
我将predict_generator()
更改为10,现在一切运行正常。