ValueError in fit_generator. Asked to retrieve element 250, but the Sequence has length 250

I keep getting this value error "Asked to retrieve element 250, but the Sequence has length 250"
The numbers ‘250’ in this error change on a different size training set or batch size, but it is always happening. And cannot understand where that specific number is coming from. And if i set the number of epochs to 1, this never happens, it always happens on some other epoch. In this case it happened on the second epoch. Isn’t the batch generator infinite? or does it have a limited number of iterations?

My train size is 23000 images, and 2000 images in validation set. Basically the same folders as jeremy had setup.

Below you can find my code attached also. Basically all i’m doing is re-creating the VGG model for cats and dogs. Full Code is pasted after the error.

ERROR

fit(train_batches, valid_batches, batch_size, nb_epoch)
Epoch 1/2
2875/2875 [==============================] - 1823s - loss: 0.6034 - acc: 0.6671 - val_loss: 0.5242 - val_acc: 0.7340
Epoch 2/2
250/2875 [=>…] - ETA: 1628s - loss: 0.5233 - acc: 0.7350

ValueError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
530 while self.is_running():
–> 531 inputs = self.queue.get(block=True).get()
532 if inputs is not None:

~/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
643 else:
–> 644 raise self._value
645

~/anaconda3/lib/python3.6/multiprocessing/pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
118 try:
–> 119 result = (True, func(*args, **kwds))
120 except Exception as e:

~/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py in get_index(i)
382 global _SHARED_SEQUENCE
–> 383 return _SHARED_SEQUENCE[i]
384

~/anaconda3/lib/python3.6/site-packages/keras/preprocessing/image.py in getitem(self, idx)
718 ‘has length {length}’.format(idx=idx,
–> 719 length=len(self)))
720 if self.seed is not None:

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

During handling of the above exception, another exception occurred:

StopIteration Traceback (most recent call last)
in ()
----> 1 fit(train_batches, valid_batches, batch_size, nb_epoch)

in fit(batches, val_batches, batch_size, nb_epoch)
85 def fit(batches, val_batches, batch_size, nb_epoch):
86 model.fit_generator(batches, steps_per_epoch=int(np.ceil(batches.samples/batch_size)), epochs=nb_epoch,
—> 87 validation_data=val_batches, validation_steps=int(np.ceil(val_batches.samples/batch_size)))
88
89

~/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
85 warnings.warn('Update your ' + object_name + 86 ' call to the Keras 2 API: ’ + signature, stacklevel=2)
—> 87 return func(*args, **kwargs)
88 wrapper._original_function = func
89 return wrapper

~/anaconda3/lib/python3.6/site-packages/keras/models.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1132 use_multiprocessing=use_multiprocessing,
1133 shuffle=shuffle,
-> 1134 initial_epoch=initial_epoch)
1135
1136 @interfaces.legacy_generator_methods_support

~/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
85 warnings.warn('Update your ' + object_name + 86 ' call to the Keras 2 API: ’ + signature, stacklevel=2)
—> 87 return func(*args, **kwargs)
88 wrapper._original_function = func
89 return wrapper

~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
2013 batch_index = 0
2014 while steps_done < steps_per_epoch:
-> 2015 generator_output = next(output_generator)
2016
2017 if not hasattr(generator_output, ‘len’):

~/anaconda3/lib/python3.6/site-packages/keras/utils/data_utils.py in get(self)
534 except Exception as e:
535 self.stop()
–> 536 raise StopIteration(e)
537
538 def _send_sequence(self):

StopIteration: Asked to retrieve element 250, but the Sequence has length 250

CODE:

datapath=“dogscats/”
#datapath="dogscats/sample/"
trainpath=datapath + 'train/'
validpath=datapath + 'valid/'
batch_size=8
nb_epoch=2

vgg_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((3,1,1))
def vgg_preprocess(x):
x = x - vgg_mean
return x[:, ::-1] # reverse axis rgb->bgr

def CD_16():
model = Sequential()
model.add(Lambda(vgg_preprocess, input_shape=(3,224,224), output_shape=(3,224,224)))

filters=64
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(MaxPooling2D((2, 2), strides=(2, 2)))


filters=128
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

filters=256
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

filters=512
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

filters=512
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(filters, kernel_size=(3, 3), activation='relu'))  # Keras2
model.add(MaxPooling2D((2, 2), strides=(2, 2)))



model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))



model.compile(optimizer=Adam(lr=0.00001), loss='categorical_crossentropy', metrics=['accuracy'])
return model

model = CD_16()
def get_batches(path, gen=image.ImageDataGenerator(), shuffle=True, batch_size=batch_size, class_mode=‘categorical’):
return gen.flow_from_directory(path, target_size=(224,224),
class_mode=class_mode, shuffle=shuffle, batch_size=batch_size)

def fit(batches, val_batches, batch_size, nb_epoch):
model.fit_generator(batches, steps_per_epoch=int(np.ceil(batches.samples/batch_size)), epochs=nb_epoch,
validation_data=val_batches, validation_steps=int(np.ceil(val_batches.samples/batch_size)))

train_batches = get_batches(path=trainpath, batch_size=batch_size)
valid_batches = get_batches(path=validpath, batch_size=batch_size)
imgs,labels = next(train_batches)
plots(imgs,titles=labels)
np.shape(imgs)
fit(train_batches, valid_batches, batch_size, nb_epoch)

I was struggling with this and I had keras version 2.0.8 installed. I then reinstalled with the following command the issue seems to have gone away. It’s still version 2.0.8, but something seems to have changed:

pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

1 Like

Thanks this worked. I had keras=2.2.4 and faced similar issue. Moving back to keras 2.2.2 resolved it.