ResNet-20 With Multi-Channel Striding Processing
This example demonstrates multi-channel striding and preprocessing used in the ResNet-20 FHE pipeline.
It shows how to prepare multi-channel kernels, shortcuts, and fully-connected weights and how strided residual blocks are executed in the encrypted domain.
It uses he_convolution_and_shortcut_optimized_with_multiple_channels() for the convolution layers with striding.
Overview
The workflow includes:
FHE Context and Key Generation
Initialize FHEONHEController and
FHEONANNStandardController.Generate rotation keys for multi-channel convolutions, pooling, and FC layers.
Data Preparation
Load and encode convolution kernels, shortcut kernels, and biases for each layer.
Support downsampling (striding) by encoding both convolution and shortcut weights with appropriate output sizes.
Multi-Channel Striding
Handle strided residual blocks by preparing downsampled shortcut kernels and matching convolution kernels.
Use multi-channel optimized encodings and rotation positions for efficient FHE computation.
Inference Loop
Encrypt input images, apply convolution blocks and residual blocks (with optional shortcut downsampling), perform global average pooling, and run the final FC layer.
Key Functions
Controller functions used in this example:
he_convolution_optimized()– 3x3 “same” convolution with hoisted rotations.he_convolution_and_shortcut_optimized_with_multiple_channels()– Stride-2 convolution and 1x1 shortcut on the multi-channel layout.he_shortcut_convolution()– 1x1 shortcut convolution.he_globalavgpool()– Global average pooling.he_linear_optimized()– Fully connected layer with pre-encoded weights.he_relu()– Chebyshev approximation of ReLU.he_sum_two_ciphertexts()– Residual connection.bootstrap_function()– CKKS bootstrapping to refresh the ciphertext levels.encode_kernel_optimized()– Encodes the 3x3 kernels with their padding masks.encode_shortcut_kernel()– Encodes the 1x1 shortcut kernels.
Functions of the example:
convolution_block()– Loads and encodes a convolution’s weights and runs it.shortcut_convolution_block() / double_shortcut_convolution_block()– 1x1 shortcut, and the stride-2 convolution with its shortcut.resnet_block()– A basic block: convolutions, bootstrapping, ReLU and the residual sum.fc_layer_block()– Final fully connected layer.