Keras
RGB images
image processing
deep learning
computer vision

How to handle RGB images in Keras

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Handling RGB images in Keras is mostly about getting shape, dtype, and preprocessing consistent from the data loader to the model input. RGB images are three-channel tensors, so mistakes usually appear as channel-order confusion, wrong input shape, or mismatched normalization. Once those basics are correct, Keras image pipelines are straightforward.

Know the Expected Tensor Shape

In modern TensorFlow and Keras setups, images are typically represented as height x width x channels. For RGB, the channel count is 3.

A model input therefore often looks like this:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(224, 224, 3)),
5    tf.keras.layers.Conv2D(16, 3, activation="relu"),
6    tf.keras.layers.GlobalAveragePooling2D(),
7    tf.keras.layers.Dense(10, activation="softmax"),
8])

If your data arrives as (224, 224) or (224, 224, 1), it is not RGB yet.

Load RGB Data Explicitly

If you load images from directories, Keras can do the channel handling for you.

python
1import tensorflow as tf
2
3dataset = tf.keras.utils.image_dataset_from_directory(
4    "images/",
5    image_size=(224, 224),
6    batch_size=32,
7    color_mode="rgb"
8)
9
10for images, labels in dataset.take(1):
11    print(images.shape)

Using color_mode="rgb" makes the intent explicit and avoids guessing about image mode conversion.

Normalize Before Training

Raw image pixels are usually uint8 values in the range 0 to 255. Most models train more reliably if the input is converted to floating point and scaled.

python
1normalizer = tf.keras.layers.Rescaling(1.0 / 255)
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(224, 224, 3)),
5    normalizer,
6    tf.keras.layers.Conv2D(16, 3, activation="relu"),
7    tf.keras.layers.GlobalAveragePooling2D(),
8    tf.keras.layers.Dense(10, activation="softmax"),
9])

Keeping normalization inside the model is often cleaner because it guarantees train and inference paths use the same scaling rule.

Be Careful with External Libraries

Keras itself expects RGB-style channel ordering in its high-level image utilities. But if you load images with OpenCV before passing them into Keras, OpenCV typically gives you BGR.

That means this kind of correction is often necessary:

python
1import cv2
2import numpy as np
3
4img = cv2.imread("example.jpg")
5img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
6img = cv2.resize(img, (224, 224))
7img = img.astype(np.float32) / 255.0

If you forget the BGR-to-RGB conversion, the model still runs, but color semantics are wrong.

Match Preprocessing to the Backbone

If you use a pretrained model from tf.keras.applications, do not assume generic 0 to 1 scaling is always the correct preprocessing. Many pretrained backbones expect a specific preprocessing function.

python
1from tensorflow.keras.applications import MobileNetV2
2from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
3
4base = MobileNetV2(include_top=False, input_shape=(224, 224, 3))

In that case, the image pipeline should call preprocess_input rather than only dividing by 255. Matching the backbone's training-time assumptions can matter more than the rest of the image pipeline details.

Support Augmentation Without Breaking Channels

Keras augmentation layers work naturally with RGB tensors as long as the input shape includes all three channels.

python
1augment = tf.keras.Sequential([
2    tf.keras.layers.RandomFlip("horizontal"),
3    tf.keras.layers.RandomRotation(0.05),
4    tf.keras.layers.RandomZoom(0.1),
5])

The important part is to keep augmentation and normalization in a consistent pipeline. Do not augment one representation and then accidentally feed another representation to the model.

Common Pitfalls

  • Forgetting that RGB input shape should end with channel count 3.
  • Mixing Keras RGB assumptions with OpenCV BGR-loaded images.
  • Feeding uint8 images directly into models without deliberate normalization.
  • Using grayscale images with an RGB model input or vice versa.
  • Resizing images inconsistently between training and inference.

Small image-pipeline mismatches are easy to miss because the model still runs. That is why printing one batch shape and one sample range early is often worth the extra line of code.

Summary

  • RGB images in Keras are usually tensors shaped height x width x 3.
  • Use Keras loaders with explicit RGB mode when possible.
  • Normalize pixel values before training, ideally inside the model or dataset pipeline.
  • Watch out for BGR-to-RGB conversion when OpenCV is involved.
  • Match preprocessing rules to the specific pretrained backbone you are using.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.