forked from remotebiosensing/rppg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·291 lines (261 loc) · 12.3 KB
/
main.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
import copy
import datetime
import json
import time
import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, random_split
from torchmeta.utils.data import BatchMetaDataLoader
from tqdm import tqdm
from dataset.dataset_loader import dataset_loader
from log import log_info_time
from loss import loss_fn
from models import is_model_support, get_model, summary
from optim import optimizers
from torch.optim import lr_scheduler
from utils.dataset_preprocess import preprocessing
from utils.funcs import normalize, plot_graph, detrend
from nets.models.Meta import Meta
with open('meta_params.json') as f:
jsonObject = json.load(f)
__PREPROCESSING__ = jsonObject.get("__PREPROCESSING__")
__TIME__ = jsonObject.get("__TIME__")
__MODEL_SUMMARY__ = jsonObject.get("__MODEL_SUMMARY__")
options = jsonObject.get("options")
params = jsonObject.get("params")
hyper_params = jsonObject.get("hyper_params")
model_params = jsonObject.get("model_params")
meta_params = jsonObject.get("meta_params")
#
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]= "4,9"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('Device:', device) # 출력결과: cuda
print('Count of using GPUs:', torch.cuda.device_count()) #출력결과: 2 (2, 3 두개 사용하므로)
print('Current cuda device:', torch.cuda.current_device())
"""
Check Model Support
"""
is_model_support(model_params["name"], model_params["name_comment"])
'''
Setting Learning Model
'''
if __TIME__:
start_time = time.time()
model = get_model(model_params["name"])
if meta_params["pre_trained"] == 1:
print('Using pre-trained on all ALL AFRL!')
model.load_state_dict(torch.load('./checkpoints/AFRL_pretrained/meta_pretrained_all_AFRL.pth'))
else:
print('Not using any pretrained models')
model = model.cuda()
model = nn.DataParallel(model).to(device)
'''
if torch.cuda.is_available():
# os.environ["CUDA_VISIBLE_DEVICES"] = '0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
# TODO: implement parallel training
# if options["parallel_criterion"] :
# print(options["parallel_criterion_comment"])
# model = DataParallelModel(model, device_ids=[0, 1, 2])
# else:
# model = DataParallel(model, output_device=0)
device = torch.device('cuda:9')
model.to(device=device)
else:
model = model.to('cpu')
'''
if __MODEL_SUMMARY__:
summary(model,model_params["name"])
if __TIME__:
log_info_time("model initialize time \t: ", datetime.timedelta(seconds=time.time() - start_time))
'''
Generate preprocessed data hpy file
'''
if __PREPROCESSING__:
if __TIME__:
start_time = time.time()
preprocessing(save_root_path=params["save_root_path"],
model_name=model_params["name"],
data_root_path=params["data_root_path"],
dataset_name=params["dataset_name"],
train_ratio=params["train_ratio"])
if __TIME__:
log_info_time("preprocessing time \t:", datetime.timedelta(seconds=time.time() - start_time))
'''
Load dataset before using Torch DataLoader
'''
if __TIME__:
start_time = time.time()
dataset = dataset_loader(save_root_path=params["save_root_path"],
model_name=model_params["name"],
dataset_name=params["dataset_name"],
option="train",
num_shots=meta_params["num_shots"],
num_test_shots=meta_params["num_test_shots"],
unsupervised=meta_params["unsupervised"]
)
train_dataset, validation_dataset = random_split(dataset,
[int(np.floor(
len(dataset) * params["validation_ratio"])),
int(np.ceil(
len(dataset) * (1 - params["validation_ratio"])))]
)
if __TIME__:
log_info_time("load train hpy time \t: ", datetime.timedelta(seconds=time.time() - start_time))
if __TIME__:
start_time = time.time()
test_dataset = dataset_loader(save_root_path=params["save_root_path"],
model_name=model_params["name"],
dataset_name=params["dataset_name"],
option="test",
num_shots=meta_params["num_shots"],
num_test_shots=meta_params["num_test_shots"],
unsupervised=meta_params["unsupervised"]
)
if __TIME__:
log_info_time("load test hpy time \t: ", datetime.timedelta(seconds=time.time() - start_time))
'''
Call dataloader for iterate dataset
'''
if __TIME__:
start_time = time.time()
if model_params["name"] == 'MetaPhys' or 'MetaPhysNet':
train_loader = BatchMetaDataLoader(train_dataset, batch_size=params["train_batch_size"],
shuffle=params["train_shuffle"])
validation_loader = BatchMetaDataLoader(validation_dataset, batch_size=params["train_batch_size"],
shuffle=params["train_shuffle"])
else:
train_loader = DataLoader(train_dataset, batch_size=params["train_batch_size"],
shuffle=params["train_shuffle"])
validation_loader = DataLoader(validation_dataset, batch_size=params["train_batch_size"],
shuffle=params["train_shuffle"])
test_loader = DataLoader(test_dataset, batch_size=params["test_batch_size"],
shuffle=params["test_shuffle"])
if __TIME__:
log_info_time("generate dataloader time \t: ", datetime.timedelta(seconds=time.time() - start_time))
'''
Setting Loss Function
'''
if __TIME__:
start_time = time.time()
criterion = loss_fn(hyper_params["loss_fn"])
inner_criterion = loss_fn(meta_params["inner_loss"])
outer_criterion = loss_fn(meta_params["outer_loss"])
# if torch.cuda.is_available():
# TODO: implement parallel training
# if options["parallel_criterion"] :
# print(options["parallel_criterion_comment"])
# criterion = DataParallelCriterion(criterion,device_ids=[0, 1, 2])
if __TIME__:
log_info_time("setting loss func time \t: ", datetime.timedelta(seconds=time.time() - start_time))
'''
Setting Optimizer
'''
if __TIME__:
start_time = time.time()
#optimizer = optimizers(model.parameters(), hyper_params["learning_rate"], hyper_params["optimizer"])
#scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.99)
if __TIME__:
log_info_time("setting optimizer time \t: ", datetime.timedelta(seconds=time.time() - start_time))
'''
Model Training Step
'''
min_val_loss = 100.0
min_val_loss_model = None
for epoch in range(hyper_params["epochs"]):
if __TIME__ and epoch == 0:
start_time = time.time()
if model_params["name"] == 'MetaPhys' or 'MetaPhysNet':
Meta(model, train_loader, validation_loader, inner_criterion)
else:
with tqdm(train_loader, desc="Train ", total=len(train_loader)) as tepoch:
model.train()
running_loss = 0.0
i = 0
for inputs, target in tepoch:
tepoch.set_description(f"Train Epoch {epoch}")
outputs = model(inputs)
if model_params["name"] in ["PhysNet", "PhysNet_LSTM","DeepPhys"]:
loss = criterion(outputs, target)
else:
loss_0 = criterion(outputs[:][0], target[:][0])
loss_1 = criterion(outputs[:][1], target[:][1])
loss_2 = criterion(outputs[:][2], target[:][2])
loss = loss_0 + loss_2 + loss_1
if ~torch.isfinite(loss):
continue
optimizer.zero_grad()
loss.backward()
running_loss += loss.item()
optimizer.step()
tepoch.set_postfix(loss=running_loss / params["train_batch_size"])
if __TIME__ and epoch == 0:
log_info_time("1 epoch training time \t: ", datetime.timedelta(seconds=time.time() - start_time))
with tqdm(validation_loader, desc="Validation ", total=len(validation_loader)) as tepoch:
model.eval()
running_loss = 0.0
with torch.no_grad():
for inputs, target in tepoch:
tepoch.set_description(f"Validation")
outputs = model(inputs)
if model_params["name"] in ["PhysNet", "PhysNet_LSTM", "DeepPhys"]:
loss = criterion(outputs, target)
else:
loss_0 = criterion(outputs[:][0], target[:][0])
loss_1 = criterion(outputs[:][1], target[:][1])
loss_2 = criterion(outputs[:][2], target[:][2])
loss = loss_0 + loss_2 + loss_1
if ~torch.isfinite(loss):
continue
running_loss += loss.item()
tepoch.set_postfix(loss=running_loss / params["train_batch_size"])
if min_val_loss > running_loss: # save the train model
min_val_loss = running_loss
checkpoint = {'Epoch': epoch,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict()}
torch.save(checkpoint, params["checkpoint_path"] + model_params["name"] + "/"
+ params["dataset_name"] + "_" + str(epoch) + "_"
+ str(min_val_loss) + '.pth')
min_val_loss_model = copy.deepcopy(model)
if epoch + 1 == hyper_params["epochs"] or epoch % 10 == 0:
if __TIME__ and epoch == 0:
start_time = time.time()
if epoch + 1 == hyper_params["epochs"]:
model = min_val_loss_model
with tqdm(test_loader, desc="test ", total=len(test_loader)) as tepoch:
model.eval()
inference_array = []
target_array = []
with torch.no_grad():
for inputs, target in tepoch:
tepoch.set_description(f"test")
outputs = model(inputs)
if model_params["name"] in ["PhysNet", "PhysNet_LSTM", "DeepPhys"]:
loss = criterion(outputs, target)
else:
loss_0 = criterion(outputs[:][0], target[:][0])
loss_1 = criterion(outputs[:][1], target[:][1])
loss_2 = criterion(outputs[:][2], target[:][2])
loss = loss_0 + loss_2 + loss_1
if ~torch.isfinite(loss):
continue
running_loss += loss.item()
tepoch.set_postfix(loss=running_loss / (params["train_batch_size"] / params["test_batch_size"]))
if model_params["name"] in ["PhysNet","PhysNet_LSTM"]:
inference_array.extend(normalize(outputs.cpu().numpy()[0]))
target_array.extend(normalize(target.cpu().numpy()[0]))
else:
inference_array.extend(outputs[:][0].cpu().numpy())
target_array.extend(target[:][0].cpu().numpy())
if tepoch.n == 0 and __TIME__:
save_time = time.time()
# postprocessing
if model_params["name"] in ["DeepPhys"]:
inference_array = detrend(np.cumsum(inference_array),100)
target_array = detrend(np.cumsum(target_array),100)
if __TIME__ and epoch == 0:
log_info_time("inference time \t: ", datetime.timedelta(seconds=save_time - start_time))
plot_graph(0, 300, target_array, inference_array)