问题
I have trained the CNN to classify images on 3 class. while training the model i have used ImageDataGenerator class from keras to apply preprocessing function on image and rescale it. Now my network is trained with a good accuracy on test set, but i don't know how to apply preprocessing function on single image prediction. If i use ImageDataGenerator it looks for directory. Suggest me some alternatives to do preprocessing function and rescaling on single image. see my code below
TRAINING SET:
train_datagen = ImageDataGenerator(preprocessing_function = tf.keras.applications.vgg16.preprocess_input,
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('./training_set',
target_size = (224, 224),
batch_size = 10,
class_mode = 'categorical')
TESTING SET:
test_datagen =ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,
rescale = 1./255)
test_set = test_datagen.flow_from_directory('./test_set',
target_size = (224, 224),
batch_size = 10,
shuffle=False,
class_mode = 'categorical')
Now,im unable to apply preprocessing function and rescaling on single image before prediction. SINGLE PREDICTION:
single_datagen = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,
rescale = 1./255)
single_test = single_datagen.flow_from_directory('./single_prediction/cc.jpg',
target_size = (224, 224),
batch_size = 1,
class_mode = 'categorical')
ERROR: NotADirectoryError: [Errno 20] Not a directory: './single_prediction/cc.jpg'
回答1:
The image data generator looks at the directory you specify and searches for sub directories within that directory that specify the classes. So create a directory called './single_prediction. Within that directory create a single sub directory call it test. Within that sub directory named test place the images that you want to test. Alternatively you can write some python code to produce the pre-processed images. Create a directory called test and place your images in it. I have not tested it but the code below should work.
import cv2
import numpy as np
import os
data_list=[]
dir=r'c:\test'
test_list=os.listdir(dir) # create a list of the files in the directory
batch_size=len(test_list) # determine number of files to process
for f in test_list: # iterate through the files
fpath=os.path.join (dir, f) # create path to the image file
img=cv2.imread(fpath) # read image using cv2
img=cv2.resize(img, (224,224)) # resize the image
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # cv2 creates bgr images, convert to rgb images
img=tf.keras.applications.vgg16.preprocess_input(img) # apply the Vgg16 preprocess function
data_list.append(img) # append processed image to the list
data=np.array(data_list)/255 # convert to an np array and rescale images
print (data.shape, batch_size)
predictions=model.predict(data,batch_size=batch_size, verbose=0 )
trials=len (predictions)
for i in range(0,trials):
predicted_class=predictions[i].argmax() # get index of highest probability
print (test_list[i], predicted_class) # print file name and class prediction
来源:https://stackoverflow.com/questions/63703280/how-to-predict-a-single-image-with-keras-imagedatagenerator