BResNet-20 Batch Implementations

This page demonstrates the ResNet-20 batch inference workflow using FHEON: encrypted-domain inference on a batch of CIFAR-10 images, with every ciphertext holding one channel of the whole batch.

Overview

The workflow includes:

  1. Context and Key Generation

    • Initializes the FHEONHEController with the ring dimension, number of slots, circuit depth and scaling factors.

    • Generates one key file per stage (layer1.bin, layer2.bin, …, the input converter and the fully connected layer), each with exactly the rotations of its layers and the bootstrapping keys of its slot count; only the keys of the current stage are loaded during the inference.

  2. Batch Data Preparation

    • Reads and preprocesses a batch of CIFAR-10 images.

    • Encrypts one ciphertext per channel (image after image) and converts it to the batch layout (he_batch_inputs_converter_memory_efficient).

  3. ResNet-20 Blocks

    • Convolution blocks, residual blocks with identity shortcuts and stride-2 blocks with 1x1 shortcut convolutions, with the multithreaded FHEONANNStandardBatchThreadedController (see FHEON’s Controllers).

    • Batch ReLU and bootstrapping of all the channels of a layer (batch_bootstrap_function).

  4. Classification

    • Encrypted global average pooling and the fully connected layer for the whole batch.

    • Decrypts the batch results and writes the predicted labels.

Batch Configurations

  • BResNet20N16.cpp – batch of 16 images

  • BResNet20N32.cpp – batch of 32 images

  • BResNet20N64.cpp – batch of 64 images

  • BResNet20N128.cpp – batch of 128 images

  • BResNet20N256.cpp – batch of 256 images

  • BResNet20N512.cpp – batch of 512 images

Key Functions

The main functions used in the examples include:

  • convolution_block() – Batch 3x3 convolution (he_batch_convolution_optimized), stride 1 or 2.

  • shortcut_convolution_block() – 1x1 stride-2 shortcut convolution (he_batch_convolution_shortcut_optimized).

  • resnet_block() – A basic block: two convolutions with bootstrapping and batch ReLU, the identity or 1x1 shortcut, and the residual sum (he_batch_sum_ciphertexts).

  • fc_layer_block() – Fully connected layer of the whole batch (he_batch_linear), after the global average pooling (he_batch_globalpool).

Full Example Source

You can view and download the full source code of these examples:

BResNet20N16.cpp

BResNet20N32.cpp

BResNet20N64.cpp

BResNet20N128.cpp

BResNet20N256.cpp

BResNet20N512.cpp