This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
data_augment.py
114 lines (91 loc) · 2.79 KB
/
data_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
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# -*- coding: utf-8 -*-
# library modules
from random import randint
import logging
# External library modules
from PIL import Image
from PIL import ImageOps
# local modules
from utils import imgs2np
logger = logging.getLogger('AlexNet.data_augment')
def mirror(image):
"""
Return the horizontal mirror of the given image
:type image: Pillow Image object
"""
return image.transpose(Image.FLIP_LEFT_RIGHT)
def rotate(image):
"""
Rotate the given image to 90 and 270 degrees
to produce to different images.
:type image: Pillow Image object
"""
# 90 degree
img1 = image.rotate(90)
# 270 degree
img2 = image.rotate(270)
return [img1, img2]
def mirror_and_rotate(image):
"""
Mirror the image and then rotate(90 and 270
degrees separately).
:type image: Pillow Image object
"""
return rotate(mirror(image))
def invert(image):
"""
Invert the pixel intensities to produce
different looking image.
:type image: Pillow Image object
"""
return ImageOps.invert(image)
def random_crop(image, times):
"""
Randomly crop the given image for `:py:times:` many times.
:type image: Pillow Image object
:param times: How many times to crop the image
"""
# random candidate
random_cand = [image.size[0] - 227,
image.size[1] - 227]
if random_cand[0] < 0:
random_cand[0] = 0
logger.warning("Image size: %d x %d", image.size[0],
image.size[1])
if random_cand[1] < 0:
random_cand[1] = 0
logger.warning("Image size: %d x %d", image.size[0],
image.size[1])
final_images = []
for i in range(times):
area = [None] * 4
area[0] = randint(0, random_cand[0])
area[1] = randint(0, random_cand[1])
area[2] = area[0] + 227
area[3] = area[1] + 227
final_images.append(image.crop(area))
return final_images
@imgs2np
def augment(image, size, times=10):
"""
Augment data using different type of methods like
- Mirroring
- Rotation
- Invertion
- Cropping
"""
augmented_imgs = [image.resize(size)]
augmented_imgs.append(mirror(augmented_imgs[0]))
augmented_imgs.extend(rotate(augmented_imgs[0]))
augmented_imgs.extend(mirror_and_rotate(augmented_imgs[0]))
augmented_imgs.append(invert(augmented_imgs[0]))
random_crps = random_crop(image, times)
for img_crp in random_crps:
augmented_imgs.append(img_crp)
#augmented_imgs = [img_crp]
augmented_imgs.append(mirror(img_crp))
augmented_imgs.extend(rotate(img_crp))
augmented_imgs.extend(mirror_and_rotate(img_crp))
augmented_imgs.append(invert(img_crp))
return augmented_imgs