在 Python Keras CNN 模型上呈现可理解的预测的问题

Issue with presenting understandable predictions on a Python Keras CNN model

如有地方错误或格式不正确,敬请见谅。

有一个问题我无法找到答案,因为我在搜索过程中可能措辞不正确。我创建了一个模型并正常工作——在 6 classes 中实现了 91.5% 的准确率。无论如何总结我的问题:

目标是 class 化垃圾图像,模型必须预测它看到的垃圾类型。 6 classes,透明和彩色塑料瓶,透明和彩色塑料袋,易拉罐和玻璃瓶。我的预期结果是检索模型预测它在 6 个 class 中看到的内容,因此 67% 确定它是彩色瓶子,21% 确定它是罐头等等

我得到的实际结果是 6 个指数浮点数的范围,这并不理想,也不能真正表明它们属于哪个 class!至于错误,我没有得到任何。我开发 class 化代码的方式是否存在问题,会阻止更易读的结果,或者我是否遗漏了什么?

我正在使用 Google Colab 作为我的 IDE,我的模型是 DenseNet-201。

提前致谢, 杰克

这是我用来 class 使用我的训练模型验证我在现实世界中收集的数据的代码。下面是显示分配给废物数组的标签的代码。我的问题是我不能相信这些标签的顺序与我接收浮点数的顺序相同!另请注意,图像是从 Google 驱动器上的文件夹循环播放的。我试过单独的图像,但得到了相同的结果。

class化测试图像的代码

# Morning Test
import numpy as np
from keras.preprocessing import image

width = 100
height = 100

new_dimensions = (width, height)

counter=0


print("Morning Test - Experiment 1 - Clear Bottle \n")

# Morning Test
# Cycle Throgh Images
for x in range (0,10):
  exp1_morning_waste1 = cv2.imread('/content/gdrive/My Drive/Rivers V2/Test Set/New Images/Exp 1/Morn/' + 'MorningBottleClExp1_' + str(x+1) +'.jpg')

  # Check for existence
  if exp1_morning_waste is not None:
    
    # Count the classifications add one
    counter+=1

    # Resize
    exp1_morning_waste = cv2.resize(exp1_morning_waste1, new_dimensions)

    # Add image to array
    exp1_morning_waste = image.img_to_array(exp1_morning_waste)

    # Axis, Dimens
    exp1_morning_waste = np.expand_dims(exp1_morning_waste, axis=0)
    exp1_morning_waste= exp1_morning_waste/255

    # Predict image
    prediction_prob = model.predict(exp1_morning_waste)

    # Print Predictions
    print(f'Probability that image is a: {prediction_prob} ')

    # Image Number
    print("Waste Item No." + str(x+1) +"\n")
    

  # No Directory or image present
  else:
    print("File not Contacted")
    break

输出>>晨测-实验1-透明瓶

图像是的概率:[[9.9152815e-01 1.2046337e-03 1.4043533e-03 5.7380428e-03 6.7023984e-06 1.1799879e-04]] 废品一号

等等……

用于训练模型的原始数据集标签

# Create dataset and label arrays
wastedata=[]
labels=[]

# Set Random Number generator
random.seed(42)

# Access waste images directory
wasteDirectory = sorted(list(os.listdir("/content/gdrive/My Drive/Rivers V2/Datasets/Waste Dataset - Pre-processing (image resizing 100x100 (Aspect Ratio + Augmentation)(V2))/")))

# Shuffle the directory
random.shuffle(wasteDirectory)

# Print directory class names
print(wasteDirectory)

# Resize and sort images in directory in the case they haven't already
for img in wasteDirectory:
    pathDir=sorted(list(os.listdir("/content/gdrive/My Drive/Rivers V2/Datasets/Waste Dataset - Pre-processing (image resizing 100x100 (Aspect Ratio + Augmentation)(V2))/"+img)))
    for i in pathDir:
        imagewaste = cv2.imread("/content/gdrive/My Drive/Rivers V2/Datasets/Waste Dataset - Pre-processing (image resizing 100x100 (Aspect Ratio + Augmentation)(V2))/"+img+'/'+i)
        imagewaste = cv2.resize(imagewaste, (100,100))
        imagewaste = img_to_array(imagewaste)

        # Assign dataset to data array
        wastedata.append(imagewaste)
        l = label = img

        # Append to labels array
        labels.append(l)

输出>> ['Clear Plastic Bottle', 'Clear Glass Bottle', 'Clear Plastic Bags', 'Coloured Plastic Bags', 'Cans', 'Coloured Plastic Bottle']

模型的预测,即这些浮点数,是相应 classes 的概率(例如,值 6.734e-1 = 6.734 * 10 ** (-1) 表示概率为67.34%)。然后,您的预测是 classes 数组中位于概率数组中最大值索引处的元素,这意味着,您想要预测模型为 class 分配的最高概率。示例:

classes = ['Clear Plastic Bottle', 'Clear Glass Bottle', 'Clear Plastic Bags', 'Coloured Plastic Bags', 'Cans', 'Coloured Plastic Bottle']
probs = [9.9152815e-01, 1.2046337e-03, 1.4043533e-03, 5.7380428e-03, 6.7023984e-06, 1.1799879e-04]
max_prob = max(probabilities)
pred = classes[probabilities.index(max_prob)]
print(f'Model predicts a {max_prob*100:.2f}% chance of the item on the image being "{pred}".')

产出

Model predicts a 99.15% probability of the item on the image being "Clear Plastic Bottle".