-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
163 lines (127 loc) · 4.07 KB
/
training.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import math
from os import listdir
from os.path import join
import numpy as np
from PIL import Image
from Augmentor.Operations import Rotate, Flip
from sklearn.preprocessing import LabelBinarizer
PATH_TO_IMAGES = './images'
def dataset(root_folder, batch_size=32):
"""
Source generator which parses folders with training samples and preparing
label encoder to convert each image's class into one-hot encoded vector.
The generator yields file names and encoded labels in batches of size equal
to `batch_size` parameter value.
Should be the very first generator in pipeline providing data for
subsequent steps.
"""
images_and_classes = []
for image_class in listdir(root_folder):
subfolder = join(root_folder, image_class)
for sample in listdir(subfolder):
filename = join(subfolder, sample)
images_and_classes.append((filename, image_class))
n_batches = int(math.ceil(len(images_and_classes) / batch_size))
classes = [c for (img, c) in images_and_classes]
binarizer = LabelBinarizer()
binarizer.fit(classes)
start = 0
for _ in range(n_batches):
batch = images_and_classes[start:(start + batch_size)]
paths, labels = zip(*batch)
encoded = binarizer.transform(labels)
start += batch_size
yield np.asarray(paths), encoded
def read_images(target_size=(224, 224)):
"""
Reads images from disk and rescales them to `target_size`.
"""
while True:
filenames, y = yield
images = []
for sample in filenames:
img = Image.open(sample)
if img.mode != 'RGB':
img = img.convert('RGB')
img = img.resize(target_size, Image.NEAREST)
images.append(img)
yield images, y
def augment(horizontal_flip=True,
vertical_flip=False,
rotate90=False,
probability=0.5):
"""
Applies a group of augmentation operations to each sample in batch.
"""
ops = []
if horizontal_flip:
ops.append(Flip(
probability=probability,
top_bottom_left_right='LEFT_RIGHT'))
if vertical_flip:
ops.append(Flip(
probability=probability,
top_bottom_left_right='TOP_BOTTOM'))
if rotate90:
ops.append(Rotate(probability=probability, rotation=90))
while True:
images, y = yield
for op in ops:
images = op.perform_operation(images)
yield images, y
def rescale_images(mean):
"""
Subtracts mean pixel value from each channel,
"""
assert len(mean) == 3, 'Mean should be an array of 3 elements'
while True:
images, y = yield
x = np.asarray([np.asarray(img, dtype=float) for img in images])
x[..., 0] -= mean[0]
x[..., 1] -= mean[1]
x[..., 2] -= mean[2]
x /= 256.0
yield x, y
def shuffle_samples():
"""
Shuffles batch samples.
"""
while True:
x, y = yield
index = np.random.permutation(len(x))
yield x[index], y[index]
class GeneratorPipeline:
"""Convenience wrapper combining a list of generators together into a
single generator.
"""
def __init__(self, source, *steps):
self.source = source
self.steps = list(steps)
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
batch = next(self.source)
self.send_none()
transformed = self.send(batch)
return transformed
def send_none(self):
for step in self.steps:
step.send(None)
def send(self, batch):
x = batch
for generator in self.steps:
x = generator.send(x)
return x
def main():
pipeline = GeneratorPipeline(
dataset(PATH_TO_IMAGES),
read_images(),
augment(rotate90=True),
rescale_images(mean=[103.939, 116.779, 123.68]),
shuffle_samples())
for i, (x, y) in enumerate(pipeline):
print('Batch', i, x.shape, y.shape)
if __name__ == '__main__':
main()