0
$\begingroup$

I am building an anomaly detection model using keras upon videos. There are total 179 frames. The original dimension of each frame is given below:

h,w,c=cv2.imread(FramesFile[178]).shape  #h=240, w=30, c=3

Then, I have applied the ResNet-50 on these video frames to extract features, it gives me (1,7,7,2048) features. And to reduce down these features I have applied conv2D layer on it to get (1, 7, 7, 512) features as shown in 2nd line of function of FramesTrain()

Here's my code for training. I have done it through convolutional LSTM

def FramesTrain():
 seq = Sequential()
 seq.add(Conv2D(512, (5, 5), padding="same"))
 seq.add(ConvLSTM2D(filters=40, kernel_size=(5, 5), input_shape=(None, 40, 40, 1), padding='same', return_sequences=True))
 seq.add(BatchNormalization())
 seq.add(ConvLSTM2D(filters=40, kernel_size=(5, 5), padding='same', return_sequences=True))
 seq.add(BatchNormalization())
 seq.add(ConvLSTM2D(filters=40, kernel_size=(5, 5), padding='same', return_sequences=True))
 seq.add(BatchNormalization())
 seq.add(ConvLSTM2D(filters=40, kernel_size=(5, 5), padding='same', return_sequences=True))
 seq.add(BatchNormalization())
 seq.add(Conv3D(filters=1, kernel_size=(5, 5, 5), activation='sigmoid', padding='same', 
       data_format='channels_last'))
 seq.compile(loss='binary_crossentropy', optimizer='adadelta')
 return seq

 model=FramesTrain()
 model.fit(FramesFeatures[0], batch_size=32,epochs=10, verbose=1)

 

But it gives me this error:

ValueError: Input 0 of layer conv_lst_m2d_60 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: (None, 7, 7, 512)

Kindly guide me how do I solve it. Regards,

$\endgroup$
1
  • $\begingroup$ This kind of issue has been asked countless times here, you should have a look here. Your problem seem to be your time dimension, your inputs should be of shape (None, timedim, 7, 7, 2048) if you want to use LSTM. the 'None' size corresponds to the batchsize or simply data_size. Here either your timedim is missing or it has been converted to batchsize, in the second case, you can try tf.expand_dims(data, 0) function to add an batchsize dimension at ind 0. $\endgroup$
    – Ubikuity
    May 25 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.