-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormal_training.py
215 lines (171 loc) · 8.82 KB
/
normal_training.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
import time
import torch
import numpy
import random
import common.torch
import common.summary
import common.numpy
from tqdm import tqdm
import gc
#TODO delete sheduler
class NormalTraining():
"""
Normal training.
"""
def __init__(self, model, trainset, testset, optimizer, scheduler, writer=common.summary.SummaryWriter(), cuda=False):
"""
Constructor.
:param model: model
:type model: torch.nn.Module
:param trainset: training set
:type trainset: torch.utils.data.DataLoader
:param testset: test set
:type testset: torch.utils.data.DataLoader
:param optimizer: optimizer
:type optimizer: torch.optim.Optimizer
:param scheduler: scheduler
:type scheduler: torch.optim.LRScheduler
:param augmentation: augmentation
:type augmentation: imgaug.augmenters.Sequential
:param writer: summary writer
:type writer: torch.utils.tensorboard.SummaryWriter or TensorboardX equivalent
:param cuda: run on CUDA device
:type cuda: bool
"""
assert isinstance(model, torch.nn.Module)
assert len(trainset) > 0
assert len(testset) > 0
assert isinstance(trainset, torch.utils.data.DataLoader)
assert isinstance(testset, torch.utils.data.DataLoader)
# assert isinstance(trainset.sampler, torch.utils.data.RandomSampler)
# assert isinstance(testset.sampler, torch.utils.data.SequentialSampler)
assert isinstance(optimizer, torch.optim.Optimizer)
# assert isinstance(scheduler, torch.optim.lr_scheduler._LRScheduler)
assert (cuda and common.torch.is_cuda(model)) or (not cuda and not common.torch.is_cuda(model))
self.writer = writer
""" (torch.util.tensorboardSummarWriter or equivalent) Summary writer. """
self.model = model
""" (torch.nn.Module) Model. """
self.trainset = trainset
""" (torch.utils.data.DatLoader) Taining set. """
self.testset = testset
""" (torch.utils.data.DatLoader) Test set. """
self.optimizer = optimizer
""" (torch.optim.Optimizer) Optimizer. """
self.scheduler = scheduler
""" (torch.optim.LRScheduler) Scheduler. """
self.cuda = cuda
""" (bool) Run on CUDA. """
self.loss = common.torch.classification_loss
""" (callable) Classificaiton loss. """
self.summary_gradients = False
""" (bool) Summary for gradients. """
self.writer.add_text('config/host', common.utils.hostname())
self.writer.add_text('config/pid', str(common.utils.pid()))
self.writer.add_text('config/model', self.model.__class__.__name__)
self.writer.add_text('config/model_details', str(self.model))
# self.writer.add_text('config/trainset', self.trainset.dataset.__class__.__name__)
# self.writer.add_text('config/testset', self.testset.dataset.__class__.__name__)
self.writer.add_text('config/optimizer', self.optimizer.__class__.__name__)
self.writer.add_text('config/scheduler', self.scheduler.__class__.__name__)
self.writer.add_text('config/cuda', str(self.cuda))
seed = int(time.time()) # time() is float
self.writer.add_text('config/seed', str(seed))
torch.manual_seed(seed)
numpy.random.seed(seed)
random.seed(seed)
# self.writer.add_text('model', str(self.model))
# self.writer.add_text('optimizer', str(common.summary.to_dict(self.optimizer)))
# self.writer.add_text('scheduler', str(common.summary.to_dict(self.scheduler)))
def train(self, epoch):
"""
Training step.
:param epoch: epoch
:type epoch: int
"""
self.model.train()
assert self.model.training is True
batches = len(self.trainset)
losses = None
errors = None
logits = None
confidences = None
accs = []
b = 0
for (inputs, targets) in tqdm(self.trainset):
gc.collect()
inputs = common.torch.as_variable(inputs, self.cuda)
# inputs = inputs.permute(0, 3, 1, 2)
assert len(targets.shape) == 1
targets = common.torch.as_variable(targets, self.cuda)
targets = targets.long()
assert len(list(targets.size())) == 1
self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.loss(outputs, targets)
errors = common.numpy.concatenate(errors, common.torch.classification_error(outputs, targets, reduction='none').detach().cpu().numpy())
losses = common.numpy.concatenate(losses, self.loss(outputs, targets, reduction='none').detach().cpu().numpy())
logits = common.numpy.concatenate(logits, torch.max(outputs, dim=1)[0].detach().cpu().numpy())
confidences = common.numpy.concatenate(confidences, torch.max(torch.nn.functional.softmax(outputs, dim=1), dim=1)[0].detach().cpu().numpy())
accs.append((torch.sum((torch.argmax(outputs, axis=1) == targets)) / float(len(targets))).detach().cpu().numpy())
loss.backward()
self.optimizer.step()
# self.scheduler.step()
if b == batches - 1:
# global_step = epoch * len(self.trainset) + b
global_step = epoch
# self.writer.add_scalar('train/lr', self.scheduler.get_last_lr()[0], global_step=global_step)
self.writer.add_scalar('train/loss', numpy.mean(losses), global_step=global_step)
self.writer.add_scalar('train/error', numpy.mean(errors), global_step=global_step)
self.writer.add_scalar('train/confidence', numpy.mean(confidences), global_step=global_step)
self.writer.add_scalar('train/accuracy', numpy.mean(accs), global_step=global_step)
# print(loss.item(), error.item())
# self.writer.add_histogram('train/logits', torch.max(outputs, dim=1)[0], global_step=global_step)
# self.writer.add_histogram('train/confidences', confidences, global_step=global_step) # torch.max(torch.nn.functional.softmax(logits, dim=1), dim=1)[0]
# if self.summary_gradients:
# for name, parameter in self.model.named_parameters():
# self.writer.add_histogram('train_weights/%s' % name, parameter.view(-1), global_step=global_step)
# self.writer.add_histogram('train_gradients/%s' % name, parameter.grad.view(-1), global_step=global_step)
self.writer.add_images('train/images', inputs[:8], global_step=global_step)
b += 1
def test(self, epoch):
"""
Test step.
:param epoch: epoch
:type epoch: int
"""
self.model.eval()
assert self.model.training is False
# reason to repeat this here: use correct loss for statistics
losses = None
errors = None
logits = None
confidences = None
for (inputs, targets) in tqdm(self.testset):
gc.collect()
inputs = common.torch.as_variable(inputs, self.cuda)
# inputs = inputs.permute(0, 3, 1, 2)
targets = common.torch.as_variable(targets, self.cuda)
targets = targets.long()
outputs = self.model(inputs)
losses = common.numpy.concatenate(losses, self.loss(outputs, targets, reduction='none').detach().cpu().numpy())
errors = common.numpy.concatenate(errors, common.torch.classification_error(outputs, targets, reduction='none').detach().cpu().numpy())
logits = common.numpy.concatenate(logits, torch.max(outputs, dim=1)[0].detach().cpu().numpy())
confidences = common.numpy.concatenate(confidences, torch.max(torch.nn.functional.softmax(outputs, dim=1), dim=1)[0].detach().cpu().numpy())
global_step = epoch # epoch * len(self.trainset) + len(self.trainset) - 1
self.writer.add_scalar('test/loss', numpy.mean(losses), global_step=global_step)
self.writer.add_scalar('test/error', numpy.mean(errors), global_step=global_step)
self.writer.add_scalar('test/logit', numpy.mean(logits), global_step=global_step)
self.writer.add_scalar('test/confidence', numpy.mean(confidences), global_step=global_step)
# self.writer.add_histogram('test/losses', losses, global_step=global_step)
# self.writer.add_histogram('test/errors', errors, global_step=global_step)
# self.writer.add_histogram('test/logits', logits, global_step=global_step)
# self.writer.add_histogram('test/confidences', confidences, global_step=global_step)
def step(self, epoch):
"""
Training + test step.
:param epoch: epoch
:type epoch: int
"""
self.train(epoch)
self.test(epoch)