-
Notifications
You must be signed in to change notification settings - Fork 11
/
inf_cvae.py
196 lines (176 loc) · 10.6 KB
/
inf_cvae.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
import argparse
import json
import math
import os.path
import time
import sys
import shutil
import trimesh.sample
import torch
import plotly.graph_objects as go
from utils.visualize_plotly import plot_point_cloud, plot_point_cloud_cmap, plot_mesh_from_name
from utils.set_seed import set_global_seed
from torch.utils.tensorboard import SummaryWriter
import trimesh as tm
import torch.nn as nn
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--pre_process', default='sharp_lift', type=str)
parser.add_argument('--s_model', default='PointNetCVAE_SqrtUnseenShadowhand', type=str)
parser.add_argument('--num_per_seen_object', default=4, type=int)
parser.add_argument('--num_per_unseen_object', default=16, type=int)
parser.add_argument('--comment', default='debug', type=str)
args_ = parser.parse_args()
tag = str(time.time())
return args_, tag
def pre_process_sharp_clamp(contact_map):
gap_th = 0.5 # delta_th = (1 - gap_th)
gap_th = min(contact_map.max().item(), gap_th)
delta_th = (1 - gap_th)
contact_map[contact_map > 0.4] += delta_th
# contact_map += delta_th
contact_map = torch.clamp_max(contact_map, 1.)
return contact_map
def identity_map(contact_map):
return contact_map
if __name__ == '__main__':
set_global_seed(seed=42)
args, time_tag = get_parser()
pre_process_map = {'sharp_lift': pre_process_sharp_clamp,
'identity': identity_map}
pre_process_contact_map_goal = pre_process_map[args.pre_process]
logs_basedir = os.path.join('logs_inf_cvae', f'{args.s_model}', f'{args.pre_process}', f'{args.comment}-{time_tag}')
vis_id_dir = os.path.join(logs_basedir, 'vis_id_dir')
vis_ood_dir = os.path.join(logs_basedir, 'vis_ood_dir')
cmap_path_id = os.path.join(logs_basedir, 'cmap_id.pt')
cmap_path_ood = os.path.join(logs_basedir, 'cmap_ood.pt')
os.makedirs(logs_basedir, exist_ok=False)
os.makedirs(vis_id_dir, exist_ok=False)
os.makedirs(vis_ood_dir, exist_ok=False)
device = "cuda"
if args.s_model == 'PointNetCVAE_SqrtUnseenShadowhand':
model_basedir = 'ckpts/SqrtUnseenShadowhand'
from ckpts.SqrtUnseenShadowhand.src.utils_model.PointNetCVAE import PointNetCVAE
model: nn.Module
model = PointNetCVAE(latent_size=128,
encoder_layers_size=[4, 64, 128, 512],
decoder_global_feat_size=512,
decoder_pointwise_layers_size=[3, 64, 64],
decoder_global_layers_size=[64, 128, 512],
decoder_decoder_layers_size=[64 + 512 + 128, 512, 64, 64, 1])
model.load_state_dict(torch.load(os.path.join(model_basedir, 'weights', 'pointnet_cvae_model.pth')))
model = model.to(device)
model.eval()
seen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['train']
unseen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['validate']
elif args.s_model == 'PointNetCVAE_SqrtUnseenBarrett':
model_basedir = 'models/SqrtUnseenBarrett'
from ckpts.SqrtUnseenBarrett.src.utils_model.PointNetCVAE import PointNetCVAE
model: nn.Module
model = PointNetCVAE(latent_size=128,
encoder_layers_size=[4, 64, 128, 512],
decoder_global_feat_size=512,
decoder_pointwise_layers_size=[3, 64, 64],
decoder_global_layers_size=[64, 128, 512],
decoder_decoder_layers_size=[64 + 512 + 128, 512, 64, 64, 1])
model.load_state_dict(torch.load(os.path.join(model_basedir, 'weights', 'pointnet_cvae_model.pth')))
model = model.to(device)
model.eval()
seen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['train']
unseen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['validate']
elif args.s_model == 'PointNetCVAE_SqrtUnseenEzgripper':
model_basedir = 'models/SqrtUnseenEzgripper'
from ckpts.SqrtUnseenEzgripper.src.utils_model.PointNetCVAE import PointNetCVAE
model: nn.Module
model = PointNetCVAE(latent_size=128,
encoder_layers_size=[4, 64, 128, 512],
decoder_global_feat_size=512,
decoder_pointwise_layers_size=[3, 64, 64],
decoder_global_layers_size=[64, 128, 512],
decoder_decoder_layers_size=[64 + 512 + 128, 512, 64, 64, 1])
model.load_state_dict(torch.load(os.path.join(model_basedir, 'weights', 'pointnet_cvae_model.pth')))
model = model.to(device)
model.eval()
seen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['train']
unseen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['validate']
elif args.s_model == 'PointNetCVAE_SqrtFullRobots':
model_basedir = 'models/SqrtFullRobots'
from ckpts.SqrtFullRobots.src.utils_model.PointNetCVAE import PointNetCVAE
model: nn.Module
model = PointNetCVAE(latent_size=128,
encoder_layers_size=[4, 64, 128, 512],
decoder_global_feat_size=512,
decoder_pointwise_layers_size=[3, 64, 64],
decoder_global_layers_size=[64, 128, 512],
decoder_decoder_layers_size=[64 + 512 + 128, 512, 64, 64, 1])
model.load_state_dict(torch.load(os.path.join(model_basedir, 'weights', 'pointnet_cvae_model.pth')))
model = model.to(device)
model.eval()
seen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['train']
unseen_object_list = json.load(open(os.path.join(model_basedir, "split_train_validate_objects.json"), 'rb'))['validate']
else:
raise NotImplementedError("Occur when load model...")
cmap_ood = []
for object_name in unseen_object_list:
print(f'unseen object name: {object_name}')
object_mesh: tm.Trimesh
object_mesh = tm.load(os.path.join('data/object', object_name.split('+')[0], object_name.split("+")[1],
f'{object_name.split("+")[1]}.stl'))
for i_sample in range(args.num_per_unseen_object):
cmap_ood_sample = {'object_name': object_name,
'i_sample': i_sample,
'object_point_cloud': None,
'contact_map_value': None}
print(f'[{i_sample}/{args.num_per_unseen_object}] | {object_name}')
object_point_cloud, faces_indices = trimesh.sample.sample_surface(mesh=object_mesh, count=2048)
contact_points_normal = torch.tensor([object_mesh.face_normals[x] for x in faces_indices]).float()
object_point_cloud = torch.Tensor(object_point_cloud).float()
object_point_cloud = torch.cat([object_point_cloud, contact_points_normal], dim=1).to(device)
z_latent_code = torch.randn(1, model.latent_size, device=device).float()
contact_map_value = model.inference(object_point_cloud[:, :3].unsqueeze(0), z_latent_code).squeeze(0)
# process the contact map value
contact_map_value = contact_map_value.detach().cpu().unsqueeze(1)
contact_map_value = pre_process_contact_map_goal(contact_map_value).to(device)
contact_map_goal = torch.cat([object_point_cloud, contact_map_value], dim=1)
cmap_ood_sample['object_point_cloud'] = object_point_cloud
cmap_ood_sample['contact_map_value'] = contact_map_value
cmap_ood.append(cmap_ood_sample)
vis_data = []
vis_data += [plot_point_cloud_cmap(contact_map_goal[:, :3].cpu().detach().numpy(),
contact_map_goal[:, 6].cpu().detach().numpy())]
vis_data += [plot_mesh_from_name(f'{object_name}')]
fig = go.Figure(data=vis_data)
fig.write_html(os.path.join(vis_ood_dir, f'unseen-{object_name}-{i_sample}.html'))
torch.save(cmap_ood, cmap_path_ood)
cmap_id = []
for object_name in seen_object_list:
print(f'seen object name: {object_name}')
object_mesh: tm.Trimesh
object_mesh = tm.load(os.path.join('data/object', object_name.split('+')[0], object_name.split("+")[1],
f'{object_name.split("+")[1]}.stl'))
for i_sample in range(args.num_per_seen_object):
cmap_id_sample = {'object_name': object_name,
'i_sample': i_sample,
'object_point_cloud': None,
'contact_map_value': None}
print(f'[{i_sample}/{args.num_per_seen_object}] | {object_name}')
object_point_cloud, faces_indices = trimesh.sample.sample_surface(mesh=object_mesh, count=2048)
contact_points_normal = torch.tensor([object_mesh.face_normals[x] for x in faces_indices]).float()
object_point_cloud = torch.Tensor(object_point_cloud).float()
object_point_cloud = torch.cat([object_point_cloud, contact_points_normal], dim=1).to(device)
z_latent_code = torch.randn(1, model.latent_size, device=device).float()
contact_map_value = model.inference(object_point_cloud[:, :3].unsqueeze(0), z_latent_code).squeeze(0)
# process the contact map value
contact_map_value = contact_map_value.detach().cpu().unsqueeze(1)
contact_map_value = pre_process_contact_map_goal(contact_map_value).to(device)
contact_map_goal = torch.cat([object_point_cloud, contact_map_value], dim=1)
cmap_id_sample['object_point_cloud'] = object_point_cloud
cmap_id_sample['contact_map_value'] = contact_map_value
cmap_id.append(cmap_id_sample)
vis_data = []
vis_data += [plot_point_cloud_cmap(contact_map_goal[:, :3].cpu().detach().numpy(),
contact_map_goal[:, 6].cpu().detach().numpy())]
vis_data += [plot_mesh_from_name(f'{object_name}')]
fig = go.Figure(data=vis_data)
fig.write_html(os.path.join(vis_id_dir, f'seen-{object_name}-{i_sample}.html'))
torch.save(cmap_id, cmap_path_id)