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 FHEController 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

  • secure_optimized_convolution() – Convolution on encrypted data.

  • secure_double_optimized_convolution_multi_channels() – Residual block with shortcut.

  • secure_relu() – Secure ReLU activation.

  • secure_flinear() – Fully connected layer on encrypted data.

  • bootstrap_function() – Optional CKKS bootstrapping.

  • optimized_encode_kernel() / encode_baisData() / encode_inputData() – Encode plaintext weights and inputs for FHE operations.

  • read_images() – Load and preprocess CIFAR-10 images.

Data Processing Functions

  • convolution_data_processing() – Prepare convolutional kernel and bias data.

  • double_data_processing() – Prepare residual block kernel and shortcut data.

  • shorcut_data_processing() – Prepare downsampled shortcut connections.

  • fclayer_data_processing() – Prepare fully connected layer weights and bias.

Block Functions

  • convolution_block() – Perform a single convolution layer.

  • resnet_block() – Apply a residual block with optional shortcut and bootstrapping.

  • FClayer_block() – Apply fully connected layer on encrypted features.

Full Example Source

ResNet20DataPreprocessed.cpp