-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
289 lines (252 loc) · 13.7 KB
/
test.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
import os
import time
import random
import numpy as np
import logging
import pickle
import argparse
import collections
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
from util import config
from util.common_util import AverageMeter, intersectionAndUnion, check_makedirs
from util.voxelize import voxelize
from util import vis_util
from util.ply import write_ply, read_ply
random.seed(123)
np.random.seed(123)
def get_parser():
parser = argparse.ArgumentParser(description='PyTorch Point Cloud Semantic Segmentation')
parser.add_argument('--config', type=str, default='config/s3dis.yaml', help='config file')
parser.add_argument('opts', help='see config/s3dis.yaml for all options', default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
assert args.config is not None
cfg = config.load_cfg_from_cfg_file(args.config)
if args.opts is not None:
cfg = config.merge_cfg_from_list(cfg, args.opts)
return cfg
def get_logger():
logger_name = "main-logger"
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
fmt = "[%(asctime)s %(levelname)s %(filename)s line %(lineno)d %(process)d] %(message)s"
handler.setFormatter(logging.Formatter(fmt))
logger.addHandler(handler)
return logger
def main():
global args, logger
args = get_parser()
logger = get_logger()
logger.info(args)
assert args.classes > 1
logger.info("=> creating model ...")
logger.info("Classes: {}".format(args.classes))
if args.data_name == 's3dis':
from model.s3dis import weak_seg_repro as Model
elif args.data_name == 'scannet':
from model.scannet import weak_seg_repro as Model
elif args.data_name == 'stpls':
from model.stpls3d import weak_seg_repro as Model
else:
raise Exception('dataset not supported yet'.format(args.data_name))
model = Model(c=args.fea_dim, k=args.classes).cuda()
logger.info(model)
criterion = nn.CrossEntropyLoss(ignore_index=args.ignore_label).cuda()
names = [line.rstrip('\n') for line in open(args.names_path)]
if os.path.isfile(args.model_path):
logger.info("=> loading checkpoint '{}'".format(args.model_path))
checkpoint = torch.load(args.model_path)
state_dict = checkpoint['state_dict']
new_state_dict = collections.OrderedDict()
#print(state_dict.items)
for k, v in state_dict.items():
name = k[7:]
new_state_dict[name] = v
model.load_state_dict(new_state_dict, strict=True)
logger.info("=> loaded checkpoint '{}' (epoch {})".format(args.model_path, checkpoint['epoch']))
args.epoch = checkpoint['epoch']
else:
raise RuntimeError("=> no checkpoint found at '{}'".format(args.model_path))
test(model, criterion, names)
def data_prepare():
train_sequences = ['Synthetic_v1', 'Synthetic_v2', 'Synthetic_v3', 'RealWorldData']
cvalid_sequences = ['OCCC_points', 'RA_points', 'USC_points', 'WMSC_points']
if args.data_name == 's3dis':
data_list = sorted(os.listdir(args.data_root))
data_list = [item[:-4] for item in data_list if 'Area_{}'.format(args.test_area) in item]
elif args.data_name == 'scannet':
data_list = sorted(os.listdir(args.data_root_val))
data_list = [item[:-4] for item in data_list if '.pth' in item]
elif args.data_name == 'stpls':
data_list = []
for sq in train_sequences:
data_list += os.listdir(args.data_root + sq)
data_list = [item for item in data_list if '{}_'.format(cvalid_sequences[args.test_area]) in item]
else:
raise Exception('dataset not supported yet'.format(args.data_name))
print("Totally {} samples in val set.".format(len(data_list)))
return data_list
def changeSemLabels(cloud):
cloud[:, 6:7] = np.where((cloud[:, 6:7] >= 2) & (cloud[:, 6:7] <= 4), 2, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] >= 5) & (cloud[:, 6:7] <= 6), 3, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] == 8), 3, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] >= 11) & (cloud[:, 6:7] <= 12), 4, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] == 14), 5, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] >= 7) & (cloud[:, 6:7] <= 10), 1, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] == 13), 1, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] >= 15) & (cloud[:, 6:7] <= 16), 0, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] == 17), 1, cloud[:, 6:7])
cloud[:, 6:7] = np.where((cloud[:, 6:7] >17), 0, cloud[:, 6:7])
return cloud
def ply2array(ply_path):
cloud = read_ply(ply_path)
cloud = np.vstack((cloud['x'], cloud['y'], cloud['z'], cloud['red'], cloud['green'], cloud['blue'], cloud['class'])).T
cloud = changeSemLabels(cloud)
return cloud
def data_load(data_name):
if args.data_name == 's3dis':
data_path = os.path.join(args.data_root, data_name + '.npy')
data = np.load(data_path) # xyzrgbl, N*7
coord, feat, label = data[:, :3], data[:, 3:6], data[:, 6]
elif args.data_name == 'scannet':
data_path = os.path.join(args.data_root_val, data_name + '.pth')
data = torch.load(data_path) # xyzrgbl, N*7
coord, feat, label = data[0], data[1], data[2]
elif args.data_name == 'stpls':
data_path = os.path.join(args.data_root, "RealWorldData", data_name)
data = ply2array(data_path)
coord, feat, label = data[:, :3], data[:, 3:6], data[:, 6]
else:
raise Exception('dataset not supported yet'.format(args.data_name))
idx_data = []
if args.voxel_size:
coord_min = np.min(coord, 0)
coord -= coord_min
idx_sort, count = voxelize(coord, args.voxel_size, mode=1)
for i in range(count.max()):
idx_select = np.cumsum(np.insert(count, 0, 0)[0:-1]) + i % count
idx_part = idx_sort[idx_select]
idx_data.append(idx_part)
else:
idx_data.append(np.arange(label.shape[0]))
#print(type(label))
return coord, feat, label, idx_data
def input_normalize(coord, feat):
coord_min = np.min(coord, 0)
coord -= coord_min
feat = feat / 255.
return coord, feat
def test(model, criterion, names):
logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
batch_time = AverageMeter()
intersection_meter = AverageMeter()
union_meter = AverageMeter()
target_meter = AverageMeter()
args.batch_size_test = 10
model.eval()
check_makedirs(args.save_folder)
pred_save, label_save = [], []
data_list = data_prepare()
for idx, item in enumerate(data_list):
end = time.time()
pred_save_path = os.path.join(args.save_folder, '{}_{}_pred.npy'.format(item, args.epoch))
label_save_path = os.path.join(args.save_folder, '{}_{}_label.npy'.format(item, args.epoch))
if os.path.isfile(pred_save_path) and os.path.isfile(label_save_path):
logger.info('{}/{}: {}, loaded pred and label.'.format(idx + 1, len(data_list), item))
pred, label = np.load(pred_save_path), np.load(label_save_path)
else:
coord, feat, label, idx_data = data_load(item)
#print(label)
pred = torch.zeros((label.size, args.classes)).cuda()
idx_size = len(idx_data)
idx_list, coord_list, feat_list, offset_list = [], [], [], []
for i in range(idx_size):
logger.info('{}/{}: {}/{}/{}, {}'.format(idx + 1, len(data_list), i + 1, idx_size, idx_data[0].shape[0], item))
idx_part = idx_data[i]
coord_part, feat_part = coord[idx_part], feat[idx_part]
if args.voxel_max and coord_part.shape[0] > args.voxel_max:
coord_p, idx_uni, cnt = np.random.rand(coord_part.shape[0]) * 1e-3, np.array([]), 0
while idx_uni.size != idx_part.shape[0]:
init_idx = np.argmin(coord_p)
dist = np.sum(np.power(coord_part - coord_part[init_idx], 2), 1)
idx_crop = np.argsort(dist)[:args.voxel_max]
coord_sub, feat_sub, idx_sub = coord_part[idx_crop], feat_part[idx_crop], idx_part[idx_crop]
dist = dist[idx_crop]
delta = np.square(1 - dist / np.max(dist))
coord_p[idx_crop] += delta
coord_sub, feat_sub = input_normalize(coord_sub, feat_sub)
idx_list.append(idx_sub), coord_list.append(coord_sub), feat_list.append(feat_sub), offset_list.append(idx_sub.size)
idx_uni = np.unique(np.concatenate((idx_uni, idx_sub)))
else:
coord_part, feat_part = input_normalize(coord_part, feat_part)
idx_list.append(idx_part), coord_list.append(coord_part), feat_list.append(feat_part), offset_list.append(idx_part.size)
batch_num = int(np.ceil(len(idx_list) / args.batch_size_test))
for i in range(batch_num):
s_i, e_i = i * args.batch_size_test, min((i + 1) * args.batch_size_test, len(idx_list))
idx_part, coord_part, feat_part, offset_part = idx_list[s_i:e_i], coord_list[s_i:e_i], feat_list[s_i:e_i], offset_list[s_i:e_i]
idx_part = np.concatenate(idx_part)
coord_part = torch.FloatTensor(np.concatenate(coord_part)).cuda(non_blocking=True)
feat_part = torch.FloatTensor(np.concatenate(feat_part)).cuda(non_blocking=True)
offset_part = torch.IntTensor(np.cumsum(offset_part)).cuda(non_blocking=True)
with torch.no_grad():
pred_part = model([coord_part, feat_part, offset_part]) # (n, k)
torch.cuda.empty_cache()
pred[idx_part, :] += pred_part
logger.info('Test: {}/{}, {}/{}, {}/{}'.format(idx + 1, len(data_list), e_i, len(idx_list), args.voxel_max, idx_part.shape[0]))
loss = criterion(pred, torch.LongTensor(label).cuda(non_blocking=True)) # for reference
pred = pred.max(1)[1].data.cpu().numpy()
if args.data_name == 's3dis':
vis_util.write_ply_color(coord, pred, os.path.join(args.save_folder, item +'_pred'+'.obj'))
vis_util.write_ply_color(coord, label, os.path.join(args.save_folder, item+'_label' + '.obj'))
vis_util.write_ply_rgb(coord, feat, os.path.join(args.save_folder, item+'_images' + '.obj'))
elif args.data_name == 'scannet':
vis_util.write_ply_color_scannet(coord, pred, os.path.join(args.save_folder, item +'_pred01'+'.obj'))
vis_util.write_ply_color_scannet(coord, label, os.path.join(args.save_folder, item+'_label' + '.obj'))
vis_util.write_ply_rgb_scannet(coord, feat, os.path.join(args.save_folder, item+'_images' + '.obj'))
elif args.data_name == 'stpls':
coordvis, featvis, _, _ = data_load(item)
vis_util.write_ply_color_stpls3d(coordvis, pred, os.path.join(args.save_folder, item +'_pred'+'.obj'))
vis_util.write_ply_color_stpls3d(coordvis, label, os.path.join(args.save_folder, item+'_label' + '.obj'))
vis_util.write_ply_rgb(coordvis, featvis, os.path.join(args.save_folder, item+'_images' + '.obj'))
else:
raise Exception('dataset not supported yet'.format(args.data_name))
# calculation 1: add per room predictions
intersection, union, target = intersectionAndUnion(pred, label, args.classes, args.ignore_label)
intersection_meter.update(intersection)
union_meter.update(union)
target_meter.update(target)
accuracy = sum(intersection) / (sum(target) + 1e-10)
batch_time.update(time.time() - end)
logger.info('Test: [{}/{}]-{} '
'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
'Accuracy {accuracy:.4f}.'.format(idx + 1, len(data_list), label.size, batch_time=batch_time, accuracy=accuracy))
pred_save.append(pred); label_save.append(label)
np.save(pred_save_path, pred); np.save(label_save_path, label)
with open(os.path.join(args.save_folder, "pred.pickle"), 'wb') as handle:
pickle.dump({'pred': pred_save}, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open(os.path.join(args.save_folder, "label.pickle"), 'wb') as handle:
pickle.dump({'label': label_save}, handle, protocol=pickle.HIGHEST_PROTOCOL)
# calculation 1
iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
mIoU1 = np.mean(iou_class)
mAcc1 = np.mean(accuracy_class)
allAcc1 = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)
# calculation 2
intersection, union, target = intersectionAndUnion(np.concatenate(pred_save), np.concatenate(label_save), args.classes, args.ignore_label)
iou_class = intersection / (union + 1e-10)
accuracy_class = intersection / (target + 1e-10)
mIoU = np.mean(iou_class)
mAcc = np.mean(accuracy_class)
allAcc = sum(intersection) / (sum(target) + 1e-10)
logger.info('Val result: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(mIoU, mAcc, allAcc))
logger.info('Val1 result: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(mIoU1, mAcc1, allAcc1))
for i in range(args.classes):
logger.info('Class_{} Result: iou/accuracy {:.4f}/{:.4f}, name: {}.'.format(i, iou_class[i], accuracy_class[i], names[i]))
logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
if __name__ == '__main__':
main()