In [1]:
!conda list |grep -i torch
In [2]:
from torchvision.models.resnet import resnet50
In [3]:
model = resnet50(pretrained=True)
In [4]:
from clipper_admin import ClipperConnection, DockerContainerManager
In [5]:
clipper_conn = ClipperConnection(DockerContainerManager())
In [6]:
clipper_conn.start_clipper(cache_size=1) # Disable PredictionCache
In [7]:
!docker ps -a
In [8]:
import io
from PIL import Image
from torch.autograd import Variable
import torchvision.transforms as transforms
# Imported from https://gist.github.com/jkarimi91/d393688c4d4cdb9251e3f939f138876e.
min_img_size = 224
transform_pipeline = transforms.Compose([transforms.Resize(min_img_size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
def predict(model, inputs):
def _predict_one(one_input_arr):
try:
img = Image.open(io.BytesIO(one_input_arr))
if img.mode != "RGB":
img = img.convert("RGB")
img = transform_pipeline(img)
img = img.unsqueeze(0)
img = Variable(img)
return [model(img).data.numpy().argmax()]
except Exception as e:
print(e)
return []
return [_predict_one(i) for i in inputs]
In [9]:
import clipper_admin.deployers.pytorch as pytorch_deployer
In [10]:
app_name = 'pytorch-test-app'
model_name = 'pytorch-test-model'
In [11]:
pytorch_deployer.deploy_pytorch_model(clipper_conn=clipper_conn,
name=model_name,
version='1',
input_type='bytes',
func=predict,
pytorch_model=model,
num_replicas=1,
batch_size=1, # Disable adaptive batching policy
pkgs_to_install=['pillow'])
In [12]:
clipper_conn.register_application(name=app_name,
input_type="bytes",
default_output="-1.0",
slo_micros=10000000) # 10s
In [13]:
clipper_conn.link_model_to_app(app_name=app_name,
model_name=model_name)
In [14]:
import time
time.sleep(30)
In [15]:
!wget https://harishnarayanan.org/images/writing/artistic-style-transfer/output_13_0.png -O elephant.jpg
In [16]:
!wget http://kikei.github.io/images/plots/2018-08-05-rabbit2.jpg -O rabbit.jpg
In [17]:
from IPython.display import display
from PIL import Image
In [18]:
display(Image.open('elephant.jpg'))
In [19]:
display(Image.open('rabbit.jpg'))
In [20]:
import json
import base64
import requests
from datetime import datetime
In [21]:
headers = {'Content-type': 'application/json'}
url = "http://{addr}/{app_name}/predict".format(
addr=clipper_conn.get_query_addr(),
app_name=app_name)
In [22]:
start = datetime.now()
req_json = json.dumps({ "input": base64.b64encode(open('elephant.jpg', "rb").read()).decode() })
r = requests.post(url, headers=headers, data=req_json)
end = datetime.now()
In [23]:
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
In [24]:
start = datetime.now()
req_json = json.dumps({ "input": base64.b64encode(open('rabbit.jpg', "rb").read()).decode() })
r = requests.post(url, headers=headers, data=req_json)
end = datetime.now()
In [25]:
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
In [26]:
clipper_conn.stop_all()
In [27]:
!docker rm -f $(docker ps -a -q) && docker image prune -f
In [ ]: