Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Keras predicting on all images in a directory
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import os
# image folder
folder_path = '/path/to/folder/'
# path to model
model_path = '/path/to/saved/model.h5'
# dimensions of images
img_width, img_height = 320, 240
# load the trained model
model = load_model(model_path)
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# load all images into a list
images = []
for img in os.listdir(folder_path):
img = os.path.join(folder_path, img)
img = image.load_img(img, target_size=(img_width, img_height))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
images.append(img)
# stack up images list to pass for prediction
images = np.vstack(images)
classes = model.predict_classes(images, batch_size=10)
print(classes)
@ZER-0-NE

This comment has been minimized.

Copy link

@ZER-0-NE ZER-0-NE commented Jun 19, 2019

Line 23: img = image.img_to_array(img)

@ritiek

This comment has been minimized.

Copy link
Owner Author

@ritiek ritiek commented Jun 19, 2019

Done, thanks!

@SHAHEENGEOLO

This comment has been minimized.

Copy link

@SHAHEENGEOLO SHAHEENGEOLO commented Jun 24, 2019

Thank You for your code please how to plot or show the predicting result as images using pyplot or another library

@tharindu326

This comment has been minimized.

Copy link

@tharindu326 tharindu326 commented Jul 20, 2019

Line 22 : img = image.load_img(folder_path + img, target_size=(img_width, img_height))
your one didnt work for me!!

@ritiek

This comment has been minimized.

Copy link
Owner Author

@ritiek ritiek commented Aug 10, 2019

@tharindu326 Fixed, thanks!

@xaber14

This comment has been minimized.

Copy link

@xaber14 xaber14 commented Aug 26, 2019

why i got this error?
OSError: Unable to open file (unable to open file: name = '/model/model.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

@puneethrj

This comment has been minimized.

Copy link

@puneethrj puneethrj commented Sep 4, 2019

The code could use
img = img/255 after line 24
or
images = images/255 after line 28

@BhagyasriYella

This comment has been minimized.

Copy link

@BhagyasriYella BhagyasriYella commented Jan 31, 2020

Hi,
I have a scenario where I have to predict images into Crack and Non-Crack. I have prepared the model for that and used your code to predict the images, but unable to save them into folders where images with Crack should be saved in "/Crack" folder and images with Non-Crack should be saved in "/Non-Crack" folder.
Can anyone please help me on how to save the predicted images.

@tharindu326

This comment has been minimized.

Copy link

@tharindu326 tharindu326 commented Feb 1, 2020

@BhagyasriYella

i just copied the code i have used. this is the way to do it! plz rearrange saving formats as you want

model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

###############change the code as follow ################

    img = image.load_img(filename, target_size=(img_width, img_height))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    classes = model.predict_classes(img, batch_size=16)
    print(classes)
    if classes == 0:
        filename1 = result_negative_folder + "/image_" + str(int(frameId)) + ".jpg"  
        cv2.imwrite(filename1, img)
    elif classes == 1:
        filename2 = result_possitive_folder + "/image_" + str(int(frameId)) + ".jpg"
        cv2.imwrite(filename2, img)

use the directories as follow
result_possitive_folder = 'G:/Uni/7th sem/load_model_test01//Crack'
result_negative_folder = 'G:/Uni/7th sem/load_model_test01//non_Crack'

or if it is a folder inside your code folder (envi), "/crack" and "/non _crack" will enough
Note: use a loop for 'frameId' or else all images will replace!! if not just save by the default image name that you loaded

@BhagyasriYella

This comment has been minimized.

Copy link

@BhagyasriYella BhagyasriYella commented Feb 3, 2020

@tharindu326

Hey, thanks for the code. My code ran successfully :)

@RamananThiru

This comment has been minimized.

Copy link

@RamananThiru RamananThiru commented Mar 18, 2020

Thank You for your code please how to plot or show the predicting result as images using pyplot or another library

Did you get any reply regarding it?

@ccwpog

This comment has been minimized.

Copy link

@ccwpog ccwpog commented May 22, 2020

Hello,
I have a similar problem like @BhagyasriYella, only that my classifier uses rescaled images. I solved this in the code via

    img /= 255.
    classes = model.predict_classes(img, batch_size=10)
    img *= 255.

However, with the rescaling and without, I do get my original images (.jpg) classified correctly (as seen on the names) but somehow I cannot open them and they are 0KB. Any ideas on why that is?

@fjonabushi

This comment has been minimized.

Copy link

@fjonabushi fjonabushi commented Jun 23, 2020

Hello,
can you please help me with my issue:
I want to show the results on a table showing the correct and incorrect predictions but after several tries I have not been able to implement the prettytable correctly in your code.
Can you please help me!
Thank you in advance

@sebyo

This comment has been minimized.

Copy link

@sebyo sebyo commented Aug 8, 2020

Hello
can you please help me !
if I want to save the predicted images into a folder ,how should I do it!

@depender321

This comment has been minimized.

Copy link

@depender321 depender321 commented Oct 22, 2020

It worked Thanks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment