2D Convolutional neural networks with variable size images - python-3.x

I have implemented a convolutional auto-encoder with Keras, using the Theano backend. I am changing my approach to try to deal with images of different sizes. As long as I use numpy's stack function to build the dataset (equal size images) I am golden. However, for different size images we cannot use stack, and fit expects a numpy array. So I changed to fit_generator to avoid the size checks. The problem is that the last layer is expecting 16 as the last dimension in the input, and I cannot understand why it is getting the dimensions of the original image.
Have a look at the code bellow, and the error output.
import numpy as np
import keras
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
AE_EPOCHS = 10
VERB = 1
batchsz = 16
outfun = 'sigmoid'
data = []
dimensions = [(10, 15), (12, 15), (7,15), (20,15), (25,15)]
for d in dimensions:
dd = np.random.rand(*d)
dd = dd.reshape((1,)+dd.shape)
data.append(dd)
input_img = Input(shape=(1, None, 15))
filtersz = 3
pad_it = 'same'
size1 = 16
size2 = 8
x = Conv2D(size1, (filtersz, filtersz), activation='relu', padding=pad_it)(input_img)
x = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
encoded = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(encoded)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
x = Conv2D(size1, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
decoded = Conv2D(1, (filtersz, filtersz), activation=outfun, padding=pad_it)(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss= 'binary_crossentropy')
x_train = data[1:]
x_test= data[0].reshape((1,)+ data[0].shape)
def mygen(xx, *args, **kwargs):
for i in xx:
yield (i,i)
thegen = mygen(x_train)
#If I use this generator somehow None is returned so it is not used
thegenval = mygen(np.array([x_test]))
hist = autoencoder.fit_generator(thegen,
epochs=AE_EPOCHS,
steps_per_epoch=4,
verbose=VERB,
validation_data=(x_test, x_test),
validation_steps=1
)
Traceback (most recent call last):
File "stacko.py", line 107, in
validation_steps=1
File "/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 1847, in fit_generator
val_x, val_y, val_sample_weight)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 1315, in _standardize_user_data
exception_prefix='target')
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 139, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected conv2d_7 to have shape (None, 1, None, 16) but got array with shape (1, 1, 10, 15)

There are two problems with the code above: first, the size of the images' axis must be a multiple of the smallest number of filters per layer (in this case 8); second, the generators for fit_generator must return batches (4D numpy arrays).
The generator is implemented with itertools.cycle and reshapes the figures as one sample batches (if working with multiple images with common sizes one could have variable size batches for each group of dimensions). The working example is below.
import numpy as np
from itertools import cycle
import keras
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
AE_EPOCHS = 10
VERB = 1
outfun = 'sigmoid'
data = []
dimensions = [(16, 32), (24, 32), (8,32), (32,32)]
for d in dimensions:
dd = np.random.rand(*d)
dd = dd.reshape((1,)+dd.shape)
data.append(dd)
input_img = Input(shape=(1, None, 32))
filtersz = 3
pad_it = 'same'
size1 = 16
size2 = 8
x = Conv2D(size1, (filtersz, filtersz), activation='relu', padding=pad_it)(input_img)
x = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
encoded = MaxPooling2D((2, 2), padding=pad_it)(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(encoded)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
x = Conv2D(size2, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
x = Conv2D(size1, (filtersz, filtersz), activation='relu', padding=pad_it)(x)
x = UpSampling2D((2, 2), data_format="channels_first")(x)
decoded = Conv2D(1, (filtersz, filtersz), activation=outfun, padding=pad_it)(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss= 'binary_crossentropy')
x_train = data[1:]
x_test= [data[0]]
def mygen(xx, *args, **kwargs):
for i in cycle(xx):
ii = i.reshape((1,)+i.shape)
yield ii,ii
thegen = mygen(x_train)
thegenval = mygen(x_test)
hist = autoencoder.fit_generator(
thegen,
epochs=AE_EPOCHS,
steps_per_epoch=3,
verbose=VERB,
validation_data=thegenval,
validation_steps=1
)

Related

Keras functional api Graph disconnected error

The following code is giving me a graph disconnected error but I cant work out where it is coming from and am not sure how to go about debugging. The error is being thrown on the last line decoder = Model(latentInputs, outputs, name="decoder"), I have compared it to working code that I modified but to no avail.
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import ReLU
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.layers import UpSampling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import GaussianNoise
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Reshape
from tensorflow.keras.layers import Add
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
import tensorflow as tf
import numpy as np
width=256
height=256
depth=3
inputShape = (height, width, depth)
chanDim = -1
filter_size = 3
latentDim = 512
# initialize the input shape to be "channels last" along with
# the channels dimension itself
inputShape = (height, width, depth)
chanDim = -1
# define the input to the encoder
inputs = Input(shape=inputShape)
x = GaussianNoise(0.2)(inputs)
x = Conv2D(128, filter_size, strides=1, padding="same")(x)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
layer_1 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_1)
x = LeakyReLU(alpha=0.2)(x)
layer_2 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_2)
x = LeakyReLU(alpha=0.2)(x)
layer_3 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_3)
x = LeakyReLU(alpha=0.2)(x)
layer_4 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_4)
x = LeakyReLU(alpha=0.2)(x)
layer_5 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_5)
x = LeakyReLU(alpha=0.2)(x)
layer_6 = Conv2D(128, filter_size, strides=2, padding="same")(x)
x = BatchNormalization(axis=chanDim)(layer_6)
x = LeakyReLU(alpha=0.2)(x)
layer_7 = Conv2D(128, filter_size, strides=2, padding="same")(x)
latent = Flatten()(layer_7)
# flatten the network and then construct our latent vector
volumeSize = K.int_shape(layer_7)
# build the encoder model
encoder = Model(inputs, latent, name="encoder")
encoder.summary()
# start building the decoder model which will accept the
# output of the encoder as its inputs
#%%
latentInputs = Input(shape=(np.prod(volumeSize[1:]),))
x = Reshape((volumeSize[1], volumeSize[2], volumeSize[3]))(latentInputs)
dec_layer_7 = Add()([x, layer_7])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_7)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_6 = Add()([x, layer_6])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_6)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_5 = Add()([x, layer_5])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_5)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_4 = Add()([x, layer_4])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_4)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_3 = Add()([x, layer_3])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_3)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_2 = Add()([x, layer_2])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_2)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
dec_layer_1 = Add()([x, layer_1])
x = Conv2DTranspose(128, filter_size, strides=2, padding="same")(dec_layer_1)
x = BatchNormalization(axis=chanDim)(x)
x = LeakyReLU(alpha=0.2)(x)
outputs = Conv2DTranspose(depth, filter_size, padding="same")(x)
# apply a single CONV_TRANSPOSE layer used to recover the
# original depth of the image
# =============================================================================
# outputs = ReLU(max_value=1.0)(x)
# =============================================================================
# build the decoder model
decoder = Model(latentInputs, outputs, name="decoder")
Error is:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_37:0", shape=(None, 256, 256, 3), dtype=float32) at layer "input_37". The following previous layers were accessed without issue: []
layer_7 refers to another model... you have to provide input for layer_7 in your decoder. a solution can be to define your decoder in this way
decoder = Model([latentInputs, encoder.input], outputs, name="decoder")
here the full example: https://colab.research.google.com/drive/1W8uLy49H_8UuD9DGZvtP7Md1f4ap3u6A?usp=sharing

Unsupervised CNN keras model

I am new with using unsupervised CNN model in python. I am trying to use CNN model for image classification with unsupervised spectrogram input images. Each image is of size 523 width and 393 height. And I have tried the following code
X_data = []
files = glob.glob ("C:/train/*.png")
for myFile in files:
image = cv2.imread (myFile)
image_resized = misc.imresize(image, (523,393))
image_resi = misc.imresize(image_resized, (28, 28))
assert image_resized.shape == (523,393, 3), "img %s has shape %r" % (myFile, image_resized.shape)
X_data.append (image_resi)
X_datatest = []
files = glob.glob ("C:/test/*.png")
for myFile in files:
image = cv2.imread (myFile)
image_resized = misc.imresize(image, (523,393))
image_resi = misc.imresize(image_resized, (28, 28))
assert image_resized.shape == (523,393, 3), "img %s has shape %r" % (myFile, image_resized.shape)
X_datatest.append (image_resi)
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
X_data = np.reshape(X_data, (len(X_data), 28, 28, 3)) # adapt this if using `channels_first` image data format
X_datatest = np.reshape(X_datatest, (len(X_datatest), 28, 28, 3)) # adapt this if using `channels_first` image data format
noise_factor = 0.5
x_train_noisy = X_data + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_data.shape)
x_test_noisy = X_datatest + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_datatest.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
input_img = Input(shape=(28, 28, 3)) # adapt this if using `channels_first` image data format
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
# at this point the representation is (7, 7, 32)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'] )
autoencoder.fit(x_train_noisy, X_data,
epochs=100,
batch_size=128,
verbose = 2,
validation_data=(x_test_noisy, X_datatest),
callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])
I have tried to make noise and fed it as labels because i didn't have labels because this is an unsupervised spectrogram data. But the output is 33% only for the accuracy. I don't know why. Can anyone help me with this and try to make me understand the numbers of filters, kernels and the resize with 28*28 based on what? And why we just use the image size which is here 523 width and 393 height?

How to use deconvolution with MNIST database

I am a newbie in CNN and I am trying the code the Deconvolution (to generate feature maps) in MNIST database (because it's the simplest one to learn for a beginner). I want my model to generate feature maps at the end.The idea is to implement the paper Saliency Detection Via Dense Convolution Network to some extent.
Here is the complete code that I am trying to run:
import keras
from keras.datasets import mnist
import keras.backend as K
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Flatten, Dropout, Activation, Reshape
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D, Conv2DTranspose, UpSampling2D
from keras.initializers import RandomNormal
init = RandomNormal(mean = 0., stddev = 0.02)
def GeneratorDeconv(image_size = 28):
L = int(image_size)
inputs = Input(shape = (100, ))
x = Dense(512*int(L/16)**2)(inputs) #shape(512*(L/16)**2,)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Reshape((int(L/16), int(L/16), 512))(x) # shape(L/16, L/16, 512)
x = Conv2DTranspose(256, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/8, L/8, 256)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(128, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/4, L/4, 128)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(64, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/2, L/2, 64)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(3, (4, 4), strides= (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L, L, 3)
images = Activation('tanh')(x)
model = Model(inputs = inputs, outputs = images)
model.summary()
return model
batch_size = 128
num_classes = 10
epochs = 1
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = GeneratorDeconv()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
The function def GeneratorDeconv(image_size = 28): I picked from ProgramCreek Python
Now, I am confused that how can I embed it into my own custom model. Up to model.compile(...) the program runs okay. But at model.fit(...) , it gives error:
ValueError: Error when checking input: expected input_2 to have 2
dimensions, but got array with shape (60000, 28, 28, 1)
I don't know how to resolve the issues. Please help.
The input to your model is:
inputs = Input(shape = (100, ))
This will take a vector in the shape of (samples, 100), so it expects a 2D input.
However:
print('x_train shape:', x_train.shape)
>>>x_train shape: (60000, 28, 28, 1)
You are inputting a 4D array, when you specified that your input took a 2D one. That is what is causing the error.
I made some edits to your architecture so the shapes match up and it actually trains:
def GeneratorDeconv(image_size = 28):
L = int(image_size)
inputs = Input(shape = (28, 28,1))
x = Dense(512*int(L/16)**2)(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(256, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(128, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(64, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(3, (4, 4), strides= (2, 2),
kernel_initializer = init,
padding = 'same')(x)
x = Flatten()(x)
x = Dense(10)(x)
images = Activation('tanh')(x)
model = Model(inputs = inputs, outputs = images)
model.summary()
return model

Keras: Error when checking input

I am using Keras autoencodes with Theano backend. And want to make autoencode for 720x1080 RGB images.
This is my code
from keras.datasets import mnist
import numpy as np
from keras.layers import Input, LSTM, RepeatVector, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from PIL import Image
x_train = []
x_train_noisy = []
for i in range(5,1000):
image = Image.open('data/trailerframes/frame' + str(i) + '.jpg', 'r')
x_train.append(np.array(image))
image = Image.open('data/trailerframes_avg/frame' + str(i) + '.jpg', 'r')
x_train_noisy.append(np.array(image))
x_train = np.array(x_train)
x_train = x_train.astype('float32') / 255.
x_train_noisy = np.array(x_train_noisy)
x_train_noisy = x_train_noisy.astype('float32') / 255.
input_img = Input(shape=(720, 1080, 3))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(x_train_noisy, x_train,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(x_train_noisy, x_train))
But it gives me an error
ValueError: Error when checking input: expected input_7 to have shape (None, 720, 1080, 3) but got array with shape (995, 720, 1280, 3)
Input error:
As simple as:
You defined your input as (720,1080,3)
You're trying to trian your model with data in the form (720,1280,3)
One of them is wrong, and I think it's a typo in the input:
#change 1080 for 1280
input_img = Input(shape=(720, 1280, 3))
Output error (target):
Now, your target data is shaped like (720,1280,3), and your last layer outputs (720,1280,1)
A simple fix is:
decoded = Conv2D(3, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)
Using the encoder:
After training that model, you can create submodels for using only the encoder or the decoder:
encoderModel = Model(input_img, decoded)
decoderInput = Input((shape of the encoder output))
decoderModel = Model(decoderInput,decoded))
These two models will share the exact same weights of the entire model, training one model will affect all three models.
For using them without training, you can use model.predict(data), which will give you the results without training.

Keras has the output layer shape mismatched compared to input for CNN

I'm getting an error in Keras where the dimension of the output is different than the dimension of the input. I don't understand where the 20 comes from. All my dimensions seem to be showing 18. Also, the Output Shape for convolution2d_70 just says 'multiple', so I'm not sure what that means. Any ideas?
Exception: Error when checking model target: expected convolution2d_70 to have shape (None, 1, 36L, 20L) but got array with shape (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))
Notice that in third convolutional layer in decoder you are missing border_mode='same'. This makes your dimensionality mismatch.

Resources