LatticaAI Documentation
  • Welcome to LatticaAI
  • Conceptual Guide
  • Architecture Overview
    • Management Client
    • Query Client
  • Platform Workflows
    • Account Management
    • Model Management
    • User Access Management
    • Query Submission
    • Credit Management
    • Worker Management
  • How-To Guides
    • Client Installation
      • How-To: Install Management Client
      • How-To: Install Query Client
    • Model Lifecycle
      • How-To: Deploy AI model
      • How-To: Modify AI Model Settings
    • Access Control
      • How-To: Create User Access Token
      • How-To: Modify User Access Token Setting
      • How-To: Remove Token's Assignment
      • How-To: Assign Token to Model
      • How-To: See List of Tokens
    • Resource Management
      • How-To: Start Worker
      • How-To: Stop Worker
      • How-To: Monitor Worker Performance
    • Secure Query Processing
      • How To: Upload Evaluation Key
      • How-To: Encrypt Input Message
      • How To: Execute Query
      • How-To: Decrypt Output Data
      • How-To: Encrypt, Execute, and Decrypt in One Step
    • Account and Finance Operations
      • How-To: View Payment Transaction History
      • How-To: Update Account Information
      • How-To: View Credit Balance and Add Credit to Your Account
      • How-To: Monitor Balance and Usage
  • Demo Tutorials
    • Image Sharpening with LatticaAI Demo Tutorial
    • Sentiment Analysis with LatticaAI Demo Tutorial
    • Health Analysis with LatticaAI Demo Tutorial
    • Digit Recognition with LatticaAI Demo Tutorial
    • Zooming Into Each Step of Demo Run with LatticaAI flow
Powered by GitBook
On this page
  • Overview of the Model
  • Achieving Full Privacy with LatticaAI

Was this helpful?

  1. Demo Tutorials

Digit Recognition with LatticaAI Demo Tutorial

PreviousHealth Analysis with LatticaAI Demo TutorialNextZooming Into Each Step of Demo Run with LatticaAI flow

Last updated 2 months ago

Was this helpful?

Overview of the Model

Our Digit recognition model is trained on the . This dataset is a collection of grayscale images of handwritten digits (0–9), each 28×28 pixels in size. We added some preprocessing and data augmentations to the training data, for better performance on real world sketches of handwritten digits.

The model architecture is FCNN (fully-connected neural network):

  1. Input Layer: flattens the 28x28 image into a 784-dimensional vector.

  2. Hidden Layer: a fully connected layer with 50 neurons and square activation.

  3. Output Layer: a fully connected layer with 10 neurons (one for each digit) and a softmax activation.

The reason we chose to use square activation instead of the popular ReLU activation, is that homomorphic operation are better suited for polynomial operators, and square is the simplest and lowest degree non-linear operator that we can use as layer activation.

Here is a sample code for inferring digit from an image using the trained model:

import torch
import matplotlib.pyplot as plt


def inference(l1_weight, l1_bias, l2_weight, l2_bias, x):
    x = x.flatten()
    x = l1_weight @ x + l1_bias
    x = x ** 2
    x = l2_weight @ x + l2_bias
    return torch.nn.functional.softmax(x, dim=0)


# load model weights
model_dict = torch.load("digits_recognizer.pth", map_location="cpu")

# digit inference
img = plt.imread("digit.png")[..., 0]
pt = torch.tensor(img)

res = inference(
    model_dict["fc1.weight"], model_dict["fc1.bias"],
    model_dict["fc2.weight"], model_dict["fc2.bias"],
    pt,
)

plt.figure()
plt.imshow(img)
plt.title(f"Prediction: {res.argmax().item()}")
plt.axis("off")

plt.show()

Achieving Full Privacy with LatticaAI

import torch
import matplotlib.pyplot as plt

from lattica_query.auth import get_demo_token
from lattica_query.lattica_query_client import QueryClient


model_id = "sketchToNumber"
my_token = get_demo_token(model_id)

client = QueryClient(my_token)

context, secret_key, client_blocks, = client.generate_key()

# SECURE digit inference
img = plt.imread("digit.png")[..., 0]
pt = torch.tensor(img)

# `res` is torch.Tensor, same as in the plain example above
res = client.run_query(context, secret_key, pt, client_blocks)

# Display the image and prediction as above...
import { getDemoToken, LatticaQueryClient } from '@Lattica-ai/lattica-query-client';

const modelId = "sketchToNumber"
const token = await getDemoToken(modelId);

const client = new LatticaQueryClient(myToken);

await client.init();

// pt is an mnist image in the form of number[], as in the python example above
const result = await client.runQuery(pt);

First our client package

See our for a detailed explanation of each step in this flow. To use the image sharpening model use the sketchToNumber model ID

step-by-step guide
MNIST dataset
264B
digit.png
image
157KB
digits_recognizer.pth
Digit Recognition flow
model architecture
install