Skip to content

Instantly share code, notes, and snippets.

@lakshmanok
Created August 28, 2018 16:09
Show Gist options
  • Select an option

  • Save lakshmanok/3161e71cd873f5c30fa0a1b46c84b31c to your computer and use it in GitHub Desktop.

Select an option

Save lakshmanok/3161e71cd873f5c30fa0a1b46c84b31c to your computer and use it in GitHub Desktop.
def read_and_preprocess(example_data):
parsed = tf.parse_single_example(example_data, {
'image/encoded': tf.FixedLenFeature((), tf.string, ''),
'image/class/label': tf.FixedLenFeature([], tf.int64, 1),
})
image_bytes = tf.reshape(parsed['image/encoded'], shape=[])
label = tf.cast(
tf.reshape(parsed['image/class/label'], shape=[]), dtype=tf.int32) - 1
# end up with pixel values that are in the -1, 1 range
image = tf.image.decode_jpeg(image_bytes, channels=NUM_CHANNELS)
image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 0-1
image = tf.expand_dims(image, 0) # resize_bilinear needs batches
image = tf.image.resize_bilinear(
image, [HEIGHT + 10, WIDTH + 10], align_corners=False)
image = tf.squeeze(image) # remove batch dimension
image = tf.random_crop(image, [HEIGHT, WIDTH, NUM_CHANNELS])
image = tf.image.random_flip_left_right(image)
image = tf.image.random_brightness(image, max_delta=63.0 / 255.0)
image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
#pixel values are in range [0,1], convert to [-1,1]
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
return image, label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment