-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
190 lines (168 loc) · 8.58 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
import torch
import torch.nn as nn
from torch.optim.lr_scheduler import ReduceLROnPlateau
from catalyst.dl.runner import SupervisedRunner
from catalyst.dl.callbacks import DiceCallback, EarlyStoppingCallback, OptimizerCallback, CriterionCallback, AUCCallback
# from catalyst.contrib.criterion.lovasz import LovaszLossMultiClass, LovaszLossBinary
import ttach as tta
import segmentation_models_pytorch as smp
import datetime
import argparse
import warnings
import gc
import json
from dataset import prepare_loaders
from models.models import get_model
from optimizers import get_optimizer
from utils import get_optimal_postprocess
from predict import predict, predict_blend
from losses.losses import FocalLoss, BCEMulticlassDiceLoss
from losses.lovasz_losses import lovasz_softmax
from catalyst import utils
from callbacks import MulticlassDiceMetricCallback
from catalyst.utils import set_global_seed, prepare_cudnn
import os
warnings.filterwarnings("once")
if __name__ == '__main__':
"""
Example of usage:
>>> python train.py --chunk_size=10000 --n_jobs=10
"""
parser = argparse.ArgumentParser(description="Train model for understanding_cloud_organization competition")
parser.add_argument("--path", help="path to files", type=str, default='/home/dex/Desktop/ml/cloud data')
# https://github.com/qubvel/segmentation_models.pytorch
parser.add_argument("--encoder", help="u-net encoder", type=str, default='resnet18')
parser.add_argument("--encoder_weights", help="pre-training dataset", type=str, default='imagenet')
parser.add_argument("--DEVICE", help="device", type=str, default='CUDA')
parser.add_argument("--scheduler", help="scheduler", type=str, default='ReduceLROnPlateau')
parser.add_argument("--loss", help="loss", type=str, default='BCEDiceLoss')
parser.add_argument("--logdir", help="logdir", type=str, default='./logs/')
parser.add_argument("--optimizer", help="optimizer", type=str, default='Adam')
parser.add_argument("--augmentation", help="augmentation", type=str, default='default')
parser.add_argument("--model_type", help="model_type", type=str, default='segm')
parser.add_argument("--segm_type", help="model_type", type=str, default='Unet')
parser.add_argument("--task", help="class or segm", type=str, default='segmentation')
parser.add_argument("--num_workers", help="num_workers", type=int, default=4)
parser.add_argument("--bs", help="batch size", type=int, default=2)
parser.add_argument("--lr", help="learning rate", type=float, default=1e-3)
parser.add_argument("--lr_e", help="learning rate for decoder", type=float, default=1e-3)
parser.add_argument("--num_epochs", help="number of epochs", type=int, default=100)
parser.add_argument("--gradient_accumulation", help="gradient_accumulation steps", type=int, default=None)
parser.add_argument("--height", help="height", type=int, default=320)
parser.add_argument("--width", help="width", type=int, default=640)
parser.add_argument("--seed", help="random seed", type=int, default=42)
parser.add_argument("--optimize_postprocess", help="to optimize postprocess", type=bool, default=False)
parser.add_argument("--train", help="train", type=bool, default=False)
parser.add_argument("--make_prediction", help="to make prediction", type=bool, default=False)
parser.add_argument("--preload", help="save processed data", type=bool, default=False)
parser.add_argument("--separate_decoder", help="number of epochs", type=bool, default=False)
parser.add_argument("--multigpu", help="use multi-gpu", type=bool, default=False)
parser.add_argument("--lookahead", help="use lookahead", type=bool, default=False)
args, unknown = parser.parse_known_args()
# args.train = False
args.optimize_postprocess = False
print(args)
if args.task == 'classification':
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
set_global_seed(args.seed)
prepare_cudnn(deterministic=True)
sub_name = f'Model_{args.task}_{args.model_type}_{args.encoder}_bs_{args.bs}_{str(datetime.datetime.now().date())}'
logdir = f"./logs/{sub_name}" if args.logdir is None else args.logdir
preprocessing_fn = smp.encoders.get_preprocessing_fn(args.encoder, args.encoder_weights)
loaders = prepare_loaders(path=args.path, bs=args.bs,
num_workers=args.num_workers, preprocessing_fn=preprocessing_fn, preload=args.preload,
image_size=(args.height, args.width), augmentation=args.augmentation, task=args.task)
test_loader = loaders['test']
del loaders['test']
model = get_model(model_type=args.segm_type, encoder=args.encoder, encoder_weights=args.encoder_weights,
activation=None, task=args.task)
optimizer = get_optimizer(optimizer=args.optimizer, lookahead=args.lookahead, model=model,
separate_decoder=args.separate_decoder, lr=args.lr, lr_e=args.lr_e)
if args.scheduler == 'ReduceLROnPlateau':
scheduler = ReduceLROnPlateau(optimizer, factor=0.6, patience=3)
else:
scheduler = ReduceLROnPlateau(optimizer, factor=0.3, patience=3)
if args.loss == 'BCEDiceLoss':
criterion = smp.utils.losses.BCEDiceLoss(eps=1.)
elif args.loss == 'BCEJaccardLoss':
criterion = smp.utils.losses.BCEJaccardLoss(eps=1.)
elif args.loss == 'FocalLoss':
criterion = FocalLoss()
# elif args.loss == 'lovasz_softmax':
# criterion = lovasz_softmax()
elif args.loss == 'BCEMulticlassDiceLoss':
criterion = BCEMulticlassDiceLoss()
elif args.loss == 'MulticlassDiceMetricCallback':
criterion = MulticlassDiceMetricCallback()
elif args.loss == 'BCE':
criterion = nn.BCEWithLogitsLoss()
else:
criterion = smp.utils.losses.BCEDiceLoss(eps=1.)
if args.multigpu:
model = nn.DataParallel(model)
if args.task == 'segmentation':
callbacks = [DiceCallback(), EarlyStoppingCallback(patience=10, min_delta=0.001), CriterionCallback()]
elif args.task == 'classification':
callbacks = [AUCCallback(class_names=['Fish', 'Flower', 'Gravel', 'Sugar'], num_classes=4),
EarlyStoppingCallback(patience=10, min_delta=0.001), CriterionCallback()]
if args.gradient_accumulation:
callbacks.append(OptimizerCallback(accumulation_steps=args.gradient_accumulation))
checkpoint = utils.load_checkpoint(f'{logdir}/checkpoints/best.pth')
model.cuda()
utils.unpack_checkpoint(checkpoint, model=model)
#
#
runner = SupervisedRunner()
if args.train:
print('Training')
runner.train(
model=model,
criterion=criterion,
optimizer=optimizer,
main_metric='dice',
minimize_metric=False,
scheduler=scheduler,
loaders=loaders,
callbacks=callbacks,
logdir=logdir,
num_epochs=args.num_epochs,
verbose=True
)
with open(f'{logdir}/args.txt', 'w') as f:
for k, v in args.__dict__.items():
f.write(f'{k}: {v}' + '\n')
torch.cuda.empty_cache()
gc.collect()
class_params = None
if args.optimize_postprocess:
print('POSTPROCESS')
del loaders['train']
checkpoint = utils.load_checkpoint(f'{logdir}/checkpoints/best.pth')
model.cuda()
utils.unpack_checkpoint(checkpoint, model=model)
runner = SupervisedRunner(model=model)
class_params = get_optimal_postprocess(loaders=loaders, runner=runner, logdir=logdir)
with open(f'{logdir}/class_params.json', 'w') as f:
json.dump(class_params, f)
if args.make_prediction:
print('MAKING PREDICTIONS')
loaders['test'] = test_loader
checkpoint = utils.load_checkpoint(f'{logdir}/checkpoints/best.pth')
# transforms = tta.Compose(
# [
# tta.HorizontalFlip(),
# # tta.Rotate90(angles=[0, 180]),
# # tta.Scale(scales=[1, 2, 4]),
# #tta.Multiply(factors=[0.9, 1, 1.1]),
# ]
# )
model.cuda()
utils.unpack_checkpoint(checkpoint, model=model)
#tta_model = tta.SegmentationTTAWrapper(model, transforms, merge_mode='mean')
#runner = SupervisedRunner(model=tta_model)
runner = SupervisedRunner(model=model)
if not class_params:
with open(f'{logdir}/class_params.json', 'r') as f:
class_params = json.load(f)
print('prediction postprocess params', class_params)
predict(loaders=loaders, runner=runner, class_params=class_params, path=args.path, sub_name=sub_name)