-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvCentralized.py
171 lines (117 loc) · 6.08 KB
/
advCentralized.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
import random
import torch.nn.functional as F
import torch
import utils.style_transfer as st
from torch.utils.data import DataLoader
from torch import optim
from tqdm import tqdm
from torch.nn import Softmax2d, LogSoftmax
from torch.nn import NLLLoss, CrossEntropyLoss
from utils.checkpointSaver import CheckpointSaver
from fdaCentralized import FdaCentralized
class AdvCentralized(FdaCentralized):
def __init__(self, args, generator, discriminator, training_dataset, metric, clients=None, b=None, L=None):
super().__init__(args, generator, training_dataset, metric, clients=clients, b=b, L=L)
self.discriminator = discriminator
self.optimizerG = optim.SGD(self.model.parameters(), lr=args.lr, momentum=args.m, weight_decay=args.wd)
self.optimizerD = optim.Adam(self.discriminator.parameters(), lr=args.lr, betas=(0.9, 0.99))
self.loss_function = NLLLoss(ignore_index=255)
self.cross_entropy_loss = CrossEntropyLoss(ignore_index=255, reduction='none')
def run_epoch(self, cur_epoch, eval_datasets=None, eval_metric=None):
"""
This method locally trains the model with the dataset of the client. It handles the training at mini-batch level
:param cur_epoch: current epoch of training
:param optimizer: optimizer used for the local training
:return: the average loss of the epoch
"""
lossD = 0
total_samples = 0
i = 0
for batch_id, (images, labels) in enumerate(self.train_loader):
images, labels = images.to(self.args.device), labels.to(self.args.device)
total_samples += self.args.bs
ohlabels = labels.detach().clone()
# one-hot encoded label
ohlabels[ohlabels == 255] = 15
# print(label.unique())
ohlabels = torch.nn.functional.one_hot(ohlabels).permute((0, 3, 1, 2))
with torch.no_grad():
outputs = self.get_outputs(images)
cpmap = Softmax2d()(outputs)
N, _, H, W = cpmap.size()
# Generate the Real and Fake Labels
targetf = torch.zeros((N,H,W), dtype=torch.long, requires_grad=False).to(self.args.device)
targetr = torch.ones((N,H,W), dtype=torch.long, requires_grad=False).to(self.args.device)
##########################
# DISCRIMINATOR TRAINING #
##########################
self.optimizerD.zero_grad()
# Train on Real
confr = LogSoftmax(dim=1)(self.discriminator(ohlabels.float()))
# compute D loss on real
LDr = NLLLoss(ignore_index=15)(confr,targetr)
# compute gradient
LDr.backward()
# Train on Fake
conff = LogSoftmax(dim=1)(self.discriminator(cpmap.data))
# compute D loss on G output (fake)
LDf = self.loss_function(conff,targetf)
# compute gradient
LDf.backward()
self.optimizerD.step()
lossD += LDr + LDf
######################
# GENERATOR TRAINING #
######################
self.optimizerG.zero_grad()
outputs = self.get_outputs(images)
cpmap = Softmax2d()(outputs)
cpmaplsmax = LogSoftmax(dim=1)(outputs)
conff = LogSoftmax(dim=1)(self.discriminator(cpmap))
LGce = self.loss_function(cpmaplsmax,labels)
LGadv = self.loss_function(conff,targetr)
LGseg = LGce + self.args.lam_adv *LGadv
LGseg.backward()
self.optimizerG.step()
self.update_metric(self.metric, outputs, labels)
return lossD / len(self.train_loader)
def train(self, eval_datasets=None, eval_metric=None, save=True):
schedulerG = self.get_scheduler(self.optimizerG, self.args.schedule)
schedulerD = self.get_scheduler(self.optimizerD, self.args.schedule)
# set G in training mode
self.model.train()
self.discriminator.train()
validate = False
if eval_datasets is not None and eval_metric is not None:
validate = True
if save:
# initialize checkpoints saver
checkpoint_saver = CheckpointSaver(dirpath='./saved_models', args=self.args, decreasing=False, top_n=1)
checkpoint_saver_D = CheckpointSaver(dirpath='./saved_models', args=self.args, decreasing=True, top_n=1)
for epoch in tqdm(range(self.args.num_epochs), total=self.args.num_epochs):
self.metric.reset()
lossD = self.run_epoch(epoch, eval_datasets, eval_metric)
self.metric.get_results()
self.args.wandb.log({'train': self.metric.results}, commit=not validate, step=epoch+1)
if validate:
eval_metric.reset()
if isinstance(eval_datasets, list):
total_samples = 0
total_loss = 0
for ds in eval_datasets:
# note that eval_metric update itself for each dataset, without resetting
loss_eval = self.test(ds, eval_metric)
n_samples = len(ds)
total_samples += n_samples
total_loss += loss_eval * n_samples
loss_eval = total_loss / total_samples
else:
loss_eval = self.test(eval_datasets, eval_metric)
self.args.wandb.log({'eval': eval_metric.results | {'loss': loss_eval}}, commit=True, step=epoch+1)
if save:
checkpoint_saver(self.model, eval_metric.results["Mean IoU"], epoch+1)
checkpoint_saver_D(self.discriminator, lossD, epoch+1, is_model = False)
# set the model again in train mode
self.model.train()
schedulerD.step()
schedulerG.step()