< Tensorflow >Tensorflow Data Augmentation YUV distortation

Method

This method augment image by disturbing the YUV field of the target image and then transform back to RGB field.

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
def all_yuv_tur(image_in, ratio=1.0):

def yuv_tur(image , case):
image = image / 255
image = tf.image.rgb_to_yuv(image)
yuv_channels = tf.split(image, 3, 2)
y = yuv_channels[0]
uv = tf.concat(yuv_channels[1:],2)
shape1 = uv.get_shape().as_list()

level=int(8*ratio)


d1 = tf.random_uniform([], maxval=level, dtype=tf.int32)
d2 = tf.random_uniform([], maxval=level, dtype=tf.int32)

if case == 0:
uv = tf.slice(uv, [0,0,0], [shape1[0]-d1, shape1[1]-d2, shape1[2]])
uv = tf.pad(uv, [[d1,0], [d2, 0], [0,0]])
elif case == 1:
uv = tf.slice(uv, [d1,d2,0], [shape1[0]-d1, shape1[1]-d2, shape1[2]])
uv = tf.pad(uv, [[0, d1], [0, d2], [0,0]])
elif case == 2:
uv = tf.slice(uv, [d1,0,0], [shape1[0]- d1, shape1[1]-d2, shape1[2]])
uv = tf.pad(uv, [[0, d1], [d2, 0], [0,0]])
elif case == 3:
uv = tf.slice(uv, [0,d2,0], [shape1[0]-d1, shape1[1]-d2, shape1[2]])
uv = tf.pad(uv, [[d1, 0], [0, d2], [0,0]])


yuv_image = tf.concat([y, uv], 2)
image = tf.image.yuv_to_rgb(yuv_image)
image = image * 255
return image


def yuv_tur_sel(image):
image = apply_with_random_selector(image,
lambda x, ordering: yuv_tur(x, ordering), num_cases=4)
return image


ran = tf.random_uniform([])
image = tf.cond(ran<0.1, lambda: yuv_tur_sel(image_in), lambda: image_in)
return image

The images below are the origin image and transfered image:

< Tensorflow >Tensorflow Data Augmentation YUV distortation

https://zhengtq.github.io/2018/12/02/tensorflow-disturb-yuv/

Author

Billy

Posted on

2018-12-02

Updated on

2021-03-13

Licensed under

Comments