1

I'm trying to run a CNN code from a Udemy course simply training on images of dogs and cats for classification. However I'm stuck on the training step with this value error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-894200829115> in <module>()
----> 1 cnn.fit(x = training_set, validation_data = test_set, epochs = 25)

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, 
*args, **kwargs)
106   def _method_wrapper(self, *args, **kwargs):
107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
109 
110     # Running inside `run_distribute_coordinator` already.

~\Anaconda3\lib\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)
1061           use_multiprocessing=use_multiprocessing,
1062           model=self,
-> 1063           steps_per_execution=self._steps_per_execution)
1064 
1065       # Container that configures and calls `tf.keras.Callback`s.

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, 
sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, 
max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1115         use_multiprocessing=use_multiprocessing,
1116         distribution_strategy=ds_context.get_strategy(),
-> 1117         model=model)
1118 
1119     strategy = ds_context.get_strategy()

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, 
sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
914         max_queue_size=max_queue_size,
915         model=model,
--> 916         **kwargs)
917 
918   @staticmethod

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, 
sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
786     peek, x = self._peek_and_restore(x)
787     peek = self._standardize_batch(peek)
--> 788     peek = _process_tensorlike(peek)
789 
790     # Need to build the Model on concrete input shapes.

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in _ 
process_tensorlike(inputs)
1019     return x
1020 
-> 1021   inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
1022   return nest.list_to_tuple(inputs)
1023 

~\Anaconda3\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, 
**kwargs)
633 
634   return pack_sequence_as(
--> 635       structure[0], [func(*x) for x in entries],
636       expand_composites=expand_composites)
637 

~\Anaconda3\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
633 
634   return pack_sequence_as(
--> 635       structure[0], [func(*x) for x in entries],
636       expand_composites=expand_composites)
637 

~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in 
convert_numpy_and_scipy(x)
1014       if issubclass(x.dtype.type, np.floating):
1015         dtype = backend.floatx()
-> 1016       return ops.convert_to_tensor(x, dtype=dtype)
1017     elif scipy_sparse and scipy_sparse.issparse(x):
1018       return _scipy_sparse_to_sparse_tensor(x)

~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, 
name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497 
1498     if ret is None:
-> 1499       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500 
1501     if ret is NotImplemented:

~\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py in 
_default_conversion_function(***failed resolving arguments***)
 50 def _default_conversion_function(value, dtype, name, as_ref):
 51   del as_ref  # Unused.
 ---> 52   return constant_op.constant(value, dtype, name=name)
 53 
 54 

 ~\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, 
shape, name)
262   """
263   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264                         allow_broadcast=True)
265 
266 

~\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, 
dtype, shape, name, verify_shape, allow_broadcast)
273       with trace.Trace("tf.constant"):
274         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276 
277   g = ops.get_default_graph()

~\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, 
value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299   """Implementation of eager constant."""
--> 300   t = convert_to_eager_tensor(value, ctx, dtype)
301   if shape is None:
302     return t

~\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in 
 convert_to_eager_tensor(value, ctx, dtype)
 96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
 97   ctx.ensure_initialized()
 ---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
 99 
100 

ValueError: object __array__ method not producing an array

Below is the code snippet:

# Importing the libraries
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
tf.__version__

# Part 1 - Data Preprocessing

# Preprocessing the Training set
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

# Preprocessing the Test set
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

# Part 2 - Building the CNN

# Initialising the CNN
cnn = tf.keras.models.Sequential()

# Step 1 - Convolution
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 
3]))

# Step 2 - Pooling
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

# Adding a second convolutional layer
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

# Step 3 - Flattening
cnn.add(tf.keras.layers.Flatten())

# Step 4 - Full Connection
cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))

# Step 5 - Output Layer
cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

# Part 3 - Training the CNN

# Compiling the CNN
cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Training the CNN on the Training set and evaluating it on the Test set
cnn.fit(x = training_set, validation_data = test_set, epochs = 25) # the line causing problem

As you can see, everything else works fine until the training step: enter image description here I'm using Python 3.7.0 installed from Anaconda3 on Win10. I've tried the following:

  1. Run on both Jupyter Notebook and local IDE (Spyder)
  2. Use different versions of Tensorflow, i.e. v2.3.1, v 2.3.0 and v2.2.0 (the version used in the lecture)

Neither of the above worked since the problem persists. The keras version installed is 2.4.3. However the code runs smoothly on Google Colab, where the only difference is that the data images were loaded and unzipped from DropBox with commands since Google Colab is not connected with host OS file system.

I would appreciate any insights and help on what causes the error and how to solve it.

1 Answer 1

1

In your code for the model shown below

cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 
3]))

input_shape is a tuple not a list. Should be input_shape=(64,64). There may be other issues but fix this first

Sign up to request clarification or add additional context in comments.

1 Comment

But this is the way they put it in the lecture. 3 corresponds to the RGB values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.