How to Save Prediction values for whole data in keras
up vote
1
down vote
favorite
I am using pre-trained VGG16 model to classify images located in the folder. Currently, I am able to classify only one single image.
- How can I modify the code to classify all the images in the folder
- How can I save the prediction values for each image ?
Below is my code :
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
filename = 'cat.jpg'
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
print('PIL image size',original.size)
plt.imshow(original)
plt.show()
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
plt.show()
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular axis
# We want the input matrix to the network to be of the form (batchsize, height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you
python machine-learning keras deep-learning
add a comment |
up vote
1
down vote
favorite
I am using pre-trained VGG16 model to classify images located in the folder. Currently, I am able to classify only one single image.
- How can I modify the code to classify all the images in the folder
- How can I save the prediction values for each image ?
Below is my code :
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
filename = 'cat.jpg'
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
print('PIL image size',original.size)
plt.imshow(original)
plt.show()
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
plt.show()
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular axis
# We want the input matrix to the network to be of the form (batchsize, height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you
python machine-learning keras deep-learning
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using pre-trained VGG16 model to classify images located in the folder. Currently, I am able to classify only one single image.
- How can I modify the code to classify all the images in the folder
- How can I save the prediction values for each image ?
Below is my code :
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
filename = 'cat.jpg'
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
print('PIL image size',original.size)
plt.imshow(original)
plt.show()
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
plt.show()
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular axis
# We want the input matrix to the network to be of the form (batchsize, height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you
python machine-learning keras deep-learning
I am using pre-trained VGG16 model to classify images located in the folder. Currently, I am able to classify only one single image.
- How can I modify the code to classify all the images in the folder
- How can I save the prediction values for each image ?
Below is my code :
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
filename = 'cat.jpg'
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
print('PIL image size',original.size)
plt.imshow(original)
plt.show()
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
plt.show()
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular axis
# We want the input matrix to the network to be of the form (batchsize, height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you
python machine-learning keras deep-learning
python machine-learning keras deep-learning
edited Nov 21 at 5:26
Milo Lu
1,56711327
1,56711327
asked Nov 20 at 15:00
Miss.lo0ora
13
13
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32
add a comment |
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You just have to list filenames and loop over them. You could use i.e. os.listdir, specifying the input folder. I removed the various imshow or plot chunks of code.
Code below:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
import os
nfiles = os.listdir("./inputfolder") # List filenames
for filename in nfiles: # Enter the loop
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular
axis
# We want the input matrix to the network to be of the form (batchsize,
height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395787%2fhow-to-save-prediction-values-for-whole-data-in-keras%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You just have to list filenames and loop over them. You could use i.e. os.listdir, specifying the input folder. I removed the various imshow or plot chunks of code.
Code below:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
import os
nfiles = os.listdir("./inputfolder") # List filenames
for filename in nfiles: # Enter the loop
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular
axis
# We want the input matrix to the network to be of the form (batchsize,
height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
add a comment |
up vote
0
down vote
You just have to list filenames and loop over them. You could use i.e. os.listdir, specifying the input folder. I removed the various imshow or plot chunks of code.
Code below:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
import os
nfiles = os.listdir("./inputfolder") # List filenames
for filename in nfiles: # Enter the loop
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular
axis
# We want the input matrix to the network to be of the form (batchsize,
height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
add a comment |
up vote
0
down vote
up vote
0
down vote
You just have to list filenames and loop over them. You could use i.e. os.listdir, specifying the input folder. I removed the various imshow or plot chunks of code.
Code below:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
import os
nfiles = os.listdir("./inputfolder") # List filenames
for filename in nfiles: # Enter the loop
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular
axis
# We want the input matrix to the network to be of the form (batchsize,
height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
You just have to list filenames and loop over them. You could use i.e. os.listdir, specifying the input folder. I removed the various imshow or plot chunks of code.
Code below:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
import os
nfiles = os.listdir("./inputfolder") # List filenames
for filename in nfiles: # Enter the loop
# load an image in PIL format
original = load_img(filename, target_size=(224, 224))
# convert the PIL image to a numpy array
# IN PIL - image is in (width, height, channel)
# In Numpy - image is in (height, width, channel)
numpy_image = img_to_array(original)
print('numpy array size',numpy_image.shape)
# Convert the image / images into batch format
# expand_dims will add an extra dimension to the data at a particular
axis
# We want the input matrix to the network to be of the form (batchsize,
height, width, channels)
# Thus we add the extra dimension to the axis 0.
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
# prepare the image for the VGG model
processed_image = vgg16.preprocess_input(image_batch.copy())
# get the predicted probabilities for each class
predictions = vgg_model.predict(processed_image)
print (predictions)
# convert the probabilities to class labels
# We will get top 5 predictions which is the default
#label = decode_predictions(predictions)
answered Nov 20 at 17:17
FMarazzi
318213
318213
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
add a comment |
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
Thank you so much Mr.FMarazzi, The folder structure is: Folder1 : contains the .py file Subfolder1: contains some files Sub-SubFolder : Contains the images that I need to read them through the loop So, Shall I replace this line nfiles = os.listdir("./inputfolder") # List filenames with nfiles = os.listdir("./Subfolder1/Sub-SubFolder") # List filenames ? Thank you in advanced
– Miss.lo0ora
Nov 20 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395787%2fhow-to-save-prediction-values-for-whole-data-in-keras%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just list files in your directory and classify them one by one. While doing this you can store your predictions in a list, file, anywhere..
– Josef Korbel
Nov 20 at 15:07
Thank you Mr.Josef for your valuable reply. Could you please share the code for that or any link showing the code if possible . With huge appreciation.
– Miss.lo0ora
Nov 20 at 16:32