Skip to content

Commit d46cd24

Browse files
committed
Add integration test
1 parent c49e9f8 commit d46cd24

File tree

7 files changed

+1107
-0
lines changed

7 files changed

+1107
-0
lines changed

tests/data/serverless/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM public.ecr.aws/lambda/python:3.8
2+
3+
COPY requirements.txt /tmp/requirements.txt
4+
RUN pip3 install -r /tmp/requirements.txt
5+
6+
COPY model.py .
7+
RUN python model.py
8+
9+
COPY app.py .
10+
COPY classes.txt .
11+
12+
CMD ["app.handler"]

tests/data/serverless/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This folder contains the source code for the image used in the
2+
`sagemaker.serverless` tests.
3+
4+
The image contains the code for a Lambda handler and all of its dependencies.
5+
The Lambda handler predicts the class of an image using a pre-trained ResNet-34
6+
model.

tests/data/serverless/app.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import urllib
2+
import json
3+
import os
4+
5+
import torch
6+
from PIL import Image
7+
from torchvision import models
8+
from torchvision import transforms
9+
10+
transform = transforms.Compose(
11+
[
12+
transforms.Resize(256),
13+
transforms.CenterCrop(224),
14+
transforms.ToTensor(),
15+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
16+
]
17+
)
18+
19+
with open("classes.txt") as file:
20+
classes = [s.strip() for s in file.readlines()]
21+
22+
23+
def handler(event, context):
24+
data = urllib.request.urlopen(event["url"])
25+
26+
image = Image.open(data)
27+
image = transform(image)
28+
image = image.unsqueeze(0)
29+
30+
model = torch.jit.load("./model.pt")
31+
outputs = model(image)
32+
target = outputs.argmax().item()
33+
34+
return {"class": classes[target]}

0 commit comments

Comments
 (0)