【问题标题】:ValueError: Error when checking input: expected input to have 4 dimensions, but got array with shape (859307, 1)ValueError:检查输入时出错:预期输入具有 4 个维度,但得到的数组具有形状 (859307, 1)
【发布时间】:2019-12-22 07:23:04
【问题描述】:

我正在创建一个接收 16x16 图像的卷积自动编码器,但我不断收到以下错误:

Traceback (most recent call last):
  File "WTApruning.py", line 69, in <module>
    validation_data=(x_test, x_test))
  File "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 709, in fit
    shuffle=shuffle)
  File "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2651, in _standardize_user_data
    exception_prefix='input')
  File "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 376, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking input: expected input to have 4 dimensions, but got array with shape (859307, 1)

从其他堆栈溢出帖子(例如 one)来看,我似乎需要为颜色通道添加另一个维度,但我添加的另一个维度是什么?

代码

path = "..."
CATEGORIES = ["x_train", "x_test"]
count = 0
data = []
x_test, x_train = [], []
for img in os.listdir(path):
    img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
    data.append(img_array)
x_train, x_test = train_test_split(data, test_size = 0.1)
x_train, x_test = train_test_split(data, test_size = 0.1)
x_train = np.array(x_train)
x_test = np.array(x_test)
# just updated
x_train = x_train.reshape(x_train,(len(x_train),16,16,1))
x_test = x_test.reshape(x_test,(len(x_test),16,16,1))

# ENCODER
encoder_img = tf.keras.layers.Input(shape=(16,16,1), name="input")
x = tf.keras.layers.Conv2D(1024, 1, activation='relu', kernel_initializer=keras.initializers.RandomUniform)(encoder_img)
x = tf.keras.layers.MaxPooling2D(1)(x)
x = tf.keras.layers.Conv2D(512, 1, activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(1)(x)
encoder_output = tf.keras.layers.Conv2D(256, 3, activation='relu')(x)

# DECODER
x = tf.keras.layers.Conv2DTranspose(512, 1, activation='relu')(encoder_output)
x = tf.keras.layers.UpSampling2D(1)(x)
x = tf.keras.layers.Conv2DTranspose(1024, 1, activation='relu')(x)
x = tf.keras.layers.UpSampling2D(1)(x)
decoder_output = tf.keras.layers.Conv2DTranspose(1, 3, activation='relu')(x)
# COMPILE
autoencoder = tf.keras.Model(inputs=encoder_img, outputs=decoder_output, name='autoencoder')
autoencoder.summary()

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train,
                epochs=20,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test))
decoded_imgs = autoencoder.predict(x_test)

添加reshape后的新错误:

Traceback (most recent call last):
  File "WTApruning.py", line 43, in <module>
    x_train = x_train.reshape(x_train,(len(x_train),16,16,1))
TypeError: only integer scalar arrays can be converted to a scalar index

没有重塑的错误:

Traceback (most recent call last):
  File "WTApruning.py", line 68, in <module>
    validation_data=(x_test, x_test))
  File "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 709, in fit
    shuffle=shuffle)
  File "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2651, in _standardize_user_data
    exception_prefix='input')
 "/PycharmProjects/predictivemodel/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 376, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking input: expected input to have 4 dimensions, but got array with shape (859307, 1)

【问题讨论】:

  • 如果你的输入是 16x16 的图像,为什么你的 x_train 形状像 (859307, 1)?
  • true..我现在检查 x_train 的输入大小
  • @MatiasValdenegro 也刚刚添加了我用来定义 x_train 和 x_test 的代码
  • 代码说明不了什么,你手动整形为 (-1, 859307, 1),为什么?
  • 删除了那一行。

标签: python tensorflow keras conv-neural-network dimension


【解决方案1】:

您输入的 x_train 不是 4d 输入。 您应该在将其输入网络之前对其进行整形。 最好的

【讨论】:

  • 用整形线更新了代码 - 谢谢。
猜你喜欢
  • 2020-09-06
  • 2021-12-03
  • 2022-01-15
  • 2020-09-01
  • 2018-08-03
  • 2018-07-25
  • 1970-01-01
  • 2018-01-09
  • 2021-11-24
相关资源
最近更新 更多