<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Prerequisites
pip install clipper_admin==0.4.1 \
            torch==1.0.1 \
            torchvision==0.2.2 \
            numpy==1.16.4 \
            pandas==0.24.2 \
            pillow==6.0.0
Step1:  Load a pretrained PyTorch model(ResNet50)
Step2:  Initialize the Clipper cluster
Step3:  Define 'predict' function
Step4:  Deploy PyTorch model and 'predict' function to the Clipper cluster
Step5:  Download sample images
Step6:  Send some requests to the Clipper cluster
Step7:  First request, which is so slow due to downloading a file from https
Step8:  Second request, which is moderate!
Step9:  Clean-up
<ASSISTANT_TASK:>
Python Code:
!conda list |grep -i torch
Explanation: Prerequisites
pip install clipper_admin==0.4.1 \
            torch==1.0.1 \
            torchvision==0.2.2 \
            numpy==1.16.4 \
            pandas==0.24.2 \
            pillow==6.0.0
End of explanation
from torchvision.models.resnet import resnet50
model = resnet50(pretrained=True)
Explanation: Load a pretrained PyTorch model(ResNet50)
End of explanation
from clipper_admin import ClipperConnection, DockerContainerManager
clipper_conn = ClipperConnection(DockerContainerManager())
clipper_conn.start_clipper(cache_size=1)  # Disable PredictionCache
!docker ps -a
Explanation: Initialize the Clipper cluster
End of explanation
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]
Explanation: Define 'predict' function
End of explanation
import clipper_admin.deployers.pytorch as pytorch_deployer
app_name = 'pytorch-test-app'
model_name = 'pytorch-test-model'
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'])
clipper_conn.register_application(name=app_name,
                                  input_type="bytes",
                                  default_output="-1.0",
                                  slo_micros=10000000)  # 10s
clipper_conn.link_model_to_app(app_name=app_name,
                               model_name=model_name)
import time
time.sleep(30)
Explanation: Deploy PyTorch model and 'predict' function to the Clipper cluster
End of explanation
!wget https://harishnarayanan.org/images/writing/artistic-style-transfer/output_13_0.png -O elephant.jpg
!wget http://kikei.github.io/images/plots/2018-08-05-rabbit2.jpg -O rabbit.jpg
from IPython.display import display
from PIL import Image
display(Image.open('elephant.jpg'))
display(Image.open('rabbit.jpg'))
Explanation: Download sample images
End of explanation
import json
import base64
import requests
from datetime import datetime
headers = {'Content-type': 'application/json'}
url = "http://{addr}/{app_name}/predict".format(
    addr=clipper_conn.get_query_addr(),
    app_name=app_name)
Explanation: Send some requests to the Clipper cluster
End of explanation
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()
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
Explanation: First request, which is so slow due to downloading a file from https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json.
End of explanation
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()
latency = (end - start).total_seconds() * 1000.0
print("'%s', %f ms" % (r.text, latency))
Explanation: Second request, which is moderate!
End of explanation
clipper_conn.stop_all()
!docker rm -f $(docker ps -a -q) && docker image prune -f
Explanation: Clean-up
End of explanation
<END_TASK>