ValueError:检查输入时出错:预期输入具有4维,但数组的形状为(859307,1)

时间:2019-08-15 21:51:21

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

我正在创建一个可接收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)

添加了重塑后的新错误:

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)

1 个答案:

答案 0 :(得分:1)

您输入的x_train不是4d输入。 您应该先将其重塑形状,然后再将其输入网络。 最好