-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_full_cloud.py
242 lines (212 loc) · 8.71 KB
/
test_full_cloud.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
#
#
# 0=================================0
# | Kernel Point Convolutions |
# 0=================================0
#
#
# ----------------------------------------------------------------------------------------------------------------------
#
# Callable script to start a training on ModelNet40 dataset
#
# ----------------------------------------------------------------------------------------------------------------------
#
# Hugues THOMAS - 06/03/2020
#
# ----------------------------------------------------------------------------------------------------------------------
#
# Imports and global variables
# \**********************************/
#
import argparse
import os
import time
import torch
from torch.utils.data import DataLoader
from data_config import (NPM3DConfig, S3DISConfig, Semantic3DConfig,
XMap3DConfig)
from models.architectures_test import (KPCNN, KPFCNN, KP_Pyramid_V1,
KP_Pyramid_V2)
from utils.tester_test import ModelTester
def parse_args():
parser = argparse.ArgumentParser(description='Train kp_pyramid')
parser.add_argument(
'model_path',
help='the path to the log and checkpoint file to load for inference')
parser.add_argument(
'--chkp-idx',
type=int,
default=500,
help='the specific checkpoint epoch number for the model')
parser.add_argument(
'--network',
type=str,
default='KPConv',
help='the network architecture (kpconv, kpconv_deform, Pyramid_v1, '
'pyramid_v2, pyramid_v1_deform, pyramid_v2_deform) to use')
parser.add_argument(
'--dataset',
type=str,
default='S3DIS',
help='the Dataset (S3DIS, XMap3D, NPM3D, Semantic3D) to use')
parser.add_argument('--work-dir', help='the dir to save logs and models')
parser.add_argument('--gpu-id',
default='3',
type=str,
help='the gpu to use')
parser.add_argument('--test-area-idx',
default=5,
type=int,
help='the test area index')
parser.add_argument('--is-on-val',
default=True,
type=lambda x: (str(x).lower() == 'true'),
help='test on validation or test dataset?')
parser.add_argument('--num-votes',
type=float,
default=100,
help='number of votes')
parser.add_argument('--seed', type=int, default=None, help='random seed')
parser.add_argument('--launcher',
choices=['none', 'pytorch', 'slurm', 'mpi'],
default='none',
help='job launcher')
parser.add_argument('--saving-path',
default=None,
type=str,
help='saving path for predictions')
parser.add_argument('--local_rank', type=int, default=0)
args = parser.parse_args()
if 'LOCAL_RANK' not in os.environ:
os.environ['LOCAL_RANK'] = str(args.local_rank)
return args
args = parse_args()
if args.dataset.lower() == 's3dis':
from datasets.S3DIS import S3DISDataset as myDataset
from datasets.S3DIS import S3DISSampler as mySampler
from datasets.S3DIS import S3DISCollate as myCollate
myConfig = S3DISConfig
elif args.dataset.lower() == 'npm3d':
from datasets.NPM3D_test import NPM3DDataset as myDataset
from datasets.NPM3D_test import NPM3DSampler as mySampler
from datasets.NPM3D_test import NPM3DCollate as myCollate
myConfig = NPM3DConfig
elif args.dataset.lower() == 'semantic3d':
from datasets.Semantic3D_test import Semantic3DDataset as myDataset
from datasets.Semantic3D_test import Semantic3DSampler as mySampler
from datasets.Semantic3D_test import Semantic3DCollate as myCollate
myConfig = Semantic3DConfig
elif args.dataset.lower() == 'xmap3d_kp':
from datasets.XMap3D_KP import XMap3DDataset as myDataset
from datasets.XMap3D_KP import XMap3DSampler as mySampler
from datasets.XMap3D_KP import XMap3DCollate as myCollate
myConfig = XMap3DConfig
elif args.dataset.lower() == 'xmap3d':
from datasets.XMap3D import XMap3DDataset as myDataset
from datasets.XMap3D import XMap3DSampler as mySampler
from datasets.XMap3D import XMap3DCollate as myCollate
myConfig = XMap3DConfig
else:
print('This Dataset has not been implemented')
# ----------------------------------------------------------------------------------------------------------------------
#
# Main Call
# \***************/
#
if __name__ == '__main__':
###############################
# Choose the model to visualize
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
chosen_log = args.model_path
# Choose the index of the checkpoint to load OR None if you want to load
# the current checkpoint
chkp_idx = args.chkp_idx
chosen_chkp = 'chkp_%04d.tar' % chkp_idx
chosen_chkp = os.path.join(chosen_log, 'checkpoints', chosen_chkp)
print('used chkp', chosen_chkp)
config = myConfig(args.network)
config.load(chosen_log)
config.saving_path = args.saving_path
config.saving = False if config.saving_path is None else True
config.validation_size = 200
config.input_threads = 10
##############
# Prepare Data
##############
print()
print('Data Preparation')
print('****************')
if args.is_on_val:
set = 'validation'
else:
set = 'test'
if config.dataset == 'NPM3D':
test_dataset = myDataset(config,
set=set,
use_potentials=True,
on_val=args.is_on_val)
elif config.dataset == 'S3DIS':
test_dataset = myDataset(config,
set=set,
use_potentials=True,
validation_split=args.test_area_idx)
else:
test_dataset = myDataset(config, set=set, use_potentials=True)
test_sampler = mySampler(test_dataset)
collate_fn = myCollate
# Data loader
test_loader = DataLoader(test_dataset,
batch_size=1,
sampler=test_sampler,
collate_fn=collate_fn,
num_workers=config.input_threads,
pin_memory=True)
# Calibrate samplers
test_sampler.calibration(test_loader, verbose=True)
print('\nModel Preparation')
print('*****************')
# Define network model
t1 = time.time()
if config.dataset_task == 'classification':
net = KPCNN(config)
elif config.dataset_task in ['cloud_segmentation', 'slam_segmentation']:
if args.network.lower() == 'pyramid_v1' or args.network.lower(
) == 'pyramid_v1_deform': # vertical
net = KP_Pyramid_V1(config,
test_dataset.label_values,
test_dataset.ignored_labels,
use_multi_layer=config.use_multi_layer)
elif args.network.lower() == 'pyramid_v2' or args.network.lower(
) == 'pyramid_v2_deform': # horizontal
net = KP_Pyramid_V2(config,
test_dataset.label_values,
test_dataset.ignored_labels,
use_multi_layer=config.use_multi_layer)
else:
net = KPFCNN(config, test_dataset.label_values,
test_dataset.ignored_labels)
else:
raise ValueError('Unsupported dataset_task for testing: ' +
config.dataset_task)
# Define a visualizer class
tester = ModelTester(net, chkp_path=chosen_chkp)
print('Done in {:.1f}s\n'.format(time.time() - t1))
print('\nStart test')
print('**********\n')
# Training
if config.dataset_task == 'classification':
a = 1 / 0
elif config.dataset_task == 'cloud_segmentation':
with torch.no_grad():
tester.cloud_segmentation_test(net,
test_loader,
config,
num_votes=args.num_votes)
elif config.dataset_task == 'slam_segmentation':
tester.slam_segmentation_test(net, test_loader, config)
else:
raise ValueError('Unsupported dataset_task for testing: ' +
config.dataset_task)
print('All test finished in {:.1f}s\n for num_votes {}'.format(
time.time() - t1, args.num_votes))