-
Notifications
You must be signed in to change notification settings - Fork 607
Imagenet example #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Imagenet example #344
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f65a0d5
progress
1vn 522bf40
progress
1vn d6c6713
progress
1vn bdc6342
Merge branch 'master' into imagenet
1vn 57b0d41
predict a truck
1vn 959e7bf
format
2f0f635
merge master
68f1fb2
finish parenthesis
fb7a1c3
remove predownload
59acf4e
update example
2e5caa9
format
50dd6cb
update example to download links
87d8508
remove logs
f38d33f
support b64 or links
b53196d
Merge branch 'master' into imagenet
1e9349b
increase timeout
7023d17
format
577c7d6
merge master
854735d
merge master
31ed013
add back tabs
309a2d5
remove pillow install
f430676
Merge branch 'master' into imagenet
8743c62
address comments, delete notebook, can use python instead
b458878
remove requests
f8ec67f
format
385476c
leave .tolist
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- kind: deployment | ||
name: imagenet | ||
|
||
- kind: api | ||
name: classifier | ||
model: s3://cortex-examples/imagenet/1566492692 | ||
request_handler: imagenet.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import requests | ||
import numpy as np | ||
import base64 | ||
from PIL import Image | ||
from io import BytesIO | ||
import math | ||
|
||
|
||
labels = requests.get( | ||
"https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt" | ||
).text.split("\n") | ||
|
||
|
||
def pre_inference(sample, metadata): | ||
if "url" in sample: | ||
image = requests.get(sample["url"]).content | ||
elif "base64" in sample: | ||
image = base64.b64decode(sample["base64"]) | ||
|
||
decoded_image = np.asarray(Image.open(BytesIO(image)), dtype=np.float32) / 255 | ||
return {"images": [decoded_image.tolist()]} | ||
|
||
|
||
def post_inference(prediction, metadata): | ||
classes = prediction["response"]["classes"] | ||
return {"class": labels[np.argmax(classes)]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import tensorflow as tf | ||
import tensorflow_hub as hub | ||
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def | ||
|
||
export_dir = "/model/15692343342" | ||
builder = tf.saved_model.builder.SavedModelBuilder(export_dir) | ||
with tf.Session(graph=tf.Graph()) as sess: | ||
module = hub.Module("https://tfhub.dev/google/imagenet/inception_v3/classification/3") | ||
input_params = module.get_input_info_dict() | ||
image_input = tf.placeholder( | ||
name="images", dtype=input_params["images"].dtype, shape=input_params["images"].get_shape() | ||
) | ||
sess.run([tf.global_variables_initializer(), tf.tables_initializer()]) | ||
classes = module(image_input) | ||
|
||
signature = predict_signature_def(inputs={"images": image_input}, outputs={"classes": classes}) | ||
|
||
builder.add_meta_graph_and_variables( | ||
sess, ["serve"], signature_def_map={"predict": signature}, strip_default_attrs=True | ||
) | ||
|
||
builder.save() |
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.