forked from lucasmansilla/DGvGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
223 lines (170 loc) · 7.75 KB
/
models.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
import torch
import time
from networks import AlexNet
from dataloader import InfiniteDataLoader
from gradient_surgery import get_agreement_func
def get_model(device, dataset, args):
if args.method == 'deep-all':
return ModelDA(device, dataset, args)
elif args.method in ['agr-sum', 'agr-rand', 'pcgrad']:
return ModelGS(device, dataset, args)
else:
raise ValueError
class ModelDA:
""" Baseline model (Deep-All). """
def __init__(self, device, dataset, args):
self.device = device
self.args = args
self.network = AlexNet(dataset.N_CLASSES).to(device)
self.loss_fn = torch.nn.CrossEntropyLoss()
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=args.lr,
weight_decay=args.weight_decay)
self._create_dataloaders(dataset, args)
self.stats = {'loss': {'train': [], 'val': [], 'test': []},
'acc': {'train': [], 'val': [], 'test': []},
'time': {'train': []}}
def _create_dataloaders(self, dataset, args):
def get_dataloader(dataset, batch_size, is_train=False):
if is_train:
return InfiniteDataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=True,
drop_last=True)
else:
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=False,
drop_last=False)
self.train_loaders = []
for dom_dataset in dataset['train']:
self.train_loaders.append(get_dataloader(dom_dataset, args.batch_size, True))
self.val_loader = get_dataloader(dataset['val'], args.batch_size)
self.test_loader = get_dataloader(dataset['test'], args.batch_size)
def _prepare_batch(self, batch):
inputs, targets = batch
inputs = inputs.to(self.device)
targets = targets.to(self.device)
return inputs, targets
def train(self):
train_iterator = zip(*self.train_loaders)
iterations = self.args.iterations
val_every = self.args.val_every
run_train_time, run_train_loss, run_train_acc = 0.0, 0.0, 0.0
max_val_acc = -1
for it in range(iterations):
train_time = time.time()
# Training
train_batches = [self._prepare_batch(batch) for batch in next(train_iterator)]
train_loss, train_acc = self._train_step(train_batches)
run_train_time += (time.time() - train_time)
run_train_loss += train_loss
run_train_acc += train_acc
if it == 0 or (it+1) % val_every == 0 or it == (iterations-1):
# Validation
val_loss, val_acc = self._validation_step()
# Save model when the validation accuracy increases
if val_acc > max_val_acc:
max_val_acc = val_acc
torch.save(self.network.state_dict(), self.args.output_dir + '/best_model.pt')
if (it+1) % val_every == 0:
run_train_time /= val_every
run_train_loss /= val_every
run_train_acc /= val_every
elif it == (iterations-1):
n_steps = iterations
n_steps -= n_steps//val_every * val_every
run_train_time /= n_steps
run_train_loss /= n_steps
run_train_acc /= n_steps
self.stats['loss']['train'].append(run_train_loss)
self.stats['acc']['train'].append(run_train_acc)
self.stats['loss']['val'].append(val_loss)
self.stats['acc']['val'].append(val_acc)
self.stats['time']['train'].append(run_train_time)
# Print stats
print(f'\titer {it+1:>5}/{iterations}: '
f'train loss: {run_train_loss:.5f}, '
f'train acc: {run_train_acc:.5f} | '
f'val loss: {val_loss:.5f}, '
f'val acc: {val_acc:.5f} | '
f'iter time: {run_train_time:.5f}')
run_train_time, run_train_loss, run_train_acc = 0.0, 0.0, 0.0
def _train_step(self, train_batches):
is_train = True
inputs = torch.cat([x for x, _ in train_batches])
targets = torch.cat([y for _, y in train_batches])
self.network.train(is_train)
self.optimizer.zero_grad()
with torch.set_grad_enabled(is_train):
outputs = self.network(inputs)
loss = self.loss_fn(outputs, targets)
loss.backward()
predictions = torch.max(outputs, 1)[1]
train_loss = loss.item()
train_acc = (predictions == targets).float().mean().item()
self.optimizer.step()
return train_loss, train_acc
def _validation_step(self):
is_train = False
val_loss, val_acc = 0.0, 0.0
self.network.train(is_train)
with torch.set_grad_enabled(is_train):
for batch in self.val_loader:
inputs, targets = self._prepare_batch(batch)
outputs = self.network(inputs)
loss = self.loss_fn(outputs, targets)
predictions = torch.max(outputs, 1)[1]
val_loss += loss.item() * inputs.size(0)
val_acc += (predictions == targets).sum().item()
val_loss /= len(self.val_loader.dataset)
val_acc /= len(self.val_loader.dataset)
return val_loss, val_acc
def test(self):
is_train = False
test_loss, test_acc = 0.0, 0.0
self.network.train(is_train)
self.network.load_state_dict(torch.load(self.args.output_dir + '/best_model.pt'))
with torch.set_grad_enabled(is_train):
for batch in self.test_loader:
inputs, targets = self._prepare_batch(batch)
outputs = self.network(inputs)
loss = self.loss_fn(outputs, targets)
predictions = torch.max(outputs, 1)[1]
test_loss += loss.item() * inputs.size(0)
test_acc += (predictions == targets).sum().item()
self.stats['loss']['test'] = test_loss / len(self.test_loader.dataset)
self.stats['acc']['test'] = test_acc / len(self.test_loader.dataset)
def get_train_stats(self):
return self.stats
class ModelGS(ModelDA):
""" Model with gradient surgery. """
def __init__(self, device, dataset, args):
super().__init__(device, dataset, args)
self.grad_fn = get_agreement_func(args.method)
def _train_step(self, train_batches):
is_train = True
train_loss, train_acc = 0.0, 0.0
domain_grads = []
self.network.train(is_train)
self.optimizer.zero_grad()
with torch.set_grad_enabled(is_train):
for batch in train_batches:
inputs, targets = batch
outputs = self.network(inputs)
loss = self.loss_fn(outputs, targets)
loss.backward()
domain_grads.append(self.network.get_grads())
predictions = torch.max(outputs, 1)[1]
train_loss += loss.item()
train_acc += (predictions == targets).float().mean().item()
self.optimizer.zero_grad()
new_grads = self.grad_fn(domain_grads) # Modify gradients according to grad_fn
self.network.set_grads(new_grads) # Update gradients
self.optimizer.step() # Update model parameters
train_loss /= len(train_batches)
train_acc /= len(train_batches)
return train_loss, train_acc