question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I am trying to finetune VGG16 model using preprocessing_function parameter in ImageDataGenerator but I am getting error "'PngImageFile' object is not subscriptable"

See original GitHub issue
import numpy as np
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras.models import Model
from keras.optimizers import SGD

# dimensions of our images.
img_width, img_height = 224,224

train_data_dir = '/train/'
validation_data_dir = '/val/'

# number of samples used for determining the samples_per_epoch
nb_train_samples = 7749
nb_validation_samples = 583
epochs = 10
batch_size = 8 

def vgg_preprocess_img(img):
    img[:,:,2] -= 103.939
    img[:,:,0] -= 116.779
    img[:,:,1] -= 123.68
    #img = img.transpose((2,0,1))
    img = np.expand_dims(img, axis=0)
    return img


train_datagen = ImageDataGenerator(
        rescale=None,             
         preprocessing_function=vgg_preprocess_img,
         shear_range=0.2,       # randomly applies shearing transformation
         zoom_range=0.2,    # randomly applies shearing transformation  
         featurewise_std_normalization=True,
         samplewise_std_normalization=True,
         zca_whitening=True,
         rotation_range=90,
         width_shift_range=.2,
         height_shift_range=.2,
        vertical_flip=True,
        horizontal_flip=True)  # randomly flip the images


val_datagen = ImageDataGenerator(
        preprocessing_function=vgg_preprocess_img,
         rescale=None)       # normalize pixel values to [0,1]


train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = train_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary')


history = model.fit_generator(
            train_generator,
            steps_per_epoch=nb_train_samples // batch_size,
            epochs=epochs,
            validation_data=validation_generator,
            validation_steps=nb_validation_samples // batch_size,
            callbacks=[reduce_lr])

with this, I am getting an error Epoch 1/10


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

~/anaconda3/envs/tensorflow/lib/python3.6/multiprocessing/pool.py in get(self, timeout) 607 else: –> 608 raise self._value 609

~/anaconda3/envs/tensorflow/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/envs/tensorflow/lib/python3.6/site-packages/keras/utils/data_utils.py in get_index(uid, i) 400 “”" –> 401 return _SHARED_SEQUENCES[uid][i] 402

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

~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/preprocessing/image.py 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:

<ipython-input-66-a642de980949> in vgg_preprocess_img(img) 27 def vgg_preprocess_img(img): —> 28 img[:,:,2] -= 103.939 29 img[:,:,0] -= 116.779

TypeError: ‘PngImageFile’ object is not subscriptable

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Dref360commented, Apr 30, 2018

Just update to the latest Keras version.

1reaction
JaneLecommented, Apr 30, 2018

I can make it work by writing the preprocessing_input myself as follows: from keras.preprocessing.image import img_to_array, array_to_img def mypreprocess_input(x): #print(x) imgarr = img_to_array(x, data_format=None) #print(imgarr) # 'RGB'->'BGR' bgr = imgarr[..., ::-1] mean = [103.939, 116.779, 123.68] bgr-=mean return array_to_img(bgr)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transfer Learning with VGG16 Model Fit Error - Stack Overflow
Your object dtypes of dataframe might not be in proper format. Use training_dataframe.info() to know the dtypes. ALso check whether there is ...
Read more >
Keras_lesson1 'JpegImageFile' object is not subscriptable
Hi, I was trying to run the following code and got the error message. Anyone can help how to resolve this?
Read more >
from tensorflow.keras.preprocessing.image import ... - You.com
You can pass the name of the preprocessing function to the preprocessing argument. If you do not want data augmentation, you do not...
Read more >
[FIXED] Image Data Generator not producing ... - PythonFixing
I was understanding image classification using Keras. There was a function called image data generator which was used to prepare an image ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found