-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_st1.py
222 lines (173 loc) · 8.03 KB
/
train_st1.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
import os
import torch
import shutil
import numpy as np
import pandas as pd
from tqdm import tqdm
from tensorboardX import SummaryWriter
from utils.models import _load_model_weights, model_dict
from utils.dataGen import make_data_generator
from utils.losses import get_loss
from utils.metrics import dice_coeff, MetricTracker
from utils.lr_scheduler import LR_Scheduler
class Trainer:
"""Object for training `solaris` models using PyTorch. """
def __init__(self, config, custom_losses=None):
# self.sv_name = datetime.strftime(datetime.now(), '%Y%m%d_%H%M%S')
self.sv_name = config['sv_name']
print('saving file name is ', self.sv_name)
self.checkpoint_dir = os.path.join('./', self.sv_name, 'checkpoints')
self.logs_dir = os.path.join('./', self.sv_name, 'logs')
if not os.path.isdir(self.checkpoint_dir):
os.makedirs(self.checkpoint_dir)
if not os.path.isdir(self.logs_dir):
os.makedirs(self.logs_dir)
self.config = config
self.batch_size = self.config['batch_size']
self.model_name = self.config['model_name']
self.model_path = self.config.get('model_path', None)
# self.num_classes = self.config['data_specs']['num_classes']
self.model = model_dict[self.model_name](**self.config['model_specs'])
if self.model_path:
self.model = _load_model_weights(self.model, self.model_path)
self.train_df, self.val_df = get_train_val_dfs(self.config)
self.train_datagen = make_data_generator(self.config,
self.train_df, stage='train')
self.val_datagen = make_data_generator(self.config,
self.val_df, stage='validate')
self.epochs = self.config['training']['epochs']
# self.optimizer = torch.optim.SGD
self.lr = self.config['training']['lr']
self.loss = get_loss(self.config['training'].get('loss'),
self.config['training'].get('loss_weights'),
custom_losses)
self.metrics = dice_coeff
self.gpu_available = torch.cuda.is_available()
if self.gpu_available:
self.gpu_count = torch.cuda.device_count()
else:
self.gpu_count = 0
self.train_writer = SummaryWriter(os.path.join(self.logs_dir, 'runs', self.sv_name, 'training'))
self.val_writer = SummaryWriter(os.path.join(self.logs_dir, 'runs', self.sv_name, 'val'))
self.initialize_model()
def initialize_model(self):
if self.gpu_available:
self.model = self.model.cuda()
if self.gpu_count > 1:
self.model = torch.nn.DataParallel(self.model)
self.optimizer = torch.optim.SGD(
self.model.parameters(), lr=self.lr,
momentum=0.9, weight_decay=1e-4, nesterov=True
)
self.lr_scheduler = LR_Scheduler('poly', self.lr, self.epochs + 1, len(self.train_datagen))
def run(self):
"""
the main function to run
"""
best_metric = 0
for epoch in range(1, self.epochs+1):
print('Epoch {}/{}'.format(epoch, self.epochs))
print('-' * 10)
self.train(epoch, best_metric)
metric_v = self.val(epoch)
is_best_metric = metric_v > best_metric
best_metric = max(metric_v, best_metric)
self.save_checkpoint({
'epoch': epoch,
'state_dict': self.model.module.state_dict() if isinstance(self.model, torch.nn.DataParallel) else self.model.state_dict(),
'best_metric': best_metric,
'optimizer': self.optimizer.state_dict()
}, is_best_metric)
# if (epoch - 1) % 20 == 0:
# self.save_checkpoint_multi({
# 'epoch': epoch,
# 'state_dict': self.model.module.state_dict() if isinstance(self.model, torch.nn.DataParallel) else self.model.state_dict(),
# 'best_metric': best_metric,
# 'optimizer': self.optimizer.state_dict()
# }, epoch)
def train(self, epoch, best_metric):
"""Run training on the model."""
Losses = MetricTracker()
self.model.train()
for idx, batch in enumerate(tqdm(self.train_datagen, desc="training", ascii=True, ncols=60)):
if torch.cuda.is_available():
data = batch['image'].cuda()
target = batch['mask'].cuda().long()
self.optimizer.zero_grad()
output = self.model(data)
loss = self.loss(output, target)
loss.backward()
self.optimizer.step()
Losses.update(loss.item(), data.size(0))
self.lr_scheduler(self.optimizer, idx, epoch, best_metric)
info = {
"Loss": Losses.avg,
}
for tag, value in info.items():
self.train_writer.add_scalar(tag, value, epoch)
print('Train Loss: {:.6f}'.format(
Losses.avg
))
return None
def val(self, epoch):
self.model.eval()
torch.cuda.empty_cache()
val_Metric = MetricTracker()
with torch.no_grad():
for idx, batch in enumerate(tqdm(self.val_datagen, desc="val", ascii=True, ncols=60)):
if torch.cuda.is_available():
data = batch['image'].cuda()
target = batch['mask'].cuda().float()
logits = self.model(data)
outputs = torch.argmax(logits, dim=1).float()
val_Metric.update(self.metrics(outputs, target), outputs.size(0))
info = {
"Dice": val_Metric.avg
}
for tag, value in info.items():
self.val_writer.add_scalar(tag, value, epoch)
print('Val Dice: {:.6f}'.format(
val_Metric.avg
))
return val_Metric.avg
def save_checkpoint(self, state, is_best):
filename = os.path.join(self.checkpoint_dir, self.sv_name + '_checkpoint.pth.tar')
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, os.path.join(self.checkpoint_dir, self.sv_name + '_model_best.pth.tar'))
def save_checkpoint_multi(self, state, epoch):
filename = os.path.join(self.checkpoint_dir, self.sv_name + f'_checkpoint_{epoch}.pth.tar')
torch.save(state, filename)
def get_train_val_dfs(config):
"""Get the training and validation dfs based on the contents of ``config``.
This function uses the logic described in the documentation for the config
files to determine where to find training and validation dataset files.
See the docs and the comments in solaris/data/config_skeleton.yml for
details.
Arguments
---------
config : dict
The loaded configuration dict for model training and/or inference.
Returns
-------
train_df, val_df : :class:`tuple` of :class:`dict` s
:class:`dict` s containing two columns: ``'image'`` and ``'label'``.
Each column corresponds to paths to find matching image and label files
for training.
"""
train_df = pd.read_csv(config['training_data_csv'])
if config['data_specs']['val_holdout_frac'] is None:
if config['validation_data_csv'] is None:
raise ValueError(
"If val_holdout_frac isn't specified in config,"
" validation_data_csv must be.")
val_df = pd.read_csv(config['validation_data_csv'])
else:
val_frac = config['data_specs']['val_holdout_frac']
val_subset = np.random.choice(train_df.index,
int(len(train_df)*val_frac),
replace=False)
val_df = train_df.loc[val_subset]
# remove the validation samples from the training df
train_df = train_df.drop(index=val_subset)
return train_df, val_df