In [1]:
from keras.applications.resnet50 import ResNet50
In [2]:
# https://keras.io/applications/#classify-imagenet-classes-with-resnet50
model = ResNet50(weights='imagenet')
In [3]:
from clipper_admin import ClipperConnection, DockerContainerManager
In [4]:
clipper_conn = ClipperConnection(DockerContainerManager())
In [5]:
clipper_conn.start_clipper(cache_size=1) # Disable PredictionCache
In [6]:
!docker ps -a
In [7]:
import io
import numpy as np
from PIL import Image
from keras.preprocessing.image import img_to_array
from keras.applications.resnet50 import preprocess_input, decode_predictions
def predict(model, inputs):
def _predict_one(one_input_arr):
try:
image = Image.open(io.BytesIO(one_input_arr))
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize((224, 224))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)
return decode_predictions(preds=model.predict(image), top=3)[0]
except Exception as e:
print(e)
return []
return [_predict_one(i) for i in inputs]
In [8]:
import clipper_admin.deployers.keras as keras_deployer
In [9]:
app_name = 'keras-test-app'
model_name = 'keras-test-model'
In [10]:
keras_deployer.deploy_keras_model(clipper_conn=clipper_conn,
name=model_name,
version='1',
input_type='bytes',
func=predict,
model_path_or_object=model,
num_replicas=1,
batch_size=1, # Disable adaptive batching policy
pkgs_to_install=['pillow'])
In [11]:
clipper_conn.register_application(name=app_name,
input_type="bytes",
default_output="-1.0",
slo_micros=10000000) # 10s
In [12]:
clipper_conn.link_model_to_app(app_name=app_name,
model_name=model_name)
In [13]:
import time
time.sleep(30)
In [14]:
!wget https://harishnarayanan.org/images/writing/artistic-style-transfer/output_13_0.png -O elephant.jpg
In [15]:
!wget http://kikei.github.io/images/plots/2018-08-05-rabbit2.jpg -O rabbit.jpg
In [16]:
from IPython.display import display
from PIL import Image
In [17]:
display(Image.open('elephant.jpg'))
In [18]:
display(Image.open('rabbit.jpg'))
In [19]:
import json
import base64
import requests
from datetime import datetime
from keras.preprocessing import image
In [20]:
headers = {'Content-type': 'application/json'}
url = "http://{addr}/{app_name}/predict".format(
addr=clipper_conn.get_query_addr(),
app_name=app_name)
In [21]:
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 [22]:
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
In [23]:
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 [24]:
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
In [25]:
clipper_conn.stop_all()
In [26]:
!docker rm -f $(docker ps -a -q) && docker image prune -f