forked from HuguesTHOMAS/KPConv-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_NeuesPalaisTrees.py
388 lines (305 loc) · 12.6 KB
/
train_NeuesPalaisTrees.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#
#
# 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
# \**********************************/
#
# Common libs
import sys
import signal
import os
import numpy as np
import sys
import torch
from traits.trait_types import self
# from numba.cuda.libdevicedecl import args
# Dataset
from datasets.ModelNet40 import *
from datasets.NeuesPalaisTrees import *
from torch.utils.data import DataLoader
from utils.config import Config
from utils.trainer import ModelTrainer
from models.architectures import KPCNN
# ----------------------------------------------------------------------------------------------------------------------
#
# Config Class
# \******************/
#
class NeuesPalaisTreesConfig(Config):
"""
Override the parameters you want to modify for this dataset
"""
####################
# Dataset parameters
####################
# Dataset name
dataset = 'NeuesPalaisTrees'
# Number of classes in the dataset (This value is overwritten by dataset class when Initializating dataset).
num_classes = 2
# Type of task performed on this dataset (also overwritten)
dataset_task = 'classification'
architecture = ['simple',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb_strided',
'resnetb_deformable',
'resnetb_deformable_strided',
'resnetb_deformable',
'global_average']
# Number of CPU threads for the input pipeline
input_threads = 10
###################c
# KPConv parameters
###################
# Number of kernel points
num_kernel_points = 15
# Size of the first subsampling grid in meter
first_subsampling_dl = 0.4
do_subsample = True
# Radius of convolution in "number grid cell". (2.5 is the standard value)
conv_radius = 2.5
# Radius of deformable convolution in "number grid cell". Larger so that deformed kernel can spread out
deform_radius = 6.0
# Radius of the area of influence of each kernel point in "number grid cell". (1.0 is the standard value)
KP_extent = 1.2
# Behavior of convolutions in ('constant', 'linear', 'gaussian')
KP_influence = 'linear'
# Aggregation function of KPConv in ('closest', 'sum')
aggregation_mode = 'sum'
# Choice of input features
in_features_dim = 1
# Can the network learn modulations
modulated = True
# Batch normalization parameters
use_batch_norm = True
batch_norm_momentum = 0.05
# Deformable offset loss
# 'point2point' fitting geometry by penalizing distance from deform point to input points
# 'point2plane' fitting geometry by penalizing distance from deform point to input point triplet (not implemented)
deform_fitting_mode = 'point2point'
deform_fitting_power = 1.0 # Multiplier for the fitting/repulsive loss
deform_lr_factor = 0.1 # Multiplier for learning rate applied to the deformations
repulse_extent = 1.2 # Distance of repulsion for deformed kernel points
#####################
# Training parameters
#####################
# Maximal number of epochs
max_epoch = 50
# Learning rate management
learning_rate = 1e-2
momentum = 0.98
lr_decays = {i: 0.1**(1/100) for i in range(1, max_epoch)}
grad_clip_norm = 100.0
# Number of batch
batch_num = 10
# Number of steps per epochs
epoch_steps = 100
# Number of validation examples per epoch
validation_size = 32
# Number of epoch between each checkpoint
checkpoint_gap = 50
# Augmentations
augment_scale_anisotropic = False
augment_symmetries = [False, False, False]
augment_rotation = 'none'
augment_scale_min = 1.0
augment_scale_max = 1.0
augment_noise = 0
augment_color = 1.0
# The way we balance segmentation loss
# > 'none': Each point in the whole batch has the same contribution.
# > 'class': Each class has the same contribution (points are weighted according to class balance)
# > 'batch': Each cloud in the batch has the same contribution (points are weighted according cloud sizes)
segloss_balance = 'none'
# # Do we nee to save convergence
saving = True
saving_path = 'Dec16_v33_0.4subsample_kpsubsample_0.4'
# ----------------------------------------------------------------------------------------------------------------------
#
# Main Call
# \***************/
#
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--do_subsample', help='Enable subsampling or not')
parser.add_argument('--first_subsampling_dl', type=float, default=None, help='The KPConv subsampling value')
parser.add_argument('--max_epoch', type=int, default=None, help='Override max epoch')
parser.add_argument('--data_path', type=str, default=None, help='Data path')
parser.add_argument('--saving_path', type=str, default=None, help='Saving path')
parser.add_argument('--num_train_models', type=int, help='# of trees')
parser.add_argument('--num_test_models', type=int, help='# of trees')
parser.add_argument('--architecture', type=str, default='rigid', help='Architecture')
parser.add_argument('--num_kernel_points', type=int, default=None, help='# of kernel points')
parser.add_argument('--class_w', type=float, nargs='+', default=None, help='Class weights')
args = parser.parse_args()
############################
# Initialize the environment
############################
# Set which gpu is going to be used
GPU_ID = '0'
# Set GPU visible device
os.environ['CUDA_VISIBLE_DEVICES'] = GPU_ID
###############
# Previous chkp
###############
# Choose here if you want to start training from a previous snapshot (None for new training)
#previous_training_path = 'Log_2020-03-19_19-53-27'
previous_training_path = ''
# Choose index of checkpoint to start from. If None, uses the latest chkp
chkp_idx = None
if previous_training_path:
# Find all snapshot in the chosen training folder
chkp_path = os.path.join('results', previous_training_path, 'checkpoints')
chkps = [f for f in os.listdir(chkp_path) if f[:4] == 'chkp']
# Find which snapshot to restore
if chkp_idx is None:
chosen_chkp = 'current_chkp.tar'
else:
chosen_chkp = np.sort(chkps)[chkp_idx]
chosen_chkp = os.path.join('results', previous_training_path, 'checkpoints', chosen_chkp)
else:
chosen_chkp = None
##############
# Prepare Data
##############
print()
print('Data Preparation')
print('****************')
# Initialize configuration class
config = NeuesPalaisTreesConfig()
# if args.num_train_models and self.mode == 'train':
# num_models = args.num_train_models
# if args.num_test_models and self.mode == 'test':
# num_models = args.num_test_models
# Parse args
if args.max_epoch is not None:
config.max_epoch = args.max_epoch
print(f'Using max_epoch from parser: {config.max_epoch}')
if args.num_kernel_points is not None:
print(f'Updating # of kernel points to {args.num_kernel_points}')
config.num_kernel_points = args.num_kernel_points
# Pass in architecture choice from parser. 'rigid' or 'deformable'
if args.architecture is not None:
if args.architecture == 'rigid':
print(f'Using rigid architecture')
config.architecture = ['simple',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb',
'global_average']
elif args.architecture == 'deformable':
print(f'Using deformable architecture')
config.architecture = ['simple',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb_strided',
'resnetb',
'resnetb_strided',
'resnetb_deformable',
'resnetb_deformable_strided',
'resnetb_deformable',
'global_average']
else:
sys.exit('Architecture must be either rigid or deformable')
# Path to input data
if args.data_path is not None:
config.path = args.data_path
else:
print(f"Error: Data path not provided...")
sys.exit(1)
# Path to saving
if args.saving_path is not None:
config.saving_path = args.saving_path
print(f'Saving to path: {config.saving_path}')
if args.first_subsampling_dl is not None:
config.first_subsampling_dl = args.first_subsampling_dl
if args.class_w is not None:
config.class_w = args.class_w
if previous_training_path:
config.load(os.path.join('results', previous_training_path))
config.saving_path = None
# Get path from argument if given
# if len(sys.argv) > 1:
# config.saving_path = sys.argv[1]
print(f'Num train models: {args.num_train_models}')
print(f'Num test models: {args.num_test_models}')
# Initialize datasets
training_dataset = NeuesPalaisTreesDataset(config, mode='train', num_train_models = int(args.num_train_models))
val_dataset = NeuesPalaisTreesDataset(config, mode='test', num_test_models = int(args.num_test_models))
test_dataset = NeuesPalaisTreesDataset(config, mode='test', num_test_models = int(args.num_test_models))
# Initialize samplers
training_sampler = NeuesPalaisTreesSampler(training_dataset, balance_labels=True)
val_sampler = NeuesPalaisTreesSampler(val_dataset, balance_labels=True)
test_sampler = NeuesPalaisTreesSampler(test_dataset, balance_labels=True)
# Initialize the dataloader
training_loader = DataLoader(training_dataset,
batch_size=1,
sampler=training_sampler,
collate_fn=NeuesPalaisTreesCollate,
num_workers=config.input_threads,
pin_memory=True)
val_loader = DataLoader(val_dataset,
batch_size=1,
sampler=val_sampler,
collate_fn=NeuesPalaisTreesCollate,
num_workers=config.input_threads,
pin_memory=True)
test_loader = DataLoader(test_dataset,
batch_size=1,
sampler=test_sampler,
collate_fn=NeuesPalaisTreesCollate,
num_workers=config.input_threads,
pin_memory=True)
# Calibrate samplers
training_sampler.calibration(training_loader)
val_sampler.calibration(val_loader)
test_sampler.calibration(test_loader)
#debug_timing(test_dataset, test_sampler, test_loader)
#debug_show_clouds(training_dataset, training_sampler, training_loader)
print('\nModel Preparation')
print('*****************')
# Define network model
t1 = time.time()
net = KPCNN(config)
# Define a trainer
trainer = ModelTrainer(net, config, chkp_path=chosen_chkp)
print('Done in {:.1f}s\n'.format(time.time() - t1))
print('\nStart training')
print('**************')
# Training
try:
trainer.train(net, training_loader, val_loader, config)
except:
print('Caught an error')
os.kill(os.getpid(), signal.SIGINT)
print('Forcing exit now')
os.kill(os.getpid(), signal.SIGINT)