1

I am facing the error in the title. I have thousands of videos and each video have 37 frames. I have extracted features for each frame with a CNN model and saved them. I have a stacked LSTM model :

batch_size = 8
features_length = 2048
seq_length = 37*batch_size
in_shape = (seq_length, features_length)
lstm_model = Sequential()
lstm_model.add(LSTM(2048, return_sequences=True, input_shape = in_shape, dropout=0.5))
lstm_model.add(Flatten())
lstm_model.add(Dense(512, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(number_of_classes, activation='softmax'))
optimizer = Adam(lr=1e-6)
lstm_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics = metrics)
lstm_model.fit_generator(generator = generator, steps_per_epoch = train_steps_per_epoch, epochs = nb_epoch, verbose = 1, callbacks=[checkpointer, tb, early_stopper, csv_logger], validation_data=val_generator, validation_steps = val_steps_per_epoch)

I have a generator; data includes all training videos.

def generator(data):

    while 1:
        X, y = [], []
        for _ in range(batch_size):
            sequence = None
            sample = random.choice(data)
            folder_content, folder_name, class_name, video_features_loc = get_video_features(sample)
            for f in folder_content:
                image_feature_location = video_features_loc + f
                feat = get_extracted_feature(image_feature_location)

                X.append(feat)
                y.append(get_one_class_rep(class_name))         
        yield np.array(X), np.array(y)

The shape of X in generator data is = (296, 2048, 1)

The shape of y in generator data is = (296, 27)

This code throws the error. I know there are couple of similar questions. I tried the suggestions there but no luck. For instance one the suggestions was reshaping the array;

X = np.reshape(X, (X.shape[2], X.shape[0], X.shape[1]))

How could I feed my input to the LSTM?

Thanks in advance

1
  • X = np.reshape(X, (X.shape[2], X.shape[0], X.shape[1])) Commented Sep 8, 2017 at 14:36

1 Answer 1

2

The error message tells you everything you need.

X should be shaped as (number of samples, 296, 2048) - It seems you have one sample only, by the shape of X.


But if you have 37 frames, you should definitely change your model for something accepting: (Batch size, 37, 2048) - Here, batch size seems to be 8.

seq_length=37 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for your answer @Daniel. I changed X shape as you said; (1, 296, 2048) 1 is the number of sample. However this time I got the error; "ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 296 target samples."
I kind of know that this error is about having different size of X and y values. I have been dealing with LSTMs for a month now in Keras. I got stuck with these input shapes every time I try to use LSTMs in Keras...
Just shape X according to what you actually have: (NumberOfExamples = numberOfVideos, TimeSteps = framesInAVideo, featuresInAFrame or pixelsInAFrame) -- Once you understand your data and shape it correctly, then you make input_shape=(TimeSteps,features).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.