Keras
video prediction
time series
machine learning
deep learning

Using Keras for video prediction time series

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Video prediction is a time-series problem where each time step is an image frame rather than a scalar value. In Keras, the usual starting point is not a plain dense network but a sequence model that can capture both temporal order and spatial structure, such as ConvLSTM2D.

Think About the Data Shape First

For frame sequences, the model input is typically shaped like:

text
(samples, timesteps, height, width, channels)

That is the first thing to get right. If you flatten frames too early, the model loses spatial information and stops behaving like a meaningful video predictor.

A toy example with grayscale frames might look like:

python
1import numpy as np
2
3x = np.random.rand(100, 5, 32, 32, 1).astype("float32")
4y = np.random.rand(100, 32, 32, 1).astype("float32")
5
6print(x.shape)
7print(y.shape)

Here each training example contains 5 input frames, and the target is the next frame.

A Simple ConvLSTM Model

Keras provides ConvLSTM2D, which mixes convolution with temporal recurrence:

python
1from tensorflow import keras
2
3model = keras.Sequential([
4    keras.layers.ConvLSTM2D(
5        filters=16,
6        kernel_size=(3, 3),
7        padding="same",
8        activation="relu",
9        input_shape=(5, 32, 32, 1)
10    ),
11    keras.layers.Conv2D(filters=1, kernel_size=(3, 3), padding="same", activation="sigmoid")
12])
13
14model.compile(optimizer="adam", loss="mse")
15model.fit(x, y, epochs=1, batch_size=8, verbose=0)

This is a minimal next-frame prediction model. It is not state of the art, but it captures the basic Keras workflow for video-like time series.

Video Prediction Is Harder Than Ordinary Time Series

Predicting a scalar or a stock price is already hard. Predicting the next image frame is harder because the model must learn:

  • motion over time
  • object persistence
  • spatial structure inside each frame
  • uncertainty about multiple possible futures

That is why simple feed-forward models usually perform poorly here. Even when the output shape is correct, the predictions can blur heavily if the architecture is too weak or the loss function is too naive.

Start with Next-Frame Prediction

A good first milestone is next-frame prediction, not long-horizon rollout. If the model cannot produce the immediate next frame reasonably well, asking it to predict ten frames into the future will usually amplify the error quickly.

That means a sensible progression is:

  1. train on short input sequences
  2. predict one next frame
  3. inspect predictions visually
  4. only then attempt recursive multi-step prediction

This keeps the debugging loop manageable.

Data Preparation Matters a Lot

Video models are sensitive to preprocessing. Common steps include:

  • resize frames to a manageable resolution
  • normalize pixel values to a fixed range
  • keep channel order consistent
  • align frame windows and targets correctly

If the time windows are off by one frame, the model may still train but learn the wrong target relationship.

Common Pitfalls

The biggest mistake is flattening frames and treating video like ordinary tabular time series. That discards spatial structure that video prediction depends on.

Another issue is using the wrong shape order. Keras sequence models for images usually expect (samples, timesteps, height, width, channels) when using the common channels-last convention.

Developers also often judge the model only by numeric loss. For video prediction, visual inspection matters because a low average pixel loss can still correspond to blurry or unrealistic frames.

Finally, do not start with a huge architecture and long sequences. A smaller controlled baseline is easier to debug and reveals data-shape mistakes quickly.

Summary

  • Video prediction is a time-series problem with image frames as time steps.
  • In Keras, ConvLSTM2D is a practical starting layer for this task.
  • Keep the input shape as (samples, timesteps, height, width, channels).
  • Start with next-frame prediction before attempting long rollouts.
  • Validate preprocessing and inspect predicted frames visually, not only through loss values.

Course illustration
Course illustration

All Rights Reserved.