Gianni
Gianni

Reputation: 3

How do I fix dimension error in a simple Autoencoder?

I am new to python and autoencoders. I just wanted to build a simple autoencoder to start with, but I keep getting this error:

ValueError: Error when checking target: expected conv2d_39 to have 4 dimensions, but got array with shape (32, 3)

Is there a better way to get my own data, besides the flow_from_directory method? I built the Autoencoder like this, but I took some layers away.

I don't know, but am I feeding the autoencoder with the tuple generated from the flow_from_directory method? Is there a way to cast this tuple into a format the autoencoder accepts?

import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense, Input, Conv2D, 
UpSampling2D, MaxPooling2D
from keras.optimizers import RMSprop

IMG_WIDTH, IMG_HEIGHT = 112, 112
input_img = Input(shape=(IMG_WIDTH, IMG_HEIGHT,3))

#encoder
def encoder(input_img):
    # 1x112x112x3
    conv1 = Conv2D(32,(3,3), activation='relu', padding='same') 
    (input_img) 
    # 32x112x112
    pool1 = MaxPooling2D(pool_size=(2,2))(conv1)
    # 32x56x56
    return pool1

#decoder
def decoder(pool1):
    # 32x56x56
    up1 = UpSampling2D((2,2))(pool1)
    # 32x112x112
    decoded = Conv2D(1,(3,3),activation='sigmoid',padding='same')(up1)
    # 1x112x112
    return decoded

autoencoder = Model(input_img, decoder(encoder(input_img)))
autoencoder.compile(loss='mean_squared_error', optimizer=RMSprop())

datagen = ImageDataGenerator(rescale=1./255)

training_set = datagen.flow_from_directory(
    r'C:\Users\user\Desktop\dataset\train',
    target_size=(112,112),
    batch_size=32,
    class_mode='categorical')

test_set = datagen.flow_from_directory(
    r'C:\Users\user\Desktop\dataset\validation',
    target_size=(112,112),
    batch_size=32,
    class_mode='categorical')

history = autoencoder.fit_generator(
    training_set,
    steps_per_epoch=2790,
    epochs=5,
    validation_data=test_set,
    validation_steps=1145)

Here is the model summary:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_14 (InputLayer)        (None, 112, 112, 3)       0         
_________________________________________________________________
conv2d_42 (Conv2D)           (None, 112, 112, 32)      896       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 56, 56, 32)        0         
_________________________________________________________________
up_sampling2d_4 (UpSampling2 (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_43 (Conv2D)           (None, 112, 112, 1)       289       
=================================================================
Total params: 1,185
Trainable params: 1,185
Non-trainable params: 0
_________________________________________________________________

I am working with 512x496 OCT images.

Upvotes: 0

Views: 277

Answers (2)

today
today

Reputation: 33460

Since you are building an autoencoder and therefore the output of the model must be the same as the input, there are two problems with your code:

  1. You must set the class_mode argument of generators to 'input' to let the labels generated be the same as the generated inputs.

  2. The last layer must have 3 filters since the input image has 3 channels: decoded = Conv2D(3, ...).

Upvotes: 2

Joe Halliwell
Joe Halliwell

Reputation: 1177

I believe you're feeding the network the labels, rather than the images. Try setting class_mode to None explicitly when constructing the data generator -- it defaults to categorical.

Upvotes: 0

Related Questions