-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdataset.py
45 lines (35 loc) · 1.11 KB
/
dataset.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import chainer
import numpy as np
import random
from six import moves
class PreprocessedDataset(chainer.dataset.DatasetMixin):
def __init__(self, pairs, mean, std, random=False):
self._pairs = pairs
self._mean = mean
self._std = std
self._random = random
def __len__(self):
return len(self._pairs)
def get_example(self, i):
image, label = self._pairs[i]
# load label data
t = np.array(label, dtype=np.int32)
# normalize data
x = np.empty_like(image)
for i in moves.range(3):
x[i] = image[i] - self._mean[i]
x[i] /= self._std[i]
# data augmentation
if self._random:
# random crop
pad_x = np.zeros((3, 40, 40), dtype=np.float32)
pad_x[:, 4:36, 4:36] = x
top = random.randint(0, 8)
left = random.randint(0, 8)
x = pad_x[:, top:top+32, left:left+32]
# horizontal flip
if random.randint(0, 1):
x = x[:, :, ::-1]
return x, t