forked from terbed/Deep-rPPG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_lstm.py
265 lines (222 loc) · 10.2 KB
/
train_lstm.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
from comet_ml import Experiment
from src.archs import PhysNetED, RateProbLSTMCNN
from src.errfuncs import LaplaceLoss, GaussLoss
from src.dset import Dataset4DFromHDF5
import os
import argparse
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
import torch.optim as optim
import numpy as np
tr = torch
def train_model(models, dataloaders, criterion, optimizers, schedulers, opath, num_epochs=35, start_epoch=0):
val_loss_history = []
train_loss_history = []
for epoch in tqdm(range(num_epochs)):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
experiment.set_epoch(epoch+start_epoch)
# Each epoch has a training and validation phase
phases = ['train', 'val']
for phase in phases:
running_loss = 0.0
h1 = h2 = None
if phase == 'train':
for i in range(len(models)):
models[i].train() # Set model to training mode -> activate droput layers and batch norm
else:
for i in range(len(models)):
models[i].eval() # Set model to evaluate mode
# Iterate over data.
for inputs, targets in dataloaders[phase]:
inputs = inputs.to(device)
targets = targets.to(device).view(-1, 1)
# zero the parameter gradients
for i in range(len(optimizers)):
optimizers[i].zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
# Signal extraction
signals = models[0](inputs).view(-1, 1, 128)
# Rate estimation
rates, _, _ = models[1](signals)
# use only the last step at training
rates = rates[-1, :]
targets = targets[-1]
if isinstance(criterion, tr.nn.L1Loss) or isinstance(criterion, tr.nn.MSELoss):
loss = criterion(rates, targets)
else: # In this case custom loss functions
loss = criterion(rates.view(-1, 1, 2), targets.view(-1, 1))
if phase == 'train':
loss.backward()
optimizers[0].step()
optimizers[1].step()
# statistics
running_loss += loss.item()
epoch_loss = running_loss / len(dataloaders[phase])
print('{} Loss: {:.4f} '.format(phase, epoch_loss))
if phase == 'val':
val_loss_history.append(epoch_loss)
with experiment.test():
experiment.log_metric("loss", epoch_loss, step=epoch+start_epoch)
if schedulers is not None:
# Learning Rate scheduler (if epoch loss is on plato)
schedulers[0].step(epoch_loss)
schedulers[1].step(epoch_loss)
else:
train_loss_history.append(epoch_loss)
with experiment.train():
experiment.log_metric("loss", epoch_loss, step=epoch+start_epoch)
experiment.log_epoch_end(epoch+start_epoch)
for i in range(len(models)):
torch.save(models[i].state_dict(), f'checkpoints/{opath}/model{i}_ep{epoch+start_epoch}.pt')
print()
if __name__ == '__main__':
# train on the GPU or on the CPU, if a GPU is not available
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
print(device)
parser = argparse.ArgumentParser()
parser.add_argument('model', type=str, nargs='+', help='DeepPhys, PhysNet, RateProbEst')
parser.add_argument('--loss', type=str, help='L1, MSE, Gauss, Laplace')
parser.add_argument('--lr', type=float, nargs='+', default=1e-4, help='learning rate')
parser.add_argument('--data', type=str, help='path to .hdf5 file containing data')
parser.add_argument('--intervals', type=int, nargs='+', help='indices: train_start, train_end, val_start, val_end, shift_idx')
parser.add_argument('--logger_name', type=str, help='project name for commet ml experiment')
parser.add_argument('--epochs', type=int, default=60, help='number of epochs')
parser.add_argument('--epoch_start', type=int, default=0, help='number of epochs')
parser.add_argument('--batch_size', type=int, default=8, help='batch size')
parser.add_argument("--pretrained_weights", type=str, nargs='+', help="if specified starts from checkpoint model")
parser.add_argument("--checkpoint_dir", type=str, help="checkpoints will be saved in this directory")
parser.add_argument('--n_cpu', type=int, default=8, help='number of cpu threads to use during generation')
parser.add_argument('--crop', type=bool, default=False, help='crop baby with yolo (preprocessing step)')
args = parser.parse_args()
# create output dir
if args.checkpoint_dir:
try:
os.makedirs(f'checkpoints/{args.checkpoint_dir}')
print("Output directory is created")
except FileExistsError:
reply = input('Override existing weights? [y/n]')
if reply == 'n':
print('Add another outout path then!')
exit(0)
# Add the following code anywhere in your machine learning file
experiment = Experiment(api_key="hs2nruoKow2CnUKisoeHccvh7", project_name=args.logger_name, workspace="terbed")
hyper_params = {
"model": args.model,
"pretrained_weights": args.pretrained_weights,
"checkpoint_dir": args.checkpoint_dir,
"loss_fn": args.loss,
"batch_size": args.batch_size,
"n_workers": args.n_cpu,
"num_epochs": args.epochs,
"learning_rate": args.lr,
"database": args.data,
"intervals": args.intervals,
"crop": args.crop,
"img_augm": True,
"freq_augm": True
}
experiment.log_parameters(hyper_params)
# Fix random seed for reproducability
np.random.seed(42)
torch.backends.cudnn.deterministic = True
torch.manual_seed(42)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(42)
# ---------------------------------------
# Construct datasets
# ---------------------------------------
ref_type = 'PulseNumerical'
trainset = Dataset4DFromHDF5(args.data,
labels=(ref_type,),
device=torch.device('cpu'),
start=args.intervals[0], end=args.intervals[1],
crop=args.crop,
augment=False,
augment_freq=False,
D=166,
ccc=False
)
testset = Dataset4DFromHDF5(args.data,
labels=(ref_type,),
device=torch.device('cpu'),
start=args.intervals[2], end=args.intervals[3],
crop=args.crop,
augment=False,
augment_freq=False)
# -------------------------
# Construct DataLoaders
# -------------------------
trainloader = DataLoader(trainset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.n_cpu,
pin_memory=True,
collate_fn=trainset.collate_fn)
testloader = DataLoader(testset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.n_cpu,
pin_memory=True)
dataloaders_ = {'train': trainloader, 'val': testloader}
print('\nDataLoaders successfully constructed!')
# --------------------------
# Define loss function
# ---------------------------
# 'L1, MSE, NegPea, SNR, Gauss, Laplace'
loss_fn = None
n_out = 2
if args.loss == 'Gauss':
loss_fn = GaussLoss()
elif args.loss == 'Laplace':
loss_fn = LaplaceLoss()
elif args.loss == 'L1':
loss_fn = tr.nn.L1Loss()
n_out = 1
elif args.loss == 'MSE':
loss_fn = tr.nn.MSELoss()
n_out = 1
else:
print('\nError! No such loss function. Choose from: Gauss, Laplace')
exit(666)
# --------------------------
# Load model
# --------------------------
models_ = [PhysNetED(), RateProbLSTMCNN(n_out)]
# ----------------------------------
# Set up training
# ---------------------------------
# Use multiple GPU if there are!
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
for i in range(len(models_)):
models_[i] = tr.nn.DataParallel(models_[i])
# If there are pretrained weights, initialize model
if len(args.pretrained_weights) == 1:
models_[0].load_state_dict(tr.load(args.pretrained_weights[0]))
print('\nPre-trained weights are loaded for PhysNet!')
elif len(args.pretrained_weights) == 2:
models_[0].load_state_dict(tr.load(args.pretrained_weights[0]))
models_[1].load_state_dict(tr.load(args.pretrained_weights[1]))
print('\nPre-trained weights are loaded for each network!')
# Copy model to working device
for i in range(len(models_)):
models_[i] = models_[i].to(device)
# ----------------------------
# Initialize optimizer
# ----------------------------
opts = []
schedulers_ = []
for i in range(len(models_)):
opts.append(optim.AdamW(models_[i].parameters(), lr=args.lr[i]))
schedulers_.append(optim.lr_scheduler.ReduceLROnPlateau(opts[i], factor=0.5, verbose=True, patience=10))
# -----------------------------
# Start training
# -----------------------------
train_model(models_, dataloaders_, criterion=loss_fn, optimizers=opts, schedulers=schedulers_,
opath=args.checkpoint_dir, num_epochs=args.epochs, start_epoch=args.epoch_start)
experiment.end()
print('\nTraining is finished without flaw!')