-
Notifications
You must be signed in to change notification settings - Fork 1
/
federated_train_algorithms.py
159 lines (133 loc) · 6.91 KB
/
federated_train_algorithms.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
from train import train_model
import copy
import random
import torch
from cnn_nets import LENET, RESNET34
import torch.optim as optim
import numpy as np
from custom_loss_fns import BasicLoss_wrapper, FedProxLoss
class FedAvg():
def __init__(self, model, device, clients, valloader, optimizer, criterion, scheduler, n_classes, train_dataset_len, c_fraction, epochs=10, client_epochs=5):
self.best_model_wts = copy.deepcopy(model)
self.init_model = copy.deepcopy(model)
self.model = model
self.device = device
self.trainloaders = clients
self.valloader = valloader
self.optimizer = optimizer
self.criterion = BasicLoss_wrapper(criterion)
self.scheduler = scheduler
self.n_classes = n_classes
self.train_dataset_len = train_dataset_len
self.c_fraction = c_fraction
self.epochs = epochs
self.client_epochs = client_epochs
def train_federated_model(self):
best_acc = 0.0
stats = []
# iterate through epochs
for i in range(self.epochs):
# get random subset of clients
fraction = int(self.c_fraction * float(len(self.trainloaders)))
client_subset = random.sample(self.trainloaders, fraction)
# train each of the clients
model_client_list = []
print("----------------------------------")
print("Running epoch number " + str(i + 1))
for ind, client in enumerate(client_subset):
#model_for_client = RESNET34(n_classes)
model_for_client = copy.deepcopy(self.model)
# model_for_client.load_state_dict(model.state_dict())
model_for_client = model_for_client.to(self.device)
optimizer_ft = self.optimizer(
model_for_client.parameters(), lr=0.001, momentum=0.9)
exp_scheduler = self.scheduler(
optimizer_ft, step_size=7, gamma=0.1)
client_model, statistics = train_model(
model_for_client, self.device, client, self.criterion, optimizer_ft, exp_scheduler, self.n_classes, num_epochs=self.client_epochs, phase='train', valloader_for_train=None)
model_client_list.append(client_model)
print(
f"Done with client number {ind + 1} with stats: {statistics}")
#del model_for_client
# torch.cuda.empty_cache()
# AVERAGE MODELS
# first model as an ititializer
model_state = model_client_list[0].state_dict()
client_data_size = client_subset[0]['size']
fraction_data_size = client_data_size
for key in model_state:
model_state[key] = (client_data_size /
self.train_dataset_len) * model_state[key]
# newly trained models
for c in range(1, len(model_client_list)):
client_model_state = model_client_list[c].state_dict()
client_new_data_size = client_subset[c]['size']
fraction_data_size += client_new_data_size
for key in model_state:
model_state[key] += (client_new_data_size /
self.train_dataset_len) * client_model_state[key]
# untrained models
self.model = self.model.to(self.device)
rest_client_model_state = self.model.state_dict()
rest_clients_data_size = self.train_dataset_len - fraction_data_size
for key in model_state:
model_state[key] += (rest_clients_data_size / self.train_dataset_len) * rest_client_model_state[key]
# generated new averaged model
averagedModel = copy.deepcopy(self.init_model)
averagedModel.load_state_dict(model_state)
self.model = copy.deepcopy(averagedModel)
# validate
self.model, statistics = train_model(
self.model, self.device, self.valloader, self.criterion, None, None, self.n_classes, num_epochs=1, phase='val')
# deep copy the model
if statistics[4][0] > best_acc:
best_acc = statistics[4][0]
self.best_model_wts = copy.deepcopy(self.model)
print("Done with validation", statistics)
stats.append([statistics[2][0], statistics[3][0],statistics[4][0]])
return self.model, self.best_model_wts, np.array(stats)
class FedProx(FedAvg):
def __init__(self, model, device, clients, valloader, optimizer, criterion, scheduler, n_classes, train_dataset_len, c_fraction, mu=0, epochs=10, client_epochs=5):
super(FedProx, self).__init__(model, device, clients, valloader, optimizer,
criterion, scheduler, n_classes, train_dataset_len, c_fraction, epochs, client_epochs)
self.mu = mu
self.criterion = FedProxLoss(criterion, mu)
class BSP():
def __init__(self, model, device, clients, valloader, optimizer, criterion, scheduler, n_classes, train_dataset_len, epochs=10):
self.model = model
self.device = device
self.clients = clients
self.valloader = valloader
self.optimizer = optimizer
self.criterion = BasicLoss_wrapper(criterion)
self.scheduler = scheduler
self.n_classes = n_classes
self.train_dataset_len = train_dataset_len
self.epochs = epochs
def train_federated_model(self):
best_acc = 0.0
stats = []
# iterate through epochs
for i in range(self.epochs):
print("----------------------------------")
print("Running epoch number " + str(i + 1))
for ind, client in enumerate(self.clients):
optimizer_ft = self.optimizer(
self.model.parameters(), lr=0.001, momentum=0.9)
exp_scheduler = self.scheduler(
optimizer_ft, step_size=7, gamma=0.1)
self.model = self.model.to(self.device)
self.model, statistics = train_model(
self.model, self.device, client, self.criterion, optimizer_ft, exp_scheduler, self.n_classes, num_epochs=1, phase='train')
print(
f"Done with client number {ind + 1} with stats: {statistics}")
self.model = self.model.to(self.device)
self.model, statistics = train_model(
self.model, self.device, self.valloader, self.criterion, None, None, self.n_classes, num_epochs=1, phase='val')
# deep copy the model
if statistics[4][0] > best_acc:
best_acc = statistics[4][0]
self.best_model_wts = copy.deepcopy(self.model)
print("Done with validation", statistics)
stats.append([statistics[2][0], statistics[3][0], statistics[4][0]])
return self.model, self.best_model_wts, np.array(stats)