Guest User

Untitled

a guest
Sep 24th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. # Number of channels in the input image
  2. CHANNELS = 3
  3.  
  4. # Dimensions of resized images (input to the neural net)
  5. HEIGHT = 200
  6. WIDTH = 200
  7.  
  8. # A placeholder for a batch of images
  9. images_placeholder = tf.placeholder(dtype=tf.string, shape=(None,))
  10.  
  11. # The CloudML Prediction API always "feeds" the Tensorflow graph with
  12. # dynamic batch sizes e.g. (?,). decode_jpeg only processes scalar
  13. # strings because it cannot guarantee a batch of images would have
  14. # the same output size. We use tf.map_fn to give decode_jpeg a scalar
  15. # string from dynamic batches.
  16. def decode_and_resize(image_str_tensor):
  17. """Decodes jpeg string, resizes it and returns a uint8 tensor."""
  18.  
  19. image = tf.image.decode_jpeg(image_str_tensor, channels=CHANNELS)
  20.  
  21. # Note resize expects a batch_size, but tf_map supresses that index,
  22. # thus we have to expand then squeeze. Resize returns float32 in the
  23. # range [0, uint8_max]
  24. image = tf.expand_dims(image, 0)
  25. image = tf.image.resize_bilinear(
  26. image, [HEIGHT, WIDTH], align_corners=False)
  27. image = tf.squeeze(image, squeeze_dims=[0])
  28. image = tf.cast(image, dtype=tf.uint8)
  29. return image
  30.  
  31. decoded_images = tf.map_fn(
  32. decode_and_resize, images_placeholder, back_prop=False, dtype=tf.uint8)
  33.  
  34. # convert_image_dtype, also scales [0, uint8_max] -> [0, 1).
  35. images = tf.image.convert_image_dtype(decoded_images, dtype=tf.float32)
  36.  
  37. # Then shift images to [-1, 1) (useful for some models such as Inception)
  38. images = tf.sub(images, 0.5)
  39. images = tf.mul(images, 2.0)
  40.  
  41. # ...
  42.  
  43. inputs = {"image_bytes": images_placeholder.name}
  44. tf.add_to_collection("inputs", json.dumps(inputs))
  45.  
  46. {"image_bytes": {"b64": "dGVzdAo="}}
  47.  
  48. echo "{"image_bytes": {"b64": "`base64 image.jpg`"}}" > instances
  49.  
  50. gcloud beta ml predict --instances=instances --model=my_model
  51.  
  52. {"instances" : [{"image_bytes": {"b64": "dGVzdAo="}}]}
Add Comment
Please, Sign In to add comment