-
Notifications
You must be signed in to change notification settings - Fork 2
/
pointhop_spark.py
186 lines (153 loc) · 6.76 KB
/
pointhop_spark.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
import os
import time
import numpy as np
import modelnet_data
from pyspark import SparkConf
from numpy import linalg as LA
import point_utils_spark as pus
from pyspark import SparkContext
from sklearn.metrics import accuracy_score
config = SparkConf().setAll(
[('spark.driver.memory', '12g'),
('spark.executor.memory', '6g'),
('spark.driver.maxResultSize', '12g')]).setAppName('POINTHOP').setMaster('local[*]')
sc = SparkContext(conf=config)
sc.setLogLevel("ERROR")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def pointhop_train(data, n_newpoint, n_sample, n_kernel, num_partition):
'''
Train based on the provided samples.
:param data: [num_samples, num_point, feature_dimension]
:param n_newpoint: point numbers used in every stage
:param n_sample: k nearest neighbors
:param n_kernel: num kernels to be preserved
:param num_partition: partition num for rdd
:return: pca_params, feature
'''
point_data = data
pca_params = {}
fea = []
pointRDD = sc.parallelize(point_data, num_partition)
for i in range(len(n_sample)):
if (i == 0 and point_data.shape[1] == n_newpoint[i]) or (i > 0 and n_newpoint[i-1] == n_newpoint[i]):
fpsRDD = pointRDD
else:
fpsRDD = pointRDD.map(lambda x: pus.fps(x, n_newpoint[i]))
fpsRDD.persist()
knnRDD = fpsRDD.zip(pointRDD).map(lambda x: pus.knn(x[0], x[1], n_sample[i]))
if i == 0:
sgRDD = pointRDD.zip(knnRDD).flatMap(lambda x: pus.sg(x[0], x[0], x[1]))
else:
sgRDD = pointRDD.zip(knnRDD).zip(pcaRDD).flatMap(lambda x: pus.sg(x[0][0], x[1], x[0][1]))
pcaRDD.unpersist()
sgRDD.persist()
kernels, energy = pus.pca(sgRDD)
kernels = kernels[:n_kernel[i]]
pca_params['Layer_{:d}/kernel'.format(i)] = kernels
if i == 0:
pcaRDD = sgRDD.map(lambda x: np.dot(x, kernels.T))
else:
bias = sgRDD.map(lambda x: LA.norm(x)).max()
pca_params['Layer_{:d}/bias'.format(i)] = bias
e = np.zeros((1, kernels.shape[0]))
e[0, 0] = 1
pcaRDD = sgRDD.map(lambda x: x + bias).map(lambda x: np.dot(x, kernels.T)).map(lambda x: x - bias * e)
pca_fea = np.array(pcaRDD.collect())
pca_fea = pca_fea.reshape((-1, n_newpoint[i], pca_fea.shape[-1]))
print('Hop ', i, ': ', pca_fea.shape)
pcaRDD = sc.parallelize(pca_fea, num_partition)
fea.append(pus.extract_single(pca_fea))
pointRDD = fpsRDD
sgRDD.unpersist()
fpsRDD.unpersist()
pcaRDD.unpersist()
pointRDD.unpersist()
fea = np.concatenate(fea, axis=-1)
return pca_params, fea
def pointhop_pred(data, n_newpoint, n_sample, pca_params, num_partition):
'''
Test based on the provided samples.
:param data: [num_samples, num_point, feature_dimension]
:param n_newpoint: point numbers used in every stage
:param n_sample: k nearest neighbors
:param pca_params: model to be used
:param num_partition: partition num for rdd
:return: feature
'''
point_data = data
fea = []
pointRDD = sc.parallelize(point_data, num_partition)
for i in range(len(n_sample)):
if (i == 0 and point_data.shape[1] == n_newpoint[i]) or (i > 0 and n_newpoint[i-1] == n_newpoint[i]):
fpsRDD = pointRDD
else:
fpsRDD = pointRDD.map(lambda x: pus.fps(x, n_newpoint[i]))
fpsRDD.persist()
knnRDD = fpsRDD.zip(pointRDD).map(lambda x: pus.knn(x[0], x[1], n_sample[i]))
if i == 0:
sgRDD = pointRDD.zip(knnRDD).flatMap(lambda x: pus.sg(x[0], x[0], x[1]))
else:
sgRDD = pointRDD.zip(knnRDD).zip(pcaRDD).flatMap(lambda x: pus.sg(x[0][0], x[1], x[0][1]))
pcaRDD.unpersist()
sgRDD.persist()
kernels = pca_params['Layer_{:d}/kernel'.format(i)]
if i == 0:
pcaRDD = sgRDD.map(lambda x: np.dot(x, kernels.T))
else:
bias = pca_params['Layer_{:d}/bias'.format(i)]
e = np.zeros((1, kernels.shape[0]))
e[0, 0] = 1
pcaRDD = sgRDD.map(lambda x: x + bias).map(lambda x: np.dot(x, kernels.T)).map(lambda x: x - bias * e)
pca_fea = np.array(pcaRDD.collect())
pca_fea = pca_fea.reshape((-1, n_newpoint[i], pca_fea.shape[-1]))
print('Hop ', i, ': ', pca_fea.shape)
pcaRDD = sc.parallelize(pca_fea, num_partition)
fea.append(pus.extract_single(pca_fea))
pointRDD = fpsRDD
sgRDD.unpersist()
fpsRDD.unpersist()
pcaRDD.unpersist()
pointRDD.unpersist()
fea = np.concatenate(fea, axis=-1)
return fea
if __name__ == '__main__':
time_start = time.time()
initial_point = 1024
n_newpoint = [1024, 128, 128, 64]
n_sample = [64, 64, 64, 64]
n_kernel = [15, 25, 40, 80]
train_data, train_label = modelnet_data.data_load(initial_point, os.path.join(BASE_DIR, 'modelnet40_ply_hdf5_2048'), True)
test_data, test_label = modelnet_data.data_load(initial_point, os.path.join(BASE_DIR, 'modelnet40_ply_hdf5_2048'), False)
train_data = train_data
train_label = train_label
test_data = test_data
test_label = test_label
print('Train data loaded!')
pca_params, feature_train = pointhop_train(train_data, n_newpoint, n_sample, n_kernel, num_partition=1000)
print(feature_train.shape)
feature_test = pointhop_pred(test_data, n_newpoint, n_sample, pca_params, num_partition=400)
print(feature_test.shape)
clf = pus.rf_classifier(feature_train, np.squeeze(train_label))
pred_train = clf.predict(feature_train)
acc_train = accuracy_score(train_label, pred_train)
print('RF Classification train accuracy: ', acc_train)
pred_test = clf.predict(feature_test)
acc_test = accuracy_score(test_label, pred_test)
print('RF Classification test accuracy: ', acc_test)
weight = pus.llsr_train(feature_train, train_label, 40)
prob_train, pred_train = pus.llsr_pred(feature_train, weight)
acc_train = accuracy_score(train_label, pred_train)
print('LLSR Classification train accuracy: ', acc_train)
prob_test, pred_test = pus.llsr_pred(feature_test, weight)
acc_test = accuracy_score(test_label, pred_test)
print('LLSR Classification test accuracy: ', acc_test)
weight = pus.llsr_train_weighted(feature_train, train_label, 40, epsilon=0.2)
prob_train, pred_train = pus.llsr_pred(feature_train, weight)
acc_train = accuracy_score(train_label, pred_train)
print('WLLSR Classification train accuracy: ', acc_train)
prob_test, pred_test = pus.llsr_pred(feature_test, weight)
acc_test = accuracy_score(test_label, pred_test)
print('WLLSR Classification test accuracy: ', acc_test)
sc.stop()
time_end = time.time()
print('Duration:', (time_end - time_start) / 60.0, 'mins')