-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.py
63 lines (49 loc) · 1.92 KB
/
input.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
import random
import numpy as np
import scipy
def random_crop(batch, crop_shape, padding=None):
oshape = np.shape(batch[0])
if padding:
oshape = (oshape[0] + 2 * padding, oshape[1] + 2 * padding)
new_batch = []
npad = ((padding, padding), (padding, padding), (0, 0))
for i in range(len(batch)):
new_batch.append(batch[i])
if padding:
new_batch[i] = np.lib.pad(batch[i], pad_width=npad, mode='constant', constant_values=0)
nh = random.randint(0, oshape[0] - crop_shape[0])
nw = random.randint(0, oshape[1] - crop_shape[1])
new_batch[i] = new_batch[i][nh:nh + crop_shape[0], nw:nw + crop_shape[1]]
return new_batch
def random_flip_leftright(batch):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
batch[i] = np.fliplr(batch[i])
return batch
def random_flip_updown(batch):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
batch[i] = np.flipud(batch[i])
return batch
def random_90degrees_rotation(batch, rotations=[0, 1, 2, 3]):
for i in range(len(batch)):
num_rotations = random.choice(rotations)
batch[i] = np.rot90(batch[i], num_rotations)
return batch
def random_rotation(batch, max_angle):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
angle = random.uniform(-max_angle, max_angle)
batch[i] = scipy.ndimage.interpolation.rotate(batch[i], angle, reshape=False)
return batch
def random_blur(batch, sigma_max=5.0):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
sigma = random.uniform(0., sigma_max)
batch[i] = scipy.ndimage.filters.gaussian_filter(batch[i], sigma)
return batch
def augmentation(batch):
# batch = random_crop(batch, (img_size, img_size), 10)
batch = random_flip_leftright(batch)
batch = random_rotation(batch, 10)
return batch