-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_cls.py
229 lines (188 loc) · 8.92 KB
/
train_cls.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
from __future__ import print_function
import os
import argparse
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import CosineAnnealingLR, LambdaLR
import numpy as np
from torch.utils.data import DataLoader
import sklearn.metrics as metrics
from tqdm import tqdm
import yaml
import random
from Datasets.ScanObjectNN_DATASET import ScanObject
from Datasets.ModelNet40_DATASET import ModelNet40
from utils.util import cal_loss
from models.PRANet_classification import PRANet_classification
from Datasets.ModelNetDataLoader_withnorm import ModelNetDataLoader
from utils.util import IOStream
from functools import partial
import time
torch.backends.cudnn.enabled = False
#
# torch.backends.cudnn.enabled = True
# torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.deterministic = True
def _init_(args):
if args.seed is not None:
global seed
seed = args.seed
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if not os.path.exists('checkpoints'):
os.makedirs('checkpoints')
if not os.path.exists('checkpoints/' + args.exp_name):
os.makedirs('checkpoints/' + args.exp_name)
if not os.path.exists('checkpoints/' + args.exp_name + '/' + 'models'):
os.makedirs('checkpoints/' + args.exp_name + '/' + 'models')
os.system('cp *.py checkpoints' + '/' + args.exp_name + '/')
os.system('cp models/*.py checkpoints' + '/' + args.exp_name + '/')
os.system('cp utils/*.py checkpoints' + '/' + args.exp_name + '/')
os.system('cp ' + args.config + ' checkpoints' + '/' + args.exp_name + '/')
def test(epoch, model, test_dataset, test_loader, device, test_num=1):
oa_list = []
mAcc_list = []
model.eval()
for i in range(test_num):
test_pred = []
test_true = []
test_dataset.resample()
for data, label in test_loader:
data, label = data.to(device), label.to(device).squeeze()
data = data.permute(0, 2, 1)
with torch.no_grad():
logits = model(data)
preds = logits.max(dim=1)[1]
test_true.append(label.cpu().numpy())
test_pred.append(preds.detach().cpu().numpy())
test_true = np.concatenate(test_true)
test_pred = np.concatenate(test_pred)
test_acc = metrics.accuracy_score(test_true, test_pred)
avg_per_class_acc = metrics.balanced_accuracy_score(test_true, test_pred)
outstr = 'Test %d, test acc: %.6f, test avg acc: %.6f' % (epoch,
test_acc,
avg_per_class_acc)
io.cprint(outstr)
oa_list.append(test_acc)
mAcc_list.append(avg_per_class_acc)
return oa_list, mAcc_list
def train(args, io):
# Load models
device = torch.device("cuda")
if args.model == 'PRANet':
model_name = PRANet_classification
else:
raise Exception("Not implemented")
if args.dataset == 'ModelNet40Norm':
model_name = partial(model_name, input_channel=6)
model = model_name(args, output_channels=args.num_classes).to(device)
io.cprint(time.asctime())
if args.checkpoint is not None:
model.load_state_dict(torch.load(args.checkpoint))
# Load dataset
assert args.dataset in ['ScanObjectNN', 'ModelNet40', 'ModelNet40Norm']
if args.dataset == 'ScanObjectNN':
train_dataset = ScanObject(
h5file_path=args.data_root + '/main_split/training_objectdataset_augmentedrot_scale75.h5',
center=True, norm=True, with_bg=True, rotation=True, jit=True,
num_point=args.num_points)
test_dataset = ScanObject(
h5file_path=args.data_root + '/main_split/test_objectdataset_augmentedrot_scale75.h5',
center=True, norm=True, with_bg=True, rotation=False, jit=False,
num_point=args.num_points)
elif args.dataset == 'ModelNet40':
train_dataset = ModelNet40(data_root=args.data_root, partition='train', num_points=args.num_points,
aug=True)
test_dataset = ModelNet40(data_root=args.data_root, partition='test', num_points=args.num_points,
aug=False)
elif args.dataset == 'ModelNet40Norm':
train_dataset = ModelNetDataLoader(args.data_root, npoint=args.num_points, split='train', uniform=args.uniform,
normal_channel=True, cache_size=15000, augment=args.augment,
random_drop=args.random_drop)
test_dataset = ModelNetDataLoader(args.data_root, npoint=args.num_points, split='test', uniform=args.uniform,
normal_channel=True, cache_size=15000, augment=False,
random_drop=args.random_drop)
else:
raise NotImplementedError('No such dataset ' + args.dataset)
train_loader = DataLoader(train_dataset, num_workers=args.workers,
batch_size=args.batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(test_dataset, num_workers=args.workers,
batch_size=(args.batch_size // 2), shuffle=False, drop_last=False)
io.cprint(str(model))
io.cprint('*********************************************\nmodel_name: %s' % args.model)
print("Use SGD")
opt = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
scheduler = CosineAnnealingLR(opt, args.epochs, eta_min=0.001)
best_acc = 0.0
# epoch = 0
# test(epoch, model, test_dataset, test_loader, device, test_num=1)
for epoch in range(args.epochs):
####################
# Train
####################
train_loss = 0.0
count = 0.0
model.train()
train_pred = []
train_true = []
train_dataset.resample()
for i, (points, label) in tqdm(enumerate(train_loader, 0), total=len(train_loader), smoothing=0.9):
# (B, N, C)
points, label = points.to(device), label.to(device).squeeze()
batch_size = points.size()[0]
points = points.permute(0, 2, 1).contiguous()
opt.zero_grad()
# (B, C, N) -> (B, C2)
logits = model(points)
# loss = criterion(logits, label)
loss = cal_loss(logits, label, args.smoothing)
tot_loss = loss
tot_loss.backward()
opt.step()
preds = logits.max(dim=1)[1]
count += batch_size
train_loss += tot_loss.item() * batch_size
train_true.append(label.cpu().numpy())
train_pred.append(preds.detach().cpu().numpy())
scheduler.step()
train_true = np.concatenate(train_true)
train_pred = np.concatenate(train_pred)
outstr = 'Train %d, loss: %.6f, train acc: %.6f, train avg acc: %.6f' % (epoch,
train_loss * 1.0 / count,
metrics.accuracy_score(
train_true, train_pred),
metrics.balanced_accuracy_score(
train_true, train_pred))
io.cprint(outstr)
####################
# Test
####################
if epoch < 10 or epoch > 200:
oa_list, mAcc_list = test(epoch, model, test_dataset, test_loader, device, test_num=1)
io.cprint('Test %d, OA std: %.6f, mean: %.6f' % (epoch, np.std(oa_list), np.average(oa_list)))
io.cprint('Test %d, mAcc std: %.6f , mean: %.6f' % (epoch, np.std(mAcc_list), np.average(mAcc_list)))
torch.save(model.state_dict(),
'checkpoints/%s/models/model_%0.6f_epoch%3d.t7' % (args.exp_name, np.average(oa_list), epoch))
if __name__ == "__main__":
# Training settings
parser = argparse.ArgumentParser(description='PR-Net Classification Training')
parser.add_argument('--config', default='cfg/ScanObjectNN_train.yaml', type=str)
args = parser.parse_args()
with open(args.config) as f:
config = yaml.load(f)
print("\n**************************")
for k, v in config['common'].items():
setattr(args, k, v)
print('\n[%s]:' % (k), v)
print("\n**************************\n")
_init_(args)
print('seed', seed)
io = IOStream('checkpoints/' + args.exp_name + '/run.log')
io.cprint(str(args))
io.cprint(
'Using GPU : ' + str(torch.cuda.current_device()) + ' from ' + str(
torch.cuda.device_count()) + ' devices')
train(args, io)