-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mlops fashion mnist automatic template (#232)
* new template * update code to properly use .file api path * removed local example file
- Loading branch information
Showing
5 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"username": "__USER__", | ||
"algoname": "__ALGO__", | ||
"langauge": "python3.9" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.