8

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

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

哪里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.shape(52, 2352 )

但我不断收到以下错误:

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

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

更新:

我这样做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 包含我的数据集中所有图像的路径。我实际上想将 data3 转换为 a flat list of 784-dim vectors,但是

image_to_feature_vector 

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

4

2 回答 2

6

您可以重塑 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)

输出形式为

[[[     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]]]
于 2017-07-31T06:27:56.297 回答
0

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

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

# 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)
于 2017-07-31T11:07:08.400 回答