Fast.ai PyTorch Serverless API (w/ AWS Lambda)
-
Install Serverless Framework via npm
npm i -g serverless@v1.27.3
-
Install python requirements plugin
sls plugin install -n serverless-python-requirements
-
Setup your model in
lib/models.py
so that it can be imported by the handler inapi/predict.py
as a method -
Define your class labels in
lib/labels.txt
with one label per line, for example:cat dog
-
Setup an AWS CLI profile if you don't have one already
-
Create an S3 Bucket that your profile can access and upload your state dictionary
-
Configure the
serverless.yml
### Change service name to whatever you please service: pytorch-serverless provider: ... ### set this to your deployment stage stage: dev ### set this to your aws region region: us-west-2 ### set this to your aws profile profile: slsadmin ... environment: ### set this to your S3 bucket name BUCKET_NAME: pytorch-serverless ### set this to your state dict filename STATE_DICT_NAME: dogscats-resnext50.h5 ### set this to your input image size IMAGE_SIZE: 224 ### set this to your image normalization stats IMAGE_STATS: ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ... variables: ### set this to your api version api_version: v0.0.1
-
You'll need to have Docker running on your machine when deploying. If you don't want to use Docker, you can just remove
dockerizePip: true
from theserverless.yml
Return prediction for a single image.
-
Headers
(required) X-API-KEY=[string] ### Your generated API Key
-
URL Parameters
(required) image_url=[url] ### URL of image to classify (optional) top_k=[integer] ### Number of top results to return (default: 3)
-
Success Response (200)
{ "predictions": [ { "label": "dog", "log": -0.00004426980376592837, "prob": 0.9999557137489319 }, { "label": "cat", "log": -10.025229454040527, "prob": 0.0000442688433395233 } ] }
-
Error Response (500)
{ "error": "Something went wrong...", "traceback": "..." }
Run function locally with params defined in tests/predict_event.json
AWS_PROFILE=yourProfile sls invoke local -f predict -p tests/predict_event.json
Deploy to AWS Lambda
sls -v deploy
Tail logs to console
sls logs -f predict -t