Python 获取错误:无法将大小为 122304 的数组重塑为形状 (52,28,28)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45408496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:59:21  来源:igfitidea点击:

Getting error: Cannot reshape array of size 122304 into shape (52,28,28)

pythonnumpyimage-processingreshapecv2

提问by akrama81

I'm trying to reshape a numpy array as:

我正在尝试将一个 numpy 数组重塑为:

data3 = data3.reshape((data3.shape[0], 28, 28))

where data3is:

在哪里data3

[[54 68 66 ..., 83 72 58]
 [63 63 63 ..., 51 51 51]
 [41 45 80 ..., 44 46 81]
 ..., 
 [58 60 61 ..., 75 75 81]
 [56 58 59 ..., 72 75 80]
 [ 4  4  4 ...,  8  8  8]]

data3.shapeis (52, 2352 )

data3.shape(52, 2352 )

But I keep getting the following error:

但我不断收到以下错误:

ValueError: cannot reshape array of size 122304 into shape (52,28,28)
Exception TypeError: TypeError("'NoneType' object is not callable",) in <function _remove at 0x10b6477d0> ignored

What is happening and how to fix this error?

发生了什么以及如何解决此错误?

UPDATE:

更新:

I'm doing this to obtain data3that is being used above:

我这样做data3是为了获得上面使用的:

def image_to_feature_vector(image, size=(28, 28)):

    return cv2.resize(image, size).flatten()

data3 = np.array([image_to_feature_vector(cv2.imread(imagePath)) for imagePath in imagePaths])  

imagePaths contains paths to all the images in my dataset. I actually want to convert the data3 to a flat list of 784-dim vectors, however the

imagePaths 包含数据集中所有图像的路径。我实际上想将 data3 转换为 a flat list of 784-dim vectors,但是

image_to_feature_vector 

function converts it to a 3072-dim vector!!

函数将其转换为 3072-dim 向量!!

回答by Kaushik Nayak

You can reshape the numpy matrix arrays such that before(a x b x c..n) = after(a x b x c..n). i.e the total elements in the matrix should be same as before, In your case, you can transform it such that transformed data3 has shape (156, 28, 28) or simply :-

您可以重塑 numpy 矩阵数组,使 before(axbx c..n) = after(axbx c..n)。即矩阵中的总元素应与以前相同,在您的情况下,您可以转换它,使转换后的 data3 具有形状 (156, 28, 28) 或简单地:-

import numpy as np

data3 = np.arange(122304).reshape(52, 2352 )

data3 = data3.reshape((data3.shape[0]*3, 28, 28))

print(data3.shape)

Output is of the form

输出的形式

[[[     0      1      2 ...,     25     26     27]
  [    28     29     30 ...,     53     54     55]
  [    56     57     58 ...,     81     82     83]
  ..., 
  [   700    701    702 ...,    725    726    727]
  [   728    729    730 ...,    753    754    755]
  [   756    757    758 ...,    781    782    783]]
  ...,
[122248 122249 122250 ..., 122273 122274 122275]
  [122276 122277 122278 ..., 122301 122302 122303]]]

回答by akilat90

First, your input image's number of elements should match the number of elements in the desired feature vector.

首先,输入图像的元素数量应与所需特征向量中的元素数量相匹配。

Assuming the above is satisfied, the below should work:

假设满足上述条件,以下应该可以工作:

# Reading all the images to a one numpy array. Paths of the images are in the imagePaths
data = np.array([np.array(cv2.imread(imagePaths[i])) for i in range(len(imagePaths))])

# This will contain the an array of feature vectors of the images
features = data.flatten().reshape(1, 784)