forked from xiongfengyan/gcnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCNN.py
314 lines (262 loc) · 12.4 KB
/
GCNN.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
from lib import models, dataLoading
import tensorflow as tf
import matplotlib.pyplot as plt
# # 1 Load Data.
# train_test_dataset(None)
# training, validation, test data.
# [vertices_geometry, vertices_fourier, adjacencies, labels]
# train_dataset,val_dataset,test_dataset=load_data('./data/test_data_batch_mini.json')
# train_dataset, val_dataset, test_dataset = input_data.load_data('./data/testExtra0725r.json', dataSeparation=[0.6, 0.2, 0.2]) #
train_dataset, val_dataset, test_dataset = dataLoading.load_data('./lib/data/test20ri.json', dataSeparation=[0.8, 0.18, 0.02]) # test228r
# Number of samples.
n_train = train_dataset[4]
# Number of classes.
n_class = len(set(train_dataset[2]))
# GeometryDescriptors or Fourier
i_type = 0
# print the Graph structure.
if True:
print('Graph structure: {0}x{0} Graph with {1} dimensionaal vecter.'.format(train_dataset[1].shape[1], train_dataset[0].shape[2]))
print('labels set: {}'.format(set(train_dataset[2])))
print(' train dataset:')
print(' vertices : {0}'.format(train_dataset[0].shape))
print(' adjacencies: {0}'.format(train_dataset[1].shape))
print(' labels : {0}'.format(train_dataset[2].shape))
print(' validation dataset:')
print(' vertices : {0}'.format(val_dataset[0].shape))
print(' adjacencies: {0}'.format(val_dataset[1].shape))
print(' labels : {0}'.format(val_dataset[2].shape))
print(' test dataset:')
print(' vertices : {0}'.format(test_dataset[0].shape))
print(' adjacencies: {0}'.format(test_dataset[1].shape))
print(' labels : {0}'.format(test_dataset[2].shape))
# # 2 Graph Convolution.
params = dict()
# The dimensions of vectives.
params['Vs'] = train_dataset[0].shape[2]
# The size of adjacencies (Number of vectices).
params['As'] = train_dataset[1].shape[1]
# The parameters of convolation and pooling layer. Two layers
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
# The parameters of full connection layer.
params['Cs'] = [16, n_class]
params['filter'] = 'monomial'
params['brelu'] = 'b1relu'
params['pool'] = 'm1pool'
params['num_epochs'] = 150
params['batch_size'] = 100
params['decay_steps'] = n_train / params['batch_size']
params['eval_frequency'] = 50
# Hyper-parameters.
params['regularization'] = 5e-4
params['dropout'] = 0.5
params['learning_rate'] = 0.001
params['decay_rate'] = 0.95
params['momentum'] = 0
params['dir_name'] = 'buildings'
# momentum,learning_rate = [0.9, 0.01]
# momentum,learning_rate = [0, 0.002, 0.003, 0.001]
# # 3 TRAINING AND EVALUTING NETWORK
# We often want to monitor:
# 1) The convergence, i.e. the training loss and the classification accuracy on the validation set.
# 2) The performance, i.e. the classification accuracy on the testing set (to be compared with the training set accuracy to spot overfitting).
# DIFINE THE MODEL COLLECTION
# multi train networks.
# The `model_perf` class in [models.py](models.py) can be used to compactly evaluate multiple models.
model_perf = models.model_perf()
if True:
# Test depth of network structures.
for i in [1,2,3,4,5,6,7,8]:
params['Fs'] = [16]*i
params['Ks'] = [3] *i
params['Ps'] = [1] *i
name = 'depth={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, train_dataset, val_dataset, test_dataset)
model_perf.show()
# Test input variable important
if False:
# DEFINE THE CNN ARCHITECTURE
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'newfs_24'
import copy
# GET THE DIMENSION OF INPUT VARIABLES
dim_variables = train_dataset[0].shape[2]
for i in range(0, dim_variables):
# Only variable i
params['Vs'] = 1
filtered_list = [i]
train_copy, val_copy, test_copy = copy.deepcopy(train_dataset), copy.deepcopy(val_dataset), copy.deepcopy(test_dataset)
train_copy[0] = train_dataset[0][:,:,filtered_list]
val_copy[0] = val_dataset[0][:,:,filtered_list]
test_copy[0] = test_dataset[0][:,:,filtered_list]
params['dir_name'] = name + '_one_' + str(i).zfill(2)
model_perf.test(models.gcnn(**params), name, params, i_type, train_copy, val_copy, test_copy)
# Except variable i
params['Vs'] = dim_variables - 1
filtered_list = [j for j in range(dim_variables)]
del filtered_list[i]
train_copy[0] = train_dataset[0][:,:,filtered_list]
val_copy[0] = val_dataset[0][:,:,filtered_list]
test_copy[0] = test_dataset[0][:,:,filtered_list]
params['dir_name'] = name + '_exc_' + str(i).zfill(2)
model_perf.test(models.gcnn(**params), name, params, i_type, train_copy, val_copy, test_copy)
del train_copy, val_copy, test_copy
model_perf.show()
# Test input variable important
if False:
# DEFINE THE CNN ARCHITECTURE
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'newfs_21'
params['Vs'] = 21
filtered_list = [i for i in range(0,21)]
import copy
train_copy, val_copy, test_copy = copy.deepcopy(train_dataset), copy.deepcopy(val_dataset), copy.deepcopy(test_dataset)
train_copy[0] = train_dataset[0][:,:,filtered_list]
val_copy[0] = val_dataset[0][:,:,filtered_list]
test_copy[0] = test_dataset[0][:,:,filtered_list]
for i in [1,2,3,4,5,6]:
params['Ks'] = [i, i, i, i, i]
name = 'newfs_24_MST3_k={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_copy, val_copy, test_copy)
del train_copy, val_copy, test_copy
model_perf.show()
if False:
# DEFINE THE CNN ARCHITECTURE
name = 'oldfs_25_3'
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
model_perf.show()
# Test graph structure and neighbors.
if False:
# For DT or MST model
for i in [1,2,3,4,5,6]:
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [i, i, i, i, i]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'newfs_24_MST2_k={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
model_perf.show()
if False:
if True:
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'newfeatures_24'
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'Convolutions_5_16_normalizing'
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
# test the order K.
for i in [1,2,3,4,5,6]:
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [i, i, i, i, i]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'MST_k_2={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
# test the order K.
for i in [1,2,3,4,5,6]:
params['Fs'] = [16, 16, 16, 16, 16]
params['Ks'] = [i, i, i, i, i]
params['Ps'] = [1, 1, 1, 1, 1]
name = 'DT_k={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
# test input features.
for i in range(0,12):
if i==1:continue
i_type = i
params['Vs'] = train_dataset[i_type].shape[2]
params['As'] = train_dataset[2].shape[1]
params['Fs'] = [24, 24, 24, 24]
params['Ks'] = [3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1]
name = 'Convolutions_features2={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
# test the order K.
for i in [1,2,3,4,5,6]:
params['Fs'] = [24, 24, 24, 24]
params['Ks'] = [i, i, i, i]
params['Ps'] = [1, 1, 1, 1]
name = 'Convolutions_k={}'.format(i)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
# The following models will be tested at night.
if False:
params['Fs'] = [16, 16]
params['Ks'] = [3, 3]
params['Ps'] = [1, 1]
for rate in [1]:
name = 'test_{}'.format(rate)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [12, 12, 12]
params['Ks'] = [3, 3, 3]
params['Ps'] = [1, 1, 1]
for rate in [1, 2]:
name = 'Convolutions_3_12_{}_{}'.format(rate, 0.001)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [16, 16, 16]
params['Ks'] = [3, 3, 3]
params['Ps'] = [1, 1, 1]
for rate in [3, 4]:
name = 'Convolutions_3_16_{}_{}'.format(rate, 0.001)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [24, 24, 24, 24, 24, 24, 24, 24, 24, 24]
params['Ks'] = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
for rate in [3]:
name = 'Convolutions_10_24_test{}'.format(rate)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [36, 36, 36, 36, 36, 36, 36, 36, 36, 36]
params['Ks'] = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
for i_type in [0, 1]:
params['Vs'] = train_dataset[i_type].shape[2]
name = 'Fourier_Geometry{}'.format(i_type)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [16, 16, 16, 16]
params['Ks'] = [3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1]
for rate in [1, 2]:
name = 'Convolutions_4_16_{}_{}'.format(rate, 0.001)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
if False:
params['Fs'] = [36, 36, 36, 36, 36]
params['Ks'] = [3, 3, 3, 3, 3]
params['Ps'] = [1, 1, 1, 1, 1]
for rate in [6]:
name = 'Convolutions_5_36_test{}'.format(rate)
params['dir_name'] = name
model_perf.test(models.gcnn(**params), name, params, i_type, train_dataset, val_dataset, test_dataset)
model_perf.show()