ValueError:无法将大小为 136415664 的数组重塑为形状 (2734,132,126,1)

数据挖掘 麻木的
2022-02-09 23:29:51

我有一个用 cv2 加载的数据集,但是当我尝试对其进行格式化时,出现上述错误。我首先将数据移动到X_trainandX_test中(加载的数据在x_trainand中x_test)。

X_train = []
X_test = []

# Image matrices are different sizes so I am making them the same size
for i in range(len(x_train)-1):
  resized = cv2.resize(x_train[i], (img_width, img_height))
  X_train.append(resized)
for i in range(len(x_test)-1):
  resized = cv2.resize(x_test[i], (img_width, img_height))
  X_test.append(resized)

# Convert to numpy arrays
X_test = np.array(X_test)
X_train = np.array(X_train)

# Gather statistics 
print(X_train.shape)    # -> (2734, 132, 126, 3)
print(X_train.size)     # -> 136415664
print(len(X_train))     # -> 2734

# Convert to black and white
X_train = X_train/ 255.
X_test = X_test/ 255.

# First line throws error
X_train = np.reshape(X_train, (len(X_train), img_height, img_width, 1))
X_test = np.reshape(X_test, (len(X_test), img_height, img_width, 1))

我究竟做错了什么?

1个回答

你需要2734×132×126×1=45,471,888值以重塑为该张量。既然你有136,415,664价值观,重塑是不可能的。如果你的第四维度是4,那么重塑将是可能的。