0

I am trying to make an image classifier CNN using TensorFlow. I am trying to load the dataset using a ImageDataGenerator. Like this:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
image_datagen = ImageDataGenerator(rescale=1/255)

IMAGE_DIMS=(200,200)
train_generator = image_datagen.flow_from_directory(
    TRAIN_DIR,
    target_size=IMAGE_DIMS,
    batch_size=80,
    class_mode="categorical",
    color_mode="grayscale",
    shuffle=True
)

model architecture:

model = keras.models.Sequential([
    keras.layers.Conv2D(16,(3,3), input_shape=(200,200,1), activation='relu'),
    keras.layers.MaxPooling2D(2, 2),
    keras.layers.Conv2D(32,(3,3), activation='relu'),
    keras.layers.MaxPooling2D(2, 2),
    keras.layers.Conv2D(64,(3,3), activation='relu'),
    keras.layers.MaxPooling2D(2, 2),
    keras.layers.Flatten(),
    keras.layers.Dense(units=512, activation="relu"),
    keras.layers.Dense(units=16, activation="softmax")
])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

I am loading an image dataset that is 200x200 pixels and greyscaled. There are 16 labels for the dataset(or the images are contained in 16 different folders). Loading the dataset works properly:

print(train_generator.labels)
print(train_generator.image_shape)
[ 0  0  0 ... 15 15 15]
(200, 200, 1)

After running:

model.fit(
    train_generator,
    steps_per_epoch=4,
    epochs=2
)

I am getting this error:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-124-95f52517d8f4> in <module>
----> 1 model.fit(
      2     train_generator,
      3     steps_per_epoch=4,
      4     epochs=2
      5 )

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1098                 _r=1):
   1099               callbacks.on_train_batch_begin(step)
-> 1100               tmp_logs = self.train_function(iterator)
   1101               if data_handler.should_sync:
   1102                 context.async_wait()

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
    826     tracing_count = self.experimental_get_tracing_count()
    827     with trace.Trace(self._name) as tm:
--> 828       result = self._call(*args, **kwds)
    829       compiler = "xla" if self._experimental_compile else "nonXla"
    830       new_tracing_count = self.experimental_get_tracing_count()

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
    886         # Lifting succeeded, so variables are initialized and we can run the
    887         # stateless function.
--> 888         return self._stateless_fn(*args, **kwds)
    889     else:
    890       _, _, _, filtered_flat_args = \

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
   2940       (graph_function,
   2941        filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 2942     return graph_function._call_flat(
   2943         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
   2944 

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1916         and executing_eagerly):
   1917       # No tape is watching; skip to running the function.
-> 1918       return self._build_call_outputs(self._inference_function.call(
   1919           ctx, args, cancellation_manager=cancellation_manager))
   1920     forward_backward = self._select_forward_and_backward_functions(

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    553       with _InterpolateFunctionError(self):
    554         if cancellation_manager is None:
--> 555           outputs = execute.execute(
    556               str(self.signature.name),
    557               num_outputs=self._num_outputs,

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57   try:
     58     ctx.ensure_initialized()
---> 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

InvalidArgumentError:  logits and labels must have the same first dimension, got logits shape [80,16] and labels shape [1280]
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-124-95f52517d8f4>:1) ]] [Op:__inference_train_function_19777]

Function call stack:
train_function

I am using jupyter Ipython notebook.

I am relatively new to TensorFlow.

What is the error about? How do I fix this issue?

Vedant Jumle
  • 133
  • 2
  • 11

1 Answers1

0

This resolution follows from this thread.

train_generator = image_datagen.flow_from_directory(
    '/Users/Anu/Documents',
    target_size=IMAGE_DIMS,
    batch_size=80,
    class_mode="sparse",
    color_mode="grayscale",
    shuffle=True
)
Mohan Radhakrishnan
  • 3,002
  • 5
  • 28
  • 42