在Python Keras CNN模型上提供可理解的预测的问题

发布于 2025-01-22 03:16:17 字数 3247 浏览 0 评论 0原文

表示歉意,如果这是错误的地方或格式,则事先不正确。

有一个问题,我很难找到答案,因为在搜索过程中可能会错误地措辞。我有一个模型并正常工作,可在6个类中实现91.5%的精度。无论如何,总结我的问题:

目标是对废物图像进行分类,模型必须预测它看到的浪费。六个类,透明和彩色的塑料瓶,透明和彩色的塑料袋,罐子和玻璃瓶。我的预期结果是检索该模型在6个类中看到的内容,因此67%确定其彩色瓶子,21%确定其罐等。

我获得的实际结果是6个指数浮动的范围取而代之的是,这不是理想的,并没有真正表明他们属于哪个类!至于错误,我没有得到任何。我开发的分类代码的方式是否存在问题,该代码可以防止更可读的结果或我缺少一些东西?

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

提前致谢, 杰克

这是我用来使用训练有素的模型对我的现实世界收集的数据进行分类的代码。下面是显示分配给废物数组的标签的代码。我的问题是,我不能相信这些标签的按顺序与我接收的浮点数相同!另外,请注意,这些图像正在从Google Drive上的文件夹中循环。我尝试了单独的图像,但得到了相同的结果。

用于对测试图像进​​行分类的代码

# 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-03 6.70233.70239884e-03 1.4043533e-03 -06 1.1799879E-04]]] 垃圾项目1号

等.....

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

# 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)

输出> ['透明塑料瓶',“透明玻璃瓶”,“透明塑料袋”,“彩色塑料袋”,“罐头”,“彩色塑料瓶”]

Apologies if this is in the wrong place or formatting is incorrect in advance.

Having an issue that I'm having trouble finding an answer to as I may have it worded incorrectly during my search. I have a model created and working correctly- achieving 91.5% accuracy across 6 classes. Anyways to summarize my issue:

The goal is the classify waste images and the model has to predict what kind of waste it sees. The 6 classes, clear and coloured plastic bottles, clear and coloured plastic bags, cans and glass bottles. My expected results are to retrieve what the model predicts what it sees across the 6 classes, so 67% sure its a coloured bottle, 21% sure its a can etc etc.

The actual results I'm getting is a range of 6 exponential floating point numbers instead, which is not ideal and doesn't really indicate which class they belong to! As for errors I'm not getting any. Is there an issue with the way I've developed the classification code that would prevent more readable results or am I missing something?

I'm using Google Colab as my IDE and my model is DenseNet-201.

Thanks in advance,
Jack

Here is the code I'm using to classify my real-world collected data using my trained model. Below this is the code showing the labels assigned into the array of wastes. My problem is I cannot trust these labels to be in the same order I'm receiving the floating point numbers in! Also to note, the images are being looped in from a folder on Google Drive. I have tried individual images but get the same results.

Code for classifying test images

# 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

Output>> Morning Test - Experiment 1 - Clear Bottle

Probability that image is a: [[9.9152815e-01 1.2046337e-03 1.4043533e-03 5.7380428e-03 6.7023984e-06
1.1799879e-04]]
Waste Item No.1

and so on.....

Original Dataset labelling for training the model

# 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-29 03:16:18

该模型的预测,即这些浮点数,是相应类的概率(例如6.734e-1 = 6.734 * 10 **(-1)的值表示概率为67.34%)。然后,您的预测是您的类数数组中最大值概率中最大值的索引中的元素,这意味着您想预测哪个类都通过模型分配了最高概率。示例:

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".

The model's predictions, i.e. these floating point numbers, are the probabilities for the respective classes (e.g. a value of 6.734e-1 = 6.734 * 10 ** (-1) indicating a probability of 67.34%). Your prediction is then the element in your array of classes at the index of the maximum value in your array of probabilities, meaning, you want to predict whichever class gets assigned the highest probability by your model. Example:

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}".')

outputs

Model predicts a 99.15% probability of the item on the image being "Clear Plastic Bottle".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文