-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
166 lines (142 loc) · 7.16 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
### This requires KERAS 1.0.7
import argparse
import os
import tensorflow as tf
import keras
from keras.models import load_model
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator
import rme.models
from rme.utils import config_gpu, load_meta, parse_training_args, parse_kwparams
from rme import datasets
from rme.callbacks import MetaCheckpoint
from rme import schedules
from rme import preprocessing
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Train a model on the desired dataset.')
parser.add_argument('--architecture', type=str, required=True)
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--save_checkpoint', type=str, default='checkpoint.h5')
parser.add_argument('--load_checkpoint', type=str, default=None)
# Hyperparameters
parser.add_argument('--kwparams', type=str, nargs='+', default=None)
# Training args
parser.add_argument('--lr', type=float, default=None)
parser.add_argument('--batch_size', type=int, default=None)
parser.add_argument('--epochs', type=int, default=None)
parser.add_argument('--schedule', type=str, default=None)
parser.add_argument('--preprocessing', type=str, default=None)
parser.add_argument('--augmented', default=False, action='store_true')
# GPU args
parser.add_argument('--gpu', type=str, default='')
parser.add_argument('--allow_growth', default=False, action='store_true')
args = parser.parse_args()
config_gpu(args.gpu, args.allow_growth)
training_args = vars(args)
if args.load_checkpoint:
# Continue training
model = load_model(args.load_checkpoint)
meta = load_meta(args.load_checkpoint)
args.dataset = meta['training_args']['dataset']
arch = getattr(rme.models, meta['training_args']['architecture'])
training_args = meta['training_args']
chkpt_cbk = MetaCheckpoint(args.save_checkpoint, meta=meta)
initial_epoch = meta['epochs'][-1] + 1
else:
try:
arch = getattr(rme.models, args.architecture)
# arch = available_archs[args.architecture]
except KeyError as e:
raise ValueError('Architecture %s is not available.' %args.architecture)
parse_training_args(training_args, arch.default_args(args.dataset))
training_args['kwparams'] = parse_kwparams(args.kwparams)
chkpt_cbk = MetaCheckpoint(args.save_checkpoint, training_args=training_args)
model = arch.model(args.dataset, **training_args['kwparams'])
opt = SGD(lr=training_args['lr'], momentum=0.9, nesterov=True)
model.compile(optimizer=opt, loss='categorical_crossentropy',
metrics=['accuracy'])
initial_epoch = 0
# Load dataset
print('Loading dataset: %s' %training_args['dataset'])
if args.dataset == 'mnist':
train_set, valid_set, test_set = datasets.mnist.load('data/mnist')
elif args.dataset == 'cifar10':
train_set, valid_set, test_set = datasets.cifar10.load('data/cifar10')
elif args.dataset == 'cifar100':
train_set, valid_set, test_set = datasets.cifar100.load('data/cifar100')
elif args.dataset == 'svhn':
train_set, valid_set, test_set = datasets.svhn.load('data/svhn')
else:
raise NotImplementedError('Dataset %s is not available.' %training_args['dataset'])
# Preprocess it
print('Preprocessing dataset: %s' %training_args['dataset'])
if training_args['preprocessing']:
try:
preprocess_fun = getattr(rme.preprocessing, training_args['preprocessing'])
print('Using custom preprocessing: %s' %training_args['preprocessing'])
except AttributeError:
raise NotImplementedError('Preprocessing %s is not availabe' %training_args['preprocessing'])
else:
print('Using standard preprocessing for architecture %s' %training_args['architecture'])
preprocess_fun = arch.preprocess_data
(train_set['data'], valid_set['data'],
test_set['data']) = preprocess_fun(train_set['data'],
valid_set['data'],
test_set['data'], args.dataset)
callbacks = [chkpt_cbk]
if valid_set is None or valid_set['data'].size == 0:
print('No validation set, using test set as validation data.')
validation_data = (test_set['data'], test_set['labels'])
else:
chkpt_path, chkpt_name = os.path.split(training_args['save_checkpoint'])
best_model_name = os.path.join(chkpt_path, 'best_' + chkpt_name)
print('Saving model with best validation accuracy with name %s.'
%best_model_name)
best_cbk = MetaCheckpoint(best_model_name, save_best_only=True,
training_args=training_args)
validation_data = (valid_set['data'], valid_set['labels'])
# Append it to callbacks list
callbacks.append(best_cbk)
if training_args['schedule'] != 'none':
# Set learning rate schedule
if training_args['schedule'] is None:
# Use default
schedule_fun = arch.schedule
else:
try:
schedule_fun = getattr(rme.schedules, training_args['schedule'])
except AttributeError:
raise NotImplementedError('Schedule %s is not availabe' %training_args['schedule'])
# raise NotImplementedError('You should implement custom schedules.')
schedule = schedule_fun(training_args['dataset'], training_args['lr'])
callbacks.append(schedule)
else:
# Use fixed learning rate
print('No learning rate scheduling. Learning rate will be constant')
print('Training with:')
print('%s' %str(training_args))
if training_args['augmented']:
print('Training with data augmentation: crops and flips.')
data_gen = ImageDataGenerator(horizontal_flip=True,
width_shift_range=0.125,
height_shift_range=0.125,
fill_mode='constant')
data_iter = data_gen.flow(train_set['data'], train_set['labels'],
batch_size=training_args['batch_size'],
shuffle=True)
model.fit_generator(data_iter,
samples_per_epoch=train_set['data'].shape[0],
nb_epoch=training_args['epochs'],
validation_data=(test_set['data'],
test_set['labels']),
callbacks=callbacks, initial_epoch=initial_epoch)
else:
model.fit(train_set['data'], train_set['labels'],
batch_size=training_args['batch_size'],
nb_epoch=training_args['epochs'],
validation_data=validation_data,
callbacks=callbacks, initial_epoch=initial_epoch,
shuffle=True)
test_loss, test_acc = model.evaluate(test_set['data'], test_set['labels'],
verbose=2)
print('Test set loss = %g. Test set accuracy = %g' %(test_loss, test_acc))