Jenni99
Jenni99

Reputation: 23

I am getting list 'index out of range' while using model.predict in keras

This is my code and I am using transfer learning to train my model. But I am getting index out of range error. It is giving this error for model.predict() function, when tried to test my model. What could be the reason?

IMAGE_SIZE = [100, 100]

train_path = 'input/train'
valid_path = 'input/val'

add preprocessing layer to the front of VGG

vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

# don't train existing weights
for layer in vgg.layers:
  layer.trainable = False

useful for getting number of classes

folders = glob('input/train/*')

our layers

x = Flatten()(vgg.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='softmax')(x)

create a model object

model = Model(inputs=vgg.input, outputs=prediction)

view the structure of the model

model.summary()

tell the model what cost and optimization method to use

model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('input/train',
                                                 target_size = (100, 100),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

val_set = test_datagen.flow_from_directory('input/val',
                                            target_size = (100, 100),
                                            batch_size = 32,
                                            class_mode = 'categorical')

fit the model

r = model.fit(
  training_set,
  validation_data=val_set,
  epochs=50,
  steps_per_epoch=len(training_set),
  validation_steps=len(val_set)
)

This particular line is giving error

model.predict('/content/input/test/0/IMG_4099.JPG')

Upvotes: 2

Views: 1616

Answers (1)

n1colas.m
n1colas.m

Reputation: 3989

Model predic doesn't accept a path as input. From documentation, predic input samples can be:

  • A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
  • A TensorFlow tensor, or a list of tensors (in case the model has multiple inputs).
  • A tf.data dataset.
  • A generator or keras.utils.Sequence instance. A more detailed description of unpacking behavior for iterator types (Dataset, generator, Sequence) is given in the Unpacking behavior for iterator-like inputs section of Model.fit.

There are multiple ways you can get a numpy array from the image path. You can, for example, use Keras preprocessing image.load_img to read the image, and then use img_to_array to obtain the numpy array. If the image in the folder is not already in the size expected by the model, you will have to use target_size argument to resize to the input shape of the model.

img = tf.keras.preprocessing.image.load_img(
    "/content/input/test/0/IMG_4099.JPG",
    target_size=(100,100)
)
img_nparray = tf.keras.preprocessing.image.img_to_array(img)
type(img_nparray) # numpy.ndarray
input_Batch = np.array([img_nparray])   # Convert single image to a batch.
predictions = model.predict(input_Batch)

Another alternative would be to use the previous declared image generator (test_datagen, again without any data augmentation for a fair prediction) pointing to the folder containing that single (or multiple) image(s).

Folder structure

├── content
│   └── input
│       └── test
│           └── 0
│               └── IMG_4099.JPG
import tensorflow as tf
from tensorflow import keras

test_datagen = ImageDataGenerator(rescale = 1./255)

test_ImgGen = test_datagen.flow_from_directory(
    '/content/input/test/0/',
    target_size = (100, 100),
    class_mode='categorical'
)

model.predict(test_ImgGen)

Upvotes: 1

Related Questions