-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_pcn.py
170 lines (133 loc) · 6.16 KB
/
test_pcn.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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import argparse
import numpy as np
import torch
import json
import time
import utils.data_loaders
from easydict import EasyDict as edict
from importlib import import_module
from pprint import pprint
from manager import Manager
import math
TRAIN_NAME = os.path.splitext(os.path.basename(__file__))[0]
from models.crapcn import CRAPCN, CRAPCN_d
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('--desc', type=str, default='Training/Testing CRA-PCN', help='description')
parser.add_argument('--net_model', type=str, default='model', help='Import module.')
parser.add_argument('--test', dest='test', help='Test neural networks', action='store_true')
parser.add_argument('--inference', dest='inference', help='Inference for benchmark', action='store_true')
parser.add_argument('--output', type=int, default=True, help='Output testing results.')
parser.add_argument('--pretrained', type=str, default='', help='Pretrained path for testing.')
args = parser.parse_args()
# Configuration for PCN
def PCNConfig():
__C = edict()
cfg = __C
#
# Dataset Config
#
__C.DATASETS = edict()
__C.DATASETS.COMPLETION3D = edict()
__C.DATASETS.COMPLETION3D.CATEGORY_FILE_PATH = './datasets/Completion3D.json'
__C.DATASETS.COMPLETION3D.PARTIAL_POINTS_PATH = '/path/to/datasets/Completion3D/%s/partial/%s/%s.h5'
__C.DATASETS.COMPLETION3D.COMPLETE_POINTS_PATH = '/path/to/datasets/Completion3D/%s/gt/%s/%s.h5'
__C.DATASETS.SHAPENET = edict()
__C.DATASETS.SHAPENET.CATEGORY_FILE_PATH = './datasets/ShapeNet.json'
__C.DATASETS.SHAPENET.N_RENDERINGS = 8
__C.DATASETS.SHAPENET.N_POINTS = 2048
__C.DATASETS.SHAPENET.PARTIAL_POINTS_PATH = '../data/PCN/%s/partial/%s/%s/%02d.pcd'
__C.DATASETS.SHAPENET.COMPLETE_POINTS_PATH = '../data/PCN/%s/complete/%s/%s.pcd'
#
# Dataset
#
__C.DATASET = edict()
# Dataset Options: Completion3D, ShapeNet, ShapeNetCars, Completion3DPCCT
__C.DATASET.TRAIN_DATASET = 'ShapeNet'
__C.DATASET.TEST_DATASET = 'ShapeNet'
#
# Constants
#
__C.CONST = edict()
__C.CONST.NUM_WORKERS = 8
__C.CONST.N_INPUT_POINTS = 2048
#
# Directories
#
__C.DIR = edict()
__C.DIR.OUT_PATH = None
__C.DIR.TEST_PATH = 'test/cra-pcn'
__C.CONST.DEVICE = '0'
__C.CONST.WEIGHTS = './pretrain/pcn/ckpt-best.pth' # 'ckpt-best.pth' # specify a path to run test and inference
#
# Network
#
__C.NETWORK = edict()
__C.NETWORK.UPSAMPLE_FACTORS = [1, 2, 4, 8] # 16384
__C.NETWORK.KP_EXTENTS = [0.1, 0.1, 0.05, 0.025] # 16384
#
# Train
#
__C.TRAIN = edict()
__C.TRAIN.BATCH_SIZE = 100
__C.TRAIN.N_EPOCHS = 400
__C.TRAIN.SAVE_FREQ = 25
__C.TRAIN.LEARNING_RATE = 0.001
__C.TRAIN.LR_MILESTONES = [50, 100, 150, 200, 250]
__C.TRAIN.LR_DECAY_STEP = 50
__C.TRAIN.WARMUP_STEPS = 200
__C.TRAIN.WARMUP_EPOCHS = 20
__C.TRAIN.GAMMA = .5
__C.TRAIN.BETAS = (.9, .999)
__C.TRAIN.WEIGHT_DECAY = 0
__C.TRAIN.LR_DECAY = 150
#
# Test
#
__C.TEST = edict()
__C.TEST.METRIC_NAME = 'ChamferDistance'
return cfg
def test_net(cfg):
# Enable the inbuilt cudnn auto-tuner to find the best algorithm to use
torch.backends.cudnn.benchmark = True
########################
# Load Train/Val Dataset
########################
test_dataset_loader = utils.data_loaders.DATASET_LOADER_MAPPING[cfg.DATASET.TEST_DATASET](cfg)
val_data_loader = torch.utils.data.DataLoader(dataset=test_dataset_loader.get_dataset(
utils.data_loaders.DatasetSubset.TEST),
batch_size=1,
num_workers=cfg.CONST.NUM_WORKERS,
collate_fn=utils.data_loaders.collate_fn,
pin_memory=True,
shuffle=False)
# Set up folders for logs and checkpoints
#cfg.DIR.TEST_PATH = os.path.join(cfg.DIR.TEST_PATH, cfg.DIR.PRETRAIN)
cfg.DIR.RESULTS = os.path.join(cfg.DIR.TEST_PATH, 'results')
cfg.DIR.LOGS = cfg.DIR.TEST_PATH
print('Saving outdir: {}'.format(cfg.DIR.TEST_PATH))
if not os.path.exists(cfg.DIR.RESULTS):
os.makedirs(cfg.DIR.RESULTS)
#######################
# Prepare Network Model
#######################
model = CRAPCN() # CRAPCN_d
if torch.cuda.is_available():
model = torch.nn.DataParallel(model).cuda()
# load pretrained model
print('Recovering from %s ...' % (cfg.CONST.WEIGHTS))
checkpoint = torch.load(cfg.CONST.WEIGHTS)
model.load_state_dict(checkpoint['model'])
manager = Manager(model, cfg)
manager.test(cfg, model, val_data_loader, outdir=cfg.DIR.RESULTS if args.output else None)
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if __name__ == '__main__':
print('cuda available ', torch.cuda.is_available())
cfg = PCNConfig()
test_net(cfg)