Reputation: 1154
I have a 7-classes images with variable sizes.
The resize has been done through flow_from_directory
, but here pops the error saying Error when checking target: expected activation_21 to have shape (1,) but got array with shape (7,)
.
The folders:
data/
train/
dogs/
dog001.jpg
dog002.jpg
...
cats/
cat001.jpg
cat002.jpg
...
sheep/
sheep001.jpg
sheep002.jpg
...
validation/
dogs/
dog001.jpg
dog002.jpg
...
cats/
cat001.jpg
cat002.jpg
...
sheep/
sheep001.jpg
sheep002.jpg
...
My model is a simple CNN:
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
training_path, # this is the target directory
target_size=(200, 350), # all images will be resized to 200x350
batch_size=batch_size, class_mode='categorical'
)
validation_generator = test_datagen.flow_from_directory(
validation_path,
target_size=(200, 350),
batch_size=batch_size,class_mode='categorical'
)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(200, 350, 3),data_format='channels_last'))
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(GlobalMaxPooling2D()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(7))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit_generator(
train_generator,
steps_per_epoch=500 // batch_size,
epochs=10,
validation_data=validation_generator,
validation_steps=500 // batch_size)
The model summary is :
________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_13 (Conv2D) (None, 198, 348, 32) 896
_________________________________________________________________
activation_17 (Activation) (None, 198, 348, 32) 0
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 99, 174, 32) 0
_________________________________________________________________
conv2d_14 (Conv2D) (None, 97, 172, 32) 9248
_________________________________________________________________
activation_18 (Activation) (None, 97, 172, 32) 0
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 48, 86, 32) 0
_________________________________________________________________
conv2d_15 (Conv2D) (None, 46, 84, 64) 18496
_________________________________________________________________
activation_19 (Activation) (None, 46, 84, 64) 0
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 23, 42, 64) 0
_________________________________________________________________
global_max_pooling2d_3 (Glob (None, 64) 0
_________________________________________________________________
dense_5 (Dense) (None, 64) 4160
_________________________________________________________________
activation_20 (Activation) (None, 64) 0
_________________________________________________________________
dropout_3 (Dropout) (None, 64) 0
_________________________________________________________________
dense_6 (Dense) (None, 7) 455
_________________________________________________________________
activation_21 (Activation) (None, 7) 0
=================================================================
Total params: 33,255
Trainable params: 33,255
Non-trainable params: 0
_________________________________________________________________
I have also tried generating seperate x_input and y_input np.arrays, but I don`t know how to resize the input of images since they have different size. Thus I cannot obtain a 4-dimensional input vector, and this approach gives me error like this:
Error when checking input: expected conv2d_16_input to have 4 dimensions, but got array with shape (5721, 1)
Upvotes: 2
Views: 3376
Reputation: 56417
Your code needs to be consistent, in your flow_from_generator
calls you set class mode to categorical
, which produces one-hot encoded class labels, but you use the sparse_categorical_crossentropy
loss, which expects integer labels (not one-hot encoded ones).
You could set the class mode to sparse
in order to get the right labels, or change the loss to categorical_crossentropy
.
Upvotes: 3