ValueError: cannot reshape array of size 2251 into shape (48,48)

ValueError: cannot reshape array of size 2251 into shape (48,48)

当我尝试运行 Google colab 中的这部分代码来训练 cnn 时出现此错误:

ValueError                                Traceback (most recent call last) <ipython-input-12-0b1325cd4065> in <module>()
      3 for xseq in datapoints:
      4     xx = [int(xp) for xp in xseq.split(' ')]
----> 5     xx = np.asarray(xx).reshape(width, height)
      6     X.append(xx.astype('float32'))
      7  ValueError: cannot reshape array of size 2251 into shape (48,48)

我的代码

# getting features for training
X = []
for xseq in datapoints:
    xx = [int(xp) for xp in xseq.split(' ')]
    xx = np.asarray(xx).reshape(width, height)
    X.append(xx.astype('float32'))

拆分 xseq 数据后,您可能会丢失一些数据。看到 2251 = (47.44 x 47.44) 的形状,所以它不适合并且不是所需形状 (48 x 48) 的倍数。

查看xseq对象的原始大小,确认需要拆分。然后确认形状与预期的 (48,48) 匹配。

另一种可能性是调整对象的大小而不是整形,但您也可能会丢失一些重要信息。