forked from zju3dv/clean-pvnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
277 lines (220 loc) · 8.2 KB
/
run.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from lib.config import cfg, args
import numpy as np
import os
def run_rgb():
import glob
from scipy.misc import imread
import matplotlib.pyplot as plt
syn_ids = sorted(os.listdir('data/ShapeNet/renders/02958343/'))[-10:]
for syn_id in syn_ids:
pkl_paths = glob.glob('data/ShapeNet/renders/02958343/{}/*.pkl'.format(syn_id))
np.random.shuffle(pkl_paths)
for pkl_path in pkl_paths:
img_path = pkl_path.replace('_RT.pkl', '.png')
img = imread(img_path)
plt.imshow(img)
plt.show()
def run_dataset():
from lib.datasets import make_data_loader
import tqdm
cfg.train.num_workers = 0
data_loader = make_data_loader(cfg, is_train=False)
for batch in tqdm.tqdm(data_loader):
pass
def run_network():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
import time
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, epoch=cfg.test.epoch)
network.eval()
data_loader = make_data_loader(cfg, is_train=False)
total_time = 0
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda()
with torch.no_grad():
torch.cuda.synchronize()
start = time.time()
network(batch['inp'], batch)
torch.cuda.synchronize()
total_time += time.time() - start
print(total_time / len(data_loader))
def run_evaluate():
from lib.datasets import make_data_loader
from lib.evaluators import make_evaluator
import tqdm
import torch
from lib.networks import make_network
from lib.utils.net_utils import load_network
torch.manual_seed(0)
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, epoch=cfg.test.epoch)
network.eval()
data_loader = make_data_loader(cfg, is_train=False)
evaluator = make_evaluator(cfg)
for batch in tqdm.tqdm(data_loader):
inp = batch['inp'].cuda()
with torch.no_grad():
output = network(inp)
evaluator.evaluate(output, batch)
evaluator.summarize()
def run_visualize():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
from lib.visualizers import make_visualizer
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, resume=cfg.resume, epoch=cfg.test.epoch)
network.eval()
data_loader = make_data_loader(cfg, is_train=False)
visualizer = make_visualizer(cfg)
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda()
with torch.no_grad():
output = network(batch['inp'], batch)
visualizer.visualize(output, batch)
def run_visualize_train():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
from lib.visualizers import make_visualizer
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, resume=cfg.resume, epoch=cfg.test.epoch)
network.eval()
data_loader = make_data_loader(cfg, is_train=True)
visualizer = make_visualizer(cfg, 'train')
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda()
with torch.no_grad():
output = network(batch['inp'], batch)
visualizer.visualize_train(output, batch)
def run_analyze():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
from lib.analyzers import make_analyzer
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, epoch=cfg.test.epoch)
network.eval()
cfg.train.num_workers = 0
data_loader = make_data_loader(cfg, is_train=False)
analyzer = make_analyzer(cfg)
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda()
with torch.no_grad():
output = network(batch['inp'], batch)
analyzer.analyze(output, batch)
def run_net_utils():
from lib.utils import net_utils
import torch
import os
model_path = 'data/model/rcnn_snake/rcnn/139.pth'
pretrained_model = torch.load(model_path)
net = pretrained_model['net']
net = net_utils.remove_net_prefix(net, 'dla.')
net = net_utils.remove_net_prefix(net, 'cp.')
pretrained_model['net'] = net
model_path = 'data/model/rcnn_snake/rcnn/139.pth'
os.system('mkdir -p {}'.format(os.path.dirname(model_path)))
torch.save(pretrained_model, model_path)
def run_linemod():
from lib.datasets.linemod import linemod_to_coco
linemod_to_coco.linemod_to_coco(cfg)
def run_tless():
from lib.datasets.tless import handle_rendering_data, fuse, handle_test_data, handle_ag_data, tless_to_coco
# handle_rendering_data.render()
# handle_rendering_data.render_to_coco()
# handle_rendering_data.prepare_asset()
# fuse.fuse()
# handle_test_data.get_mask()
# handle_test_data.test_to_coco()
handle_test_data.test_pose_to_coco()
# handle_ag_data.ag_to_coco()
# handle_ag_data.get_ag_mask()
# handle_ag_data.prepare_asset()
# tless_to_coco.handle_train_symmetry_pose()
# tless_to_coco.tless_train_to_coco()
def run_ycb():
from lib.datasets.ycb import handle_ycb
handle_ycb.collect_ycb()
def run_render():
from lib.utils.renderer import opengl_utils
from lib.utils.vsd import inout
from lib.utils.linemod import linemod_config
import matplotlib.pyplot as plt
obj_path = 'data/linemod/cat/cat.ply'
model = inout.load_ply(obj_path)
model['pts'] = model['pts'] * 1000.
im_size = (640, 300)
opengl = opengl_utils.NormalRender(model, im_size)
K = linemod_config.linemod_K
pose = np.load('data/linemod/cat/pose/pose0.npy')
depth = opengl.render(im_size, 100, 10000, K, pose[:, :3], pose[:, 3:] * 1000)
plt.imshow(depth)
plt.show()
def run_custom():
from tools import handle_custom_dataset
data_root = 'data/custom'
handle_custom_dataset.sample_fps_points(data_root)
handle_custom_dataset.custom_to_coco(data_root)
def run_detector_pvnet():
from lib.networks import make_network
from lib.datasets import make_data_loader
from lib.utils.net_utils import load_network
import tqdm
import torch
from lib.visualizers import make_visualizer
network = make_network(cfg).cuda()
network.eval()
data_loader = make_data_loader(cfg, is_train=False)
visualizer = make_visualizer(cfg)
for batch in tqdm.tqdm(data_loader):
for k in batch:
if k != 'meta':
batch[k] = batch[k].cuda()
with torch.no_grad():
output = network(batch['inp'], batch)
visualizer.visualize(output, batch)
def run_demo():
from lib.datasets import make_data_loader
from lib.visualizers import make_visualizer
import tqdm
import torch
from lib.networks import make_network
from lib.utils.net_utils import load_network
import glob
from PIL import Image
torch.manual_seed(0)
meta = np.load(os.path.join(cfg.demo_path, 'meta.npy'), allow_pickle=True).item()
demo_images = glob.glob(cfg.demo_path + '/*jpg')
network = make_network(cfg).cuda()
load_network(network, cfg.model_dir, epoch=cfg.test.epoch)
network.eval()
visualizer = make_visualizer(cfg)
mean, std = np.array([0.485, 0.456, 0.406]), np.array([0.229, 0.224, 0.225])
for demo_image in demo_images:
demo_image = np.array(Image.open(demo_image)).astype(np.float32)
inp = (((demo_image/255.)-mean)/std).transpose(2, 0, 1).astype(np.float32)
inp = torch.Tensor(inp[None]).cuda()
with torch.no_grad():
output = network(inp)
visualizer.visualize_demo(output, inp, meta)
if __name__ == '__main__':
globals()['run_'+args.type]()