Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (18.4k points)

When I am working with the image classification I am getting the below-mentioned error

ValueError: Error when checking input: expected flatten_input to have 4 dimensions, but got array with shape (80, 80, 3)

Traceback (most recent call last):

  File "/home/ubuntu/capstone/TFcaps.py", line 163, in <module>

    validation_steps=total_val // batch_size

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py", line 819, in fit

    use_multiprocessing=use_multiprocessing)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 235, in fit

    use_multiprocessing=use_multiprocessing)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 593, in _process_training_inputs

    use_multiprocessing=use_multiprocessing)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 706, in _process_inputs

    use_multiprocessing=use_multiprocessing)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 702, in _init_

    x = standardize_function(x)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 684, in standardize_function

    return dataset.map(map_fn, num_parallel_calls=dataset_ops.AUTOTUNE)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 1591, in map

    self, map_func, num_parallel_calls, preserve_cardinality=True)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3926, in _init_

    use_legacy_function=use_legacy_function)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3147, in _init_

    self._function = wrapper_fn._get_concrete_function_internal()

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2395, in _get_concrete_function_internal

    args, *kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected

    graph_function, ,  = self._maybe_define_function(args, kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function

    graph_function = self._create_graph_function(args, kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function

    capture_by_value=self._capture_by_value),

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func

    func_outputs = python_func(*func_args, **func_kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3140, in wrapper_fn

    ret = _wrapper_helper(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3082, in _wrapper_helper

    ret = autograph.tf_convert(func, ag_ctx)(*nested_args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper

    raise e.ag_error_metadata.to_exception(e)

ValueError: in converted code:

 

    /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py:677 map_fn

        batch_size=None)

    /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py:2410 _standardize_tensors

        exception_prefix='input')

    /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_utils.py:573 standardize_input_data

        'with shape ' + str(data_shape))

 

    ValueError: Error when checking input: expected flatten_input to have 4 dimensions, but got array with shape (80, 80, 3)

I have built the model which looks like this

 model = tf.keras.Sequential([

    tf.keras.layers.Flatten(input_shape=(80, 80, 3)),

    tf.keras.layers.Dense(128, activation='relu'),

    tf.keras.layers.Dense(4)])

The image shapes are as follows:

Image shape:  (80, 80, 3)

Label:  [False False  True False]

<TakeDataset shapes: ((80, 80, 3), (4,)), types: (tf.float32, tf.bool)>

I am completing my model like this:

model.compile(optimizer=tf.keras.optimizers.Adam(lr=LR), loss=tf.keras.losses.CategoricalCrossentropy(), metrics=["accuracy"])

I am fitting model like this as mentioned:

history = model.fit(

    train_data,

    steps_per_epoch=total_train // batch_size,

    epochs=epochs,

    validation_data=val_data,

    validation_steps=total_val // batch_size

)

how do I solve this problem? please suggest me with a solution

1 Answer

0 votes
by (36.8k points)
edited by

Before using model.fit()., convert the dimensionality of the train input from 3 to 4 which is as follows:

train_data[0] = np.reshape(train_data[0], ((-1, 80, 80, 3)))

and also covert the train input lable is 2 dimention :

train_data[1] = np.reshape(train_data[1], ((-1, 4)))

Operations should also be performed on validating the data i.e val_data's, input images and labels

The solution mentioned above is assumed with train_data[0] and train_data[1] which can be used for indexing the input train image and train labels, if they are not modified with indexing or slicing to access the elements then reshape them to 4 or 2 dimensions as required.

I have also noticed that boolean values are also used to represent TRUE/FALSE for target values, For training the model we need the labels to be numeric, To do this we need Keras to be converted to categorical(). this process is called one-hot encoding, the code is mentioned below, hope it will help you

tf.keras.utils.to_categorical(

    y,

    num_classes=None,

    dtype='float32'

)

In the above code, y indicates the data, num_classes is assigned as none and data type is assigned with float32.

To learn more about data science in python and get Data Science certification

Browse Categories

...