-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathperform_training.py
138 lines (119 loc) · 6.48 KB
/
perform_training.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 16 23:24:46 2020
@author: Dani Kiyasseh
"""
import torch
from tqdm import tqdm
from prepare_miscellaneous import obtain_contrastive_loss, flatten_arrays, calculate_auc, change_labels_type
#%%
""" Functions in this script:
1) contrastive_single
2) one_epoch_contrastive
3) one_epoch_finetuning
4) finetuning_single
"""
#%%
def contrastive_single(phase,inference,dataloaders,model,optimizer,device,weighted_sampling,epoch_count=None,new_task_epochs=None,trial=None,save_path_dir=None): #b/c it is single, models_list contains one model only
""" One Epoch's Worth of Training for Contrastive Learning Paradigm """
running_loss = 0.0
outputs_list = []
labels_list = []
modality_list = []
indices_list = []
task_names_list = []
pids_list = []
batch_num = 0
batch = 0
for inputs,labels,pids,modality,task_names,indices in tqdm(dataloaders[phase]):
batch += 1
""" Send Data to Device """
inputs = inputs.to(device)
labels = labels.to(device)
with torch.set_grad_enabled('train1' in phase):# and inference == False): #('train' in phase and inference == False)
outputs = model(inputs) #(BxHx2) in CPPC, (BxHx12) in CMLC, (BxHx24) in CMSMLC
loss = obtain_contrastive_loss(outputs,pids,trial)
""" Backpropagation and Update Step """
if phase == 'train1': #only perform backprop for train1 phase
loss.backward()
#for param in model.parameters():
# print(param.grad)
""" Network Parameters """
if isinstance(optimizer,tuple):
optimizer[0].step()
""" Task-Instance Parameters """
optimizer[1].step()
optimizer[0].zero_grad()
optimizer[1].zero_grad()
else:
optimizer.step()
optimizer.zero_grad()
""" Calculate Metrics """
running_loss += loss.item() * inputs.shape[0]
if labels.data.dtype != torch.long:
labels.data = labels.data.type(torch.long)
outputs_list.append(outputs.cpu().detach().numpy())
labels_list.append(labels.cpu().detach().numpy())
modality_list.append(modality)
indices_list.append(indices)
task_names_list.append(task_names)
pids_list.append(pids)
batch_num += 1
outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list = flatten_arrays(outputs_list,labels_list,modality_list,indices_list,task_names_list,pids_list)
epoch_loss = running_loss / len(dataloaders[phase].dataset)
return epoch_loss, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list
def one_epoch_contrastive(weighted_sampling,phase,inference,dataloader,model,optimizer,device,bptt_steps=0,epoch_count=None,new_task_epochs=None,trial=None,save_path_dir=None):
epoch_loss, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list = contrastive_single(phase,inference,dataloader,model,optimizer,device,weighted_sampling,epoch_count=epoch_count,new_task_epochs=new_task_epochs,trial=trial,save_path_dir=save_path_dir)
return {"epoch_loss": epoch_loss}, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list
def finetuning_single(phase,inference,dataloaders,model,optimizer,device,weighted_sampling,criterion,classification,epoch_count=None,new_task_epochs=None,trial=None,save_path_dir=None): #b/c it is single, models_list contains one model only
""" One Epoch's Worth of Training for Contrastive Learning Paradigm """
running_loss = 0.0
outputs_list = []
labels_list = []
modality_list = []
indices_list = []
task_names_list = []
pids_list = []
batch_num = 0
batch = 0
for inputs,labels,pids,modality,task_names,indices in tqdm(dataloaders[phase]):
batch += 1
""" Send Data to Device """
inputs = inputs.to(device)
labels = labels.to(device)
labels = change_labels_type(labels,criterion)
with torch.set_grad_enabled('train1' in phase):# and inference == False): #('train' in phase and inference == False)
outputs = model(inputs)
loss = criterion(outputs,labels)
""" Backpropagation and Update Step """
if phase == 'train1': #only perform backprop for train1 phase
loss.backward()
""" Network Parameters """
if isinstance(optimizer,tuple):
optimizer[0].step()
""" Task-Instance Parameters """
optimizer[1].step()
optimizer[0].zero_grad()
optimizer[1].zero_grad()
else:
optimizer.step()
optimizer.zero_grad()
""" Calculate Metrics """
running_loss += loss.item() * inputs.shape[0]
if labels.data.dtype != torch.long:
labels.data = labels.data.type(torch.long)
outputs_list.append(outputs.cpu().detach().numpy())
labels_list.append(labels.cpu().detach().numpy())
modality_list.append(modality)
indices_list.append(indices)
task_names_list.append(task_names)
pids_list.append(pids)
batch_num += 1
outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list = flatten_arrays(outputs_list,labels_list,modality_list,indices_list,task_names_list,pids_list)
epoch_loss = running_loss / len(dataloaders[phase].dataset)
epoch_auroc = calculate_auc(classification,outputs_list,labels_list,save_path_dir)
return epoch_loss, epoch_auroc, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list
def one_epoch_finetuning(weighted_sampling,phase,inference,dataloader,model,optimizer,device,criterion,classification,bptt_steps=0,epoch_count=None,new_task_epochs=None,trial=None,save_path_dir=None):
epoch_loss, epoch_auroc, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list = finetuning_single(phase,inference,dataloader,model,optimizer,device,weighted_sampling,criterion,classification,epoch_count=epoch_count,new_task_epochs=new_task_epochs,trial=trial,save_path_dir=save_path_dir)
return {"epoch_loss": epoch_loss, 'epoch_auroc': epoch_auroc}, outputs_list, labels_list, modality_list, indices_list, task_names_list, pids_list