Image tensors and channels
Quick answer
Image tensors represent pixels with explicit batch, channel and spatial axes. PyTorch Conv2d normally receives NCHW: batch, channels, height, width. Grayscale uses one channel and RGB uses three. Examples are static Python snippets tested locally on PyTorch CPU. ByteMind execution is unverified.
Reorder pixels
import torchx=torch.tensor([[[10,20,30],[40,50,60]]],dtype=torch.uint8)y=x.permute(2,0,1).unsqueeze(0).float()/255print(list(y.shape)); print((y[0,:,0,1]*255).round().int().tolist())Expected output (locally tested): [1, 3, 1, 2] [40, 50, 60]. The second pixel retains its channels.
How it works
Convert HWC to CHW with permute(2,0,1), not reshape; add a batch axis with unsqueeze(0). Convert uint8 to floating point and divide by 255 if the original range is 0–255. Fit dataset normalization on training data only. For groups=1 the filter tensor has shape (Cout,Cin,kH,kW). Each output map sums contributions from all input channels and adds a bias if enabled. Parameter count is Cout*(CinkHkW+1) with bias. The same filter weights are reused across spatial positions. Grouped convolution restricts connectivity and requires both channel counts divisible by groups; the weight shape uses Cin/groups instead of Cin.
Filter parameters
import torchm=torch.nn.Conv2d(3,8,3)print(list(m.weight.shape),sum(p.numel() for p in m.parameters()))Expected output (locally tested): [8, 3, 3, 3] 224. Each of eight filters has 27 weights and one bias.
Common mistakes
Do not reshape HWC into CHW: the shape can be correct while pixels are scrambled. Do not confuse batch size and channel count.
Practice and next steps
Inspect a pixel before and after permutation. Calculate the parameters of a 3-to-8 convolution with 3x3 filters and bias.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Image tensors and channels — knowledge check
Apply image tensors and channels to small examples and debugging scenarios.
What is N in NCHW?
Which operation reorders HWC correctly?
How many RGB channels?
What does each groups=1 filter see?
Where fit dataset normalization?