Вопрос

I got this error ValueError: Shapes (None, 1) and (None, 3) are incompatible when training my Sequential model. I could not figure out which shapes are actually incompatible. This is the first time I do image classification. Here are my codes:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(3, activation='softmax'))

model.summary()

model.compile(optimizer='rmsprop',
  loss='categorical_crossentropy',
  metrics=['accuracy'])

# this throws value error
model.fit(
  train_generator,
  steps_per_epoch=25,
  epochs=20,
  validation_data=valid_generator,
  validation_steps=5,
  verbose=2
)

I want the last Dense layer to return an array of 3 probability scores (large, medium, small).

Here is how I created the train_generator and valid_generator:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
image_datagen = ImageDataGenerator(
  rescale=1./255,
  rotation_range=20,
  horizontal_flip=True,
  shear_range = 0.2,
  fill_mode = 'nearest')

index = len(df)//10
df_train = df[index:]
df_valid = df[:index]

train_generator = image_datagen.flow_from_dataframe(
  df_train,
  x_col = 'png_image',
  y_col = 'target',
  target_size=(150, 150),
  batch_size=4,
  color_mode = 'rgb',
  class_mode='sparse',
)

valid_generator = image_datagen.flow_from_dataframe(
  df_valid,
  x_col = 'png_image',
  y_col = 'target',
  target_size=(150, 150),
  batch_size=4,
  color_mode = 'rgb',
  class_mode='sparse',
)

The dataframes look like this:

dataframe head

Это было полезно?

Решение

Have you tried setting class_mode='categorical' in your generators?

I believe class_mode='sparse' works when your loss function is defined as 'sparse_categorical_crossentropy' whereas 'categorical_crossentropy' works for class_mode='categorical'.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с datascience.stackexchange
scroll top