-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_shapenet.py
163 lines (126 loc) · 5.54 KB
/
test_shapenet.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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import argparse
import os
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, Manager_shapenet55
from models.crapcn import CRAPCN_sn55, CRAPCN_sn55_d
TRAIN_NAME = os.path.splitext(os.path.basename(__file__))[0]
# 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=False, help='Output testing results.')
parser.add_argument('--pretrained', type=str, default='', help='Pretrained path for testing.')
parser.add_argument('--mode', type=str, default='median', help='Testing mode [easy, median, hard].')
args = parser.parse_args()
# Configuration for ShapeNet55
def ShapeNet55Config():
__C = edict()
cfg = __C
#
# Dataset Config
#
__C.DATASETS = edict()
__C.DATASETS.SHAPENET55 = edict()
__C.DATASETS.SHAPENET55.CATEGORY_FILE_PATH = './data/ShapeNet55-34/ShapeNet-Unseen21/' # option = [ShapeNet-55, ShapeNet-34, ShapeNet-Unseen21]
__C.DATASETS.SHAPENET55.N_POINTS = 2048
__C.DATASETS.SHAPENET55.COMPLETE_POINTS_PATH = './data/ShapeNet55-34/shapenet_pc/%s'
#
# Dataset
#
__C.DATASET = edict()
# Dataset Options: Completion3D, ShapeNet, ShapeNetCars, Completion3DPCCT
__C.DATASET.TRAIN_DATASET = 'ShapeNet55'
__C.DATASET.TEST_DATASET = 'ShapeNet55'
#
# Constants
#
__C.CONST = edict()
__C.CONST.NUM_WORKERS = 8
__C.CONST.N_INPUT_POINTS = 2048
#
# Directories
#
__C.DIR = edict()
__C.DIR.OUT_PATH = 'results/'
__C.DIR.TEST_PATH = 'test/'
__C.CONST.DEVICE = '0, 1, 2, 3'
# __C.CONST.WEIGHTS = None # 'ckpt-best.pth' # specify a path to run test and inference
#
# Network
#
__C.NETWORK = edict()
__C.NETWORK.UPSAMPLE_FACTORS = [1, 4, 4]
#
# Train
#
__C.TRAIN = edict()
__C.TRAIN.BATCH_SIZE = 40
__C.TRAIN.N_EPOCHS = 400
__C.TRAIN.LEARNING_RATE = 0.001
__C.TRAIN.LR_DECAY = 100
__C.TRAIN.WARMUP_EPOCHS = 20
__C.TRAIN.GAMMA = .5
__C.TRAIN.BETAS = (.9, .999)
__C.TRAIN.WEIGHT_DECAY = 0
#
# 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.RESULTS = os.path.join(cfg.DIR.TEST_PATH, 'outputs')
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)
model = CRAPCN_sn55() # or 'CRAPCN_sn55_d'
if torch.cuda.is_available():
model = torch.nn.DataParallel(model).cuda()
checkpoint = torch.load(args.pretrained)
model.load_state_dict(checkpoint['model'])
##################
# Training Manager
##################
manager = Manager_shapenet55(model, cfg)
# Start training
manager.test(cfg, model, val_data_loader, outdir=cfg.DIR.RESULTS if args.output else None, mode=args.mode)
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__':
# Check python version
seed = 1128
set_seed(seed)
print('cuda available ', torch.cuda.is_available())
# Init config
cfg = ShapeNet55Config()
test_net(cfg)