tracel-ai/burn · error

More than 3 channels not supported ({channels})

Error message

More than 3 channels not supported ({channels})

What it means

save_tensor_as_image can only map tensor channels to RGB pixels for 1-, 2-, or 3-channel images (1 = monochrome/scalar mapping, 3 = direct RGB). Any channel count greater than 3 hits the catch-all arm and aborts with unimplemented!, since the image buffer only has 3 pixel components to fill.

Source

Thrown at crates/burn-vision/src/utils/save.rs:158

                let mut pixel = [0 as f32; 3];
                match opts.color_opts {
                    ColorDisplayOpts::Rgb => match channels {
                        1 => {
                            pixel[0] = channel_vals[0];
                            pixel[1] = 0.0;
                            pixel[2] = 0.0;
                        }
                        2 => {
                            pixel[0] = channel_vals[0];
                            pixel[1] = channel_vals[1];
                            pixel[2] = 0.0;
                        }
                        3 => {
                            pixel[0] = channel_vals[0];
                            pixel[1] = channel_vals[1];
                            pixel[2] = channel_vals[2];
                        }
                        _ => unimplemented!("More than 3 channels not supported ({channels})"),
                    },
                    ColorDisplayOpts::Monochrome { min, max } => {
                        let val: f32 = channel_vals.iter().sum();
                        pixel[0] = min[0] * (1.0 - val) + max[0] * val;
                        pixel[1] = min[1] * (1.0 - val) + max[1] * val;
                        pixel[2] = min[2] * (1.0 - val) + max[2] * val;
                    }
                }

                let pixel = [
                    (pixel[0] * 255.0) as u8,
                    (pixel[1] * 255.0) as u8,
                    (pixel[2] * 255.0) as u8,
                ];
                img.put_pixel(x, y, Rgb(pixel));
            }
        }
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Slice the tensor to at most 3 channels before saving, e.g. `tensor.slice(s![.., .., 0..3])` or select the 3 channels you want as RGB.
  2. Reduce multi-channel data yourself (mean/max over channels) to a 1-channel tensor and save with ColorDisplayOpts::Monochrome.
  3. Save each channel as a separate monochrome image if all channels are needed.

Example fix

// before
save_tensor_as_image(&feature_map, "out.png", ColorDisplayOpts::Rgb)?; // 64 channels: panics
// after
let rgb = feature_map.clone().slice(2..2, 0..3); // keep first 3 channels
save_tensor_as_image(&rgb, "out.png", ColorDisplayOpts::Rgb)?;
Defensive patterns

Strategy: validation

Validate before calling

let channels = tensor.dims()[tensor.dims().len() - 1];
if channels > 3 {
    tensor = tensor.slice(2..2, 0..3); // or reduce channels before saving
}
save_tensor_as_image(&tensor, "out.png", opts)?;

Type guard

fn is_saveable(channels: usize) -> bool {
    matches!(channels, 1..=3)
}

Prevention

When it happens

Trigger: Calling `save_tensor_as_image` on a tensor with C > 3 in its channel dimension, e.g. [H, W, 4] RGBA tensors, [B, H, W, C] batches with C > 3, or feature-map tensors with many channels, saved with ColorDisplayOpts::Rgb.

Common situations: Trying to visualize intermediate conv feature maps (16/64/512 channels) as images; saving RGBA (4-channel) tensors; forgetting to slice/select channels before saving debug visualizations.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/2dd89cdb3602ad62. Report an issue: GitHub.