Beherit
Beherit

Reputation: 89

ValueError: Error when checking target: expected dense_3 to have shape (1000,) but got array with shape (1,)

I tried to implement a VGG-16 architecture of my Keras model, however I got the error message complaining checking target shapes.

img_width, img_height = 512, 560

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)
# build the VGG16 network
model = Sequential([
Conv2D(64, (3, 3), input_shape=input_shape, padding='same', activation='relu'),
Conv2D(64, (3, 3), activation='relu', padding='same'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(128, (3, 3), activation='relu', padding='same'),
Conv2D(128, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Flatten(),
Dense(4096, activation='relu'),
Dense(4096, activation='relu'),
Dense(1000, activation='softmax')
])

model.summary()
model.compile(loss = 'binary_crossentropy',
              optimizer = 'rmsprop',
              metrics = ['accuracy'])
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_datagen = ImageDataGenerator(
    rotation_range = 180,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    brightness_range = (0.8, 1.2),
    rescale = 1. / 255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
    vertical_flip = True
)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    #target_size=(224, 224),
    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),
    #target_size=(224, 224),
    batch_size = batch_size,
    class_mode = 'binary'
)

history = 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)

I tried to train and fit my model, however I got error messages shown below, how do I fix this problem? It looks like my last dense layer is with dimension 1000 why it's still complaining this?

    Found 576 images belonging to 2 classes.
Found 145 images belonging to 2 classes.
Epoch 1/50
Traceback (most recent call last):
  File "Trimer_useful_life_VGG.py", line 109, in <module>
    validation_steps = nb_validation_samples // batch_size)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training_generator.py", line 217, in fit_generator
    class_weight=class_weight)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 1211, in train_on_batch
    class_weight=class_weight)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
    exception_prefix='target')
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (1000,) but got array with shape (1,)

Upvotes: 1

Views: 46

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

This mode is configured to output 1000 classes, to use it for two classes and binary_crossentropy loss, you should change the last layer to:

Dense(1, activation='sigmoid')

This configuration allows for binary classification as 0-1, if you need more classes you need to put the number of classes in the last Dense, and use a softmax activation.

Upvotes: 3

Related Questions