Error in Image generator : Asked to retrieve element 0, but the Sequence has length 0

Bilal
  • I'm trying to calculate the True Positive, True Negative, False Positive, False Negative ratios in binary class coloured image classification problem.

  • I have binary class, faces and backgrounds colour images, and I have to classify them using MLP.

My problem is that: I get the Error:

ValueError: Asked to retrieve element 0, but the Sequence has length 0

Edit: Full Traceback

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
    128       raise ValueError('{} is not supported in multi-worker mode.'.format(
    129           method.__name__))
--> 130     return method(self, *args, **kwargs)
    131 
    132   return tf_decorator.make_decorator(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1577           use_multiprocessing=use_multiprocessing,
   1578           model=self,
-> 1579           steps_per_execution=self._steps_per_execution)
   1580 
   1581       # Container that configures and calls `tf.keras.Callback`s.

/usr/local/lib/python3.6/dist-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()

/usr/local/lib/python3.6/dist-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

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
    784     # Since we have to know the dtype of the python generator when we build the
    785     # dataset, we have to look at a batch to infer the structure.
--> 786     peek, x = self._peek_and_restore(x)
    787     peek = self._standardize_batch(peek)
    788     peek = _process_tensorlike(peek)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _peek_and_restore(x)
    918   @staticmethod
    919   def _peek_and_restore(x):
--> 920     return x[0], x
    921 
    922   def _handle_multiprocessing(self, x, workers, use_multiprocessing,

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
     55                              'but the Sequence '
     56                              'has length {length}'.format(idx=idx,
---> 57                                                           length=len(self)))
     58         if self.seed is not None:
     59             np.random.seed(self.seed + self.total_batches_seen)

ValueError: Asked to retrieve element 0, but the Sequence has length 0
  • While trying to predict each folder from the 2 classes separately(and not the root folder which contains 2 folders, one for each class as in training).

My code which generating the Error is :

test_face_dir = "/content/test/TESTSET/face"
test_background_dir = "/content/test/TESTSET/background"
# Face DG
test_datagen_face = ImageDataGenerator(rescale=1./255)

test_generator_face = test_datagen_face.flow_from_directory(
    test_face_dir,
    target_size=img_window[:2],
    batch_size=batch_size,
    class_mode='binary',
    color_mode='rgb'
    )
# Background DG
test_datagen_background = ImageDataGenerator(rescale=1./255)

test_generator_background = test_datagen_background.flow_from_directory(
    test_background_dir,
    target_size=img_window[:2],
    batch_size=batch_size,
    class_mode='binary',
    color_mode='rgb'
    )
#-----------------------------------------
prediction_face = simpleMLP.predict(test_generator_face)
prediction_background = simpleMLP.predict(test_generator_background)
#-----------------------------------------
# th = 0.5 #threshold
# Face
prediction_face[prediction_face>=th]=1 
prediction_face[prediction_face<th]=0
pred_face = np.squeeze(prediction_face)
print('pred shape: ', pred_face.shape,int(np.sum(pred_face)))
# Background
prediction_background[prediction_background>=th]=1 
prediction_background[prediction_background<th]=0
pred_background = np.squeeze(prediction_background)
print('pred shape: ', pred_background.shape,int(np.sum(pred_background)))

Bilal

The Error was generated because I'm using class_mode='binary', while I'm specifying one folder of images instead of two folders faces, and backgrounds in my case, when I chose the parent folder which contains both of them this problem disappeared.

Any way to calculate the True Positive, True Negative, False Positive, False Negative ratios I have got the Ground Truth as following:

# Get Y_GroundTruth
y_label = (np.expand_dims(test_generator_eval.classes, axis=1).ravel()).astype(int)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

dictionary update sequence element #0 has length 3; 2 is required

Download image using SDWebImage, error -999 (or) SDWebImageErrorDomain Code=0 "Downloaded image has 0 pixels

ValueError: dictionary update sequence element #0 has length 1; 2 is required

Django - Dictionary update sequence element #0 has length 1; 2 is required

ValueError: dictionary update sequence element #0 has length 3; 2 is required when attempting to coerce generator function into dictionary

Array shows 0 as length when it has elements in it

console.log of element.children shows 0 length but has three entries when expanded later

Tuple (key, value) to dictionary ValueError: dictionary update sequence element #0 has length 6; 2 is required

dictionary update sequence element #0 has length 1; 2 is required when loading page

Chi Square Analysis - expected frequencies has a zero element at (0,). error

Value error dictionary update sequence element #0 has length 1; 2 is required

document.getElementsByClassName('name') has a length of 0

dictionary update sequence element #0 has length 15; 2 is required

Spring JPA + Boot +Sequence Generator always 0

dictionary update sequence element #0 has length 4; 2 is required

c# array has always the length of 0

Getting an error: "ValueError: dictionary update sequence element #0 has length 1; 2 is required" when used map function

array length is 0 even though it has values

Dictionary update sequence element #0 has length 1; 2 is required in Django

Array has elements but length is 0

Python - ValueError: dictionary update sequence element #0 has length 15; 2 is required

Error in tuneRF in R in if (n == 0) stop("data (x) has 0 rows") : argument is of length zero

I have a JSON array with 4 elements in angular 7, but it shows 0 when I asked for it`s length

ValueError: dictionary update sequence element #0 has length 25; 2 is required When Unpacking a tuple with a dictionary

default_attrs.update(attrs) ValueError: dictionary update sequence element #0 has length 1; 2 is required

Convert To Dictionary in Robot Framework gives the error "ValueError: dictionary update sequence element #0 has length 1; 2 is required"

dictionary update sequence element #0 has length 1; 2 is required Error | Python

Django dictionary update sequence element #0 has length 0; 2 is required

ValueError: dictionary update sequence element #0 has length 14; 2 is required