tensorflow
image translation
image processing
machine learning
data augmentation

How to translateor shift images in tensorflow

Master System Design with Codemia

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

Introduction

Shifting an image in TensorFlow means translating pixels horizontally, vertically, or both without changing the semantic content. The best API depends on whether you want random augmentation during training or a deterministic fixed shift for preprocessing and debugging.

Core Sections

Random translation during model training

For modern Keras pipelines, the most direct solution is RandomTranslation. It applies a bounded shift as part of the model or input pipeline and is ideal for augmentation.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(224, 224, 3)),
5    tf.keras.layers.RandomTranslation(
6        height_factor=0.1,
7        width_factor=0.1
8    ),
9    tf.keras.layers.Conv2D(16, 3, activation="relu"),
10    tf.keras.layers.GlobalAveragePooling2D(),
11    tf.keras.layers.Dense(1, activation="sigmoid"),
12])

The height_factor and width_factor values are fractions of the input size, not raw pixel counts. That makes the layer convenient for training pipelines that operate on uniformly sized images.

Legacy augmentation with ImageDataGenerator

If you are working in older Keras code, ImageDataGenerator also supports translation through shift ranges.

python
1from tensorflow.keras.preprocessing.image import ImageDataGenerator
2
3datagen = ImageDataGenerator(
4    width_shift_range=0.1,
5    height_shift_range=0.1
6)

This is still useful in maintenance work, but for new code the preprocessing layers are usually cleaner and easier to keep inside the model graph.

Deterministic translation for a fixed offset

Random augmentation is not always what you want. If you need to shift an image by a known amount, for example 10 pixels right and 5 pixels down, you should use a deterministic image transform rather than a random layer.

One practical option is TensorFlow Addons:

python
1import tensorflow as tf
2import tensorflow_addons as tfa
3
4image = tf.random.uniform((224, 224, 3))
5shifted = tfa.image.translate(image, translations=[10.0, 5.0])
6
7print(shifted.shape)

That explicitly applies a translation measured in pixels. It is useful for test cases, offline preprocessing, and reproducible augmentation.

tf.roll is not the same thing

Some developers reach for tf.roll because it moves array contents:

python
shifted = tf.roll(image, shift=[5, 10], axis=[0, 1])

But tf.roll wraps pixels around the image border. In many computer-vision tasks that is not a real translation, because pixels that move off one side reappear on the opposite side. For natural image augmentation, that wrap-around behavior is often undesirable.

Border fill behavior matters

Any real translation creates empty space near the border. Different APIs fill that space differently, using zeros, reflection, or nearest pixels. That choice can affect model behavior, especially on small images.

When evaluating a translation method, check:

  • whether the shift is measured in fractions or pixels
  • what value fills the exposed border
  • whether the operation is random or deterministic

Those details matter more than the high-level name of the transform.

If you are translating batched tensors, also confirm the expected shape ordering. Most TensorFlow image APIs assume batch, height, width, channels, and a silent shape mismatch can look like a broken augmentation step.

Common Pitfalls

  • Using tf.roll when you wanted a true translation rather than wrap-around movement.
  • Applying random translation in validation or test pipelines and making evaluation noisy.
  • Confusing fraction-based shifts in RandomTranslation with pixel-based shifts in lower-level APIs.
  • Forgetting to inspect the border fill behavior after translation.
  • Mixing legacy ImageDataGenerator augmentation with newer preprocessing layers without a clear reason.

Summary

  • Use RandomTranslation for modern Keras data augmentation during training.
  • Use ImageDataGenerator shift ranges mainly when maintaining older pipelines.
  • For fixed pixel offsets, prefer a deterministic transform such as tensorflow_addons.image.translate.
  • Avoid tf.roll unless wrap-around semantics are actually what you want.
  • Always verify border handling and whether the shift is random or reproducible.

Course illustration
Course illustration

All Rights Reserved.