Skip to content

Commit

Permalink
mlops fashion mnist automatic template (#232)
Browse files Browse the repository at this point in the history
* new template

* update code to properly use .file api path

* removed local example file
  • Loading branch information
zeryx authored Jul 22, 2022
1 parent 9811f11 commit 34f9f61
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
Empty file.
5 changes: 5 additions & 0 deletions templates/python39-mlops-fashionmnist/algorithmia.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"username": "__USER__",
"algoname": "__ALGO__",
"langauge": "python3.9"
}
7 changes: 7 additions & 0 deletions templates/python39-mlops-fashionmnist/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
algorithmia>=1.0.0,<2.0
datarobot-mlops==8.0.7
pyaml==21.10.1
pillow<9.0
torch==1.11.0
torchvision==0.12.0
numpy>=1.21
42 changes: 42 additions & 0 deletions templates/python39-mlops-fashionmnist/src/__ALGO__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from Algorithmia import ADK
from datarobot.mlops.mlops import MLOps
import torch
from torchvision import transforms
from time import time
import pandas as pd
from PIL import Image
from src.labels import labels_map
import os

# The model itself is stored in an Algorithmia Data Collection
def load(state):
# We load the model in this way to ensure that the model is loaded only once
model_path = state.client.file(os.environ['MODEL_PATH']).getFile(as_path=True)
state['model'] = torch.jit.load(model_path)
state['mlops'] = MLOps().init()
state['labels'] = labels_map
return state


# As we're using the ADK system, state is a dictionary that contains the model and the labels
# Input is expected to be an image file on the Algorithmia Data API; more information on the Data API can be found here.
# https://algorithmia.com/developers/data/hosted
def apply(input, state):
start_t = time()
local_img = state.client.file(input).getFile(as_path=True)
transform = transforms.Compose([transforms.Resize(28),
transforms.ToTensor()])
img = Image.open(local_img).convert('L')
tensor = transform(img)
output = state['model'].forward(tensor)
_, predicted = torch.max(output.data, 1)
output = torch.softmax(output, 0).reshape(1, -1)
prediction = state['labels'][int(predicted.item())]
end_t = time()
state['mlops'].report_deployment_stats(1, end_t - start_t)
state['mlops'].report_predictions_data(predictions=output.tolist())
return prediction


algorithm = ADK(apply, load)
algorithm.init(mlops=True)
Empty file.

0 comments on commit 34f9f61

Please sign in to comment.