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')