This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
train.py
175 lines (157 loc) · 6.59 KB
/
train.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
import os
import argparse
import torch
import torch.utils.tensorboard
from torch.utils.data import DataLoader
from torch.nn.utils import clip_grad_norm_
from tqdm.auto import tqdm
from datasets import *
from utils.misc import *
from utils.transforms import *
from utils.denoise import *
from models.denoise import *
from models.utils import chamfer_distance_unit_sphere
# Arguments
parser = argparse.ArgumentParser()
## Dataset and loader
parser.add_argument('--dataset_root', type=str, default='./data')
parser.add_argument('--dataset', type=str, default='PUNet')
parser.add_argument('--patch_size', type=int, default=1000)
parser.add_argument('--resolutions', type=str_list, default=['10000_poisson', '30000_poisson', '50000_poisson'])
parser.add_argument('--noise_min', type=float, default=0.005)
parser.add_argument('--noise_max', type=float, default=0.020)
parser.add_argument('--train_batch_size', type=int, default=32)
# parser.add_argument('--val_batch_size', type=int, default=64)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--aug_rotate', type=eval, default=True, choices=[True, False])
## Model architecture
parser.add_argument('--supervised', type=eval, default=True, choices=[True, False])
parser.add_argument('--frame_knn', type=int, default=32)
parser.add_argument('--num_train_points', type=int, default=128)
parser.add_argument('--num_clean_nbs', type=int, default=4, help='For supervised training.')
parser.add_argument('--num_selfsup_nbs', type=int, default=8, help='For self-supervised training.')
parser.add_argument('--dsm_sigma', type=float, default=0.01)
parser.add_argument('--score_net_hidden_dim', type=int, default=128)
parser.add_argument('--score_net_num_blocks', type=int, default=4)
## Optimizer and scheduler
parser.add_argument('--lr', type=float, default=1e-4)
parser.add_argument('--weight_decay', type=float, default=0)
parser.add_argument('--max_grad_norm', type=float, default=float("inf"))
## Training
parser.add_argument('--seed', type=int, default=2020)
parser.add_argument('--logging', type=eval, default=True, choices=[True, False])
parser.add_argument('--log_root', type=str, default='./logs')
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--max_iters', type=int, default=1*MILLION)
parser.add_argument('--val_freq', type=int, default=2000)
parser.add_argument('--val_upsample_rate', type=int, default=4)
parser.add_argument('--val_num_visualize', type=int, default=4)
parser.add_argument('--val_noise', type=float, default=0.015)
parser.add_argument('--ld_step_size', type=float, default=0.2)
parser.add_argument('--tag', type=str, default=None)
args = parser.parse_args()
seed_all(args.seed)
# Logging
if args.logging:
log_dir = get_new_log_dir(args.log_root, prefix='D%s_' % (args.dataset), postfix='_' + args.tag if args.tag is not None else '')
logger = get_logger('train', log_dir)
writer = torch.utils.tensorboard.SummaryWriter(log_dir)
ckpt_mgr = CheckpointManager(log_dir)
log_hyperparams(writer, log_dir, args)
else:
logger = get_logger('train', None)
writer = BlackHole()
ckpt_mgr = BlackHole()
logger.info(args)
# Datasets and loaders
logger.info('Loading datasets')
train_dset = PairedPatchDataset(
datasets=[
PointCloudDataset(
root=args.dataset_root,
dataset=args.dataset,
split='train',
resolution=resl,
transform=standard_train_transforms(noise_std_max=args.noise_max, noise_std_min=args.noise_min, rotate=args.aug_rotate)
) for resl in args.resolutions
],
patch_size=args.patch_size,
patch_ratio=1.2,
on_the_fly=True
)
val_dset = PointCloudDataset(
root=args.dataset_root,
dataset=args.dataset,
split='test',
resolution=args.resolutions[0],
transform=standard_train_transforms(noise_std_max=args.val_noise, noise_std_min=args.val_noise, rotate=False, scale_d=0),
)
train_iter = get_data_iterator(DataLoader(train_dset, batch_size=args.train_batch_size, num_workers=args.num_workers, shuffle=True))
# Model
logger.info('Building model...')
model = DenoiseNet(args).to(args.device)
logger.info(repr(model))
# Optimizer and scheduler
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay,
)
# Train, validate and test
def train(it):
# Load data
batch = next(train_iter)
pcl_noisy = batch['pcl_noisy'].to(args.device)
pcl_clean = batch['pcl_clean'].to(args.device)
# Reset grad and model state
optimizer.zero_grad()
model.train()
# Forward
if args.supervised:
loss = model.get_supervised_loss(pcl_noisy=pcl_noisy, pcl_clean=pcl_clean)
else:
loss = model.get_selfsupervised_loss(pcl_noisy=pcl_noisy)
# Backward and optimize
loss.backward()
orig_grad_norm = clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
# Logging
logger.info('[Train] Iter %04d | Loss %.6f | Grad %.6f' % (
it, loss.item(), orig_grad_norm,
))
writer.add_scalar('train/loss', loss, it)
writer.add_scalar('train/lr', optimizer.param_groups[0]['lr'], it)
writer.add_scalar('train/grad_norm', orig_grad_norm, it)
writer.flush()
def validate(it):
all_clean = []
all_denoised = []
for i, data in enumerate(tqdm(val_dset, desc='Validate')):
pcl_noisy = data['pcl_noisy'].to(args.device)
pcl_clean = data['pcl_clean'].to(args.device)
pcl_denoised = patch_based_denoise(model, pcl_noisy, ld_step_size=args.ld_step_size)
all_clean.append(pcl_clean.unsqueeze(0))
all_denoised.append(pcl_denoised.unsqueeze(0))
all_clean = torch.cat(all_clean, dim=0)
all_denoised = torch.cat(all_denoised, dim=0)
avg_chamfer = chamfer_distance_unit_sphere(all_denoised, all_clean, batch_reduction='mean')[0].item()
logger.info('[Val] Iter %04d | CD %.6f ' % (it, avg_chamfer))
writer.add_scalar('val/chamfer', avg_chamfer, it)
writer.add_mesh('val/pcl', all_denoised[:args.val_num_visualize], global_step=it)
writer.flush()
# scheduler.step(avg_chamfer)
return avg_chamfer
# Main loop
logger.info('Start training...')
try:
for it in range(1, args.max_iters+1):
train(it)
if it % args.val_freq == 0 or it == args.max_iters:
cd_loss = validate(it)
opt_states = {
'optimizer': optimizer.state_dict(),
# 'scheduler': scheduler.state_dict(),
}
ckpt_mgr.save(model, args, cd_loss, opt_states, step=it)
# ckpt_mgr.save(model, args, 0, opt_states, step=it)
except KeyboardInterrupt:
logger.info('Terminating...')