-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
477 lines (398 loc) · 14.5 KB
/
test.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# Based on https://github.com/pytorch/examples/tree/master/imagenet
import os
import sys
import random
import shutil
import logging
import argparse
from tqdm import tqdm, trange
import numpy as np
from scipy.stats import pearsonr
import torch
import torch.nn as nn
import torch.optim as optim
from torch.backends import cudnn
from torch.utils.data import DataLoader
from models import classifier
from utils.dataset import MAHNOBHCIDataset, VIPLHRDataset, UBFCDataset
from utils.utils import AverageMeter
from utils.augmentation import Transformer, RandomROI
parser = argparse.ArgumentParser()
# Training setting
parser.add_argument("--gpu", default=None, type=int)
parser.add_argument(
"--seed", default=None, type=int, help="seed for initializing training. "
)
parser.add_argument(
"--epochs", default=50, type=int, help="number of total epochs to run"
)
parser.add_argument("--batch_size", default=128, type=int)
parser.add_argument("--lr", default=1e-3, type=float, help="learning rate")
parser.add_argument("--wd", default=0, type=float, help="weight decay")
parser.add_argument("--dropout", default=0.0, type=float, help="ResNet dropout value")
# Test setting
parser.add_argument("--evaluate", action="store_true")
parser.add_argument("--scratch", action="store_true")
parser.add_argument(
"--pretrained", default="", type=str, help="path to pretrained checkpoint"
)
# Data setting
parser.add_argument("--dataset_name", default="mahnob-hci", type=str)
parser.add_argument("--dataset_dir", default=None, type=str)
parser.add_argument(
"--workers",
default=4,
type=int,
metavar="N",
help="number of data loading workers (default: 1)",
)
parser.add_argument("--vid_frame", default=150, type=int)
parser.add_argument("--vid_frame_stride", default=2, type=int)
# Log setting
parser.add_argument("--log_dir", default="./path/to/your/log", type=str)
parser.add_argument("--wandb", action="store_true")
parser.add_argument("--run_tag", nargs="+", default=None)
parser.add_argument("--run_name", default=None, type=str)
# Model setting
parser.add_argument("--model_depth", default=18, type=int)
parser.add_argument("--finetune", default="fc", type=str)
def main():
args = parser.parse_args()
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
logging.basicConfig(
filename=os.path.join(args.log_dir, "test_output.log"),
format="[%(asctime)s] %(levelname)s: %(message)s",
level=logging.DEBUG,
)
if args.seed is not None:
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
logging.info(
"You have chosen to seed training. "
"This will turn on the CUDNN deterministic setting, "
"which can slow down your training considerably! "
"You may see unexpected behavior when restarting "
"from checkpoints."
)
else:
cudnn.benchmark = True
if args.gpu is None:
logging.info("You have not specify a GPU, use the default value 0")
args.gpu = 0
# Log config
if args.wandb:
import wandb
wandb.init(
project="temp",
notes="Test model",
tags=args.run_tag,
name=args.run_name,
job_type="test",
dir=args.log_dir,
config=args,
)
args = wandb.config
# Simply call main_worker function
try:
main_worker(args)
except Exception as e:
logging.critical(e, exc_info=True)
print(e)
def main_worker(args):
best_loss = sys.maxsize
print("Use GPU: {} for training".format(args.gpu))
logging.info("Use GPU: {} for training".format(args.gpu))
torch.cuda.set_device(args.gpu)
device = torch.device("cuda", args.gpu)
# Create SLF-RPM model
print(
"\n=> Creating SLF-RPM Classifier Model: 3D ResNet-{}".format(
args.model_depth
)
)
logging.info(
"=> Creating SLF-RPM Pretrain Model: 3D ResNet-{}".format(
args.model_depth
)
)
model = classifier.LinearClsResNet3D(
model_depth=args.model_depth, n_class=1, dropout=args.dropout
)
if args.finetune == "fc":
# Freeze all layers but the last fc
for name, param in model.named_parameters():
if name not in ["encoder_q.fc.weight", "encoder_q.fc.bias"]:
param.requires_grad = False
logging.info("=> Finetune only fc layer")
elif args.finetune == "all":
logging.info("=> Finetune all model layers")
else:
print('Invalid argument "finetune"!')
logging.critical('Invalid argument "finetune"!')
sys.exit()
# Check grad
print("\n===========Check Grad============")
for name, param in model.named_parameters():
print(name, param.requires_grad)
print("=================================\n")
# Load from pretrained model
if args.pretrained:
if os.path.isfile(args.pretrained):
print("=> Loading checkpoint '{}'".format(args.pretrained))
checkpoint = torch.load(args.pretrained, map_location="cpu")
state_dict = checkpoint["state_dict"]
for k in list(state_dict.keys()):
# Retain only encoder and contexter weights
if k.startswith("module.encoder_q") and not k.startswith(
"module.encoder_q.fc"
):
state_dict[k[len("module.") :]] = state_dict[k]
elif k.startswith("encoder_q") and not k.startswith("encoder_q.fc"):
continue
elif not k.startswith("fc"):
state_dict["encoder_q.{}".format(k)] = state_dict[k]
# Delete renamed or unused k
del state_dict[k]
msg = model.load_state_dict(state_dict, strict=False)
assert set(msg.missing_keys) == {
"encoder_q.fc.weight",
"encoder_q.fc.bias",
}, "Missing keys: {};\n Have: {}".format(
set(msg.missing_keys), list(state_dict.keys())
)
print("=> Loaded pre-trained model '{}'".format(args.pretrained))
logging.info("=> Loaded pre-trained model '{}'".format(args.pretrained))
else:
print("=> Error: No checkpoint found at '{}'".format(args.pretrained))
logging.critical("=> No checkpoint found at '{}'".format(args.pretrained))
sys.exit()
elif args.scratch:
print("=> Pretrained model does not specify, train from the scratch!")
logging.info("=> Pretrained model does not specify, train from the scratch!")
else:
print(
"=> Error: Pretrained model does not specify, and scratch does not specify!"
)
logging.critical(
"=> Pretrained model does not specify, and scratch does not specify!"
)
sys.exit()
model = model.to(device)
print(model)
# Loss function
criterion = nn.L1Loss().to(device)
# Optimise only the linear classifier
parameters = list(filter(lambda p: p.requires_grad, model.parameters()))
if args.finetune == "fc":
assert len(parameters) == 2 # fc.weight, fc.bias
optimiser = optim.Adam(parameters, lr=args.lr, weight_decay=args.wd)
# Load data
augmentation = [RandomROI([0])]
if args.dataset_name == "mahnob-hci":
augmentation = Transformer(
augmentation, mean=[0.2796, 0.2394, 0.1901], std=[0.1655, 0.1429, 0.1145]
)
train_dataset = MAHNOBHCIDataset(
args.dataset_dir,
True,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
val_dataset = MAHNOBHCIDataset(
args.dataset_dir,
False,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
assert not [
i for i in val_dataset.files if i in train_dataset.files
], "Train/Val datasets are intersected!"
elif args.dataset_name == "vipl-hr-v2":
augmentation = Transformer(
augmentation, mean=[0.3888, 0.2767, 0.2460], std=[0.2899, 0.2378, 0.2232]
)
train_dataset = VIPLHRDataset(
args.dataset_dir,
True,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
val_dataset = VIPLHRDataset(
args.dataset_dir,
False,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
elif args.dataset_name == "ubfc-rppg":
augmentation = Transformer(
augmentation, mean=[0.4642, 0.3766, 0.3744], std=[0.2947, 0.2393, 0.2395]
)
train_dataset = UBFCDataset(
args.dataset_dir,
True,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
val_dataset = UBFCDataset(
args.dataset_dir,
False,
augmentation,
args.vid_frame,
args.vid_frame_stride,
)
else:
print("Unsupported datasets!")
return
train_sampler = None
train_loader = DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=(train_sampler is None),
sampler=train_sampler,
num_workers=args.workers,
pin_memory=True,
drop_last=False,
)
val_loader = DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True,
drop_last=False,
)
if args.evaluate:
print("=> Loading checkpoint '{}'".format(args.pretrained))
logging.info("=> Loading checkpoint '{}'".format(args.pretrained))
checkpoint = torch.load(args.pretrained, map_location="cpu")
state_dict = checkpoint["state_dict"]
model.load_state_dict(state_dict, strict=True)
print("=> Loaded pre-trained model '{}'".format(args.pretrained))
logging.info("=> Loaded pre-trained model '{}'".format(args.pretrained))
mae, std, rmse, r = validate(val_loader, model, criterion, device)
print(
"Evaluation Result\n MAE: {:.4f}; SD: {:.4f}; RMSE: {:.4f}; R: {:.4f};".format(
mae, std, rmse, r
)
)
logging.info(
"Evaluation Result\n MAE: {:.4f}; SD: {:.4f}; RMSE: {:.4f}; R: {:.4f};".format(
mae, std, rmse, r
)
)
return
# Train model
for epoch in trange(args.epochs, desc="Epoch"):
train_loss = train(train_loader, model, criterion, optimiser, device)
# Evaluate on validation set
val_loss, std, rmse, r = validate(val_loader, model, criterion, device)
if args.wandb:
wandb.log(
{
"train_loss": train_loss,
"val_loss": val_loss,
"std": std,
"rmse": rmse,
"r": r,
}
)
is_best = val_loss < best_loss
best_loss = min(val_loss, best_loss)
if is_best:
state = {
"epoch": epoch + 1,
"state_dict": model.state_dict(),
"optimiser": optimiser.state_dict(),
"best_loss": best_loss,
}
path = os.path.join(args.log_dir, "best_test_model.pth.tar")
torch.save(state, path)
print("\nModel saved at epoch {}".format(epoch + 1))
logging.info("Model saved at epoch {}".format(epoch + 1))
# Logs
if args.wandb:
wandb.run.summary["val_loss"] = best_loss
print(
"""Test Train Loss: {:.4f}, Test Val Loss/Best: {:.4f}/{:.4f},
Test SD: {:.4f}, Test RMSE: {:.4f}, Test R: {:.4f}""".format(
train_loss, val_loss, best_loss, std, rmse, r
)
)
logging.info(
"({}/{}) Test Train Loss: {:.4f}, Test Val Loss/Best: {:.4f}/{:.4f}, Test SD: {:.4f}, Test RMSE: {:.4f}, Test R: {:.4f}".format(
epoch + 1, args.epochs, train_loss, val_loss, best_loss, std, rmse, r
)
)
if args.wandb:
shutil.copyfile(
os.path.join(args.log_dir, "test_output.log"),
os.path.join(wandb.run.dir, "test_output.log"),
)
def train(train_loader, model, criterion, optimizer, device):
losses = AverageMeter("Loss", ":.4e")
"""
Switch to eval mode:
Under the protocol of linear classification on frozen features/models,
it is not legitimate to change any part of the pre-trained model.
BatchNorm in train mode may revise running mean/std (even if it receives
no gradient), which are part of the model parameters too.
"""
model.eval()
for videos, targets in tqdm(train_loader, desc="Train Iteration"):
# Process input
videos = videos.to(device, non_blocking=True)
targets = targets.reshape(-1, 1).to(device, non_blocking=True)
# Compute output
preds = model(videos)
# Loss
loss = criterion(preds, targets)
losses.update(loss.item(), videos.size(0))
# Compute gradient
optimizer.zero_grad()
loss.backward()
optimizer.step()
return losses.avg
@torch.no_grad()
def validate(val_loader, model, criterion, device):
maes = AverageMeter("MAE", ":.4e")
mses = AverageMeter("MSE", ":.4e")
all_pred = []
all_true = []
mse_loss_func = nn.MSELoss()
# Switch to eval mode
model.eval()
for videos, targets in tqdm(val_loader, desc="Val Iteration"):
# Process input
videos = videos.to(device, non_blocking=True)
targets = targets.reshape(-1, 1).to(device, non_blocking=True)
# Compute output
preds = model(videos)
# Loss
mae = criterion(preds, targets)
mse = mse_loss_func(preds, targets)
maes.update(mae.item(), targets.size(0))
mses.update(mse.item(), targets.size(0))
all_pred.append(preds.detach().cpu())
all_true.append(targets.detach().cpu())
all_pred = torch.cat(all_pred).flatten()
all_true = torch.cat(all_true).flatten()
# Mean and Std
diff = all_pred - all_true
mean = torch.mean(diff)
std = torch.std(diff)
# MSE
mse_loss = mses.avg
# RMSE
rmse_loss = np.sqrt(mse_loss)
r, _ = pearsonr(all_true, all_pred)
return maes.avg, std, rmse_loss, r
if __name__ == "__main__":
main()