-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain.py
More file actions
211 lines (165 loc) · 6.69 KB
/
train.py
File metadata and controls
211 lines (165 loc) · 6.69 KB
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
import argparse
import os
import numpy as np
import torch
import json
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import ModelCheckpoint, TQDMProgressBar
from models.utils.init_weights import init_weights
def get_opt(job_id, load):
if load:
print('Loading options with job_id')
with open('checkpoints/' + str(job_id) + '/infos.json', "r") as outfile:
opt = json.load(outfile)
else:
print('Loading default options for new job')
with open('default_options.json', "r") as outfile:
opt = json.load(outfile)
parser = argparse.ArgumentParser()
parser.add_argument('--job_id', required=True)
# so we can pass other default options as program argument
for key, value in opt.items():
parser.add_argument('--' + key, default=value, type=type(value))
opt = vars(parser.parse_args())
# If checkpoints folder does not exist, create it
if not os.path.exists('checkpoints/'):
os.mkdir('checkpoints/')
try:
os.mkdir('checkpoints/' + str(opt['job_id']))
except:
print('Folder for job ' + str(opt['job_id']) + ' already exists.')
exit()
with open('checkpoints/' + str(opt['job_id']) + '/infos.json', "w") as outfile:
json.dump(opt, outfile, sort_keys=True, indent=4)
print('Options:\n', json.dumps(opt, sort_keys=True, indent=4), end="\n\n")
return opt
# Custom PyTorch Dataset for compressed numpy arrays
class NumpyDataset(torch.utils.data.Dataset):
def __init__(self, coeffs, infos, vertices=None):
self.coeffs = coeffs
self.infos = infos
self.vertices = vertices
def __len__(self):
return len(self.coeffs)
def __getitem__(self, idx):
coeff = torch.from_numpy(self.coeffs[idx]).float()
info = self.infos[idx]
if self.vertices is not None:
vert = torch.from_numpy(self.vertices[idx]).float()
return info, coeff, vert
else:
return info, coeff
def get_dataloader(opt, dataset_type, batch_size, shuffle=True):
# Load arrays with np.memmap and infos from JSON
coeffs_path = os.path.join(opt['path'], f"{dataset_type}_coeffs.npy")
infos_path = os.path.join(opt['path'], f"{dataset_type}_infos.json")
coeffs = np.memmap(coeffs_path, dtype='float32', mode='r')
# Infer shape from file size and expected dimensions
with open(infos_path, 'r') as f:
infos = json.load(f)
# Get shape from infos or opt
nb_samples = len(infos)
nb_freq = opt['nb_freq']
coeffs = coeffs[32:].reshape((nb_samples, nb_freq, 3))
if dataset_type == 'vald':
vertices_path = os.path.join(opt['path'], f"vald_vertices.npy")
vertices = np.memmap(vertices_path, dtype='float32', mode='r')
nb_vertices = opt.get('nb_vertices', 6890)
vertices = vertices[32:].reshape((nb_samples, nb_vertices, 3))
else:
vertices = None
# Set mean and std for standardization (computed on training set)
opt['mean'] = torch.tensor(coeffs.mean(axis=0)).cuda().unsqueeze(0) # [nb_freq, 3]
opt['std'] = torch.tensor(coeffs.std(axis=0)).cuda().unsqueeze(0) # [nb_freq, 3]
dataset = NumpyDataset(coeffs, infos, vertices)
return torch.utils.data.DataLoader(
dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=opt["num_workers"],
pin_memory=True,
persistent_workers=True if opt["num_workers"] > 0 else False
)
def load_trainer(job_id=None, profiler="simple", seed_everything_flag=True):
if job_id is None:
load = False
else:
load = True
torch.set_float32_matmul_precision('high') # or 'medium'
if seed_everything_flag:
seed_everything(42, workers=True)
print()
opt = get_opt(job_id, load)
with open(opt['path'] + '/infos.json', "r") as f:
opt_dataset = json.load(f)
for key, value in opt_dataset.items():
if key in opt:
continue
opt[key] = value
if opt['loss_type'] == "MSE":
opt['loss'] = torch.nn.MSELoss()
else:
print('Loss type not implemented.')
exit()
opt['nb_vertices'] = 6890
# get path from which the script in launched
path_repo = os.getcwd()
opt['TRIV'] = np.loadtxt(path_repo + '/data/SMPL/smpl_faces.txt', dtype='int32')
path_evecs = path_repo + '/data/SMPL/evecs_GL_6890.npy'
print('Loading eigen vectors...')
opt['evecs'] = torch.from_numpy(np.load(path_evecs)).float().to(opt['device'])[:, :opt['nb_freq']]
# datasets
print('Loading datasets...')
opt['dataloader_train'] = get_dataloader(opt, 'train', opt['train_batch_size'])
opt['dataloader_vald'] = get_dataloader(opt, 'vald', opt['vald_batch_size'], shuffle=False)
# model
exec("from models." + opt['model_type'] + " import " + opt['model_type'])
opt['model_class'] = eval(opt['model_type']) # Store the class for checkpoint loading
opt['model'] = opt['model_class'](opt).to(opt['device'])
opt['model'].apply(init_weights)
nb_params = sum(p.numel() for p in opt['model'].parameters() if p.requires_grad)
print('Number of parameters:', str(nb_params), end="\n\n")
# pytorch lightning
logger = TensorBoardLogger(
save_dir="tb_logs",
name="",
version=str(opt['job_id']),
)
checkpoint_callback = ModelCheckpoint(
dirpath='checkpoints/' + str(opt['job_id']),
filename='{epoch:02d}_{spatial_validation_loss:.2f}',
every_n_epochs=1,
save_top_k=1,
save_last=True,
monitor='spatial_validation_loss',
mode='min'
)
progress_bar = TQDMProgressBar(
refresh_rate=1
)
pl_trainer = pl.Trainer(
accelerator='gpu', devices=1,
profiler=profiler,
max_epochs=opt['num_iterations'],
check_val_every_n_epoch=opt['check_val_every_n_epoch'],
logger=logger,
precision=32,
default_root_dir='checkpoints/',
callbacks=[checkpoint_callback, progress_bar],
deterministic=False,
benchmark=True,
)
if load:
best_checkpoint_filename = 'checkpoints/' + str(job_id) + "/last.ckpt"
print('Loading checkpoint:', best_checkpoint_filename)
opt['model'] = opt['model_class'].load_from_checkpoint(best_checkpoint_filename, opt=opt).to(opt["device"])
return pl_trainer, opt
if __name__ == "__main__":
trainer, opt = load_trainer() # without arguments for new job
trainer.fit(
opt['model'],
opt['dataloader_train'],
opt['dataloader_vald']
)