Keras
ImageDataGenerator
width_shift_range
height_shift_range
data augmentation

Understanding width_shift_range and height_shift_range arguments in Keras's ImageDataGenerator class

Master System Design with Codemia

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

Introduction

width_shift_range and height_shift_range control random horizontal and vertical translations during image augmentation. They help a model become less sensitive to small object position changes by moving the training image left, right, up, or down before it is fed to the network.

What a Shift Actually Does

A shift does not change the class label. It changes where the image content appears inside the frame. For example, a centered digit can be moved slightly to the left or up so the model learns that exact placement is not the only valid version of the object.

This is useful when your real-world images may not be perfectly aligned.

width_shift_range

width_shift_range controls horizontal movement. Positive and negative random offsets are sampled during augmentation so the image may move left or right.

A fractional value is interpreted relative to image width:

python
from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(width_shift_range=0.2)

If the image width is 100 pixels, a setting of 0.2 means the image can be shifted by up to about 20 pixels horizontally.

An integer value is interpreted in pixels:

python
datagen = ImageDataGenerator(width_shift_range=10)

That means up to 10 pixels of horizontal translation.

height_shift_range

height_shift_range does the same thing vertically.

python
from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(height_shift_range=0.1)

If the image height is 200 pixels, this allows a vertical shift of up to about 20 pixels.

You can combine both:

python
1datagen = ImageDataGenerator(
2    width_shift_range=0.15,
3    height_shift_range=0.15
4)

Now each augmented image may be moved in both directions.

Example With Real Data Flow

Here is a small runnable example using NumPy image data:

python
1import numpy as np
2from tensorflow.keras.preprocessing.image import ImageDataGenerator
3
4x = np.random.rand(4, 64, 64, 3).astype("float32")
5y = np.array([0, 1, 0, 1])
6
7datagen = ImageDataGenerator(
8    width_shift_range=0.2,
9    height_shift_range=0.1
10)
11
12generator = datagen.flow(x, y, batch_size=2, shuffle=False)
13
14batch_x, batch_y = next(generator)
15print(batch_x.shape, batch_y)

The labels stay the same, but the images are randomly shifted as they are yielded.

What Happens to Empty Areas

When an image is shifted, empty space appears at the edges. Keras fills those new regions according to fill_mode.

python
1datagen = ImageDataGenerator(
2    width_shift_range=0.2,
3    height_shift_range=0.2,
4    fill_mode="nearest"
5)

This matters because the fill behavior changes the augmented image appearance. For some tasks, a poor fill mode can create artifacts that the model learns accidentally.

How Much Shift Is Reasonable

The correct amount depends on the dataset. Small shifts often improve robustness. Very large shifts can move the object partly out of frame and create unrealistic training examples.

As a rough rule:

  • small objects near the center often tolerate modest shifts
  • tight crops may require very conservative shifts
  • medical or document images may be harmed by aggressive translation

Augmentation is only helpful when the synthetic variation still looks like valid data from the real problem.

A Note on API Age

ImageDataGenerator is still widely used, especially in older Keras code, but many newer projects use preprocessing layers or tf.data pipelines for augmentation. The shift concepts are the same even if the API changes.

So understanding these arguments is still useful, especially when maintaining existing training code.

Common Pitfalls

The biggest mistake is assuming 0.2 always means 0.2 pixels. For fractional settings, it means a fraction of the image dimension, not a literal pixel count.

Another issue is choosing shifts so large that the important object is pushed out of the frame. That can hurt training instead of helping it.

Developers also forget about fill_mode, which controls what appears in the newly exposed edges after translation.

Finally, augmentation should reflect realistic variation. If the production data is tightly aligned, extreme shifting may create synthetic samples that do not match the real task.

Summary

  • 'width_shift_range controls horizontal translation and height_shift_range controls vertical translation.'
  • Float values are fractions of image size, while integer values are pixel counts.
  • The shifted image keeps the same label but appears in a different position within the frame.
  • 'fill_mode controls how newly exposed edge regions are filled.'
  • Use enough shifting to improve robustness, but not so much that the augmented data becomes unrealistic.

Course illustration
Course illustration

All Rights Reserved.