如何在keras中保存整个数据的预测值

时间:2018-11-20 15:00:10

标签: python machine-learning keras deep-learning

我正在使用经过预先训练的VGG16模型对文件夹中的图像进行分类。目前,我只能对一个图像进行分类。

  1. 如何修改代码以对文件夹中的所有图像进行分类
  2. 如何保存每个图像的预测值?

下面是我的代码:

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)

谢谢

1 个答案:

答案 0 :(得分:0)

您只需要列出文件名并遍历它们即可。您可以使用os.listdir来指定输入文件夹。我删除了各种显示或打印代码块。

以下代码:

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)