-
Notifications
You must be signed in to change notification settings - Fork 1
/
main001_pytorch_resnet101_cyclic_lr.py
326 lines (278 loc) · 10.4 KB
/
main001_pytorch_resnet101_cyclic_lr.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# ############################################################
#
# Imports
#
# ############################################################
import torch
from torch import nn, optim
from torch.nn import DataParallel
import torch.nn.functional as F
# Nvidia DALI, GPU Data Augmentation Library
from nvidia.dali.plugin.pytorch import DALIClassificationIterator
# Utilities
import random
import logging
import time
from timeit import default_timer as timer
import os
import pickle
# Output
import pandas as pd
# local import
from src.instrumentation import setup_logs, logspeed
from src.datafeed import *
from src.net_classic_arch import *
from src.training import train
from src.prediction import predict, output
from src.learning_rate_pr import CyclicLR
# Command-line interface
from argparse import ArgumentParser
# ############################################################
#
# Parser
#
# ############################################################
def parse_args():
parser = ArgumentParser(description="Train/Validate on fold."
" Train on full dataset."
" Predict using an existing weight.")
parser.add_argument('--fulldata', '-fd', action='store_true', default=False,
help='Train on the full dataset instead of fold 0')
parser.add_argument('--predictonly', '-po', type=str, default=None,
help='Predict only, using weights at the specified path.')
parser.add_argument('--dataparallel', '-dp', action='store', default=True,
help='Use DataParallel')
return parser.parse_args()
# ############################################################
#
# Environment variables
#
# ############################################################
# Setting random seeds for reproducibility.
# Parallel sum reductions are non-deterministic due to the non-associativity of floating points
# Note that matrix multiplication and convolution are doing sum reductions ¯\_(ツ)_/¯
torch.manual_seed(1337)
torch.cuda.manual_seed(1337)
# np.random.seed(1337)
random.seed(1337)
WEIGHTS_DIR = './snapshots' # Path for save intermediate and final weights of models
TRAIN_DIR = './input/train'
TRAIN_FULL_IMG_LIST = './preprocessing/full_input.txt'
TRAIN_FOLD_IMG_LIST = './preprocessing/fold0_train.txt'
VAL_IMG_LIST = './preprocessing/fold0_val.txt'
LABEL_ENCODER_PATH = './preprocessing/labelEncoder.pickle'
TEST_DIR = './input/test'
TEST_IMG_LIST = './preprocessing/test_data.txt'
SUBMISSION_FILE = './input/sample_submission.csv'
OUT_DIR = './outputs'
NUM_THREADS = 18
EPOCHS = 45
BATCH_SIZE = 112 # This will be split onto all GPUs
VAL_BATCH_SIZE = 112 # We can use large batches when weights are frozen
REPORT_EVERY_N_BATCH = 5
PRETRAINED = False
UNFROZE_AT_EPOCH = 3
BATCH_FROZEN = 112 # We can use large batches when weights are frozen
# GPU data augmentation
# Note that it's probably better to do augmentation on CPU for compute intensive models
# So that you can maximize the batch size and training on GPU.
DATA_AUGMENT_USE_GPU = True
DATA_AUGMENT_GPU_DEVICE = 1
if PRETRAINED:
# ImgNet normalization
NORM_MEAN = [0.485, 0.456, 0.406]
NORM_STD = [0.229, 0.224, 0.225]
else:
# Dataset normalization parameter
NORM_MEAN = [0.6073162, 0.5655911, 0.528621]
NORM_STD = [0.26327327, 0.2652084, 0.27765632]
with open(LABEL_ENCODER_PATH, 'rb') as fh:
LABEL_ENCODER = pickle.load(fh)
CRITERION = nn.CrossEntropyLoss
FINAL_ACTIVATION = lambda x: torch.softmax(x, dim=1)
model_family = 'resnet'
model_name = 'resnet101'
def gen_model_and_optimizer(dataset_size, data_parallel, weights = None):
# Delay generating model, so that:
# - it can be collected if needed
# - DataParallel doesn't causes issue when loading a saved model
model, feature_extractor, classifier = initialize_model(
model_family = model_family,
model_name = model_name,
num_classes = LABEL_ENCODER.classes_.size,
frozen_weights = PRETRAINED,
use_pretrained = PRETRAINED,
data_parallel = data_parallel,
weights = weights
)
optimizer = optim.SGD(
feature_extractor,
lr = 0.02,
momentum = 0.09
)
optimizer.add_param_group({
'params': classifier,
'lr': 0.002
})
# One-cycle policy - TODO parametrize that better
batches_per_epoch = dataset_size / BATCH_SIZE
final_epochs = 5
step_up = int(EPOCHS * batches_per_epoch / 2 - final_epochs / 2)
scheduler = CyclicLR(
optimizer,
base_lr = [0.02, 0.002],
max_lr = [0.2, 0.2],
step_size_up = step_up,
mode = 'triangular'
)
# Make sure if there is a reference issue we see it ASAP
del feature_extractor
del classifier
return model, optimizer, scheduler
init = "pretrained" if PRETRAINED else "from-scratch"
LOG_SUFFIX = f"{model_name}-{init}-001-cycliclr"
@logspeed
def main_train(args, run_name, logger):
# ############################################################
#
# Processing pipeline
#
# ############################################################
train_pipe = SimplePipeline(
img_dir=TRAIN_DIR,
img_list_path= TRAIN_FULL_IMG_LIST if args.fulldata else TRAIN_FOLD_IMG_LIST,
batch_size=BATCH_SIZE,
crop_size=224,
ch_mean = NORM_MEAN,
ch_std = NORM_STD,
num_threads = NUM_THREADS,
use_gpu = DATA_AUGMENT_USE_GPU,
gpu_id = DATA_AUGMENT_GPU_DEVICE,
seed = 1337
)
train_pipe.build()
train_loader = DALIClassificationIterator(train_pipe, size = train_pipe.epoch_size("Datafeed"))
if not args.fulldata:
val_pipe = ValPipeline(
img_dir=TRAIN_DIR,
img_list_path=VAL_IMG_LIST,
batch_size=VAL_BATCH_SIZE,
crop_size=224,
ch_mean = NORM_MEAN,
ch_std = NORM_STD,
num_threads = NUM_THREADS,
use_gpu = DATA_AUGMENT_USE_GPU,
gpu_id = DATA_AUGMENT_GPU_DEVICE,
seed = 1337
)
val_pipe.build()
val_loader = DALIClassificationIterator(val_pipe, size = val_pipe.epoch_size("Datafeed"))
model, optimizer, scheduler = gen_model_and_optimizer(train_loader._size, args.dataparallel)
num_classes = LABEL_ENCODER.classes_.size
logger.info(f"Found {num_classes} unique classes to classify.")
model_name = model.module.__class__.__name__ if args.dataparallel else model.__class__.__name__
logger.info(f"Optimizer initial configuration:\n{optimizer}")
criterion = CRITERION()
# If network is pretrained, we need to freeze feature layers
# first so that the classifier adapt to their output range
# after a couple epoch we can unfreeze to train everything
if PRETRAINED:
pretraining_pipe = SimplePipeline(
img_dir=TRAIN_DIR,
img_list_path= TRAIN_FULL_IMG_LIST if args.fulldata else TRAIN_FOLD_IMG_LIST,
batch_size=BATCH_FROZEN,
crop_size=224,
ch_mean = NORM_MEAN,
ch_std = NORM_STD,
num_threads = NUM_THREADS,
use_gpu = DATA_AUGMENT_USE_GPU,
gpu_id = DATA_AUGMENT_GPU_DEVICE,
seed = 1337
)
pretraining_pipe.build()
pretrain_loader = DALIClassificationIterator(pretraining_pipe, size = pretraining_pipe.epoch_size("Datafeed"))
logger.info(f"Pre-training with frozen feature extraction layers with batch size {BATCH_FROZEN} for {UNFROZE_AT_EPOCH} epochs")
weights = train(
model = model, train_loader = pretrain_loader,
criterion = criterion, optimizer = optimizer,
batch_size = BATCH_FROZEN, epochs = UNFROZE_AT_EPOCH,
report_freq = REPORT_EVERY_N_BATCH,
snapshot_dir = WEIGHTS_DIR,
run_name = run_name,
data_parallel = args.dataparallel,
evaluate = not args.fulldata,
lr_scheduler = None,
val_loader = None if args.fulldata else val_loader,
)
logger.info(f"End pretraining, unfreeze all weights.\n")
logger.info(f"Training {model.module.__class__.__name__} with batch size {BATCH_SIZE} for {EPOCHS} epochs.")
weights = train(
model = model, train_loader = train_loader,
criterion = criterion, optimizer = optimizer,
batch_size = BATCH_SIZE, epochs = EPOCHS,
report_freq = REPORT_EVERY_N_BATCH,
snapshot_dir = WEIGHTS_DIR,
run_name = run_name,
data_parallel = args.dataparallel,
evaluate = not args.fulldata,
lr_scheduler = scheduler,
val_loader = None if args.fulldata else val_loader,
)
return weights
@logspeed
def main_predict(weights, dataparallel):
test_pipe = ValPipeline(
img_dir=TEST_DIR,
img_list_path=TEST_IMG_LIST,
batch_size=VAL_BATCH_SIZE,
crop_size=224,
ch_mean = NORM_MEAN,
ch_std = NORM_STD,
num_threads = NUM_THREADS,
use_gpu = DATA_AUGMENT_USE_GPU,
gpu_id = DATA_AUGMENT_GPU_DEVICE,
seed = 1337
)
test_pipe.build()
test_loader = DALIClassificationIterator(test_pipe, size = test_pipe.epoch_size("Datafeed"))
model, _ , _= gen_model_and_optimizer(test_loader._size, dataparallel, weights = weights)
return predict(test_loader, model, FINAL_ACTIVATION)
@logspeed
def main():
args = parse_args()
best_score=None
model_weights_path = ''
if args.predictonly:
# Pretrained
run_name = time.strftime("%Y-%m-%d_%H%M-") + LOG_SUFFIX + ("-fulldata" if args.fulldata else "")
tmp_logfile = os.path.join(OUT_DIR, f'{run_name}--run-in-progress.log')
logger = setup_logs(tmp_logfile)
model_weights_path = args.predictonly
else:
# Training
run_name = time.strftime("%Y-%m-%d_%H%M-") + f"{LOG_SUFFIX}"
tmp_logfile = os.path.join(OUT_DIR, f'{run_name}--run-in-progress.log')
logger = setup_logs(tmp_logfile)
model_weights_path, best_score = main_train(args, run_name, logger)
# Load model
logger.info(f'===> loading model for prediction: {model_weights_path}')
checkpoint = torch.load(model_weights_path)
# Predict
pred = main_predict(checkpoint['state_dict'], args.dataparallel)
# Output
X_test = pd.read_csv(SUBMISSION_FILE)
output(pred, X_test, LABEL_ENCODER, OUT_DIR, run_name)
# ############################################################
#
# Cleanup
#
# ############################################################
if best_score: # Prediction only of full dataset training
final_logfile = os.path.join('./outputs/', f'{run_name}--best_val_score-{best_score:.4f}.log')
os.rename(tmp_logfile, final_logfile)
else:
final_logfile = os.path.join('./outputs/', f'{run_name}.log')
os.rename(tmp_logfile, final_logfile)
logger.info(" ===> Finished all tasks!")
main()
logging.shutdown()