Keras_lesson1 'JpegImageFile' object is not subscriptable


I was trying to run the following code and got the error message. Anyone can help how to resolve this?

model.fit_generator(train_generator, train_generator.n // batch_size, epochs=1, workers=3,
validation_data=validation_generator, validation_steps=validation_generator.n // batch_size)

Epoch 1/1

TypeError Traceback (most recent call last)
~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/utils/ in get(self)
577 while self.is_running():
–> 578 inputs = self.queue.get(block=True).get()
579 self.queue.task_done()

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

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

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/utils/ in get_index(uid, i)
400 “”"
–> 401 return _SHARED_SEQUENCES[uid][i]

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/preprocessing/ in getitem(self, idx)
824 self.batch_size * (idx + 1)]
–> 825 return self._get_batches_of_transformed_samples(index_array)

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/preprocessing/ in _get_batches_of_transformed_samples(self, index_array)
1232 if self.image_data_generator.preprocessing_function:
-> 1233 img = self.image_data_generator.preprocessing_function(img)
1234 if self.target_size is not None:

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/applications/ in preprocess_input(x, data_format, mode)
177 return _preprocess_symbolic_input(x, data_format=data_format,
–> 178 mode=mode)

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/applications/ in _preprocess_symbolic_input(x, data_format, mode)
131 # ‘RGB’->‘BGR’
–> 132 x = x[…, ::-1]
133 mean = [103.939, 116.779, 123.68]

TypeError: ‘JpegImageFile’ object is not subscriptable

The above exception was the direct cause of the following exception:

StopIteration Traceback (most recent call last)
in ()

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/legacy/ in wrapper(*args, **kwargs)
89 warnings.warn('Update your ' + object_name + 90 ' call to the Keras 2 API: ’ + signature, stacklevel=2)
—> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/engine/ 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)
2190 batch_index = 0
2191 while steps_done < steps_per_epoch:
-> 2192 generator_output = next(output_generator)
2194 if not hasattr(generator_output, ‘len’):

~/anaconda3/envs/fastai/lib/python3.6/site-packages/keras/utils/ in get(self)
582 except Exception as e:
583 self.stop()
–> 584 six.raise_from(StopIteration(e), e)
586 def _send_sequence(self):

~/anaconda3/envs/fastai/lib/python3.6/site-packages/ in raise_from(value, from_value)

StopIteration: ‘JpegImageFile’ object is not subscriptable

I’m getting similar issues, right after I upgraded Keras. Downgrading back to keras 2.1.4 seems to do the trick.

pip uninstall keras
pip install -Iv keras==2.1.4


I’am getting same issues as well and downgrading pip seems not working for me ,
can someone please help ?
I’m working on google compute with ubuntu 16.0.1

thanks, this works for me!

hi Allen, after downgrading, Keras code does run but it is very slow. Training step takes estimated 3 hours to complete 1 epoch if I read the time correctly). Could you share your machine details and how long did it take for you?

I’m on google cloud :
Machine type : n1-highmem-4 (4 vCPUs, 26 GB memory)
GPUs : 1 x NVIDIA Tesla K80
Harddisk : 50GB SSD persistent disk

python3 -c ‘import tensorflow as tf; print(tf.version)’
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally

conda list | grep tensorflow
tensorflow-gpu 1.5.0
tensorflow-gpu 1.0.1 py36_4 anaconda
tensorflow-tensorboard 1.5.1

StopIteration: ‘JpegImageFile’ object is not subscriptable
@pcfour Can someone provide an alternate solution to this .

My issue was with a known bug in Keras. And downgrading worked for me. I recommend you provide a bit more information, including the Keras and backend versions (Tensorflow/CNTK, not sure what your setup looks like) and full stack trace. Maybe someone else has a similar issue to you.

Good luck!

Just solved it today, after downgrading to keras 2.1.4, i needed to restart the entire instance, made sure the keras backend is tensorflow, checked that tensorflow-gpu is installed and added the following code block as per

import keras
config = tf.ConfigProto( device_count = {‘GPU’: 1 , ‘CPU’: 1} )
sess = tf.Session(config=config)

I’m confused, this seems like an issue with Keras if the only solution is downgrading? Is there a reason why the current version is acting this way with JPEGs?

I also have the same problem.
I have downgraded Keras to 2.1.4 but now when I run this cell :

model.fit_generator(train_generator, train_generator.n // batch_size, epochs=3,
validation_data=validation_generator, validation_steps=validation_generator.n // batch_size)

My notebook restart without sending any error?
Anyone know what to do?

I have tensorflow-gpu 1.5 and Keras 2.1.4 and try to run the notebook on paperspace.

I’ve solved the problem building the datagens in the following way:

‘train_datagen = ImageDataGenerator(preprocess_input, shear_range= 0.2,
zoom_range=0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator(preprocess_input)’

1 Like

Good work!