Pad images on the right. Each image is a float array laid out as [channels, height, width], and all images must share a height and channel count.
dotnet add package RecursiveAI.OneModel.TrainingRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel.Training.Data; var collator = new ImageBatchCollator(4); ImageExample[] images = [ new(new float[] { 1, 2 }, 1, 1, 2), new(new float[] { 3, 4, 5 }, 1, 1, 3) ]; using var batch = collator.Collate( images, [0, 1], default); Console.WriteLine(batch.Images.Shape); var widths = batch.Widths.Data<int>(); Console.WriteLine( string.Join(", ", widths.ToArray()));
Unzip the example, then dotnet run.
[2,1,1,4] 2, 3
The inputs are two grayscale rows, of widths 2 and 3. ImageExample takes pixels, channels, height, and width, in that order. The collator rounds the widest row up to a multiple of 4. The [0, 1] values are dataset indices, not widths; default supplies the batch context because the example doesn’t use random augmentation.
The full program
The program also prints the flattened pixels, showing the right-hand zero padding.
using RecursiveAI.OneModel.Training.Data; ImageExample[] images = [ new(new float[] { 1, 2 }, 1, 1, 2), new(new float[] { 3, 4, 5 }, 1, 1, 3) ]; var collator = new ImageBatchCollator(4); using var batch = collator.Collate( images, [0, 1], default); var widths = batch.Widths.Data<int>(); Console.WriteLine(batch.Images.Shape); Console.WriteLine(string.Join(", ", widths.ToArray())); var pixels = batch.Images.Data<float>(); Console.WriteLine(string.Join(", ", pixels.ToArray()));
[2,1,1,4] 2, 3 1, 2, 0, 0, 3, 4, 5, 0
If the heights or channel counts differ, Collate throws ArgumentException. Keep the batch alive while reading its tensors: disposing it returns their buffers to the pool.
Variations
Replace the collator line to fill with -1 instead of zero. Choose a value in the same pixel scale as the input you pass to the network.
var collator = new ImageBatchCollator(4, padValue: -1);[2,1,1,4] 2, 3 1, 2, -1, -1, 3, 4, 5, -1
Related recipes
The short answer
Use ImageBatchCollator from RecursiveAI.OneModel.Training to stack planar [C, H, W] float images of the same height and channel count. Set widthMultiple to round the widest input up to an alignment your model needs. The result is [B, C, H, paddedWidth]; batch.Widths gives you the unpadded widths in batch order.
Do you need to pad variable-size images before batching them?
If you need one dense tensor containing multiple images, its width must be the same for every sample. Padding supplies that shared width without stretching or cropping the original columns. The collator pads to the widest image in the current batch, not in your entire dataset. With widthMultiple set to 1 (the default), it never adds columns beyond the batch maximum. If a network expects widths aligned to a particular stride, use that multiple instead. This collator only handles differences in width: it doesn’t resize images that have different heights or convert between grayscale and RGB.
What is a planar image tensor?
A planar [C, H, W] tensor stores the whole first channel before the second. For RGB that’s a plane of red values, then a plane of green, then a plane of blue. It is not the same layout as an RGB buffer that stores three adjacent values per pixel. OneModel’s ImageTensors.Rgb8ToPlanar converts interleaved byte pixels into float planes; it doesn’t decode a JPEG or PNG file. The grayscale rows in this example already have one plane each.
How do you keep track of image widths after padding?
Keep batch.Widths next to batch.Images. If your model turns each image into a sequence, the original width lets you find how many output positions belong to the image rather than padding. Apply the model’s actual output-length rule to each width. For a network whose output length is the input width divided by 4 and rounded up, a real width of 5 means two valid positions, even if a wider neighbor made the batch tensor 12 columns wide. The collator records widths; it doesn’t mask attention or set loss lengths automatically.