如何在多 class class 化问题中显示带有预测的特定 class 的图像?

How to show an image of a certain class with predictions in a multiclass classification problem?

我一直在研究多class class化问题,我需要制作一个函数来显示时尚 MNIST 数据集的某个 class 的图像并对其做出预测。例如,绘制 T-shirt class 的 3 张图像及其预测。我尝试过不同的东西,但还没有成功。我缺少条件语句,我不知道如何以及在我的函数中在哪里实现它。

这是我到目前为止的想法:

# Make function to plot image
def 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 class
def 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)

这些是 class 个名字 'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'。当我调用显示函数 display_image() 并将 class 索引作为参数传递 display_image(class_indx=15) 时,我得到了 3 次相同的图像和相同的预测(注意我的错误的方法,我传递的是索引号,它应该是 class 名称 )。我需要一个接受 str(class 名称)并显示 class 的 3 个不同预测的函数。例如,display_image('Dress') 应该 return Dress class 的 3 个图像以及我的模型做出的 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)