Skip to content

base64 image encoding

Szymon Maszke edited this page Apr 14, 2020 · 5 revisions

torchlambda supports base64 encoding out-of-the box. This approach is recommended in order to speed up data passing (if only feasible) and should be your go-to for image-related tasks (classification, style transfer etc.).

1. Neural network

In this tutorial we assume you already have exported your PyTorch model to torchscript and have is saved as model.ptc. If that's not the case please follow first step of ResNet18 deployment on AWS Lambda and come back after that.

2. Defining input

Following YAML settings will allow you to use base64 encoding. Save contents below as torchlambda.yaml (or any other, just remember to pass it appropriately to functions in the next steps):

---
input:
  name: data
  type: base64
  validate: true
  shape: [1, 3, width, height]
  cast: float
  divide: 255
normalize:
  means: [0.485, 0.456, 0.406]
  stddevs: [0.229, 0.224, 0.225]
return:
  output:
    type: double
    name: output
    item: false

Please notice that all you have to do is specify input type as base64 and you are good to. If you aren't sure what each field does, see YAML settings file reference or introductory tutorial mentioned previously.

Also notice you may want to modify those settings a little bit (e.g. shape and return) to fit your exact use case.

3. Create example payload

Python code below will create a base64 encoded random image of shape [1, 3, 1024, 1024] with all necessary fields specified and save it in the file called payload.json in your current working directory.

import base64
import json
import struct

import numpy as np


def create_payload():
    width = 1024
    height = 1024
    data = np.random.randint(low=0, high=255, size=(1, 3, width, height)).flatten()
    data = base64.b64encode(
        struct.pack("<{}B".format(len(data)), *(data.tolist()))
    ).decode()

    payload = {"width": width, "height": height, "data": data}

    with open("payload.json", "w") as file:
        json.dump(payload, file)


if __name__ == "__main__":
    create_payload()

4. Deploying your network

You can either start deployment on AWS Lambda or test it locally. For the first case follow the tutorial ResNet18-deployment-on-AWS-Lambda from step 5. For the second option just follow Test-Lambda-deployment-locally.