-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
239 lines (176 loc) · 9.43 KB
/
train.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset, Subset, ConcatDataset
from dataset.dataSetSplit import DatasetSplit
import torch.optim as optim
from IIoTmodel import DNN
import numpy as np
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report
from al_strategies.entropySampling import EntropySampler
from al_strategies.marginSampling import MarginSampler
from al_strategies.leastConfidence import LeastConfidenceSampler
from al_strategies.randomSampling import RandomSampler
from torch.utils.data import TensorDataset
import pandas as pd
class DNNModel(object):
def __init__(self, args, model, train_dataset, labeled_dataset, test_dataset, idxs, labeled_idxs, logger):
self.args = args
self.logger = logger
self.train_loader = DataLoader(DatasetSplit(train_dataset, idxs), batch_size=args.batch_size, shuffle=True)
self.labeled_loader = DataLoader(DatasetSplit(labeled_dataset, labeled_idxs), batch_size=args.batch_size, shuffle=True)
self.test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False)
self.num_samples = args.num_samples
self.batch_size = args.batch_size
print("Hello")
print(len(self.train_loader))
print(len(self.train_loader) * args.batch_size)
self.labeled_dataset = labeled_dataset
self.labeled_idxs = labeled_idxs
self.train_dataset = train_dataset
self.idxs = idxs
self.labeled_loader2 = DataLoader(DatasetSplit(labeled_dataset, labeled_idxs), shuffle=True)
self.subset_loader = DataLoader(DatasetSplit(train_dataset, idxs), shuffle=True)
print("train_dataset", len(self.subset_loader))
print("labeled_dataset", len(self.labeled_loader2))
self.device = args.device
# Default criterion set to NLL loss function
# self.criterion = nn.NLLLoss()
self.criterion = nn.CrossEntropyLoss()
# for one hot encoding
# self.criterion= nn.BCELoss()
self.client_epochs = args.client_epochs
self.net = model
self.optimizer = optim.Adam(self.net.parameters(), lr=args.lr)
self.history = {'train_loss': [], 'test_loss': []}
def train(self, model, training_loader):
mean_losses_superv = []
# self.net.train()
total = 0
correct = 0
for epoch in range(self.args.client_epochs):
h = np.array([])
for x, y, z in training_loader:
self.optimizer.zero_grad()
x = x.float()
output = self.net(x)
y = y.long()
loss = self.criterion(output, y)
h = np.append(h, loss.item())
# raise
# ===================backward====================
loss.backward()
self.optimizer.step()
output = output.argmax(axis=1)
total += y.size(0)
y = y.float()
output = output.float()
correct += (output == y).sum().item()
# raise
# ===================log========================
mean_loss_superv = np.mean(h)
train_acc = correct / total
mean_losses_superv.append(mean_loss_superv)
path = "state_dict_model_IIoT_edge.pt"
torch.save(self.net.state_dict(), path)
return sum(mean_losses_superv) / len(mean_losses_superv), train_acc, self.net.state_dict()
def train_with_sampling(self, model):
print("length of labeled dataset before AL", len(self.labeled_loader) * self.batch_size)
print("length of training dataset before AL", len(self.train_loader) * self.batch_size)
num_samples = int(self.num_samples * len(self.subset_loader))
print("length of num_samples", num_samples)
if(self.args.al_method == "entropysampling"):
self.entropy_sampler = EntropySampler(self.net)
unlabeled_indices = self.entropy_sampler.sample(self.args, self.subset_loader, num_samples)
elif(self.args.al_method == "marginsampling"):
self.margin_sampler = MarginSampler(self.net)
unlabeled_indices = self.margin_sampler.sample(self.args, self.subset_loader, num_samples)
elif(self.args.al_method == "randomsampling"):
self.random_sampler = RandomSampler(self.net)
unlabeled_indices = self.random_sampler.sample(self.args, self.subset_loader, num_samples)
else:
self.least_confidence_sampler = LeastConfidenceSampler(self.net)
unlabeled_indices = self.least_confidence_sampler.sample(self.args, self.subset_loader, num_samples)
# Get dataset consisting of labeled_idxs of the labeled dataset
labeled_split_dataset = DatasetSplit(self.labeled_dataset, self.labeled_idxs)
# Get dataset for unlabeled_idxs of the training dataset
unlabeled_split_dataset = DatasetSplit(self.train_dataset, unlabeled_indices)
combined_dataset = ConcatDataset([labeled_split_dataset, unlabeled_split_dataset])
combined_loader = DataLoader(combined_dataset, batch_size=self.args.batch_size, shuffle=True)
# Train the model on the combined dataset
loss, train_acc, w = self.train(self.net, combined_loader)
return loss, train_acc, w, unlabeled_indices
def test_inference(self, model, test_dataset):
model.load_state_dict(torch.load("state_dict_model_IIoT_edge.pt"))
self.net.eval()
test_loss = 0
correct = 0
total = 0
output_list = torch.zeros(0, dtype=torch.long)
target_list = torch.zeros(0, dtype=torch.long)
with torch.no_grad():
for data, target in self.test_loader:
data, target = data.to(self.args.device), target.to(self.args.device)
output = model(data.float())
batch_loss = self.criterion(output, target.long())
# raise
test_loss += batch_loss.item()
total += target.size(0)
target = target.float()
output = output.argmax(axis=1)
output = output.float()
output_list = torch.cat([output_list, output.view(-1).long()])
target_list = torch.cat([target_list, target.view(-1).long()])
correct += (output == target).sum().item()
test_loss /= total
acc = correct / total
f1score = f1_score(target_list, output_list, average="macro") # labels=np.unique(output_list))))
precision = precision_score(target_list, output_list, average="macro")
recall = recall_score(target_list, output_list, average="macro")
# Format the metrics to have six decimal places
f1score = format(f1score, ".6f")
precision = format(precision, ".6f")
recall = format(recall, ".6f")
class_report = classification_report(target_list, output_list, digits=4)
return acc, f1score, precision, recall, class_report, test_loss
def testglobal_inference(self, model, val_dataset):
self.val_loader = DataLoader(val_dataset, batch_size=self.args.batch_size, shuffle=False)
self.net = model
self.net.eval()
test_loss = 0
correct = 0
total = 0
output_list = torch.zeros(0, dtype=torch.long)
target_list = torch.zeros(0, dtype=torch.long)
with torch.no_grad():
for data, target in self.val_loader:
data, target = data.to(self.args.device), target.to(self.args.device)
output = model(data.float())
batch_loss = self.criterion(output, target.long())
# print("done... test...")
# raise
test_loss += batch_loss.item()
total += target.size(0)
target = target.float()
output = output.argmax(axis=1)
output = output.float()
output_list = torch.cat([output_list, output.view(-1).long()])
target_list = torch.cat([target_list, target.view(-1).long()])
correct += (output == target).sum().item()
test_loss /= total
acc = correct / total
f1score = f1_score(target_list, output_list, average="macro", zero_division=0)
precision = precision_score(target_list, output_list, average="macro", zero_division=0)
recall = recall_score(target_list, output_list, average="macro", zero_division=0)
# Format the metrics to have six decimal places
f1score = format(f1score, ".6f")
precision = format(precision, ".6f")
recall = format(recall, ".6f")
class_report = classification_report(target_list, output_list, digits=4)
# print(' F1 Score : ' + str(f1_score(target_list, output_list, average = "macro")))
# #labels=np.unique(output_list))))
# print(' Precision : '+str(precision_score(target_list, output_list,
# average="macro", labels=np.unique(output_list))))
# print(' Recall : '+str(recall_score(target_list, output_list, average="macro",
# labels=np.unique(output_list))))
# print("report", classification_report(target_list,output_list, digits=4))
return acc, f1score, precision, recall, class_report, test_loss