ResNet-34 Optimized Implementation

This page demonstrates the ResNet-34 inference workflow using FHEON. It highlights a high-performance setup with optimized key generation, slot management, and ciphertext bootstrapping for encrypted inference on CIFAR-100 images.

Overview

The workflow consists of:

  1. Context and Key Generation

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

    • Generates rotation keys for each convolutional block and the fully connected layer.

    • Rotation keys are serialized and reloaded per block to reduce memory usage.

  2. Data Preparation

    • Loads CIFAR-100 test images in binary format.

    • Preprocesses, normalizes, and encrypts image vectors.

  3. ResNet-34 Layers - Implements convolutional, residual, and shortcut blocks. - Bootstrapping is applied selectively between stages to refresh ciphertext precision. - Slot sizes are adjusted dynamically across layers for performance. - Final classification is performed with a fully connected layer.

  4. Inference Loop

    • Iterates over the encrypted dataset.

    • Sequentially applies convolutional blocks, residual connections, ReLU activations, global average pooling, and the final fully connected layer.

    • Decrypts predictions and writes outputs to file.

Key Functions

  • convolution_block() – Standard encrypted convolution with bias addition.

  • shortcut_convolution_block() – Implements residual shortcut connections.

  • double_shortcut_convolution_block() – Optimized downsampling with dual convolution + shortcut.

  • resnet_block() – Full ResNet residual block with bootstrapping and ReLU.

  • FClayer_block() – Fully connected encrypted layer for classification.

Optimizations

  • Rotation Key Management: Keys are generated once, serialized, and loaded per block.

  • Dynamic Slot Handling: Slot size is reduced after downsampling and restored for fully connected layers.

  • Bootstrapping Strategy: Applied only at critical depth points to maintain precision.

  • Efficient Memory Use: Encoded kernels and biases are cleared after use to reduce memory overhead.

Full Example Source

You can view the full optimized ResNet-34 implementation here:

ResNet34FHE.cpp