ValueError:输入到`.fit()`应该具有等级4。具有形状的数组

发布于 2025-02-10 21:46:21 字数 2260 浏览 1 评论 0原文

我遇到了这个错误,但不知道为什么。

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 134, in <module>
    train_generator.fit(xtrain.reshape(704, 227, 227))
ValueError: cannot reshape array of size 36070300 into shape (704,227,227)

以下是导致错误消息的行。

#Import the packages
from keras.preprocessing.image import ImageDataGenerator

# Image Data Augmentation
train_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
val_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
test_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(704, 227, 227))
val_generator.fit(x_val.reshape(704, 227, 227))
test_generator.fit(xtest.reshape(704, 227, 227))

更新: 我将代码修改为以下内容:

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(704, 227, 227))
val_generator.fit(x_val.reshape(704, 227, 227))
test_generator.fit(xtest.reshape(704, 227, 227))

并且现在遇到此错误:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 136, in <module>
    val_generator.fit(x_val.reshape(704, 227, 227))
ValueError: cannot reshape array of size 4946784 into shape (704,227,227)

因此,我删除了704,并留下-1,如下所示,

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(-1, 227, 227))
val_generator.fit(x_val.reshape(-1, 227, 227))
test_generator.fit(xtest.reshape(-1, 227, 227))

但是我收到此错误消息:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 135, in <module>
    train_generator.fit(xtrain.reshape(-1, 227, 227))
  File "C:\Users\JoshG\AppData\Local\Programs\Python\Python39\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 935, in fit
    raise ValueError('Input to `.fit()` should have rank 4. '
ValueError: Input to `.fit()` should have rank 4. Got array with shape: (704, 227, 227)

I am getting this error, but do not know why.

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 134, in <module>
    train_generator.fit(xtrain.reshape(704, 227, 227))
ValueError: cannot reshape array of size 36070300 into shape (704,227,227)

Below is the line that is causing the error message.

#Import the packages
from keras.preprocessing.image import ImageDataGenerator

# Image Data Augmentation
train_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
val_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
test_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(704, 227, 227))
val_generator.fit(x_val.reshape(704, 227, 227))
test_generator.fit(xtest.reshape(704, 227, 227))

UPDATE:
I modified the code to the following:

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(704, 227, 227))
val_generator.fit(x_val.reshape(704, 227, 227))
test_generator.fit(xtest.reshape(704, 227, 227))

and now getting this error:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 136, in <module>
    val_generator.fit(x_val.reshape(704, 227, 227))
ValueError: cannot reshape array of size 4946784 into shape (704,227,227)

So I then removed the 704 and left the -1 as shown below

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(-1, 227, 227))
val_generator.fit(x_val.reshape(-1, 227, 227))
test_generator.fit(xtest.reshape(-1, 227, 227))

but I then get this error message:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 135, in <module>
    train_generator.fit(xtrain.reshape(-1, 227, 227))
  File "C:\Users\JoshG\AppData\Local\Programs\Python\Python39\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 935, in fit
    raise ValueError('Input to `.fit()` should have rank 4. '
ValueError: Input to `.fit()` should have rank 4. Got array with shape: (704, 227, 227)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文