-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
309 lines (286 loc) · 10.1 KB
/
eval.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import argparse
import os
import json
from functools import partial
parser = argparse.ArgumentParser()
parser.add_argument(
"--save",
action="store_true",
help="If specified, the script will save the results under model folder. Otherwise, results are shown on fly.",
)
parser.add_argument("--gpu", type=str, default="0", help="Device ID of the GPU to use.")
parser.add_argument(
"--sample_grid", type=float, default=0.1, help="Sampling grid resolution in meters."
)
parser.add_argument(
"--sample_nrots", type=int, default=16, help="Number of rotations to sample."
)
parser.add_argument(
"--sample_batchsize",
type=int,
default=256,
help="Increase batch size to get higher speed.",
)
parser.add_argument(
"--max_refine_its",
type=int,
default=3,
help="Maximum # of iterations for the refinement branch",
)
parser.add_argument(
"--dataset_path",
type=str,
required=True,
help="Location of inference dataset on disk.",
)
parser.add_argument(
"--dataset",
type=str,
required=True,
help="zind/s3df/s3ds/s3de. ZInD or S3D with full/simple/empty furnishing-level.",
choices=["zind", "s3df", "s3ds", "s3de"],
)
parser.add_argument(
"--mode",
type=str,
default="refine",
help="match/refine. Use `match` mode to skip refinement.",
choices=["match", "refine"],
)
parser.add_argument(
"--fov",
type=float,
default=None,
help="Overwrite testing fov. (default is same as training). If model trained using mixed-fov, need specify a single fov when eval",
)
parser.add_argument(
"--interesting",
type=int,
default=0,
help="0/1. Use `1` to indicate saliency-aware FoV sampling.",
)
parser.add_argument(
"--log_dir",
type=str,
required=True,
help="Directory where model is saved and logs written.",
)
parser.add_argument(
"--eval_all",
type=int,
default=1,
help="0/1. Use `0` to evaluate 1 sample for each floor map.",
)
parser.add_argument(
"--suffix", type=str, default=None, help="Add suffix to result folder name"
)
opt = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpu
opt.eval_all = bool(opt.eval_all)
opt.interesting = bool(opt.interesting)
print(opt)
import os
import pickle
import cv2
import numpy as np
import torch
from torch.utils.data import DataLoader
from dataset.zind_dataset import ZindDataset
from dataset.s3d_dataset import S3dDataset
from model.fplocnet import FpLocNet
from eval_utils import *
def mkdir_if_not_exist(path):
if not os.path.isdir(path):
os.makedirs(path)
if __name__ == "__main__":
with open(os.path.join(opt.log_dir, "cfg.json"), "r") as f:
cfg = json.load(f)
# overwrite some cfg if need
if opt.fov is not None:
cfg["fov"] = opt.fov
assert not (
isinstance(cfg["fov"], list) or isinstance(cfg["fov"], tuple)
) # if model trained using mixed-fov, need specify a single fov when eval
cfg["find_interesting_fov"] = opt.interesting
print(cfg)
if opt.dataset == "zind":
_dataset = ZindDataset
elif opt.dataset == "s3df":
_dataset = partial(S3dDataset, s3d_eval_furnishing="full")
elif opt.dataset == "s3ds":
_dataset = partial(S3dDataset, s3d_eval_furnishing="simple")
elif opt.dataset == "s3de":
_dataset = partial(S3dDataset, s3d_eval_furnishing="empty")
else:
raise "Unknown dataset"
dataloader = DataLoader(
_dataset(
opt.dataset_path,
is_training=False,
n_sample_points=None,
line_sampling_interval=0.1,
return_empty_when_invalid=True,
return_all_panos=opt.eval_all,
crop_fov=cfg["fov"],
view_type=cfg["view_type"],
find_interesting_fov=cfg["find_interesting_fov"],
),
batch_size=1,
shuffle=False,
num_workers=1,
drop_last=False,
)
model = FpLocNet(cfg).cuda()
ckpt = torch.load(os.path.join(opt.log_dir, "ckpt"))
model.load_state_dict(ckpt["model_state_dict"], strict=True)
global_step = ckpt["global_step"]
print(f"Model with step={global_step} loaded.")
if opt.save:
if opt.suffix is None:
folder_name = "results-{0}-fov{1:03d}-{2}-{3}".format(
opt.mode,
int(cfg["fov"]),
"sal" if opt.interesting else "rnd",
opt.dataset,
)
else:
folder_name = "results-{0}-fov{1:03d}-{2}-{3}-{4}".format(
opt.mode,
int(cfg["fov"]),
"sal" if opt.interesting else "rnd",
opt.dataset,
opt.suffix,
)
save_dir = os.path.join(opt.log_dir, folder_name)
mkdir_if_not_exist(os.path.join(save_dir, "score_maps"))
mkdir_if_not_exist(os.path.join(save_dir, "rot_maps"))
mkdir_if_not_exist(os.path.join(save_dir, "results"))
mkdir_if_not_exist(os.path.join(save_dir, "query_images"))
mkdir_if_not_exist(os.path.join(save_dir, "terrs"))
mkdir_if_not_exist(os.path.join(save_dir, "rerrs"))
mkdir_if_not_exist(os.path.join(save_dir, "raws"))
idx = 0
for data in dataloader:
if len(data.keys()) == 0:
idx += 1
continue
for k in data.keys():
if torch.is_tensor(data[k]) and not data[k].is_cuda:
data[k] = data[k].cuda()
if cfg["disable_semantics"]:
data["bases_feat"][..., -2:] = 0
if opt.eval_all:
data["query_image"] = data["query_image"].squeeze(0)
data["gt_loc"] = data["gt_loc"].squeeze(0)
data["gt_fov"] = data["gt_fov"].squeeze(0)
data["gt_rot"] = data["gt_rot"].squeeze(0)
sample_ret = sample_floorplan(
data,
model,
cfg,
sample_grid=opt.sample_grid,
batch_size=opt.sample_batchsize,
)
match_ret = match_images(
sample_ret,
data,
model,
cfg,
mode=opt.mode,
sample_nrots=opt.sample_nrots,
max_refine_its=opt.max_refine_its,
)
fmt = {
"idx": idx,
"sampling_fps": sample_ret["sampling_fps"],
"sampling_time": sample_ret["sampling_time"],
"matching_fps": match_ret["matching_fps"],
"matching_time": match_ret["matching_time"],
"median_terr": np.median(match_ret["terrs"]),
"median_rerr": np.median(match_ret["rerrs"]),
}
print(
"{idx}, {sampling_fps:.0f} sampling_fps, {sampling_time:.2f} sampling_time, {matching_fps:.2f} matching_fps, {matching_time:.2f} matching_time, {median_terr:.4f} median_terr, {median_rerr:.4f} median_rerr".format(
**fmt
)
)
for i in range(match_ret["n_images"]):
score_map = match_ret["score_maps"][i]
rot_map = match_ret["rot_maps"][i]
loc_gt = match_ret["loc_gts"][i]
loc_est = match_ret["loc_ests"][i]
score_map_viz = (
cv2.resize(score_map, (0, 0), fx=2, fy=2) ** 2 * 255
).astype(np.uint8)
rot_map_viz = (
cv2.resize(rot_map / (2 * np.pi), (0, 0), fx=2, fy=2) * 255
).astype(np.uint8)
result_viz = render_result(
data["bases"][0].cpu().numpy(),
data["bases_feat"][0].cpu().numpy(),
loc_gt,
loc_est,
)
query_image_viz = data["query_image"][i].permute(1, 2, 0).cpu().numpy() * (
0.229,
0.224,
0.225,
) + (0.485, 0.456, 0.406)
query_image_viz = cv2.cvtColor(
(query_image_viz * 255).astype(np.uint8), cv2.COLOR_BGR2RGB
)
if opt.save:
if i == 0:
np.savetxt(
os.path.join(save_dir, "terrs", f"terrs_{idx:04d}.txt"),
match_ret["terrs"],
)
np.savetxt(
os.path.join(save_dir, "rerrs", f"rerrs_{idx:04d}.txt"),
match_ret["rerrs"],
)
with open(
os.path.join(save_dir, "raws", f"raw_{idx:04d}.pkl"), "wb"
) as f:
merged_raw = {**sample_ret, **match_ret, **data}
merged_raw["idx"] = idx
merged_raw[
"samples_feat"
] = None # too large to store, render in runtime
merged_raw["fp_feat"] = None
merged_raw["img_feats"] = None
for k in merged_raw:
if torch.is_tensor(merged_raw[k]):
merged_raw[k] = merged_raw[k].cpu().numpy()
pickle.dump(merged_raw, f)
cv2.imwrite(
os.path.join(
save_dir, "score_maps", f"score_map_{idx:04d}_{i:03d}.png"
),
score_map_viz,
)
cv2.imwrite(
os.path.join(
save_dir, "rot_maps", f"rot_map_{idx:04d}_{i:03d}.png"
),
rot_map_viz,
)
cv2.imwrite(
os.path.join(save_dir, "results", f"result_{idx:04d}_{i:03d}.png"),
result_viz,
)
cv2.imwrite(
os.path.join(
save_dir, "query_images", f"query_image_{idx:04d}_{i:03d}.png"
),
query_image_viz,
)
else:
cv2.imshow("score_map_viz", score_map_viz)
cv2.imshow("rot_map_viz", rot_map_viz)
cv2.imshow("result_viz", result_viz)
cv2.imshow("query_image_viz", query_image_viz)
if cv2.waitKey(1 if opt.save else 0) == 113:
exit(0)
pass
idx += 1