ResNet-20 FHE Implementation With Data Preprocessing

This example demonstrates ResNet-20 inference using FHEON. It shows how to set up a model with preprocessed weights. Here, everything is loaded into the system before inference is started.

Overview

The workflow includes:

  1. FHE Context and Key Generation

    • Initialize FHEONHEController with parameters like ring degree, slots, circuit depth, and scaling factors.

    • Generate rotation keys for convolutional blocks, residual blocks,global pooling, and fully connected layers.

  2. Data Preparation

    • Read and process all models weights for the entire network.

    • Read, normalize, and encrypt CIFAR-10 images for inference.

  3. ResNet-20 Layers

    • Initial convolution layer (Conv1).

    • Three residual stages, each with multiple residual blocks using encrypted convolutions and optional shortcut connections.

    • Encrypted-domain ReLU activations and bootstrapping where required.

    • Global average pooling followed by a fully connected classification layer.

  4. Inference Loop

    • Iterates over input images, applying layers sequentially.

    • Decrypts results and writes predictions to a file.

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_globalavgpool() – Global average pooling.

  • he_linear() – Fully connected layer.

  • 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.

Functions of the example:

  • convolution_data_processing() – Encodes every convolution’s kernels and biases once, before the inference.

  • shorcut_data_processing() / double_data_processing() – Encode the shortcut and stride-2 block weights.

  • fclayer_data_processing() – Encodes the fully connected layer.

  • convolution_block() / resnet_block() / FClayer_block() – Run the layers on the pre-encoded weights.

Full Example Source

ResNet20DataPreprocessed.cpp