mouhand hassan
mouhand hassan

Reputation: 83

ValueError: Error when checking model target: expected convolution2d_2 to have shape (None, 26, 26, 64) but got array with shape (250, 227, 227, 1)

I'm using Keras with Tensorflow as backend , here is my code:

#image loading and preprocessing
import os
from PIL import Image as Image
import numpy as np

#files is a list of images
files = [os.path.join('Save', file_i)
 for file_i in os.listdir('Save')
 if '.jpg' in file_i]

imgs = []
for image in files:
    img = Image.open(image) 
    img = img.resize((227,227),Image.BILINEAR)
    img = img.convert('L')

    img = np.asarray(img)

    array = img.astype('float32')
    array /= 255        
    imgs.append(array)  

imgs = np.asarray(imgs)

The_data = imgs.reshape(imgs.shape[0], 227, 227,1)
The_data = The_data.reshape(10, 25, 227, 227, 1)

from keras.models import Sequential
from keras.layers.convolutional import Convolution2D,Deconvolution2D
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import TimeDistributed
import numpy as np
import pylab as plt



model = Sequential()
#2 Convolution layer


model.add(TimeDistributed(Convolution2D(128, 11, 11 , border_mode='same', subsample = (4,4)), input_shape=(None,227, 227, 1)))
model.add(TimeDistributed(Convolution2D(64, 5, 5, border_mode='same', subsample = (2,2))))

model.add(TimeDistributed(ConvLSTM2D(nb_filter=64, nb_row=3, nb_col=3,
                   border_mode='same', return_sequences=True)))
model.add(BatchNormalization())
model.add(TimeDistributed(ConvLSTM2D(nb_filter=32, nb_row=3, nb_col=3,
                   border_mode='same', return_sequences=True)))
model.add(BatchNormalization())
model.add(TimeDistributed(ConvLSTM2D(nb_filter=64, nb_row=3, nb_col=3,
                   border_mode='same', return_sequences=True)))
model.add(BatchNormalization())

model.add(TimeDistributed(Deconvolution2D(128, 5, 5,border_mode='same', output_shape=(None,57, 57, 128), subsample = (2,2))))
model.add(TimeDistributed(Deconvolution2D(1, 11, 11,border_mode='same', output_shape=(None,227, 227, 1), subsample = (4,4))))


model.compile(optimizer='adadelta', loss='binary_crossentropy')
model.fit(The_data,The_data, batch_size=5,nb_epoch=1)
model.summary()

I`m trying to read some images and do some preprocessing to them and then apply (A) 2 convolution layers , (B) three ConvLSTM layers , and (C) 2 Deconvolution layers.

I'm trying to implement the algorithm used in this research paper but I see that each of the layers(conv,deconv,convlstm) requires something different, i have searched and know that convlstm need 5-dim inputs (number of frames) but how to change input shape for it since it is not the first layer in the model.

overview of algorithm here

I have three main questions here :

1- The Convultion2d throw that error

Error when checking model target: expected convolution2d_2 to have shape (None, 26, 26, 64)but got array with shape(250, 227, 227, 1)`

2- I have comment ConvLSTM2D because it throw that error

ValueError: Input 0 is incompatible with layer convlstm2d_1: expected ndim=5, found ndim=4

and i have also comment Deconvolution because i dont know what output_shape is supposed to be. I know at the end I should have the input images reconstructed.

3- In model.fit , i have no labeled data since I`m doing unsupervised learning , should I leave it that way or what ?

Upvotes: 3

Views: 1716

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40516

The problems lied in:

  1. Wrong input_shape - data should be cropped to a video 5-d format. It was done be reshaping and cropping.
  2. Adding TimeDistributed to conv and deconv layers.
  3. Changing a deconv output shape to appropriate values.
  4. Changing border_mode to same.

All other details might be found in comments under the question.

Upvotes: 1

Related Questions