-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
130 lines (112 loc) · 4.56 KB
/
main.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
import pickle
import argparse
import os
import os.path as osp
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch_geometric.transforms as T
from psbody.mesh import Mesh
from models import AE
from datasets import MeshData
from utils import utils, writer, train_eval, DataLoader, mesh_sampling
parser = argparse.ArgumentParser(description='mesh autoencoder')
parser.add_argument('--exp_name', type=str, default='interpolation_exp')
parser.add_argument('--dataset', type=str, default='CoMA')
parser.add_argument('--split', type=str, default='interpolation')
parser.add_argument('--test_exp', type=str, default='bareteeth')
parser.add_argument('--n_threads', type=int, default=4)
parser.add_argument('--device_idx', type=int, default=0)
# network hyperparameters
parser.add_argument('--out_channels',
nargs='+',
default=[16, 16, 16, 32],
type=int)
parser.add_argument('--latent_channels', type=int, default=8)
parser.add_argument('--in_channels', type=int, default=3)
parser.add_argument('--K', type=int, default=6)
# optimizer hyperparmeters
parser.add_argument('--optimizer', type=str, default='Adam')
parser.add_argument('--lr', type=float, default=8e-3)
parser.add_argument('--lr_decay', type=float, default=0.99)
parser.add_argument('--decay_step', type=int, default=1)
parser.add_argument('--weight_decay', type=float, default=0)
# training hyperparameters
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--epochs', type=int, default=300)
# others
parser.add_argument('--seed', type=int, default=1)
args = parser.parse_args()
args.work_dir = osp.dirname(osp.realpath(__file__))
args.data_fp = osp.join(args.work_dir, 'data', args.dataset)
args.out_dir = osp.join(args.work_dir, 'out', args.exp_name)
args.checkpoints_dir = osp.join(args.out_dir, 'checkpoints')
print(args)
utils.makedirs(args.out_dir)
utils.makedirs(args.checkpoints_dir)
writer = writer.Writer(args)
device = torch.device('cuda', args.device_idx)
torch.set_num_threads(args.n_threads)
# deterministic
torch.manual_seed(args.seed)
cudnn.benchmark = False
cudnn.deterministic = True
# load dataset
template_fp = osp.join('template', 'template.obj')
meshdata = MeshData(args.data_fp,
template_fp,
split=args.split,
test_exp=args.test_exp)
train_loader = DataLoader(meshdata.train_dataset,
batch_size=args.batch_size,
shuffle=True)
test_loader = DataLoader(meshdata.test_dataset, batch_size=args.batch_size)
# generate/load transform matrices
transform_fp = osp.join(args.data_fp, 'transform.pkl')
if not osp.exists(transform_fp):
print('Generating transform matrices...')
mesh = Mesh(filename=template_fp)
ds_factors = [4, 4, 4, 4]
_, A, D, U, F = mesh_sampling.generate_transform_matrices(mesh, ds_factors)
tmp = {'face': F, 'adj': A, 'down_transform': D, 'up_transform': U}
with open(transform_fp, 'wb') as fp:
pickle.dump(tmp, fp)
print('Done!')
print('Transform matrices are saved in \'{}\''.format(transform_fp))
else:
with open(transform_fp, 'rb') as f:
tmp = pickle.load(f, encoding='latin1')
edge_index_list = [utils.to_edge_index(adj).to(device) for adj in tmp['adj']]
down_transform_list = [
utils.to_sparse(down_transform).to(device)
for down_transform in tmp['down_transform']
]
up_transform_list = [
utils.to_sparse(up_transform).to(device)
for up_transform in tmp['up_transform']
]
model = AE(args.in_channels,
args.out_channels,
args.latent_channels,
edge_index_list,
down_transform_list,
up_transform_list,
K=args.K).to(device)
print(model)
if args.optimizer == 'Adam':
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay)
elif args.optimizer == 'SGD':
optimizer = torch.optim.SGD(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay,
momentum=0.9)
else:
raise RuntimeError('Use optimizers of SGD or Adam')
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
args.decay_step,
gamma=args.lr_decay)
train_eval.run(model, train_loader, test_loader, args.epochs, optimizer,
scheduler, writer, device)
train_eval.eval_error(model, test_loader, device, meshdata, args.out_dir)