Keras has an inconsistent output layer shape compared to CNN input

I get an error in Keras where the dimension of the result is different from the input dimension. I donโ€™t understand where 20. comes from. All my measurements seem to show 18. Also, the Output Shape for convolution2d_70 just says โ€œmultipleโ€, so I'm not sure what that means. Any ideas?

Exception: error while checking the target model: it is expected that convolution2d_70 will have the form (None, 1, 36L, 20L), but received an array with the form (49L, 1L, 36L, 18L)

from keras.layers import Dense, Input, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model

from os import listdir
import os.path
import numpy as np
import re

versions = !pip freeze
for line in versions:
    if re.search('Keras', line):
          print line


samples = 100
x = np.ndarray([samples,36,18])
i=0

for i in range(samples):
    x[i] = np.random.randint(15, size=(36, 18))
    i+=1

#TODO: CREATE A REAL TEST SET
x_train = x[:49]
x_test = x[50:]
print x_train.shape
print x_test.shape

#INPUT LAYER
input_img = Input(shape=(1,x_train.shape[1],x_train.shape[2]))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point the representation is (8, 4, 4) i.e. 128-dimensional

x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

#MODEL
autoencoder = Model(input=input_img, output=decoded)

#SEPERATE ENCODER MODEL
encoder = Model(input=input_img, output=encoded)

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(8, 4, 4))

# retrieve the last layer of the autoencoder model
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]

print decoder_layer3.get_config()

# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))

#COMPILER
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.summary()

x_train = x_train.astype('float32') / 15.
x_test = x_test.astype('float32') / 15.
x_test = np.reshape(x_test, (len(x_test), 1, x_test.shape[1], x_test.shape[2]))
x_train = np.reshape(x_train, (len(x_train), 1, x_train.shape[1], x_train.shape[2]))

autoencoder.fit(x_train,
               x_train,
                nb_epoch=5,
                batch_size=1,
                verbose=True,
                shuffle=True,
                validation_data=(x_test,x_test))
+4
source share
1 answer

, border_mode='same'. .

+1

All Articles