【问题标题】:How to vertically stack the results of a for loop to a 2D array?如何将for循环的结果垂直堆叠到二维数组中?
【发布时间】:2022-11-21 12:51:54
【问题描述】:

我训练了 CNN 模型来对 35 个人的图像进行分类。为了测试经过训练的 CNN 模型,我使用了 70 张图像(每个人 2 张)。编写了以下 for 循环来预测 70 张图像的概率。

我需要将 70 张图像 (70 * 35) 的预测概率分配给 ndarray predicted_probabilities

actual_values_images = []
predicted_values_images = []
predicted_probabilities = np.empty((70, 35), int)

for testImage in test_image_folder: 
    img = folder_path+str(testImage)
    img = image.load_img(img, target_size=(64, 64))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)

    result=classifier.predict(img,verbose=0) 
    actual_values_images.append(str(testImage[1:-4]))

    predicted_probabilities = numpy.vstack([predicted_probabilities, result])
    
    predicted_values_images.append(ResultMap[np.argmax(result)])
    predicted_probabilities_images.append(result)

但是当上面的代码运行时,predicted_probabilities 的形状将是 (140, 35)。看起来相同的结果被垂直附加了两次。如何正确地将概率值垂直附加到二维数组以获得 (70, 35) 的形状?

【问题讨论】:

    标签: python for-loop multidimensional-array numpy-ndarray vstack


    【解决方案1】:

    最简单的方法是将结果直接分配给数组。

    actual_values_images = []
    predicted_values_images = []
    predicted_probabilities = np.empty((70, 35), int)
    
    for index, testImage in enumerate(test_image_folder):
        img = folder_path+str(testImage)
        img = image.load_img(img, target_size=(64, 64))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
    
        result=classifier.predict(img,verbose=0) 
        actual_values_images.append(str(testImage[1:-4]))
    
        predicted_probabilities[index] = result[0] # assign directly to the array
        
        predicted_values_images.append(ResultMap[np.argmax(result)])
        predicted_probabilities_images.append(result)
    

    稍微好一点的方法是将结果收集在列表中,然后将其转换为数组。但最有效的方法是批量处理图像,然后合并结果。速写:

    def load2tensor(image_path):
      img = image.load_img(image_path, target_size=(64, 64))
      img = image.img_to_array(img)
      return img
    
    def batchify(images_list, actual_values, batch_size=32):
      batch = []
      batchNames = []
      batchActual = []
      for img, actual in zip(images_list, actual_values):
        batchActual.append(actual)
        batchNames.append(img)
        batch.append(load2tensor(img))
        if len(batch) == batch_size:
          yield np.vstack(batch), batchNames, batchActual
          batch = []
        continue
      if len(batch) > 0:
        yield np.vstack(batch), batchNames, batchActual
      return
    
    actual_values_images = []
    predicted_values_images = []
    predicted_probabilities = []
    
    for batch, names, actual in batchify(test_images, actual_values_images, batch_size=32):
      result = classifier.predict(batch, verbose=0)
      predicted_probabilities.append(result)
      # ... rest of the code with actual_values_images, predicted_values_images
    
    

    我不确定这是否完全正确,但它应该给你一个大概的想法。

    【讨论】:

    • 我将另一个模型的概率分配给形状为 (70,35) 的二维数组。我需要对两个模型的概率进行平均,因此需要将此概率值分配给形状数组 (70,35)。
    • @DakshilaKamalsooriya 嗯,最后所有这些代码都会产生 (70, 35)。问题在于您创建了一个空数组然后附加结果,因此它从 70 开始增长
    猜你喜欢
    • 2019-11-16
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多