1
votes

I'm creating a convolutional autoencoder that takes in 16x16 images but I keep getting the following error:

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)

From other stack overflow posts such as this one, it seems like I need to add another dimension for the colour channel but what would the other dimension I add be?

Code

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)

New Error after reshaping is added:

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

Error without reshaping:

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
If your input is 16x16 images, why is your x_train shaped like (859307, 1)? - Dr. Snoopy
true..I'll check the input size of x_train right now - iiooii
@MatiasValdenegro also just added the code that I used to define x_train and x_test - iiooii
Code doesn't tell much, you are manually reshaping to (-1, 859307, 1), why? - Dr. Snoopy
Deleted that line. - iiooii

1 Answers

1
votes

You input x_train is not a 4d input. you should reshape it before feeding it into the network. Best