From 34f9f610e7001dd84076c899797f4555bd4370ee Mon Sep 17 00:00:00 2001 From: James Sutton <1892175+zeryx@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:25:59 -0700 Subject: [PATCH] mlops fashion mnist automatic template (#232) * new template * update code to properly use .file api path * removed local example file --- templates/python39-mlops-fashionmnist/.adk | 0 .../algorithmia.conf | 5 +++ .../requirements.txt | 7 ++++ .../src/__ALGO__.py | 42 +++++++++++++++++++ .../src/__init__.py | 0 5 files changed, 54 insertions(+) create mode 100644 templates/python39-mlops-fashionmnist/.adk create mode 100644 templates/python39-mlops-fashionmnist/algorithmia.conf create mode 100644 templates/python39-mlops-fashionmnist/requirements.txt create mode 100644 templates/python39-mlops-fashionmnist/src/__ALGO__.py create mode 100644 templates/python39-mlops-fashionmnist/src/__init__.py diff --git a/templates/python39-mlops-fashionmnist/.adk b/templates/python39-mlops-fashionmnist/.adk new file mode 100644 index 00000000..e69de29b diff --git a/templates/python39-mlops-fashionmnist/algorithmia.conf b/templates/python39-mlops-fashionmnist/algorithmia.conf new file mode 100644 index 00000000..8b4f8899 --- /dev/null +++ b/templates/python39-mlops-fashionmnist/algorithmia.conf @@ -0,0 +1,5 @@ +{ + "username": "__USER__", + "algoname": "__ALGO__", + "langauge": "python3.9" +} diff --git a/templates/python39-mlops-fashionmnist/requirements.txt b/templates/python39-mlops-fashionmnist/requirements.txt new file mode 100644 index 00000000..b671de71 --- /dev/null +++ b/templates/python39-mlops-fashionmnist/requirements.txt @@ -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 diff --git a/templates/python39-mlops-fashionmnist/src/__ALGO__.py b/templates/python39-mlops-fashionmnist/src/__ALGO__.py new file mode 100644 index 00000000..5719f115 --- /dev/null +++ b/templates/python39-mlops-fashionmnist/src/__ALGO__.py @@ -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) diff --git a/templates/python39-mlops-fashionmnist/src/__init__.py b/templates/python39-mlops-fashionmnist/src/__init__.py new file mode 100644 index 00000000..e69de29b