ValueError: cannot reshape array of size 1048576 into shape (1024,1024,3)

ValueError: cannot reshape array of size 1048576 into shape (1024,1024,3)

以下是 .ipynb 文件中的代码片段。

for image_path in TEST_IMAGE_PATHS:
  print(image_path)  
  image = Image.open(image_path)
  print('yooo')  
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  print(image_np)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)
  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)

我正在尝试在图像数据集上测试我的模型的准确性。从上面的代码我得到以下错误

ValueError                                Traceback (most recent call last)
<ipython-input-30-ee1cf025b3f1> in <module>
      6   # the array based representation of the image will be used later in order to prepare the
      7   # result image with boxes and labels on it.
----> 8   image_np = load_image_into_numpy_array(image)
      9   print('yooo')
     10   print(image_np)

<ipython-input-15-af094dcdd84a> in load_image_into_numpy_array(image)
      2   (im_width, im_height) = image.size
      3   return np.array(image.getdata()).reshape(
----> 4       (im_height, im_width, 3)).astype(np.uint8)

ValueError: cannot reshape array of size 1048576 into shape (1024,1024,3)

有人可以帮我解决这个错误吗?

有两种像素编码方式:

1- 你用十六进制代码来表示颜色的值

2- 您使用了 0 到 255 之间的三元组值

这里你有一个 1024*1024 = 1048576 像素,这意味着它已经用十六进制值编码,你正试图将它加载到形状矩阵 (1024,1024,3) 意味着它是RGB 三元组。

如何解决: 重塑为 (1024,1024) 然后通过将十六进制公式分解为三个值 (RGB) 扩展为 (1024,1024,3)。(here is a way to do it, if image.format == "PNG":image = image.convert('RGB') 已提议 here 作为解决此问题的方法