-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.py
83 lines (65 loc) · 2.88 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
import numpy as np
import h5py
import torch
from PhysNetModel import PhysNet
from utils_data import *
from utils_sig import *
from sacred import Experiment
from sacred.observers import FileStorageObserver
import json
ex = Experiment('model_pred', save_git_info=False)
@ex.config
def my_config():
e = 29 # the model checkpoint at epoch e
train_exp_num = 1 # the training experiment number
train_exp_dir = './results/%d'%train_exp_num # training experiment directory
time_interval = 30 # get rppg for 30s video clips, too long clips might cause out of memory
ex.observers.append(FileStorageObserver(train_exp_dir))
if torch.cuda.is_available():
device = torch.device('cuda')
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
else:
device = torch.device('cpu')
@ex.automain
def my_main(_run, e, train_exp_dir, device, time_interval):
# load test file paths
test_list = list(np.load(train_exp_dir + '/test_list.npy'))
pred_exp_dir = train_exp_dir + '/%d'%(int(_run._id)) # prediction experiment directory
with open(train_exp_dir+'/config.json') as f:
config_train = json.load(f)
model = PhysNet(config_train['S'], config_train['in_ch']).to(device).eval()
model.load_state_dict(torch.load(train_exp_dir+'/epoch%d.pt'%(e), map_location=device)) # load weights to the model
@torch.no_grad()
def dl_model(imgs_clip):
# model inference
img_batch = imgs_clip
img_batch = img_batch.transpose((3,0,1,2))
img_batch = img_batch[np.newaxis].astype('float32')
img_batch = torch.tensor(img_batch).to(device)
rppg = model(img_batch)[:,-1, :]
rppg = rppg[0].detach().cpu().numpy()
return rppg
for h5_path in test_list:
h5_path = str(h5_path)
with h5py.File(h5_path, 'r') as f:
imgs = f['imgs']
bvp = f['bvp']
# bvppeak = f['bvp_peak']
fs = config_train['fs']
duration = np.min([imgs.shape[0], bvp.shape[0]]) / fs
num_blocks = int(duration // time_interval)
rppg_list = []
bvp_list = []
# bvppeak_list = []
for b in range(num_blocks):
rppg_clip = dl_model(imgs[b*time_interval*fs:(b+1)*time_interval*fs])
rppg_list.append(rppg_clip)
bvp_list.append(bvp[b*time_interval*fs:(b+1)*time_interval*fs])
# bvppeak_list.append(bvppeak[b*time_interval*fs:(b+1)*time_interval*fs])
rppg_list = np.array(rppg_list)
bvp_list = np.array(bvp_list)
# bvppeak_list = np.array(bvppeak_list)
# results = {'rppg_list': rppg_list, 'bvp_list': bvp_list, 'bvppeak_list':bvppeak_list}
results = {'rppg_list': rppg_list, 'bvp_list': bvp_list}
np.save(pred_exp_dir+'/'+h5_path.split('/')[-1][:-3], results)