ResNet-20 Fine-Tuning (Standard Layout)
This page demonstrates one encrypted fine-tuning step of a pretrained ResNet-20 on a batch of 16 CIFAR-10 images, in the standard training layout (one channel of the batch per ciphertext).
Workflow
Context and keys. A bootstrappable CKKS context (ring
2^15, 2^14 slots) and one key set for all the convolution widths and strides, the backward dilations and the batch norm broadcasts.Forward pass. Stem, three stages of three basic blocks and the global average pooling + fully connected layer, with a bootstrapping before every primitive. Missing parameters are read from the PyTorch CSV exports (
pretrainedFromCsv).Loss. Softmax by repeated squaring (
he_forward_softmax_normalized), predictions and the cross-entropy loss.Backward pass. Cross-entropy + softmax gradient, fully connected and pooling gradients, then the blocks in reverse order, every parameter updated in place.
Precision
The model uses the multithreaded standard controllers with the numerics of Encrypted Training:
2-iteration bootstrapping with its precision argument (15 bits), batch norm with Newton started per channel from the
pretrained running variances (weights_folder + <layer>_running_var.csv; run with
--weights-folder ../weights/tuneresnet20/), and the repeated-squaring softmax.
Build and Run
cmake -DCMAKE_BUILD_TYPE=Release -DMODE=TRAINING ..
make -j$(nproc) tuneResNet20
./tuneResNet20 --weights-folder ../weights/tuneresnet20/ --index-value 0
Key Functions
The main functions used in this example include:
he_forward_convolution_optimized() / backward_pass_convolution()– 3x3 convolution with encrypted weights, stride 1 or 2.he_forward_convolution_shortcut_optimized() / backward_pass_convolution_shortcut()– 1x1 stride-2 shortcut.he_forward_batch_norm() / backward_pass_batch_norm()– Batch normalization with trainable gamma and beta.he_forward_relu() / backward_pass_relu()– ReLU and its derivative (Chebyshev approximations).he_forward_global_avgpool() / backward_pass_global_avgpool()– Global average pooling.he_forward_linear_multiple_outputs() / backward_pass_fully_connected()– Fully connected layer.he_forward_softmax_normalized() / backward_pass_cross_entropy_and_softmax()– Softmax and the loss gradient.
Full Example Source
You can view and download the full source code of this example: