-
Notifications
You must be signed in to change notification settings - Fork 5
/
evaluate_scannet.py
203 lines (170 loc) · 8.54 KB
/
evaluate_scannet.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
"""
Modified from PointConv: https://github.com/DylanWusee/pointconv
Evaluation on ScanNet: Generalize neccenary .ply and .txt file
Author: Jingyu Gong and Jiachen Xu
Date: June 2020
"""
import argparse
import math
from datetime import datetime
import h5py
from plyfile import PlyData, PlyElement
import numpy as np
import tensorflow as tf
import socket
import importlib
import os
import sys
from datetime import datetime
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, 'models'))
sys.path.append(os.path.join(BASE_DIR, 'utils'))
sys.path.append(os.path.join(BASE_DIR, 'scannet'))
#sys.path.append(os.path.join(BASE_DIR, 'scannet/preprocessing'))
sys.path.append(os.path.join(BASE_DIR, 'scannet/visualize'))
import provider
import tf_util
import scannet_dataset_sw_rgb
import pc_util
from visualize_labels_on_mesh import visualize
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=0, help='GPU to use [default: GPU 0]')
parser.add_argument('--model', default='scene_encoder_rsl', help='Model name [default: model]')
parser.add_argument('--batch_size', type=int, default=8, help='Batch Size during training [default: 8]')
parser.add_argument('--num_point', type=int, default=8192, help='Point Number [256/512/1024/2048] [default: 8192]')
parser.add_argument('--model_path', default='log/model.ckpt', help='model checkpoint file path [default: log/model.ckpt]')
parser.add_argument('--ply_path', default='scannet', help='ply path from original Scannet')
parser.add_argument('--dump_dir', default='dump', help='dump folder path [dump]')
parser.add_argument('--num_votes', type=int, default=5, help='Aggregate classification scores from multiple rotations [default: 5]')
parser.add_argument('--with_rgb',help='With rgb or not', action='store_true')
FLAGS = parser.parse_args()
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
MODEL_PATH = FLAGS.model_path
GPU_INDEX = FLAGS.gpu
WITH_RGB = FLAGS.with_rgb
PLY_PATH = FLAGS.ply_path
MODEL = importlib.import_module(FLAGS.model) # import network module
DUMP_DIR = FLAGS.dump_dir + datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
if not os.path.exists(DUMP_DIR): os.mkdir(DUMP_DIR)
LOG_FOUT = open(os.path.join(DUMP_DIR, 'log_evaluate.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')
BANDWIDTH = 0.05
NUM_CLASSES = 21
HOSTNAME = socket.gethostname()
print(WITH_RGB)
DATA_PATH = os.path.join(BASE_DIR, 'scannet')
print("start loading whole scene data ...")
TEST_DATASET_WHOLE_SCENE = scannet_dataset_sw_rgb.ScannetDatasetWholeScene_evaluation(root=DATA_PATH, split='test', with_rgb = WITH_RGB)
def log_string(out_str):
LOG_FOUT.write(out_str+'\n')
LOG_FOUT.flush()
print(out_str)
def evaluate(num_votes):
with tf.device('/gpu:'+str(GPU_INDEX)):
if WITH_RGB:
pointclouds_pl = tf.placeholder(tf.float32, shape=(BATCH_SIZE, NUM_POINT, 6))
else:
pointclouds_pl = tf.placeholder(tf.float32, shape=(BATCH_SIZE, NUM_POINT, 3))
print(pointclouds_pl)
labels_pl = tf.placeholder(tf.int32, shape=(BATCH_SIZE, NUM_POINT))
labels_onehot_pl = tf.placeholder(tf.float32, shape=(BATCH_SIZE, NUM_POINT, NUM_CLASSES))
smpws_pl = tf.placeholder(tf.float32, shape=(BATCH_SIZE, NUM_POINT))
external_scene_encode_pl = tf.placeholder(tf.int32, shape=(BATCH_SIZE, NUM_CLASSES))
is_training_pl = tf.placeholder(tf.bool, shape=())
cos_loss_weight = tf.placeholder(tf.float32, shape=())
#pred, end_points = MODEL.get_model(pointclouds_pl, is_training_pl, NUM_CLASSES, BANDWIDTH)
pred_origin, end_points, external_scene_feature = MODEL.get_scene_model(pointclouds_pl, is_training_pl, NUM_CLASSES, BANDWIDTH)
#MODEL.get_loss(pred, labels_pl, smpws_pl)
loss, pred = MODEL.get_scene_loss(cos_loss_weight, pred_origin, labels_pl, labels_onehot_pl, smpws_pl, external_scene_feature, external_scene_encode_pl, end_points['feats'], pointclouds_pl)
losses = tf.get_collection('losses')
total_loss = tf.add_n(losses, name='total_loss')
saver = tf.train.Saver()
# Create a session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
config.log_device_placement = False
sess = tf.Session(config=config)
# Restore variables from disk.
saver.restore(sess, MODEL_PATH)
log_string("Model restored.")
ops = {'pointclouds_pl': pointclouds_pl,
'labels_pl': labels_pl,
'is_training_pl': is_training_pl,
'pred': pred,
'cos_loss_weight': cos_loss_weight}
eval_one_epoch(sess, ops, num_votes)
def add_vote(vote_label_pool, point_idx, pred_label):
B = pred_label.shape[0]
N = pred_label.shape[1]
for b in range(B):
for n in range(N):
vote_label_pool[int(point_idx[b, n]), int(pred_label[b, n])] += 1
return vote_label_pool
test_class = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])
def eval_one_epoch(sess, ops, num_votes=1, topk=1):
is_training = False
file_list = "./scannet/scannetv2_test.txt"
with open(file_list) as fl:
scene_id = fl.read().splitlines()
num_batches = len(TEST_DATASET_WHOLE_SCENE)
total_seen_class = [0 for _ in range(NUM_CLASSES)]
total_correct_class = [0 for _ in range(NUM_CLASSES)]
total_iou_deno_class = [0 for _ in range(NUM_CLASSES)]
log_string(str(datetime.now()))
log_string('---- GENERATING TEST RESULTS ----')
log_string('vote num: '+str(num_votes))
for batch_idx in range(num_batches):
print("test %d %s ..."%(batch_idx, scene_id[batch_idx]))
whole_scene_points_index = TEST_DATASET_WHOLE_SCENE.scene_points_id[batch_idx]
whole_scene_points_num = TEST_DATASET_WHOLE_SCENE.scene_points_num[batch_idx]
whole_scene_label = TEST_DATASET_WHOLE_SCENE.semantic_labels_list[batch_idx]
vote_label_pool = np.zeros((whole_scene_label.shape[0], NUM_CLASSES))
for vote_idx in range(num_votes):
scene_data, scene_label, scene_smpw, scene_point_index = TEST_DATASET_WHOLE_SCENE[batch_idx]
num_blocks = scene_data.shape[0]
s_batch_num = (num_blocks + BATCH_SIZE - 1) // BATCH_SIZE
if WITH_RGB:
batch_data = np.zeros((BATCH_SIZE, NUM_POINT, 6))
else:
batch_data = np.zeros((BATCH_SIZE, NUM_POINT, 3))
batch_label = np.zeros((BATCH_SIZE, NUM_POINT))
batch_point_index = np.zeros((BATCH_SIZE, NUM_POINT))
for sbatch in range(s_batch_num):
start_idx = sbatch * BATCH_SIZE
end_idx = min((sbatch + 1)*BATCH_SIZE, num_blocks)
real_batch_size = end_idx - start_idx
batch_data[0:real_batch_size,...] = scene_data[start_idx:end_idx, ...]
batch_label[0:real_batch_size,...] = scene_label[start_idx:end_idx, ...]
batch_point_index[0:real_batch_size,...] = scene_point_index[start_idx:end_idx, ...]
if WITH_RGB:
batch_data[:, :, 3:6] /= 1.0 #255.0
feed_dict = {ops['pointclouds_pl']: batch_data,
ops['labels_pl']: batch_label,
ops['is_training_pl']: is_training,
ops['cos_loss_weight']: 1.0}
pred_val = sess.run(ops['pred'], feed_dict=feed_dict)#BxNxNUM_CLASSES
batch_pred_label = np.argmax(pred_val[:, :, 1:], 2) + 1#BxN
vote_label_pool = add_vote(vote_label_pool, batch_point_index[0:real_batch_size,...], batch_pred_label[0:real_batch_size,...])
pred_label = np.argmax(vote_label_pool, 1)
for l in range(NUM_CLASSES):
total_seen_class[l] += np.sum((whole_scene_label==l))
total_correct_class[l] += np.sum((pred_label==l) & (whole_scene_label==l))
total_iou_deno_class[l] += np.sum(((pred_label==l) | (whole_scene_label==l)) & (whole_scene_label > 0))
print(total_correct_class)
print(total_iou_deno_class)
print(total_seen_class)
whole_scene_data = np.zeros(whole_scene_points_num)
whole_scene_data[whole_scene_points_index] = test_class[pred_label.astype(np.int32)]
filename = os.path.join(DUMP_DIR, scene_id[batch_idx] + '.txt')
with open(filename, 'w') as pl_save:
for i in whole_scene_data:
pl_save.write(str(int(i))+'\n')
pl_save.close()
print("Done!")
if __name__=='__main__':
with tf.Graph().as_default():
evaluate(num_votes=FLAGS.num_votes)
LOG_FOUT.close()