0

I am getting an error when trying to create a CNN model in Keras to create a denoising autoencoder. My Keras backend is TensorFlow.

My input data is a numpy array. The numpy array were taken from grayscale images. I split this using sklearn train_test_split. I have resized the data and get an error at the output layer.

/opt/anaconda/anaconda3/lib/python3.6/site-
packages/keras/engine/training.py in _standardize_input_data(data, 
names, shapes, check_batch_axis, exception_prefix)
151                             ' to have shape ' + str(shapes[i]) +
152                             ' but got array with shape ' +
--> 153                             str(array.shape))
154     return arrays
155 

ValueError: Error when checking target: expected conv2d_transpose_36 
to have shape (None, 279, 559, 1) but got array with shape (129, 258, 
540, 1)

train_images = os.listdir('data/train')
test_images = os.listdir('data/test')
clean_images = os.listdir('data/train_cleaned')

def set_common_size(img_link, std_size = (258, 540)):
    '''Function will take in the argument of a link to an image and return the image 
    with the standard size.'''
    img = Image.open(img_link)
    img = image.img_to_array(img)
    img = np.resize(img, std_size)
    return img / 255

train_data = []
test_data = []
cleaned_data = []
for img in train_images:
img_file = set_common_size('data/train/' + img)
train_data.append(img_file)
for img in test_images:
    img_file = set_common_size('data/test/' + img)
    test_data.append(img_file)
for img in clean_images:
    img_file = set_common_size('data/train_cleaned/' + img)
    cleaned_data.append(img_file)
train_data = np.asarray(train_data)
test_data = np.asarray(test_data)
cleaned_data = np.asarray(cleaned_data)

x_train, x_test, y_train, y_test = train_test_split(train_data, cleaned_data, test_size=0.1, random_state=42)

input_shape = x_train[0].shape
input_layer = Input(input_shape)

#Layer 1 
layer1 = Conv2D(64, 3, activation='relu', padding='same')(input_layer)
layer1 = MaxPooling2D(2)(layer1)
#Layer 2
layer2 = Conv2D(128, 3, activation='relu', padding='same')(layer1)
layer2 = MaxPooling2D(2)(layer2)
#Layer 3 
layer3 = Conv2D(256, 3, activation='relu', padding='same')(layer2)
layer3 = MaxPooling2D(2)(layer3)
#Bottleneck
encoded = layer3
#Layer1
uplayer1 = UpSampling2D(2)(layer3)
uplayer1 = Conv2DTranspose(256, 2, activation='relu')(uplayer1)
#Layer2
uplayer2 = UpSampling2D(2)(uplayer1)
uplayer2 = Conv2DTranspose(128, 5, activation='relu')(uplayer2)
#Layer3
uplayer3 = UpSampling2D(2)(uplayer2)
uplayer3 = Conv2DTranspose(64, 10, activation='relu')(uplayer3)
output = Conv2DTranspose(1, 3, activation='sigmoid')(uplayer3)

model = Model(input=input_layer, output=output)
print(model.summary())

model.compile(optimizer='adadelta', loss='binary_crossentropy')

model.fit(
        x_train, y_train, 
        epochs=100,
        shuffle=True,
        validation_data=(x_test, y_test)
        )

Here are the Model Summary results:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 258, 540, 1)       0         
_________________________________________________________________
conv2d_60 (Conv2D)           (None, 258, 540, 64)      640       
_________________________________________________________________    
max_pooling2d_37 (MaxPooling (None, 129, 270, 64)      0         
_________________________________________________________________
conv2d_61 (Conv2D)           (None, 129, 270, 128)     73856     
_________________________________________________________________
max_pooling2d_38 (MaxPooling (None, 64, 135, 128)      0         
_________________________________________________________________
conv2d_62 (Conv2D)           (None, 64, 135, 256)      295168    
_________________________________________________________________
max_pooling2d_39 (MaxPooling (None, 32, 67, 256)       0         
_________________________________________________________________
up_sampling2d_37 (UpSampling (None, 64, 134, 256)      0         
_________________________________________________________________
conv2d_transpose_29 (Conv2DT (None, 65, 135, 256)      262400    
_________________________________________________________________
up_sampling2d_38 (UpSampling (None, 130, 270, 256)     0         
_________________________________________________________________
conv2d_transpose_30 (Conv2DT (None, 134, 274, 128)     819328    
_________________________________________________________________
up_sampling2d_39 (UpSampling (None, 268, 548, 128)     0         
___________________________________________________________    
_________________________________________________________________
conv2d_transpose_32 (Conv2DT (None, 279, 559, 1)       577       
=============================================================______
conv2d_transpose_31 (Conv2DT (None, 277, 557, 64)      819264====
Trainable params: 2,271,233
Non-   params: 0
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55
Eric Z
  • 1
  • 1

1 Answers1

0

There is a mismatch between:

  • the shape of the outputs your model creates: (279, 559, 1) (shown in the summary line conv2d_transpose_32 (Conv2DT (None, 279, 559, 1) which is actually the last layer, but your console output looks a bit messed up)

and

  • the shape of the outputs you are expecting: (258, 540, 1) (the shape of the entries in y_train that you feed in)

Use the model summary to see where the deviations from the expected shapes start and use the proper padding values to get the expected output shapes.

KiraMichiru
  • 958
  • 6
  • 13