This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Add a resize option for inception package to avoid sending large data to online prediction #215
Merged
Merged
Changes from all commits
Commits
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
3,164 changes: 1,587 additions & 1,577 deletions
3,164
datalab/notebook/static/extern/lantern-browser.html
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| """Reusable utility functions. | ||
| """ | ||
|
|
||
| import collections | ||
| import google.cloud.ml as ml | ||
| import multiprocessing | ||
| import os | ||
|
|
@@ -162,6 +163,7 @@ def check_dataset(dataset, mode): | |
| raise ValueError('Invalid dataset. Expect only "image_url" or "image_url,label" ' + | ||
| 'STRING columns.') | ||
|
|
||
|
|
||
| def get_sources_from_dataset(p, dataset, mode): | ||
| """get pcollection from dataset.""" | ||
|
|
||
|
|
@@ -184,3 +186,54 @@ def get_sources_from_dataset(p, dataset, mode): | |
| return p | 'Read source from BigQuery (%s)' % mode >> beam.io.Read(bq_source) | ||
| else: | ||
| raise ValueError('Invalid DataSet. Expect CsvDataSet or BigQueryDataSet') | ||
|
|
||
|
|
||
| def decode_and_resize(image_str_tensor): | ||
| """Decodes jpeg string, resizes it and returns a uint8 tensor.""" | ||
|
|
||
| # These constants are set by Inception v3's expectations. | ||
| height = 299 | ||
| width = 299 | ||
| channels = 3 | ||
|
|
||
| image = tf.image.decode_jpeg(image_str_tensor, channels=channels) | ||
| # Note resize expects a batch_size, but tf_map supresses that index, | ||
| # thus we have to expand then squeeze. Resize returns float32 in the | ||
| # range [0, uint8_max] | ||
| image = tf.expand_dims(image, 0) | ||
| image = tf.image.resize_bilinear(image, [height, width], align_corners=False) | ||
| image = tf.squeeze(image, squeeze_dims=[0]) | ||
| image = tf.cast(image, dtype=tf.uint8) | ||
| return image | ||
|
|
||
|
|
||
| def resize_image(image_str_tensor): | ||
| """Decodes jpeg string, resizes it and re-encode it to jpeg.""" | ||
|
|
||
| image = decode_and_resize(image_str_tensor) | ||
| image = tf.image.encode_jpeg(image, quality=100) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could decode_and_resize do
so then decode_and_resize can be renamed resize_image
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decode_and_resize() is also used by model (it does not need to encode back) so this function can only do 1 and 2. |
||
| return image | ||
|
|
||
|
|
||
| def load_images(image_files, resize=True): | ||
| """Load images from files and optionally resize it.""" | ||
|
|
||
| images = [] | ||
| for image_file in image_files: | ||
| with ml.util._file.open_local_or_gcs(image_file, 'r') as ff: | ||
| images.append(ff.read()) | ||
| if resize is False: | ||
| return images | ||
|
|
||
| # To resize, run a tf session so we can reuse 'decode_and_resize()' | ||
| # which is used in prediction graph. This makes sure we don't lose | ||
| # any quality in prediction, while decreasing the size of the images | ||
| # submitted to the model over network. | ||
| image_str_tensor = tf.placeholder(tf.string, shape=[None]) | ||
| image = tf.map_fn(resize_image, image_str_tensor, back_prop=False) | ||
| feed_dict = collections.defaultdict(list) | ||
| feed_dict[image_str_tensor.name] = images | ||
| with tf.Session() as sess: | ||
| images_resized = sess.run(image, feed_dict=feed_dict) | ||
| return images_resized | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not 300? is there some kind of "start counting at zero" going on here? Add comment if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just Inception expects such dimension. Don't know the reason behind but there is already comments saying this is expected by Inception.