For a binary problem, I want to predict a single image? My test image size varies in resolutions. I am getting bellow error with the code I am trying to use. The code I got from below link- How to predict input image using trained model in Keras?
ValueError: Input 0 of layer dense_2 is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape [None, 576]
For single image prediction
from keras.models import load_model
from keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
import os
from keras.models import load_model
# dimensions of our images
img_width, img_height = 80, 80
# load the model we saved
model = load_model("new3.h5")
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# predicting images
img = image.load_img('46.jpg', target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict_classes(images, batch_size=32)
print(classes)
Full Code:
train_data_path = "/content/drive/My Drive/s/train"
valid_data_path = "/content/drive/My Drive/s/valid"
test_data_path = "/content/drive/My Drive/s/test"
img_rows = 100
img_cols = 100
epochs = 1
print(epochs)
batch_size = 32
num_of_train_samples = 2061
num_of_test_samples = 500
#Image Generator
train_datagen = ImageDataGenerator(rescale=1. / 255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
valid_datagen = ImageDataGenerator(rescale=1. / 255)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_path,
target_size=(img_rows, img_cols),
batch_size=batch_size,
shuffle=True,
class_mode='categorical')
validation_generator = valid_datagen.flow_from_directory(valid_data_path,
target_size=(img_rows, img_cols),
batch_size=batch_size,
shuffle=True,
class_mode='categorical')
test_generator = test_datagen.flow_from_directory(test_data_path,
target_size=(img_rows, img_cols),
batch_size=batch_size,
shuffle=False,
class_mode='categorical')
model = Sequential()
model.add(Conv2D((32), (3, 3), input_shape=(img_rows, img_cols, 3), kernel_initializer="glorot_uniform"))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D((32), (3, 3),kernel_initializer="glorot_uniform"))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dropout(0.5))
model.add(Dense(512))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['acc'])
#Train
history=model.fit(train_generator,
steps_per_epoch=num_of_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=num_of_test_samples // batch_size)