1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| IMAGE_SIZE_CROPPED = 24 IMAGE_HEIGHT = 32 IMAGE_WIDTH = 32 IMAGE_DEPTH = 3
def map_decorator(func): def wrapper(steps, times, values, image, label): return tf.py_function( func, inp=(steps, times, values, image, label), Tout=(steps.dtype, times.dtype, values.dtype, image.dtype, tf.float32) ) return wrapper
@map_decorator def map_fun_with_time(steps, times, values, image, label): time.sleep(0.05)
map_enter = time.perf_counter()
image = tf.reshape(image,[IMAGE_DEPTH, IMAGE_HEIGHT, IMAGE_WIDTH]) image = tf.divide(tf.cast(tf.transpose(image,[1, 2, 0]),tf.float32),255.0)
label = tf.one_hot(label, 10)
distorted_image = tf.image.random_crop(image, [IMAGE_SIZE_CROPPED,IMAGE_SIZE_CROPPED,IMAGE_DEPTH]) distorted_image = tf.image.random_flip_left_right(distorted_image) distorted_image = tf.image.random_brightness(distorted_image, max_delta=63) distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8) distorted_image = tf.image.per_image_standardization(distorted_image)
map_elapsed = time.perf_counter() - map_enter
return tf.concat((steps, [["Map"]]), axis=0),\ tf.concat((times, [[map_enter, map_elapsed]]), axis=0),\ tf.concat((values, [values[-1]]), axis=0),\ distorted_image,\ label
@map_decorator def map_fun_test_with_time(steps, times, values, image, label): time.sleep(0.05)
map_enter = time.perf_counter()
image = tf.reshape(image,[IMAGE_DEPTH,IMAGE_HEIGHT,IMAGE_WIDTH]) image = tf.divide(tf.cast(tf.transpose(image,[1,2,0]),tf.float32),255.0) label = tf.one_hot(label,10) distorted_image = tf.image.resize(image, [IMAGE_SIZE_CROPPED,IMAGE_SIZE_CROPPED]) distorted_image = tf.image.per_image_standardization(distorted_image)
map_elapsed = time.perf_counter() - map_enter
return tf.concat((steps, [["Map"]]), axis=0),\ tf.concat((times, [[map_enter, map_elapsed]]), axis=0),\ tf.concat((values, [values[-1]]), axis=0),\ distorted_image,\ label
|