I have an already trained model and I want to make binary classification predictions on images in a directory. I have over 100,000 images so for efficiency, I want to do batch predictions. How can I do batch predictions on my images, get the prediction results, and store the images in two separate folders after they have been predicted on based on the class?
This is what my code looks like so far...
model_filepath = r"C:\Users\model_200.h5"
model = tf.keras.models.load_model(model_filepath)
test_dir = r"C:\Users\image_testing_folder"
batch_size = 64
IMG_HEIGHT = 200
IMG_WIDTH = 200
test_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
test_image_gen = test_image_generator.flow_from_directory(directory=str(test_dir),
batch_size=batch_size,
shuffle=False,
target_size=(IMG_HEIGHT, IMG_WIDTH),
)
predictions = (model.predict(test_image_gen) > 0.5).astype("int32")
predictions
One solution would be to tie the prediction with an image filepath, then use shutil.move() to move original image to a destination folder. How would I do this? Is there a better way to do batch predictions than using an ImageDataGenerator and .flow_from_directory?