-
Notifications
You must be signed in to change notification settings - Fork 35
/
train.py
207 lines (172 loc) · 7.01 KB
/
train.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
from functools import partial
from importlib import import_module
import json
import os
import random
import re
import shutil
import time
import numpy as np
import chainer
from chainer import iterators
from chainer import optimizers
from chainer import training
from chainer.datasets import TransformDataset
from chainer.datasets import cifar
import chainer.links as L
from chainer.training import extensions
from chainercv import transforms
import cv2 as cv
from skimage import transform as skimage_transform
USE_OPENCV = False
def cv_rotate(img, angle):
if USE_OPENCV:
img = img.transpose(1, 2, 0) / 255.
center = (img.shape[0] // 2, img.shape[1] // 2)
r = cv.getRotationMatrix2D(center, angle, 1.0)
img = cv.warpAffine(img, r, img.shape[:2])
img = img.transpose(2, 0, 1) * 255.
img = img.astype(np.float32)
else:
# scikit-image's rotate function is almost 7x slower than OpenCV
img = img.transpose(1, 2, 0) / 255.
img = skimage_transform.rotate(img, angle, mode='edge')
img = img.transpose(2, 0, 1) * 255.
img = img.astype(np.float32)
return img
def transform(
inputs, mean, std, random_angle=15., pca_sigma=255., expand_ratio=1.0,
crop_size=(32, 32), train=True):
img, label = inputs
img = img.copy()
# Random rotate
if random_angle != 0:
angle = np.random.uniform(-random_angle, random_angle)
img = cv_rotate(img, angle)
# Color augmentation
if train and pca_sigma != 0:
img = transforms.pca_lighting(img, pca_sigma)
# Standardization
img -= mean[:, None, None]
img /= std[:, None, None]
if train:
# Random flip
img = transforms.random_flip(img, x_random=True)
# Random expand
if expand_ratio > 1:
img = transforms.random_expand(img, max_ratio=expand_ratio)
# Random crop
if tuple(crop_size) != (32, 32):
img = transforms.random_crop(img, tuple(crop_size))
return img, label
def create_result_dir(prefix):
result_dir = 'results/{}_{}_0'.format(
prefix, time.strftime('%Y-%m-%d_%H-%M-%S'))
while os.path.exists(result_dir):
i = result_dir.split('_')[-1]
result_dir = re.sub('_[0-9]+$', result_dir, '_{}'.format(i))
if not os.path.exists(result_dir):
os.makedirs(result_dir)
shutil.copy(__file__, os.path.join(result_dir, os.path.basename(__file__)))
return result_dir
def run_training(
net, train, valid, result_dir, batchsize=64, devices=-1,
training_epoch=300, initial_lr=0.05, lr_decay_rate=0.5,
lr_decay_epoch=30, weight_decay=0.0005):
# Iterator
train_iter = iterators.MultiprocessIterator(train, batchsize)
test_iter = iterators.MultiprocessIterator(valid, batchsize, False, False)
# Model
net = L.Classifier(net)
# Optimizer
optimizer = optimizers.MomentumSGD(lr=initial_lr)
optimizer.setup(net)
if weight_decay > 0:
optimizer.add_hook(chainer.optimizer.WeightDecay(weight_decay))
# Updater
if isinstance(devices, int):
devices['main'] = devices
updater = training.StandardUpdater(
train_iter, optimizer, device=devices)
elif isinstance(devices, dict):
updater = training.ParallelUpdater(
train_iter, optimizer, devices=devices)
# 6. Trainer
trainer = training.Trainer(
updater, (training_epoch, 'epoch'), out=result_dir)
# 7. Trainer extensions
trainer.extend(extensions.LogReport())
trainer.extend(extensions.observe_lr())
trainer.extend(extensions.Evaluator(
test_iter, net, device=devices['main']), name='val')
trainer.extend(extensions.PrintReport(
['epoch', 'main/loss', 'main/accuracy', 'val/main/loss',
'val/main/accuracy', 'elapsed_time', 'lr']))
trainer.extend(extensions.PlotReport(
['main/loss', 'val/main/loss'], x_key='epoch', file_name='loss.png'))
trainer.extend(extensions.PlotReport(
['main/accuracy', 'val/main/accuracy'], x_key='epoch',
file_name='accuracy.png'))
trainer.extend(extensions.ExponentialShift(
'lr', lr_decay_rate), trigger=(lr_decay_epoch, 'epoch'))
trainer.run()
return net
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_file', type=str, default='models/vgg.py')
parser.add_argument('--model_name', type=str, default='VGG')
parser.add_argument('--gpus', type=int, nargs='*', default=[0])
parser.add_argument('--seed', type=int, default=0)
# Train settings
parser.add_argument('--batchsize', type=int, default=128)
parser.add_argument('--training_epoch', type=int, default=500)
parser.add_argument('--initial_lr', type=float, default=0.05)
parser.add_argument('--lr_decay_rate', type=float, default=0.5)
parser.add_argument('--lr_decay_epoch', type=float, default=25)
parser.add_argument('--weight_decay', type=float, default=0.0005)
# Data augmentation settings
parser.add_argument('--random_angle', type=float, default=15.0)
parser.add_argument('--pca_sigma', type=float, default=25.5)
parser.add_argument('--expand_ratio', type=float, default=1.2)
parser.add_argument('--crop_size', type=int, nargs='*', default=[28, 28])
args = parser.parse_args()
# Enable autotuner of cuDNN
chainer.config.autotune = True
# Set the random seeds
random.seed(args.seed)
np.random.seed(args.seed)
if len(args.gpus) > 1 or args.gpus[0] >= 0:
chainer.cuda.cupy.random.seed(args.seed)
gpus = {'main': args.gpus[0]}
if len(args.gpus) > 1:
gpus.update({'gpu{}'.format(i): i for i in args.gpus[1:]})
args.gpus = gpus
# Load model
ext = os.path.splitext(args.model_file)[1]
mod_path = '.'.join(os.path.split(args.model_file)).replace(ext, '')
mod = import_module(mod_path)
net = getattr(mod, args.model_name)(10)
# create result dir
result_dir = create_result_dir(args.model_name)
shutil.copy(args.model_file, os.path.join(
result_dir, os.path.basename(args.model_file)))
with open(os.path.join(result_dir, 'args'), 'w') as fp:
fp.write(json.dumps(vars(args)))
print(json.dumps(vars(args), sort_keys=True, indent=4))
train, valid = cifar.get_cifar10(scale=255.)
mean = np.mean([x for x, _ in train], axis=(0, 2, 3))
std = np.std([x for x, _ in train], axis=(0, 2, 3))
train_transform = partial(
transform, mean=mean, std=std, random_angle=args.random_angle,
pca_sigma=args.pca_sigma, expand_ratio=args.expand_ratio,
crop_size=args.crop_size, train=True)
valid_transform = partial(transform, mean=mean, std=std, train=False)
train = TransformDataset(train, train_transform)
valid = TransformDataset(valid, valid_transform)
run_training(
net, train, valid, result_dir, args.batchsize, args.gpus,
args.training_epoch, args.initial_lr, args.lr_decay_rate,
args.lr_decay_epoch, args.weight_decay)