如何从python中的文件夹中预测多个图像

数据挖掘 Python 分类 图像分类 预测
2021-10-09 01:29:54

这是从文件夹中预测多个图像的代码。但是为所有图像获得相同的标签(类)。我无法找出为什么每张图像都显示相同的标签。

# import the necessary packages
from tensorflow.keras.models import load_model
import argparse
import pickle
import cv2
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.imagenet_utils import decode_predictions
import numpy as np
import logging, os
import sys 
from keras.preprocessing import image
import tensorflow as tf
import math
import operator
from pathlib import Path

# disable the warnings
logging.disable(logging.WARNING)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"

image_path = "test_image_folder"

images = []
    
# load all images into a list
for img in os.listdir(image_path):
        img = os.path.join(image_path, img)
        img = image.load_img(img, target_size=(64,64))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        # normalize the image
        processed_image = np.array(img, dtype="float") / 255.0
        images.append(processed_image)
        
images = np.vstack(images)

# relative paths to the model and labels
model_path = os.path.join("Output", 'VGG_model.h5')
label_file_path = os.path.join("Output", 'labels')

# load the model and the label encoder
model = load_model(model_path)
lb = pickle.loads(open(label_file_path, "rb").read())

# make a prediction on the image
images_data = []
filenames = []
for filename in os.listdir(image_path):    
    pred_result = model.predict(images)
    images_data.append(pred_result)
    filenames.append(filename)

#sorts attributes according to confidence score (how probable attribute exists)
top_k = []
pred = [] 
for i in range(len(images_data)):
            rank = images_data[i][0].argsort()[-len(images_data[i][0]):][::-1]
            top_k.append(rank)
            top = top_k[i][:15]
            print(filenames[i])
            for node_id in top:
                human_string = label_file_path[node_id]
                score = images_data[i][0][node_id]
                print('%s (score = %.5f)' % (human_string, score))
2个回答

您正在一个文件夹上循环以预测每个图像 -

for filename in os.listdir(image_path):    
    pred_result = model.predict(images)
    images_data.append(pred_result)
    filenames.append(filename)

但是 predict 函数的参数没有改变。它的堆叠值在上面定义为 -

images = np.vstack(images)

同样的预测被附加到images_data

假设您的预测没有失败,这意味着每个预测都是对 images_data 中堆叠的所有图像的预测。
因此,对于每次迭代,for i in range(len(images_data)):
images_data[i][0]只会返回第一个预测。

更改为for i in range(len(images_data[0])):并且images_data[i]应该可以工作

我不知道,但我会研究这 3 个选项来判断问题所在:

  • 图片加载错误?检查每个加载的图像中是否有不同的数据
  • 型号错了?检查 model.predict 输出的内容是否实际变化
  • 后期处理错了?