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

Image Sharpening with LatticaAI Demo Tutorial

PreviousDemo TutorialsNextSentiment Analysis with LatticaAI Demo Tutorial

Last updated 25 days ago

Was this helpful?

Overview of the Model

Our Image Sharpening model enhances the clarity and detail of an input image by applying a specialized 2D filter through convolution.

  • Input Format: RGB image tensor of shape (3, 200, 200), with pixel values in the [0,1] range.

  • Output: Sharpened image preserving original dimensions.

The equivalent pytorch code for the operator is:

import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np


# Load image as numpy ndarray (ignore alpha channel if it exists)
np_img = plt.imread('my_img.png')[..., :3]  # shape format (H, W, C)
assert np_img.shape[-1] == 3, "Image must have 3 channels"

# Normalize pixel values
if np_img.dtype == np.uint8:
  np_img = np_img / 255.0

# Convert to PyTorch tensor and arrange dimensions as (C, H, W)
pt = torch.tensor(np_img, dtype=torch.float64).permute(2, 0, 1)
# Resize image to expected input size
pt = F.interpolate(pt.unsqueeze(0), size=(200, 200), mode='bilinear').squeeze(0)  # shape (3, 200, 200)

# Define sharpening kernel
sharpen_kernel = torch.tensor([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=torch.float64)

# Apply convolution for each channel separately (3 for RGB)
res = F.conv2d(pt, sharpen_kernel.expand(3, 1, 3, 3), groups=3, padding=1)
# Clamp to range [0, 1]
res = torch.clamp(res, 0, 1)

# Display the original and sharpened images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(pt.permute(1, 2, 0))
plt.axis("off")

plt.subplot(1, 2, 2)
plt.title("Sharpened Image")
plt.imshow(res.permute(1, 2, 0))
plt.axis("off")

plt.tight_layout()
plt.show()

Achieving Full Privacy with LatticaAI

In order to convert this simple code to use homomorphic operations, all you need are the following few extra steps:

  1. Install Lattica python package and obtain a JWT token

  2. Generate encryption keys

  3. Replace the actual convolution with our function that will:

    1. preprocess the image and encrypt it

    2. send the encrypted data to the cloud for computation

    3. receive and decrypt the encrypted result using your private key

Everything else remains the same.

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

model_id = "imageEnhancement"
my_token = get_demo_token(model_id)

client = QueryClient(my_token)

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

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

# Display the original and sharpened images...
import { getDemoToken, LatticaQueryClient } from '@Lattica-ai/lattica-query-client';

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

const client = new LatticaQueryClient(myToken);

await client.init();

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 imageEnhancement model ID

step-by-step guide
install