Deploy Image classification on GCP as cloud function.
Cloud function has good response time and ability to scale. These can be configured accordingly using requirements.txt.
For more understanding, this post describes how TF is used for deploying classification model.
This app uses imagenet pre-trained model resnet-18 available in torchvision to give image classification according to 1000 predefined categories in imagenet_class_index.json.
- clone this repo
 - cd 
$REPO;zip -r torch_gcp_fn.zip . 
This will make a zip file of this repo.
- On GCP console, go to 
Cloud Functionsand clickCreate Function. - Fill 
nameas desired, add memory2GB, set TrigerHTTP. - Under 
Source code, selectZip upload. - Select Runtime as 
Python3 - set 
functions to executetohandler - Expand 
Environment variables, networking, timeouts and more - set 
Timeoutto max as540 - click create
 
- once the function is launched correctly, there will be green tick
 - to start inference and testing, run following with updated values
 
import requests
function_url = "" # add url path to cloud function previously created
file_path = "" # add local image path 
resp = requests.post(function_url, files={"file":open(file_path,'rb')})
print(resp.json())In the requirements.txt, to install pytorch full path to wheel is given, instead if only torch was written it would have given errors. Common error encountered was code3, INVALID_ARGUMENT.