エラーが発生します:サイズ122304の配列を(52,28,28)の形に整形できません。

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 を 784次元ベクトルのフラットリスト に変換したいのです。

image_to_feature_vector 

関数は3072次元のベクトルに変換します!

質問へのコメント (6)

numpy行列の配列は、before(a x b x c...n) = after(a x b x c...n)となるように形を変えることができます。 が(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]]]
解説 (2)

まず、入力画像の要素数が、求める特徴ベクトルの要素数と一致する必要があります。

上記を満たすと仮定すると、以下のようになります:

# 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)
解説 (4)