The performance difference between (b, c, h, w) and (b, h, w, c) depends on several factors:
General Performance Considerations
1. GPU Performance (NVIDIA CUDA)
- Channel-first (b, c, h, w) is generally faster on GPUs, especially with NVIDIA's cuDNN library
- cuDNN (CUDA Deep Neural Network library) is heavily optimized for channel-first format
- Most GPU convolution kernels expect contiguous channel data for efficient memory access patterns
2. CPU Performance
- Channel-last (b, h, w, c) is often faster on CPUs
- Modern CPUs with AVX instructions can vectorize operations more efficiently when channels are contiguous in memory
- Intel's MKL-DNN (now oneDNN) is optimized for channel-last format
Practical Performance Differences
On GPUs (typical scenario):
- Channel-first can be 10-30% faster for convolution operations
- The difference is more pronounced for smaller batch sizes
- Modern GPUs and frameworks are reducing this gap
On CPUs:
- Channel-last can be 20-50% faster for many operations
- Better cache locality when processing pixels
Framework-Specific Optimizations
PyTorch:
- Defaults to channel-first (b, c, h, w)
- Has "channels_last" memory format option since v1.5 for better performance on some hardware
- Example:
model = model.to(memory_format=torch.channels_last)
TensorFlow:
- Defaults to channel-last (b, h, w, c)
- Can use channel-first with
data_format='channels_first' parameter
- Has mixed performance depending on the operation
Recommendations
- For GPU training: Use channel-first (b, c, h, w) unless you have specific hardware that performs better with channel-last
- For CPU inference: Consider channel-last (b, h, w, c)
- For mixed workloads: Profile your specific use case
Modern Trends
Recent developments are making the performance gap smaller:
- NVIDIA's Tensor Cores can handle both formats efficiently
- Frameworks are adding automatic format conversion optimizations
- New hardware (like Apple Silicon) may have different optimal formats
Bottom line: If you're using GPUs (the most common case for deep learning), channel-first (b, c, h, w) is typically faster. However, the best choice depends on your specific hardware, framework version, and workload. When in doubt, profile both options with your actual model and data.