Skip to content

Commit 779321a

Browse files
committed
Update examples
1 parent 33e74aa commit 779321a

File tree

18 files changed

+34
-50
lines changed

18 files changed

+34
-50
lines changed

examples/pytorch/image-classifier/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ The `predict()` function will be triggered once per request. The AlexNet model r
3737
# predictor.py
3838

3939
def predict(sample, metadata):
40-
if "url" in sample:
41-
image = requests.get(sample["url"]).content
42-
elif "base64" in sample:
43-
image = base64.b64decode(sample["base64"])
44-
40+
image = requests.get(sample["url"]).content
4541
img_pil = Image.open(BytesIO(image))
4642
img_tensor = preprocess(img_pil)
4743
img_tensor.unsqueeze_(0)

examples/pytorch/image-classifier/predictor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import requests
2-
import base64
32
from PIL import Image
43
from io import BytesIO
54
from torchvision import transforms
@@ -21,11 +20,7 @@
2120

2221

2322
def predict(sample, metadata):
24-
if "url" in sample:
25-
image = requests.get(sample["url"]).content
26-
elif "base64" in sample:
27-
image = base64.b64decode(sample["base64"])
28-
23+
image = requests.get(sample["url"]).content
2924
img_pil = Image.open(BytesIO(image))
3025
img_tensor = preprocess(img_pil)
3126
img_tensor.unsqueeze_(0)

examples/pytorch/iris-classifier/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def init(metadata):
2525
# download the model from S3 (location specified in the metadata field of our api configuration)
2626
s3 = boto3.client("s3")
2727
bucket, key = re.match(r"s3:\/\/(.+?)\/(.+)", metadata["model"]).groups()
28-
s3.download_file(bucket, key, "iris_model.pth")
28+
s3.download_file(bucket, key, "weights.pth")
2929

30-
model.load_state_dict(torch.load("iris_model.pth"))
30+
model.load_state_dict(torch.load("weights.pth"))
3131
model.eval()
3232
```
3333

@@ -72,7 +72,7 @@ A `deployment` specifies a set of resources that are deployed together. An `api`
7272
path: src/predictor.py
7373
python_path: src/
7474
metadata:
75-
model: s3://cortex-examples/pytorch/iris-classifier/nn.pth
75+
model: s3://cortex-examples/pytorch/iris-classifier/weights.pth
7676
tracker:
7777
model_type: classification
7878
```

examples/pytorch/iris-classifier/cortex.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
path: src/predictor.py
88
python_path: src/
99
metadata:
10-
model: s3://cortex-examples/pytorch/iris-classifier/nn.pth
10+
model: s3://cortex-examples/pytorch/iris-classifier/weights.pth
1111
tracker:
1212
model_type: classification

examples/pytorch/iris-classifier/src/my_model.py renamed to examples/pytorch/iris-classifier/src/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from sklearn.datasets import load_iris
2-
from sklearn.model_selection import train_test_split
31
import torch
42
import torch.nn as nn
53
import torch.nn.functional as F
64
from torch.autograd import Variable
5+
from sklearn.datasets import load_iris
6+
from sklearn.model_selection import train_test_split
77
from sklearn.metrics import accuracy_score
88

99

@@ -54,4 +54,4 @@ def forward(self, X):
5454

5555
print("prediction accuracy {}".format(accuracy_score(test_y.data, predict_y.data)))
5656

57-
torch.save(model.state_dict(), "iris_model.pth")
57+
torch.save(model.state_dict(), "weights.pth")

examples/pytorch/iris-classifier/src/predictor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import torch
2-
from my_model import IrisNet
31
import boto3
42
import re
3+
import torch
4+
from model import IrisNet
55

66
labels = ["iris-setosa", "iris-versicolor", "iris-virginica"]
77

@@ -11,8 +11,8 @@
1111
def init(metadata):
1212
s3 = boto3.client("s3")
1313
bucket, key = re.match(r"s3:\/\/(.+?)\/(.+)", metadata["model"]).groups()
14-
s3.download_file(bucket, key, "iris_model.pth")
15-
model.load_state_dict(torch.load("iris_model.pth"))
14+
s3.download_file(bucket, key, "weights.pth")
15+
model.load_state_dict(torch.load("weights.pth"))
1616
model.eval()
1717

1818

examples/sklearn/mpg-estimation/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Deploy a Sklearn Linear Regression model
1+
# Deploy a scikit-learn linear regression model
22

33
This example shows how to deploy a sklearn linear regression model trained on the MPG dataset.
44

@@ -18,12 +18,11 @@ model = None
1818
def init(metadata):
1919
global model
2020

21-
# download the model from S3 (location specified in the metadata field of our api configuration)
21+
# download the model from S3 (path specified in the metadata field of our api configuration)
2222
s3 = boto3.client("s3")
2323
bucket, key = re.match(r"s3:\/\/(.+?)\/(.+)", metadata["model"]).groups()
24-
s3.download_file(bucket, key, "mpg.joblib")
25-
26-
model = load("mpg.joblib")
24+
s3.download_file(bucket, key, "linreg.joblib")
25+
model = load("linreg.joblib")
2726
```
2827

2928
### Predict
@@ -34,14 +33,15 @@ The `predict()` function will be triggered once per request. We extract the feat
3433
# predictor.py
3534

3635
def predict(sample, metadata):
37-
arr = [
36+
input_array = [
3837
sample["cylinders"],
3938
sample["displacement"],
4039
sample["horsepower"],
4140
sample["weight"],
4241
sample["acceleration"],
4342
]
44-
result = model.predict([arr])
43+
44+
result = model.predict([input_array])
4545
return np.asscalar(result)
4646
```
4747

@@ -60,7 +60,7 @@ A `deployment` specifies a set of resources that are deployed together. An `api`
6060
- kind: api
6161
name: mpg
6262
predictor:
63-
path: src/predictor.py
63+
path: predictor.py
6464
metadata:
6565
model: s3://cortex-examples/sklearn/mpg-estimation/linreg.joblib
6666
tracker:

examples/sklearn/mpg-estimation/cortex.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- kind: api
55
name: mpg
66
predictor:
7-
path: src/predictor.py
7+
path: predictor.py
88
metadata:
99
model: s3://cortex-examples/sklearn/mpg-estimation/linreg.joblib
1010
tracker:

examples/sklearn/mpg-estimation/src/model.py renamed to examples/sklearn/mpg-estimation/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sklearn.linear_model import LinearRegression
44
from sklearn.model_selection import train_test_split
55
from joblib import dump
6-
import boto3
6+
77

88
if __name__ == "__main__":
99
df = pd.read_csv(
@@ -21,4 +21,4 @@
2121

2222
regression_model = LinearRegression()
2323
regression_model.fit(X_train, y_train)
24-
dump(regression_model, "mpg.joblib")
24+
dump(regression_model, "linreg.joblib")
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from joblib import load
21
import boto3
3-
import numpy as np
42
import re
3+
import numpy as np
4+
from joblib import load
55

66

77
model = None
@@ -11,17 +11,18 @@ def init(metadata):
1111
global model
1212
s3 = boto3.client("s3")
1313
bucket, key = re.match(r"s3:\/\/(.+?)\/(.+)", metadata["model"]).groups()
14-
s3.download_file(bucket, key, "mpg.joblib")
15-
model = load("mpg.joblib")
14+
s3.download_file(bucket, key, "linreg.joblib")
15+
model = load("linreg.joblib")
1616

1717

1818
def predict(sample, metadata):
19-
arr = [
19+
input_array = [
2020
sample["cylinders"],
2121
sample["displacement"],
2222
sample["horsepower"],
2323
sample["weight"],
2424
sample["acceleration"],
2525
]
26-
result = model.predict([arr])
26+
27+
result = model.predict([input_array])
2728
return np.asscalar(result)

0 commit comments

Comments
 (0)