LeNet-5 FHE Implementation

This page demonstrates the LeNet-5 inference workflow using FHEON. It provides an example of how to set up the FHE context, configure the FHEONANNStandardController, and perform encrypted-domain inference on MNIST images using a LeNet-5 architecture.

Overview

The workflow includes:

  1. Context and Key Generation

    • Initializes the FHEONHEController with parameters such as ring degree, number of slots, circuit depth, and scaling factors.

    • Generates rotation keys for convolution, average pooling, and fully connected layers, then serializes them for optimized FHE operations.

  2. Data Preparation

    • Read and encode all the model weights and biases.

    • Reads and preprocesses MNIST images.

    • Encrypts the image data for inference.

  3. LeNet-5 Layers

    • Implements two convolutional layers (Conv1 and Conv2) followed by average pooling.

    • Implements three fully connected layers (FC1, FC2, FC3) for classification.

    • Applies encrypted-domain ReLU activations and bootstrapping where needed.

  4. Inference Loop

    • Iterates over all input MNIST images.

    • Sequentially applies convolution, pooling, ReLU, and fully connected layers.

    • Decrypts results and writes predicted labels to a file.

Key Functions

Controller functions used in this example:

  • he_convolution() – Convolution on encrypted data (valid, strided).

  • he_avgpool_optimzed() – 2x2 average pooling.

  • he_linear() – Fully connected layer.

  • he_relu() – Chebyshev approximation of ReLU.

  • bootstrap_function() – CKKS bootstrapping to refresh the ciphertext levels.

  • encode_kernel() – Encodes the convolution kernels.

  • encode_bais_input() – Encodes a bias vector.

  • encode_input() – Encodes weights or inputs as plaintexts.

Full Example Source

You can view and download the full source code of this example:

LeNet5.cpp