In [1]:
from IPython.display import Image
import graphlab
graphlab.canvas.set_target('ipynb')
Let's say you want to make an app which can recommend dresses to you based on a photo you took. You need a way to deploy the model previously built. Turi Predictive Services helps do this in an easy and scalable way. In this notebook, we demonstrate how do that for the dress recommender model.
In [8]:
reference_sf = graphlab.SFrame('data/sf_processed.sframe/')
pretrained_model = graphlab.load_model('data/imagenet_model')
nn_model = graphlab.load_model('data/nearest_dress_model')
In [135]:
reference_sf
Out[135]:
In [9]:
def dress_similar(url):
img = graphlab.Image(url)
image_sf = graphlab.SFrame()
image_sf['image'] = [img]
image_sf['features'] = pretrained_model.extract_features(image_sf)
ans = nn_model.query(image_sf, k=5)
return ans
In [11]:
QUERY_URL = 'http://static.ddmcdn.com/gif/blue-dress.jpg'
Image(QUERY_URL)
Out[11]:
In [58]:
def retrieve_image(nearest_neighbors_output, input_sframe):
joined = input_sframe.join(nearest_neighbors_output, on={'_id':'reference_label'})
sorted_sf = joined.sort('rank')
return sorted_sf['image']
In [85]:
images = retrieve_image(dress_similar(QUERY_URL), reference_sf)
In [87]:
images.show()
This section shows you how to deploy a Predictive Service to EC2. The EC2 instances used by the Predictive Service will be launched in your own AWS account, so you will be responsible for the cost.
similar_dress
function to the predictive service, so that it can be queried by the outside world. Note: In order to follow along here, you need AWS credentials. You can then uncomment the code below. Be sure that the deployment state path is in an s3 bucket you have access to.
In [88]:
import graphlab as gl
# Replace with your path.
ps_state_path = 's3://<your-bucket-name>/predictive_service/ps'
# Set your AWS credentials.
gl.aws.set_credentials(<key>, <secret>)
# Create an EC2 config
ec2_config = gl.deploy.Ec2Config()
# Launch a predictive service
ps = gl.deploy.predictive_service.create(name = 'sklearn-predictive-service',
ec2_config = ec2_config, state_path = ps_state_path, num_hosts = 1)
In [98]:
import graphlab as gl
ps = gl.deploy.predictive_service.load('s3://gl-demo-usw2/predictive_service/demolab/ps-1.6')
ps
Out[98]:
In [93]:
#ps.add('dress_similar', dress_similar)
ps.update('dress_similar', dress_similar)
ps.apply_changes()
In [99]:
ps.query('dress_similar', url=QUERY_URL)
Out[99]:
Query from anywhere. Here, we issue a request via the requests
library, and convert the returning JSON back into an SFrame. This could easily be done from outside of Python, though.
In [127]:
import json
import requests
def restful_query(url):
# Query the service.
headers = {'content-type': 'application/json' }
payload = {'api_key':'b437e588-0f2b-45e1-81c8-ce3acfa81ade', 'data':{'url':url}}
end_point = 'http://demolab-one-six-2015364754.us-west-2.elb.amazonaws.com/query/dress_similar'
resp = requests.post(end_point,json.dumps(payload), headers=headers)
# Join with existing data.
ans = gl.SArray(json.loads(resp.content)['response']).unpack('')
return retrieve_image(ans, reference_sf)
In [129]:
restful_query('http://static.ddmcdn.com/gif/blue-dress.jpg').show()