forked from rahilgholami/ASSISTO-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
277 lines (227 loc) · 11.4 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
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
# Copyright (c) Rahil Gholamipoor
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
## Utilities
from __future__ import print_function
import argparse
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]= "3,4,5"
## Libraries
import random
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.metrics import roc_curve, auc
## Torch
import torch
torch.set_num_threads(6)
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
## Custom Imports
from src.utils import *
from src.utils import SupConLoss
from src.resnet1d import ResNet1D
device = 'cuda'
#############################################################
def get_data(x):
# device provides quality score for some specific parameters. These parameters are encoded with the respective quality index
# into a multi-dimensional vector space
HR = x[:, 0, :] # Hear Rate
HR_q = x[:, 1, :] # quality index
HR_q_onehot = F.one_hot(HR_q.view(1, -1), 101).view(HR_q.size(0), HR_q.size(1), 101)
HR = torch.mul(HR.unsqueeze(2).float(), HR_q_onehot.float())
HR = HR.permute(0, 2, 1)
SPo2 = x[:, 2, :] # Oxygen saturation
SPo2_q = x[:, 3, :] # quality index
SPo2_q_onehot = F.one_hot(SPo2_q.reshape(1, -1), 101).reshape(SPo2_q.size(0), SPo2_q.size(1), 101)
SPo2 = torch.mul(SPo2.unsqueeze(2).float() , SPo2_q_onehot.float())
SPo2 = SPo2.permute(0, 2, 1)
B_p = x[:, 4, :].float().unsqueeze(1) # Blood Perfusion
activity_value = x[:, 6, :].float().unsqueeze(1)
#steps = x[:, 8, :].float().unsqueeze(1)
B_w = x[:, 9, :].float().unsqueeze(1) # Blood pressure wave
HRV = x[:, 10, :] # Heart rate variability
HRV_q = x[:, 11, :] # quality index
HRV_q_onehot = F.one_hot(HRV_q.view(1, -1), 101).view(HRV_q.size(0), HRV_q.size(1), 101)
HRV = torch.mul(HRV.unsqueeze(2).float(), HRV_q_onehot.float())
HRV = HRV.permute(0, 2, 1)
RR = x[:, 12, :] # Respiration rate
RR_q = x[:, 13, :] # quality index
RR_q_onehot = F.one_hot(RR_q.view(1, -1), 101).view(RR_q.size(0), RR_q.size(1), 101)
RR = torch.mul(RR.unsqueeze(2).float() , RR_q_onehot.float())
RR = RR.permute(0, 2, 1)
Energy = x[:, 14, :]
Energy_q = x[:, 15, :] # quality index
Energy_q_onehot = F.one_hot(Energy_q.view(1, -1), 101).view(Energy_q.size(0), Energy_q.size(1), 101)
Energy = torch.mul(Energy.unsqueeze(2).float() , Energy_q_onehot.float())
Energy = Energy.permute(0, 2, 1)
local_temp_1 = ((x[:, 18, :].float())/100).unsqueeze(1) # Temperature
local_temp_2 = ((x[:, 19, :].float())/100).unsqueeze(1) # Temperature
object_temp_1 = ((x[:, 20, :].float())/100).unsqueeze(1) # Temperature
object_temp_2 = ((x[:, 21, :].float())/10).unsqueeze(1) # Temperature
data = torch.cat((HR, SPo2, B_p, activity_value, B_w, HRV, RR, Energy, local_temp_1, local_temp_2,
object_temp_1, object_temp_2), dim=1)
return data
#############################################################
def h_preparation(loader, model, device):
with torch.no_grad():
h_lst = []
for i, ((x, _, _, _, _)) in enumerate(loader):
x = (x.permute(0, 2, 1)).to(device)
h = model(get_data(x))
h_lst.append(h)
h_total = torch.cat(h_lst, dim=0)
return h_total
def find_x_best(h_query, h_ref):
'''
for a given test sample find the closet match from the training set in the embedding space(highest cosine similarity).
'''
with torch.no_grad():
norm_query = h_query.norm(dim=1).view(-1, 1)
h_query = h_query/norm_query
h_ref = h_ref/h_ref.norm(dim=1).view(-1, 1)
scores = torch.matmul(h_query, h_ref.transpose(1, 0)) #[query_bs, ref_bs]
cos_score, best_idx = scores.topk(1, dim=1) #select top k best
cos_norm_score = cos_score * norm_query
return cos_score.squeeze(), cos_norm_score.squeeze(), norm_query
def calculate_aucroc(s_1, s_2, name):
with torch.no_grad():
file = open(name+"_auroc.txt", "a+")
l1 = torch.zeros(s_1.size(0))
l2 = torch.ones(s_2.size(0))
label = torch.cat((l1, l2), dim=0).view(-1, 1).cpu().numpy()
scores = torch.cat((s_1, s_2), dim=0).cpu().numpy()
FPR, TPR, _ = roc_curve(label, scores, pos_label=0)
print(f"AUC: {auc(FPR, TPR)}")
file.write(f"AUC :{auc(FPR, TPR)} \r\n")
file.close()
return auc(FPR, TPR)
#############################################################
def test(args, model, exp, device, train_loader, test_loader, ood_loader):
model.eval()
print("in test")
with torch.no_grad():
h_train = h_preparation(train_loader, model, device)
h_test = h_preparation(test_loader, model, device)
h_ood = h_preparation(ood_loader, model, device)
test_cos, test_cos_norm, test_norm = find_x_best(h_test, h_train)
ood_cos, ood_cos_norm, ood_norm = find_x_best(h_ood, h_train)
cos_auc = calculate_aucroc(test_cos, ood_cos, f"results/{exp}/_CosineSim")
cos_norm_auc = calculate_aucroc(test_cos_norm, ood_cos_norm, f"results/{exp}/_CosineSim-Norm")
norm_auc = calculate_aucroc(test_norm, ood_norm, f"results/{exp}/_Norm")
return cos_auc
#############################################################
def train(args, model, device, loader, optimizer, epoch, writer):
model.train()
loader = tqdm(loader)
total_loss = 0
#criterion = SupConLoss(temperature=args.temperature, base_temperature=args.temperature)
for batch_idx, (x_1, x_2, labels) in enumerate(loader):
optimizer.zero_grad()
x_1 = (x_1.permute(0, 2, 1)).to(device)
x_2 = (x_2.permute(0, 2, 1)).to(device)
bsz = labels.shape[0]
x_all = torch.cat((get_data(x_1), get_data(x_2)), dim=0)
h = model(x_all)
h = normalize(h)
sim_matrix = torch.mm(h, h.t())
loss = NT_xent(sim_matrix, temperature=args.temperature)
#bsz = labels.shape[0]
#h1, h2 = torch.split(h, [bsz, bsz], dim=0)
#features = torch.cat([h1.unsqueeze(1), h2.unsqueeze(1)], dim=1) #[bsz, n_views, hdim]
#loss = criterion(features, labels.to(device))
loss.backward()
optimizer.step()
loader.set_description(f'Train epoch: {epoch}; loss: {loss.item():.5f}')
total_loss+=loss
with torch.no_grad():
ave_loss = total_loss/float(len(loader))
writer.add_scalar("Loss", ave_loss.item(), global_step=epoch, walltime=0.001)
#############################################################
def main():
## Settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--train_data', default=None, help='path to training data')
parser.add_argument('--test_data', default=None, help='path to test set')
parser.add_argument('--ood_data', default=None, help='path to ood data')
parser.add_argument('--epochs', type=int, default=500)
parser.add_argument('--save_step', type=int, default=10, help='Epoch steps to save model')
parser.add_argument('--temperature', type=int, default=0.07, help='temperature in the loss function')
parser.add_argument('--batch-size', type=int, default=128)
parser.add_argument('--lr', type=int, default=1e-3, help='learning rate')
parser.add_argument('--weight-decay', type=int, default=1e-3)
parser.add_argument('--window-size', type=int, default=1000, help='the number of measurements over time')
parser.add_argument('--hdim', type=int, default=128, help='dimension of latent space')
parser.add_argument('--input-channel', type=int, default=512, help='number of input channels')
parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
print('use_cuda is', use_cuda)
model = ResNet1D(
in_channels=args.input_channel,
base_filters=64,
kernel_size=16,
stride=2,
groups=32,
n_block=24,
n_classes=args.hdim,
downsample_gap=6,
increasefilter_gap=12,
use_do=True)
model = nn.DataParallel(model)
model = model.to(device)
## Loading the dataset
params = {'num_workers': 6,
'pin_memory': False} if use_cuda else {}
print('===> loading training dataset')
trainset = load_data(args.train_data)
training_set = RawDataset(trainset, args.window_size)
train_loader = data.DataLoader(training_set, batch_size=args.batch_size, shuffle=True, **params)
print("trainingset size:", len(training_set))
training_set1 = RawDataset(trainset, args.window_size, train=False)
train_loader1 = data.DataLoader(training_set1, batch_size=args.batch_size, shuffle=False)
print("trainingset size:", len(training_set1))
test_set = RawDataset(load_data(args.test_data), args.window_size, train=False)
test_loader = data.DataLoader(test_set, batch_size=args.batch_size, shuffle=False)
print("testset size:", len(test_set))
ood_set = RawDataset(load_data(args.ood_data), args.window_size, train=False)
ood_loader = data.DataLoader(ood_set, batch_size=args.batch_size, shuffle=False)
print("oodset size:", len(ood_set))
## optimizer
optimizer = optim.Adam(
filter(lambda p: p.requires_grad, model.parameters()),
betas=(0.9, 0.98), eps=1e-09, lr= args.lr, weight_decay=args.weight_decay, amsgrad=True)
model_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('===> Model total parameter: {}\n'.format(model_params))
exp = f'train_NXent_tem{args.temperature}_w{args.window_size}'
if not os.path.isdir('runs'):
os.makedirs('runs')
if not os.path.isdir(f'results/{exp}'):
os.makedirs(f'results/{exp}')
if not os.path.isdir(f'ckpts/{exp}'):
print("Creating the directory")
os.makedirs(f'ckpts/{exp}')
writer = SummaryWriter(f'runs/{exp}')
## Start training
for epoch in range(args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch, writer)
torch.save(
{'model': model.module.state_dict(), 'args': args},
f'ckpts/{exp}/h_net.pt')
if epoch%args.save_step == 0:
cos_auc = test(args, model, exp, device, train_loader1, test_loader, ood_loader)
writer.close()
if __name__ == '__main__':
main()