-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvClient.py
165 lines (121 loc) · 6.47 KB
/
advClient.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
import copy
import torch
from client import Client
from torch import optim
from torch.nn import Softmax2d, LogSoftmax
from torch.nn import NLLLoss
from tqdm import tqdm
from utils.loss_utils import KnowledgeDistillationLoss
class AdvClient(Client):
def __init__(self, args, dataset, model, discriminator, test_client=False):
super().__init__(args, dataset, model, test_client)
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)
if self.args.step == '5c':
self.kd_loss = KnowledgeDistillationLoss(reduction='mean', alpha=self.args.alpha_kd)
def run_epoch(self, cur_epoch, self_trainer=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
:param self_trainer: SelfTrainer class instance to use pseudo labels instead of the actual ones
"""
for batch_id, (images, labels) in enumerate(self.train_loader):
images, labels = images.to(self.args.device), labels.to(self.args.device)
if self_trainer is not None:
# no discriminator training
# thus note that the self.discriminator is the same of the centralized one
######################
# GENERATOR TRAINING #
######################
self.optimizerG.zero_grad()
outputs = self.get_outputs(images)
cpmap = Softmax2d()(outputs)
cpmaplsmax = LogSoftmax(dim=1)(outputs)
# substitute actual labels with the pseudo ones
labels, pred, mask = self_trainer.get_pseudolab_pred_mask(images)
# semi supervised loss
LGsemi = self.loss_function(cpmaplsmax,labels)
# knowledge distillation loss
LGkd = self.kd_loss(outputs, pred, mask=mask)
conf = LogSoftmax(dim=1)(self.discriminator(cpmap))
N, _, H, W = cpmap.size()
targetr = torch.ones((N,H,W), dtype=torch.long).to(self.args.device)
# adversarial loss
LGadv = self.loss_function(conf, targetr)
self.args.wandb.log({'LGsemi': LGsemi}, commit=False, step=cur_epoch+1)
self.args.wandb.log({'LGkd': LGkd}, commit=False, step=cur_epoch+1)
self.args.wandb.log({'LGadv': LGadv}, commit=True, step=cur_epoch+1)
print(f"""
{LGsemi =}
{LGkd =}
{LGadv =}
----------
""")
LGseg = LGsemi + self.args.lam_adv * LGadv + self.args.lam_kd * LGkd
LGseg.backward()
self.optimizerG.step()
else: # train with ground truth labels
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 = self.loss_function(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()
######################
# 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()
def train(self, self_trainer=None):
"""
This method locally trains the model with the dataset of the client. It handles the training at epochs level
(by calling the run_epoch method for each local epoch of training)
:param self_trainer: SelfTrainer class instance to use pseudo labels instead of the actual ones
:return: length of the local dataset, copy of the model parameters
"""
schedulerG = self.get_scheduler(self.optimizerG, self.args.schedule)
schedulerD = self.get_scheduler(self.optimizerD, self.args.schedule)
# set models in training mode
self.model.train()
self.discriminator.train()
for epoch in range(self.args.num_epochs):
self.run_epoch(epoch, self_trainer)
schedulerD.step()
schedulerG.step()
return len(self.dataset), copy.deepcopy(self.model.state_dict()), copy.deepcopy(self.discriminator.state_dict())
def set_parameters_discriminator(self, parameters):
self.discriminator.load_state_dict(parameters)