ML Foundations

Move a NumPy image channel axis without reshaping pixels

Move channels from axis one to the last axis and verify values, not only the output shape.

Example results for Move a NumPy image channel axis without reshaping pixels. source shape: 1, 2, 2, 3
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

An axis describes what an index means

An image batch can be represented with axes for batch, channel, height and width. Another consumer may expect batch, height, width and channel. Moving the channel axis changes which index selects a channel; the values belonging to each pixel must move with it. [1]

Changing only the shape is not generally the same operation. Reshape reads and places values according to its documented indexing order, while moveaxis reorders axes. When more than one channel is present, a shape-compatible reshape can attach the wrong channels to a pixel. [1, 2]

References: [1], [2]

Compare the same pixel

Run python examples/numpy_moveaxis_channel_layout.py. The input shape is one batch, two channels, two rows and three columns. Moving axis one to the last position gives shape (1, 2, 3, 2). The first pixel then contains values zero and six from the two original channels. [1]

The same input reshaped to that shape has the same dimensions but gives zero and one at the first pixel. The example checks the moveaxis result against an explicit transpose and prints that the two layouts are unequal. This checks values as well as shape. [1, 2]

Executed with Python 3.12.14; stdout and checksum recorded in private evidence.

import numpy as np

source = np.arange(12).reshape(1, 2, 2, 3)
channels_last = np.moveaxis(source, 1, -1)
shape_only = source.reshape(1, 2, 3, 2)
print('source shape:', source.shape)
print('moved shape:', channels_last.shape)
print('pixel after move:', channels_last[0, 0, 0].tolist())
print('pixel after reshape:', shape_only[0, 0, 0].tolist())
print('layouts equal:', bool(np.array_equal(channels_last, shape_only)))
assert channels_last[0, 0, 0].tolist() == [0, 6]
assert np.array_equal(channels_last, source.transpose(0, 2, 3, 1))

Recorded output

source shape: (1, 2, 2, 3)
moved shape: (1, 2, 3, 2)
pixel after move: [0, 6]
pixel after reshape: [0, 1]
layouts equal: False

References: [1], [2]

Check the input convention first

Use the axis positions required by the producer and consumer; this example assumes channel-first input and channel-last output. A one-channel fixture may fail to expose an incorrect reshape because its channel dimension has length one, so include multiple channels in an alignment check.

NumPy documents moveaxis as returning a view. If later code mutates the moved array, check whether sharing with the source is intended. This example does not prescribe image scaling, channel semantics or performance. [1]

References: [1]

Sources

  1. NumPy moveaxis
  2. NumPy reshape