-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_nadirfloor.py
281 lines (208 loc) · 12.9 KB
/
eval_nadirfloor.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
import argparse
import datetime
import json
import random
import os
import time
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import DataLoader
import util.misc as utils
#from models import build_model
###NEW
from util.plot_utils import plot_room_map, plot_score_map, plot_floorplan_with_regions
import cv2
from nadirfloor_evaluator import NadirFloor_Evaluator
from shapely.geometry import Polygon
import trimesh
import matplotlib.pyplot as plt
from datasets.poly_data import build as build_poly
from models.nadirfloornet import build_nadirfloor, get_floorplan
#####derived from RoomFormer evaluate; small adaption to parse unified s3d and zind data
@torch.no_grad()
def evaluate_nadirfloor(model, dataset_root, data_loader, device, output_dir, plot_pred=True, plot_density=True, plot_gt=True,
eval_set = 'test', export_prediction = True, invalid_scenes_ids = None):
##model.eval()
quant_result_dict = None
scene_counter = 0
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for batched_inputs in data_loader:
###########BUILD dataset from args path (i.e. COCO annotations and images)
samples = [x["image"].to(device) for x in batched_inputs] ######input density_map - default: a list with 1 element: 1 x H x W
scene_ids = [x["image_id"] for x in batched_inputs]
gt_instances = [x["instances"].to(device) for x in batched_inputs] ###### usually 1 - batch
evaluator = NadirFloor_Evaluator()#####init evaluator for nadir planes from s3d or ZInD
####prepare GT annotations
for i in range(len(gt_instances)):####### gt_instances are always 1 for evaluation
gt_polys = []
for j, poly in enumerate(gt_instances[i].gt_masks.polygons):
gt_polys.append(np.array(poly).reshape(-1,2).astype(np.int32))
metric_info = None
if (export_prediction):
metric_info = dataset_root+'/'+eval_set+'/'+f'{scene_ids[i]:05d}'+'.json'####NB. predicted by nadir shape module ###CHECK i
###logits and queries prediction
outputs = model(samples)
room_polys, floorplan3d = get_floorplan(outputs, metric_info)
quant_result_dict_scene = evaluator.evaluate_scene(room_polys=room_polys, gt_polys=gt_polys)
print('processing',str(scene_ids[i]))
if quant_result_dict is None:
quant_result_dict = quant_result_dict_scene
else:
for k in quant_result_dict.keys():
quant_result_dict[k] += quant_result_dict_scene[k]
scene_counter += 1
# draw GT map
if plot_gt:
for i, gt_inst in enumerate(gt_instances): #####for each SCENE
# plot regular room floorplan
gt_polys = []
density_map = np.transpose((samples[i] * 255).cpu().numpy(), [1, 2, 0])
density_map = np.repeat(density_map, 3, axis=2)
gt_corner_map = np.zeros([256, 256, 3])
for j, poly in enumerate(gt_inst.gt_masks.polygons):
corners = poly[0].reshape(-1, 2) ####as corners
gt_polys.append(corners)
gt_room_polys = [np.array(r) for r in gt_polys]
gt_floorplan_map = plot_floorplan_with_regions(gt_room_polys, scale=1000)
cv2.imwrite(os.path.join(output_dir, '{}_gt.png'.format(scene_ids[i])), gt_floorplan_map)
if plot_pred:
# plot regular room floorplan
room_polys = [np.array(r) for r in room_polys]
floorplan_map = plot_floorplan_with_regions(room_polys, scale=1000)
cv2.imwrite(os.path.join(output_dir, '{}_pred_floorplan.png'.format(scene_ids[i])), floorplan_map)
if plot_density:
density_map = np.transpose((samples[i] * 255).cpu().numpy(), [1, 2, 0])
density_map = np.repeat(density_map, 3, axis=2)
pred_room_map = np.zeros([256, 256, 3])
for room_poly in room_polys:
pred_room_map = plot_room_map(room_poly, pred_room_map)
# plot predicted polygon overlaid on the density map
pred_room_map = np.clip(pred_room_map + density_map, 0, 255)
cv2.imwrite(os.path.join(output_dir, '{}_pred_room_map.png'.format(scene_ids[i])), pred_room_map)
if (export_prediction):
print('export predicted polygons')
obj_name = output_dir+'/'+f'{scene_ids[i]:05d}'+'.obj'
print('saving obj at', obj_name)
trimesh.exchange.export.export_scene(floorplan3d, obj_name)
for k in quant_result_dict.keys():
quant_result_dict[k] /= float(scene_counter)
metric_category = ['room','corner','angles']
for metric in metric_category:
prec = quant_result_dict[metric+'_prec']
rec = quant_result_dict[metric+'_rec']
f1 = 2*prec*rec/(prec+rec)
quant_result_dict[metric+'_f1'] = f1
print("*************************************************")
print(quant_result_dict)
print("*************************************************")
with open(os.path.join(output_dir, 'results.txt'), 'w') as file:
file.write(json.dumps(quant_result_dict))
def get_args_parser():
parser = argparse.ArgumentParser('OmniFloor', add_help=False)
parser.add_argument('--batch_size', default=1, type=int)
# backbone
parser.add_argument('--backbone', default='resnet50', type=str,
help="Name of the convolutional backbone to use")
parser.add_argument('--lr_backbone', default=0, type=float)
parser.add_argument('--dilation', action='store_true',
help="If true, we replace stride with dilation in the last convolutional block (DC5)")
parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
help="Type of positional embedding to use on top of the image features")
parser.add_argument('--position_embedding_scale', default=2 * np.pi, type=float,
help="position / size * scale")
parser.add_argument('--num_feature_levels', default=4, type=int, help='number of feature levels')
# Transformer
parser.add_argument('--enc_layers', default=6, type=int,
help="Number of encoding layers in the transformer")
parser.add_argument('--dec_layers', default=6, type=int,
help="Number of decoding layers in the transformer")
parser.add_argument('--dim_feedforward', default=1024, type=int,
help="Intermediate size of the feedforward layers in the transformer blocks")
parser.add_argument('--hidden_dim', default=256, type=int,
help="Size of the embeddings (dimension of the transformer)")
parser.add_argument('--dropout', default=0.1, type=float,
help="Dropout applied in the transformer")
parser.add_argument('--nheads', default=8, type=int,
help="Number of attention heads inside the transformer's attentions")
parser.add_argument('--num_queries', default=800, type=int,
help="Number of query slots (num_polys * max. number of corner per poly)")
parser.add_argument('--num_polys', default=20, type=int,
help="Number of maximum number of room polygons")
parser.add_argument('--dec_n_points', default=4, type=int)
parser.add_argument('--enc_n_points', default=4, type=int)
parser.add_argument('--query_pos_type', default='sine', type=str, choices=('static', 'sine', 'none'),
help="Type of query pos in decoder - \
1. static: same setting with DETR and Deformable-DETR, the query_pos is the same for all layers \
2. sine: since embedding from reference points (so if references points update, query_pos also \
3. none: remove query_pos")
parser.add_argument('--with_poly_refine', default=True, action='store_true',
help="iteratively refine reference points (i.e. positional part of polygon queries)")
parser.add_argument('--masked_attn', default=False, action='store_true',####exp
help="if true, the query in one room will not be allowed to attend other room")
parser.add_argument('--semantic_classes', default=-1, type=int,
help="Number of classes for semantically-rich floorplan: \
1. default -1 means non-semantic floorplan \
2. 19 for Structured3D: 16 room types + 1 door + 1 window + 1 empty")
# aux
parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_true',
help="Disables auxiliary decoding losses (loss at each layer)")
# dataset parameters
parser.add_argument('--dataset_root', default='./results/s3d_nadirmaps', type=str)###SUB BEST
parser.add_argument('--eval_set', default='test', type=str)
parser.add_argument('--device', default='cuda',
help='device to use for training / testing')
###model checkpoints
parser.add_argument('--checkpoint', default='checkpoints/DEMO_RUNS/nadirfloornet_s3d.pth', help='resume from checkpoint')## ##
parser.add_argument('--output_dir', default='eval_nadirfloor_s3d_demo',help='path where to save result')#####
# visualization options
parser.add_argument('--plot_pred', default=True, type=bool, help="plot predicted floorplan")
parser.add_argument('--plot_density', default=True, type=bool, help="plot predicited room polygons overlaid on the density map")
parser.add_argument('--plot_gt', default=True, type=bool, help="plot ground truth floorplan")
parser.add_argument('--seg_image', default=False, type=bool, help='nadir map is a segmentation map')###NOT USED HERE - default: False
parser.add_argument('--save_metric', default=False, type=bool, help='save metric information')#### NOT USED HERE - default: False
parser.add_argument('--save_model', default=False, type=bool, help='save metric information')
parser.add_argument('--seed', default=42, type=int) ####NB. 42 same of RoomFormer for comparison
return parser
def main(args):
# fix the seed for reproducibility
seed = args.seed
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
device = torch.device(args.device)
# build model
model = build_nadirfloor(args, train=False)
model.to(device)
dataset_eval = build_poly(args.eval_set, args)############### BUILD dataset from args path (i.e. COCO annotations and images)
sampler_eval = torch.utils.data.SequentialSampler(dataset_eval)
def trivial_batch_collator(batch):
"""
A batch collator that does nothing.
"""
return batch
data_loader_eval = DataLoader(dataset_eval, args.batch_size, sampler=sampler_eval, drop_last=False, collate_fn=trivial_batch_collator, num_workers=0,pin_memory=True)
output_dir = Path(args.output_dir)
checkpoint = torch.load(args.checkpoint, map_location='cpu')
missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model'], strict=False)
unexpected_keys = [k for k in unexpected_keys if not (k.endswith('total_params') or k.endswith('total_ops'))]
if len(missing_keys) > 0:
print('Missing Keys: {}'.format(missing_keys))
if len(unexpected_keys) > 0:
print('Unexpected Keys: {}'.format(unexpected_keys))
save_dir = os.path.join(os.path.dirname(args.checkpoint), output_dir)
evaluate_nadirfloor(
model, args.dataset_root, data_loader_eval,
device, save_dir,
plot_pred=args.plot_pred,
plot_density=args.plot_density,
plot_gt=args.plot_gt,
export_prediction = args.save_model,
eval_set = args.eval_set,
invalid_scenes_ids = []###
)
if __name__ == '__main__':
parser = argparse.ArgumentParser('Evaluation script', parents=[get_args_parser()])
args = parser.parse_args()
main(args)