-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_augment.py
104 lines (82 loc) · 2.82 KB
/
image_augment.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy as np
from scipy.misc import imresize
from scipy.ndimage.interpolation import rotate
def subtract(image):
image = image / 255
return image
def normalize(image):
return image
def center_crop(image, crop_size):
h, w, _ = image.shape
top = (h - crop_size[0]) // 2
left = (w - crop_size[1]) // 2
bottom = top + crop_size[0]
right = left + crop_size[1]
image = image[top:bottom, left:right, :]
return image
def random_crop(image, crop_size):
h, w, _ = image.shape
top = np.random.randint(0, h - crop_size[0])
left = np.random.randint(0, w - crop_size[1])
bottom = top + crop_size[0]
right = left + crop_size[1]
image = image[top:bottom, left:right, :]
return image
def horizontal_flip(image, rate=0.5):
if np.random.rand() < rate:
image = image[:, ::-1, :]
return image
def vertical_flip(image, rate=0.5):
if np.random.rand() < rate:
image = image[::-1, :, :]
return image
def scale_augmentation(image, scale_range, crop_size):
scale_size = np.random.randint(*scale_range)
image = imresize(image, (scale_size, scale_size))
image = random_crop(image, crop_size)
return image
def random_rotation(image, angles=[90, -90]):
h, w, _ = image.shape
angle = np.random.choice(angles)
image = rotate(image, angle)
image = imresize(image, (h, w))
return image
def cutout(image_origin, mask_size, mask_value='mean'):
image = np.copy(image_origin)
if mask_value == 'mean':
mask_value = image.mean()
elif mask_value == 'random':
mask_value = np.random.randint(0, 256)
h, w, _ = image.shape
top = np.random.randint(0 - mask_size // 2, h - mask_size)
left = np.random.randint(0 - mask_size // 2, w - mask_size)
bottom = top + mask_size
right = left + mask_size
if top < 0:
top = 0
if left < 0:
left = 0
image[top:bottom, left:right, :].fill(mask_value)
return image
def random_erasing(image_origin, p=0.5, s=(0.02, 0.4), r=(0.3, 3), mask_value='random'):
image = np.copy(image_origin)
if np.random.rand() > p:
return image
if mask_value == 'mean':
mask_value = image.mean()
elif mask_value == 'random':
mask_value = np.random.randint(0, 256)
h, w, _ = image.shape
mask_area = np.random.randint(h * w * s[0], h * w * s[1])
mask_aspect_ratio = np.random.rand() * r[1] + r[0]
mask_height = int(np.sqrt(mask_area / mask_aspect_ratio))
if mask_height > h - 1:
mask_height = h - 1
mask_width = int(mask_aspect_ratio * mask_height)
if mask_width > w - 1:
mask_width = w - 1
top = np.random.randint(0, h - mask_height)
left = np.random.randint(0, w - mask_width)
bottom = top + mask_height
right = left + mask_width
image[top:bottom, left:right, :].fill(mask_value)