Keras训练形状错误

时间:2018-03-15 11:40:45

标签: python tensorflow keras

在Keras中编写我的第一个图像分类器,这是代码

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint


# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = 'training_images'
validation_data_dir = 'validation_images'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Dense(14951, activation='softmax'))

monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=0, mode='auto')
checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) # save best model

model.compile(loss='categorical_crossentropy', optimizer='adam', callbacks=[monitor,checkpointer], epochs=1000, metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

model.load_weights('best_weights.hdf5') # load weights from best model
model.save('last_model.h5')

当我尝试训练模型时,虽然我遇到了形状错误

Using TensorFlow backend.
Found 981214 images belonging to 14951 classes.
Found 237925 images belonging to 14951 classes.
Epoch 1/50
Traceback (most recent call last):
  File "train.py", line 78, in <module>
    validation_steps=nb_validation_samples // batch_size)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wra
pper
    return func(*args, **kwargs)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/models.py", line 1276, in fit_generato
r
    initial_epoch=initial_epoch)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wra
pper
    return func(*args, **kwargs)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/engine/training.py", line 2224, in fit
_generator
    class_weight=class_weight)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1877, in tra
in_on_batch
    class_weight=class_weight)
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1480, in _st
andardize_user_data
    exception_prefix='target')
  File "/home/muiruri_samuel/.local/lib/python2.7/site-packages/keras/engine/training.py", line 123, in _sta
ndardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (14951,) but got array with shape (1,
)

这是我第一次遇到形状错误。 我已经找到了我可能犯错误的地方,但由于这个模型是基于二元“圣诞老人”或“非圣诞老人”,我可能怀疑我应该给它最终给出的类别形状。 / p>

1 个答案:

答案 0 :(得分:0)

我认为主要问题在于:

msg_control

由于您有14951个类,根本不是二进制分类,您将类模式设置为&#34; binary&#34;。这应该设置为&#34;分类&#34;:

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

然后由发电机产生的标签将具有正确的形状,并且训练应该起作用。