-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_occ_generate.py
executable file
·126 lines (92 loc) · 4.53 KB
/
init_occ_generate.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
import os
import sys
import time
import torch
import shutil
import trimesh
import argparse
import pandas as pd
import numpy as np
import open3d as o3d
from tqdm import tqdm
from collections import defaultdict
from artihand import config, data
from artihand.checkpoints import CheckpointIO
from dependencies.halo.halo_adapter.transform_utils import xyz_to_xyz1
2
parser = argparse.ArgumentParser(
description='Extract meshes from occupancy process.'
)
parser.add_argument('--config', type=str, help='Path to config file.', default='configs/init_occ/init_occ.yaml')
parser.add_argument('--no-cuda', action='store_true', help='Do not use cuda.')
parser.add_argument('--latest', action='store_true', help='Use latest model instead of best.')
parser.add_argument('--subset', type=str, default='train', choices=['train', 'val', 'test'], help='Dataset subset')
parser.add_argument('--out_dir', type=str, default='/data/hand_data/initial_mesh_prediction_shape_att_check2', help='Path to output directory to store intermediate results.')
parser.add_argument('--split_idx', type=int, default=0, help='Dataset split index. (-1: no split)')
parser.add_argument('--splits', type=int, default=4, help='Dataset split index. (-1: no split)')
if __name__ == '__main__':
args = parser.parse_args()
cfg = config.load_config(args.config, 'configs/init_occ/default.yaml')
is_cuda = (torch.cuda.is_available() and not args.no_cuda)
device = torch.device("cuda" if is_cuda else "cpu")
out_dir = cfg['training']['out_dir']
generation_dir = os.path.join(out_dir, cfg['generation']['generation_dir'])
if args.latest:
generation_dir = generation_dir + '_latest'
out_time_file = os.path.join(generation_dir, 'time_generation_full.pkl')
out_time_file_class = os.path.join(generation_dir, 'time_generation.pkl')
batch_size = cfg['generation']['batch_size']
input_type = cfg['data']['input_type']
dataset = config.get_dataset(args.subset, cfg, splits=args.splits, split_idx=args.split_idx)
# Model
model = config.get_model(cfg, device=device, dataset=dataset)
checkpoint_io = CheckpointIO(out_dir, model=model)
if args.latest:
checkpoint_io.load('best_model.pt')
else:
checkpoint_io.load(cfg['test']['model_file'])
# Generator
generator = config.get_generator(model, cfg, device=device)
# Determine what to generate
generate_mesh = cfg['generation']['generate_mesh']
# Loader
test_loader = torch.utils.data.DataLoader(
dataset, batch_size=1, num_workers=1, shuffle=False)
# Statistics
time_dicts = []
# Generate
model.eval()
mesh_dir = os.path.join(generation_dir, 'meshes')
generation_vis_dir = os.path.join(generation_dir, 'vis', )
args.out_dir = os.path.join(args.out_dir, args.subset)
if not os.path.exists(args.out_dir):
os.makedirs(args.out_dir)
for i, data in enumerate(tqdm(test_loader)):
img, camera_params, mano_data, idx = data
# Create directories if necessary
if generate_mesh and not os.path.exists(mesh_dir):
os.makedirs(mesh_dir)
# Generate outputs
out_file_dict = {}
if generate_mesh:
out = generator.init_occ_generate_mesh(data)
# Get statistics
left_mesh, right_mesh = out
# Write output
left_mesh_out_path = os.path.join(args.out_dir, '%07d_left.obj' % idx)
right_mesh_out_path = os.path.join(args.out_dir, '%07d_right.obj' % idx)
print(f"Generating {left_mesh_out_path}...")
print(f"Generating {right_mesh_out_path}...")
os.makedirs(os.path.dirname(left_mesh_out_path), exist_ok=True)
# Save left hand mesh
left_mesh.vertices = left_mesh.vertices * 0.4
left_mesh.vertices = torch.matmul(mano_data['left']['root_rot_mat'].squeeze().cpu().T, xyz_to_xyz1(torch.Tensor(left_mesh.vertices)).unsqueeze(-1))[:, :3, 0]
left_mesh.vertices = left_mesh.vertices * 1000
left_mesh.vertices = left_mesh.vertices * [-1, 1, 1]
trimesh.repair.fix_inversion(left_mesh)
left_mesh.export(left_mesh_out_path)
# Save right hand mesh
right_mesh.vertices = right_mesh.vertices * 0.4
right_mesh.vertices = torch.matmul(mano_data['right']['root_rot_mat'].squeeze().cpu().T, xyz_to_xyz1(torch.Tensor(right_mesh.vertices)).unsqueeze(-1))[:, :3, 0]
right_mesh.vertices = right_mesh.vertices * 1000
right_mesh.export(right_mesh_out_path)