-
Notifications
You must be signed in to change notification settings - Fork 23
/
train_batch.py
148 lines (119 loc) · 5.12 KB
/
train_batch.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
from datasets import PartDataset
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim
from kdnet import KDNet_Batch
def split_ps(point_set):
#print point_set.size()
num_points = point_set.size()[0]/2
diff = point_set.max(dim=0)[0] - point_set.min(dim=0)[0]
dim = torch.max(diff, dim = 1)[1][0,0]
cut = torch.median(point_set[:,dim])[0][0]
left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut))
right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut))
middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut))
if torch.numel(left_idx) < num_points:
left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0)
if torch.numel(right_idx) < num_points:
right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0)
left_ps = torch.index_select(point_set, dim = 0, index = left_idx)
right_ps = torch.index_select(point_set, dim = 0, index = right_idx)
return left_ps, right_ps, dim
def split_ps_reuse(point_set, level, pos, tree, cutdim):
sz = point_set.size()
num_points = np.array(sz)[0]/2
max_value = point_set.max(dim=0)[0]
min_value = -(-point_set).max(dim=0)[0]
diff = max_value - min_value
dim = torch.max(diff, dim = 1)[1][0,0]
cut = torch.median(point_set[:,dim])[0][0]
left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut))
right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut))
middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut))
if torch.numel(left_idx) < num_points:
left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0)
if torch.numel(right_idx) < num_points:
right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0)
left_ps = torch.index_select(point_set, dim = 0, index = left_idx)
right_ps = torch.index_select(point_set, dim = 0, index = right_idx)
tree[level+1][pos * 2] = left_ps
tree[level+1][pos * 2 + 1] = right_ps
cutdim[level][pos * 2] = dim
cutdim[level][pos * 2 + 1] = dim
return
test = False
import sys
if len(sys.argv) > 1 and sys.argv[1] == 'test':
test = True
d = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', classification = True, train = False)
else:
d = PartDataset(root = 'shapenetcore_partanno_segmentation_benchmark_v0', classification = True)
l = len(d)
print(len(d.classes), l)
levels = (np.log(2048)/np.log(2)).astype(int)
net = KDNet_Batch().cuda()
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
if test:
net.load_state_dict(torch.load(sys.argv[2]))
net.eval()
sum_correct = 0
sum_sample = 0
for it in range(10000):
optimizer.zero_grad()
losses = []
corrects = []
points_batch = []
cutdim_batch = []
targets = []
bt = 20
for batch in range(bt):
j = np.random.randint(l)
point_set, class_label = d[j]
targets.append(class_label)
if batch == 0 and it ==0:
tree = [[] for i in range(levels + 1)]
cutdim = [[] for i in range(levels)]
tree[0].append(point_set)
for level in range(levels):
for item in tree[level]:
left_ps, right_ps, dim = split_ps(item)
tree[level+1].append(left_ps)
tree[level+1].append(right_ps)
cutdim[level].append(dim)
cutdim[level].append(dim)
else:
tree[0] = [point_set]
for level in range(levels):
for pos, item in enumerate(tree[level]):
split_ps_reuse(item, level, pos, tree, cutdim)
#print level, pos
#cutdim_v = [(torch.from_numpy(np.array(item).astype(np.int64))) for item in cutdim]
cutdim_v = [(torch.from_numpy(np.array(item).astype(np.int64))) for item in cutdim]
points = torch.stack(tree[-1])
points_batch.append(torch.unsqueeze(torch.squeeze(points), 0).transpose(2,1))
cutdim_batch.append(cutdim_v)
points_v = Variable(torch.cat(points_batch, 0)).cuda()
target_v = Variable(torch.cat(targets, 0)).cuda()
cutdim_processed = []
for i in range(len(cutdim_batch[0])):
cutdim_processed.append(torch.stack([item[i] for item in cutdim_batch], 0))
pred = net(points_v, cutdim_processed)
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target_v.data).cpu().sum()
loss = F.nll_loss(pred, target_v)
if not test:
loss.backward()
losses.append(loss.data[0])
if not test:
optimizer.step()
else:
sum_correct += correct
sum_sample += bt
if sum_sample > 0:
print("accuracy: %d/%d = %f" % (sum_correct, sum_sample, sum_correct / float(sum_sample)))
print('batch: %d, loss: %f, correct %d/%d' %( it, np.mean(losses), correct, bt))
if it % 1000 == 0:
torch.save(net.state_dict(), 'save_model_%d.pth' % (it))