This is an example of how to deploy a PyTorch model on GCP AI platform.
Download a pre-trained model and labels into models/
dir:
mkdir models/
wget https://download.pytorch.org/models/resnet18-5c106cde.pth -P models/
wget https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json -P models/
First set up a few configurations. Make a bucket to upload packages and data beforehand.
NAME="TorchDemo"
VERSION_NAME="v1"
PACKAGE_VERSION="0.1"
ROOT_PATH="gs://bucket-name/torch-deploy"
MODEL_PATH="${ROOT_PATH}/models"
PACKAGE_PATH="${ROOT_PATH}/packages"
REGION="asia-northeast1"
First create a model:
gcloud ai-platform models create $NAME \
--regions $REGION \
--enable-logging
Prepare the package and model data for deploy:
# Package and upload the predictor.
python setup.py sdist
gsutil -m rsync -x "\..*" dist/ $PACKAGE_PATH/
# Upload any model data to the origin path.
gsutil -m rsync -x "\..*" models/ $MODEL_PATH/
Once the package and data are avail, deploy the model with the following:
gcloud beta ai-platform versions create $VERSION_NAME \
--model $NAME \
--runtime-version 1.15 \
--python-version 3.7 \
--origin $MODEL_PATH \
--package-uris ${PACKAGE_PATH}/predictor-${PACKAGE_VERSION}.tar.gz \
--prediction-class predictor.Predictor
To test if the deployed model works, create an input data in JSON file:
tee instances.json <<EOF
{"image_url": "https://..."}
EOF
Then make a prediction:
gcloud ai-platform predict \
--model $NAME \
--version $VERSION_NAME \
--json-instances instances.json
PyTorch CPU package is not available via standard PyPI index. See setup.py
for how to workaroud installation steps.