如何在多类分类问题中显示特定类别的图像及其预测结果?

我一直在处理一个多类分类问题,需要创建一个函数来显示Fashion MNIST数据集中特定类别的图像并对其进行预测。例如,绘制3张T-shirt类别的图像及其预测结果。我尝试了不同的方法但尚未成功。我缺少一个条件语句,并且不知道如何以及在哪里在我的函数中实现它。

这是我目前想到的方案:

# Make function to plot imagedef plot_image(indx, predictions, true_labels, target_images):  """  Picks an image, plots it and labels it with a predicted and truth label.  Args:  indx: index number to find the image and its true label.  predictions: model predictions on test data (each array is a predicted probability of values between 0 to 1).  true_labels: array of ground truth labels for images.  target_images: images from the test data (in tensor form).  Returns:  A plot of an image from `target_images` with a predicted class label  as well as the truth class label from `true_labels`.  """  # Set target image  target_image = target_images[indx]  # Truth label  true_label = true_labels[indx]  # Predicted label  predicted_label = np.argmax(predictions)  # find the index of max value  # Show image  plt.imshow(target_image, cmap=plt.cm.binary)  plt.xticks([])  plt.yticks([])  # Set colors for right or wrong predictions  if predicted_label == true_label:    color = 'green'  else:    color = 'red'  # Labels appear on the x-axis along with accuracy %  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],                                100*np.max(predictions),                                class_names[true_label]),                                color=color)# Function to display image of a classdef display_image(class_indx):  # Set figure size  plt.figure(figsize=(10,10))  # Set class index  class_indx = class_indx  # Display 3 images  for i in range(3):    plt.subplot(1, 3, i+1)    # plot_image function    plot_image(indx=class_indx, predictions=y_probs[class_indx],               true_labels=test_labels, target_images=test_images_norm)

这些是类别名称 'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'。当我调用显示函数display_image()并传递类别索引作为参数display_image(class_indx=15)时,我得到了相同的图像和相同的预测结果三次(*注意我的错误方法,我传递的是索引号而不是类别名称*)。我需要一个函数,该函数接受一个str(类别名称)并显示该类别的3个不同预测结果。例如,display_image('Dress')应该返回3张Dress类别的图像以及我的模型对其进行的3个不同预测结果,如Prediction#1 (65%)Prediction#2 (100%)Prediction#3 (87%)。谢谢!


回答:

我认为你已经非常接近解决你的问题了。你只需要从你感兴趣的类别中随机抽取三个样本。我猜你已经使用了le = LabelEncoder()来编码你的目标向量。如果是的,那么你将拥有这样的类别:labels = list(le.classes_)。然后我会这样做:

def display_image(class_of_interest: str, nb_samples: int=3):    plt.figure(figsize=(10,10))        class_indx = class_names.index(class_of_interest)    target_idx = np.where(true_labels==class_indx)[0]    imgs_idx = np.random.choice(target_idx, nb_samples, replace=False)    for i in range(nb_samples):       plt.subplot(1, nb_samples, i+1)       plot_image(indx=imgs_idx[i],                   predictions=y_probs[imgs_idx[i]],                  true_labels=test_labels,                   target_images=test_images_norm)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注