-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_aug.py
50 lines (47 loc) · 1.82 KB
/
img_aug.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
import Augmentor
import os
def makedir(path):
'''
if path does not exist in the file system, create it
'''
if not os.path.exists(path):
os.makedirs(path)
datasets_root_dir = './datasets/Mix/' #'./datasets/tiny-imagenet-200/' #'./datasets/cub200_cropped/'
dir = datasets_root_dir + 'train/' #'train_cropped/'
target_dir = datasets_root_dir + 'train_augmented/' #'train_cropped_augmented/'
makedir(target_dir)
folders = [os.path.join(dir, folder) for folder in next(os.walk(dir))[1]]
target_folders = [os.path.join(target_dir, folder) for folder in next(os.walk(dir))[1]]
for i in range(len(folders)):
fd = folders[i]
# modification on the next line to correct the behavior of "augmentor",
# found on a pullrequest on the original ProtoPnet github repo
tfd = os.path.abspath(target_folders[i]) #target_folders[i]
# rotation
p = Augmentor.Pipeline(source_directory=fd, output_directory=tfd)
p.rotate(probability=1, max_left_rotation=15, max_right_rotation=15)
p.flip_left_right(probability=0.5)
for i in range(10):
p.process()
del p
# skew
p = Augmentor.Pipeline(source_directory=fd, output_directory=tfd)
p.skew(probability=1, magnitude=0.2) # max 45 degrees
p.flip_left_right(probability=0.5)
for i in range(10):
p.process()
del p
# shear
p = Augmentor.Pipeline(source_directory=fd, output_directory=tfd)
p.shear(probability=1, max_shear_left=10, max_shear_right=10)
p.flip_left_right(probability=0.5)
for i in range(10):
p.process()
del p
# random_distortion
#p = Augmentor.Pipeline(source_directory=fd, output_directory=tfd)
#p.random_distortion(probability=1.0, grid_width=10, grid_height=10, magnitude=5)
#p.flip_left_right(probability=0.5)
#for i in range(10):
# p.process()
#del p