-
Notifications
You must be signed in to change notification settings - Fork 0
/
xus_client.py
318 lines (254 loc) · 10.4 KB
/
xus_client.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import numpy as np
from collections import OrderedDict
import warnings
import torch
from torch.utils.tensorboard import SummaryWriter
import time
from config import *
from tqdm import tqdm
import utils.loss
from utils.logs import Logger
from sklearn.metrics import accuracy_score, roc_auc_score
import flwr as fl
def getACC(y_true, y_score, task, threshold=0.5):
'''Accuracy metric.
:param y_true: the ground truth labels, shape: (n_samples, n_labels) or (n_samples,) if n_labels==1
:param y_score: the predicted score of each class,
shape: (n_samples, n_labels) or (n_samples, n_classes) or (n_samples,) if n_labels==1 or n_classes==1
:param task: the task of current dataset
:param threshold: the threshold for multilabel and binary-class tasks
'''
y_true = y_true.squeeze()
y_score = y_score.squeeze()
if task == 'multi-label, binary-class':
y_pre = y_score > threshold
acc = 0
for label in range(y_true.shape[1]):
label_acc = accuracy_score(y_true[:, label], y_pre[:, label])
acc += label_acc
ret = acc / y_true.shape[1]
elif task == 'binary-class':
if y_score.ndim == 2:
y_score = y_score[:, -1]
else:
assert y_score.ndim == 1
ret = accuracy_score(y_true, y_score > threshold)
else:
ret = accuracy_score(y_true, np.argmax(y_score, axis=-1))
return ret
def getAUC(y_true, y_score, task):
'''AUC metric.
:param y_true: the ground truth labels, shape: (n_samples, n_labels) or (n_samples,) if n_labels==1
:param y_score: the predicted score of each class,
shape: (n_samples, n_labels) or (n_samples, n_classes) or (n_samples,) if n_labels==1 or n_classes==1
:param task: the task of current dataset
'''
y_true = y_true.squeeze()
y_score = y_score.squeeze()
if task == 'multi-label, binary-class':
auc = 0
for i in range(y_score.shape[1]):
label_auc = roc_auc_score(y_true[:, i], y_score[:, i])
auc += label_auc
ret = auc / y_score.shape[1]
elif task == 'binary-class':
if y_score.ndim == 2:
y_score = y_score[:, -1]
else:
assert y_score.ndim == 1
ret = roc_auc_score(y_true, y_score)
else:
auc = 0
for i in range(y_score.shape[1]):
y_true_binary = (y_true == i).astype(float)
y_score_binary = y_score[:, i]
auc += roc_auc_score(y_true_binary, y_score_binary)
ret = auc / y_score.shape[1]
return ret
# 实例化训练日志
log_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
logpath = './logs/log/'
if not os.path.exists(logpath):
os.makedirs(logpath)
logname = os.path.basename(__file__).split(
".")[0] + "_" + task + "_" + model + "_" + str(log_time) + "_log.txt"
logfile = os.path.join(logpath, logname)
log = Logger(logfile, level='info')
warnings.filterwarnings("ignore", category=UserWarning)
if task == "classification" and model == "DenseNet3D":
from network.DenseNet3D import DenseNet3d as Net
if task == "classification" and model == "DenseNet":
from network.DenseNet import DenseNet as Net
if task == "segmentation" and model == "UNet3D":
from network.UNet3D import UNet3D as Net
if data_random_split:
from utils.dataset import load_random_split_data as load_data
trainloader, testloader, num_examples = load_data(data_path)
else:
from utils.dataset import load_data_from_path as load_data
trainloader, testloader, num_examples = load_data(trainset, testset)
def train(net, trainloader, epochs):
"""Train the network on the training set."""
# 损失函数
global criterion, optimizer
if loss_func == "cross_entropy_3d":
criterion = utils.loss.CrossEntropy3D().to(DEVICE)
if loss_func == "dice_loss":
n_classes = num_classes
criterion = utils.loss.DiceLoss(n_classes).to(DEVICE)
if loss_func == "cross_entropy":
criterion = torch.nn.CrossEntropyLoss().to(DEVICE)
if loss_func == "mse":
criterion = torch.nn.MSELoss().to(DEVICE)
# 优化器
if optimiz == "adam":
optimizer = torch.optim.Adam(net.parameters(), lr, betas, eps,
weight_decay, amsgrad)
if optimiz == "sgd":
optimizer = torch.optim.SGD(net.parameters(), lr, momentum, dampening,
weight_decay, nesterov)
# 评价参数初始化
correct, total, loss, accuracy, running_loss_ = 0, 0, 0.0, 0.0, 0.0
y_true = np.array([])
y_score = np.array([])
y_score = None
# 迭代训练
for _ in range(0, epochs):
# 当前训练轮次loss初始化:
running_loss = 0.0
for images, labels in tqdm(trainloader):
images = images.to(DEVICE)
labels = labels.to(DEVICE)
# print("images shape: ", images.shape)
# images = images.half()
outputs = net(images)
print("================", outputs.shape)
pred = torch.argmax(outputs.data, dim=1)
print("=======pred========", pred.shape)
loss = criterion(outputs, labels)
running_loss += loss
correct += (pred == labels).sum().item()
total += labels.size(0)
optimizer.zero_grad()
loss.backward()
optimizer.step()
optimizer.zero_grad(set_to_none=True)
y_true = np.append(y_true, labels.cpu().detach().numpy())
if y_score is None:
y_score = outputs.cpu().detach().numpy().reshape(outputs.shape)
else:
y_score = np.vstack([
y_score,
outputs.cpu().detach().numpy().reshape(outputs.shape)
])
print(y_true.shape, y_score.shape)
del outputs, pred, images, labels
torch.cuda.empty_cache()
for para in net.parameters():
print("net model params: ", para)
# y_true = y_true.numpy()
# y_score = y_score.detach().numpy()
running_loss_ = running_loss / len(trainloader)
if task == "segmentation":
accuracy = correct / (total * img_size[0] * img_size[1] *
img_size[2])
auc = 0.0
if task == "classification":
accuracy = correct / total
try:
auc = getAUC(y_true, y_score, task=class_number_type)
except:
auc = 0.0
print(
"epoch {}/{}, training loss: {}, training accuracy: {}".format(
str(_ + 1), str(epochs), running_loss_, accuracy))
log.logger.info(
"epoch: {}/{}, training loss: {}, training accuracy: {}".format(
str(_ + 1), str(epochs), running_loss_, accuracy))
return running_loss_, accuracy, auc
def test(net, testloader):
"""Validate the network on the entire test set."""
# 损失函数
global criterion
if loss_func == "cross_entropy_3d":
criterion = utils.loss.CrossEntropy3D().to(DEVICE)
if loss_func == "dice_loss":
n_classes = num_classes
criterion = utils.loss.DiceLoss(n_classes).to(DEVICE)
if loss_func == "cross_entropy":
criterion = torch.nn.CrossEntropyLoss().to(DEVICE)
if loss_func == "mse":
criterion = torch.nn.MSELoss().to(DEVICE)
# 评价参数初始化
correct, total, loss, accuracy = 0, 0, 0.0, 0.0
y_true = np.array([])
y_score = None
# 迭代测试
with torch.no_grad():
for data in tqdm(testloader):
images, labels = data[0].to(DEVICE), data[1].to(DEVICE)
# images = images.half()
outputs = net(images)
loss += criterion(outputs, labels).item()
pred = torch.argmax(outputs.data, dim=1)
correct += (pred == labels).sum().item()
total += labels.size(0)
y_true = np.append(y_true, labels.cpu().detach().numpy())
if y_score is None:
y_score = outputs.cpu().detach().numpy().reshape(outputs.shape)
else:
y_score = np.vstack([
y_score,
outputs.cpu().detach().numpy().reshape(outputs.shape)
])
print(y_true.shape, y_score.shape)
del data, images, labels, pred, outputs
loss = loss / len(testloader)
if task == "segmentation":
accuracy = correct / (total * img_size[0] * img_size[1] * img_size[2])
auc = 0.0
if task == "classification":
accuracy = correct / total
#acc = getACC(y_true, y_score, task=class_number_type)
try:
auc = getAUC(y_true, y_score, task=class_number_type)
except:
auc = 0.0
return loss, accuracy, auc
# Load model and data (DenseNet, Local Data)
#net = Net().half().to(DEVICE)
# net = Net().to(DEVICE)
#net.eval()
net = Net()
# net.half()
# net.cuda()
# net.eval()
class FlowerClient(fl.client.NumPyClient):
def get_parameters(self, config):
return [val.cpu().numpy() for _, val in net.state_dict().items()]
def set_parameters(self, parameters):
params_dict = zip(net.state_dict().keys(), parameters)
state_dict = OrderedDict({k: torch.tensor(v) for k, v in params_dict})
net.load_state_dict(state_dict, strict=True)
def fit(self, parameters, config):
self.set_parameters(parameters)
loss, accuracy, auc = train(net, trainloader, epochs)
return self.get_parameters(config={}), len(trainloader.dataset), {
"loss=" + str(float(auc)) + "_" + "accuracy": accuracy
}
# return self.get_parameters(config={}), len(trainloader.dataset), {tuple([loss, auc]): accuracy}
# return self.get_parameters(config={}), len(trainloader.dataset), {"loss": loss, "accuracy": accuracy}
def evaluate(self, parameters, config):
self.set_parameters(parameters)
# param_path = os.path.join(save_path, f"model_round_{server_round}_{task}_{model}.pth")
# param_path = f"./save_model/model_round_99_classification_DenseNet.pth"
param_path = model_path
#net.load_state_dict(torch.load(param_path))
loss, accuracy, auc = test(net, testloader)
return auc, len(testloader.dataset), {"accuracy": accuracy}
# Start Flower client
fl.client.start_numpy_client(
server_address=server_address,
client=FlowerClient(),
)