Reputation: 75
I am currently attempting a project in Google/Udacity's Tensorflow Course using a dataset acquired as follows:
_URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
zip_file = tf.keras.utils.get_file(origin=_URL,
fname="flower_photos.tgz",
extract=True)
Unfortunately, I ran into the following error:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [100,5] and labels shape [500]
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-43-02964d57939c>:8) ]] [Op:__inference_test_function_3591]
I looked at other posts, but it still seemed a bit tricky to figure out. My initial thought is that I might be using the incorrect loss function.
Here is the code running into problems:
image_gen = ImageDataGenerator(rescale = 1./255, horizontal_flip=True, zoom_range=0.5, rotation_range=45, width_shift_range=0.15, height_shift_range=0.15)
train_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE, directory = train_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE),class_mode='binary')
image_gen = ImageDataGenerator(rescale = 1./255)
val_data_gen = image_gen.flow_from_directory(batch_size=BATCH_SIZE, directory = val_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE))
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150,150,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(5),
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
The batch size is 100 and input dimension is 150,150 The summary is as follows: Model: "sequential_4"
Layer (type) Output Shape Param #
conv2d_12 (Conv2D) (None, 148, 148, 16) 448
max_pooling2d_12 (MaxPooling (None, 74, 74, 16) 0
conv2d_13 (Conv2D) (None, 72, 72, 32) 4640
max_pooling2d_13 (MaxPooling (None, 36, 36, 32) 0
conv2d_14 (Conv2D) (None, 34, 34, 64) 18496
max_pooling2d_14 (MaxPooling (None, 17, 17, 64) 0
dropout_4 (Dropout) (None, 17, 17, 64) 0
flatten_4 (Flatten) (None, 18496) 0
dense_8 (Dense) (None, 512) 9470464
dense_9 (Dense) (None, 5) 2565
Total params: 9,496,613 Trainable params: 9,496,613 Non-trainable params: 0
Any thoughts on what may be wrong?
Upvotes: 1
Views: 5976
Reputation: 1
In the generator I updated the class_mode as 'sparse' and it worked fine.
train_data_gen = image_gen.flow_from_directory(train_dir, target_size = (IMG_SHAPE, IMG_SHAPE), batch_size = batch_size, class_mode = 'sparse')
Upvotes: 0
Reputation: 22021
pay attention to class_mode in your generator
'int': means that the labels are encoded as integers (e.g. for sparse_categorical_crossentropy loss). 'categorical' means that the labels are encoded as a categorical vector (e.g. for categorical_crossentropy loss). 'binary' means that the labels (there can be only 2) are encoded as float32 scalars with values 0 or 1 (e.g. for binary_crossentropy). None (no labels).
it seems you need 'int' instead of 'binary' for both train and validation generator
Upvotes: 4