-
Notifications
You must be signed in to change notification settings - Fork 1
/
HOTS.py
167 lines (128 loc) · 5.85 KB
/
HOTS.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 22 11:28:11 2020
@author: marcorax93
Script used for HOTS on N-MNIST,
this version uses batched-kmeans as a clustering algorihm, a subsampling layer
and two different classifiers (normalized histograms distance as in the original paper,
and a support vector machine trained on the histograms)
"""
from scipy import io
import numpy as np
import random, gc, pickle
from Libs.HOTSLib import n_mnist_rearranging, learn, infer, signature_gen,\
histogram_accuracy, dataset_resize,spac_downsample,recon_rates_svm
#%% Data loading and parameters setting
## Data loading
train_set_orig = n_mnist_rearranging(io.loadmat('N-MNIST/train_set.mat')['train_set'])
test_set_orig = n_mnist_rearranging(io.loadmat('N-MNIST/test_set.mat')['test_set'])
n_recording_labels_train=[len(train_set_orig[label]) for label in range(len(train_set_orig))]
n_recording_labels_test=[len(test_set_orig[label]) for label in range(len(train_set_orig))]
# using a subset of N-MNIST to lower memory usage
files_dataset_train = min(n_recording_labels_train)//1
files_dataset_test = min(n_recording_labels_test)//1
num_labels = len(test_set_orig)
# N-MNIST resolution
res_x = 28
res_y = 28
# Network parameters
layers = 2
surf_dim = [7,3]#lateral dimension of surfaces
n_clusters = [16,512]
n_jobs = 21
n_pol = [-1,16]#input polarities of each layer (if -1 polarity is discarded.)
n_batches=[10,10]#batches of data for minibatchkmeans
n_batches_test=[10,10]
u=7 #Spatial downsample factor
n_runs = 1 # run the code multiple times on reshuffled data to better assest performance
seeds = [1,2,3,4,5]
# HOTS tau for first and second layer.
tau = [5000,92000]
#%%% BENCH HOTS
H_kmeansss = [] #save the networks layer for every run
H_res = [] #save the networks layer for every run
for run in range(n_runs):
run_euc_res = []
run_norm_res = []
run_svc_res = []
run_svc_norm_res = []
run_kmeansss = []
#Random data shuffling
train_set_orig = n_mnist_rearranging(io.loadmat('N-MNIST/train_set.mat')['train_set'])
test_set_orig = n_mnist_rearranging(io.loadmat('N-MNIST/test_set.mat')['test_set'])
train_set_orig = dataset_resize(train_set_orig,res_x,res_y)
test_set_orig = dataset_resize(test_set_orig,res_x,res_y)
for label in range(num_labels):
random.Random(seeds[run]).shuffle(train_set_orig[label])
random.Random(seeds[run]).shuffle(test_set_orig[label])
train_set = [train_set_orig[label][:files_dataset_train] for label in range(num_labels)]
test_set = [test_set_orig[label][:files_dataset_test] for label in range(num_labels)]
layer_res_x = res_x
layer_res_y = res_y
for layer in range(layers):
print('##################____LAYER_'+str(layer)+'____###################')
print('TRAIN SET LEARNING')
[train_set, kmeans] = learn(train_set, surf_dim[layer], layer_res_x,
layer_res_y, tau[layer], n_clusters[layer],
n_pol[layer], n_batches[layer], n_jobs)
run_kmeansss.append(kmeans)
train_set=spac_downsample(train_set,u)
print('TEST SET INFERING')
test_set = infer(test_set, surf_dim[layer], layer_res_x, layer_res_y,
tau[layer], n_pol[layer], kmeans, n_batches_test[layer],
n_jobs)
test_set=spac_downsample(test_set,u)
layer_res_x=layer_res_x//u
layer_res_y=layer_res_y//u
# gc.collect()
print('SIGNATURE GENERATION')
[signatures, norm_signatures, svc, norm_svc] = signature_gen(train_set, n_clusters[layer], n_jobs)
print('TESTING')
[test_signatures, test_norm_signatures,
euc_accuracy, norm_euc_accuracy,
euc_label, norm_euc_label] = histogram_accuracy(test_set, n_clusters[layer], signatures,
norm_signatures, n_jobs)
run_euc_res.append(euc_accuracy)
run_norm_res.append(norm_euc_accuracy)
rec_rate_svc,rec_rate_norm_svc = recon_rates_svm(svc,norm_svc,test_signatures,test_norm_signatures, test_set)
run_svc_res.append(rec_rate_svc)
run_svc_norm_res.append(rec_rate_norm_svc)
print(run)
print('Euclidean accuracy: '+str(euc_accuracy)+'%')
print('Normalized euclidean accuracy: '+str(norm_euc_accuracy)+'%')
print('Svc accuracy: '+str(rec_rate_svc)+'%')
print('Normalized Svc accuracy: '+str(rec_rate_norm_svc)+'%')
gc.collect()
H_kmeansss.append(run_kmeansss)
H_res.append([run_euc_res, run_norm_res, run_svc_res, run_svc_norm_res])
#%% Save run (Uncomment all code to save)
filename='Results/test_result_new.pkl'
with open(filename, 'wb') as f:
pickle.dump([H_kmeansss, H_res], f)
#%% Load previous results
# filename='Results/test_result_new.pkl'
# with open(filename, 'rb') as f: # Python 3: open(..., 'rb')
# H_kmeansss, H_res = pickle.load(f)
#%% Results:
#Layer 1 mean:
H1=np.mean(np.array(H_res)[:,0])
#Layer 2 mean:
H2=np.mean(np.array(H_res)[:,1])
#Layer 1 Standard Deviation:
H1_sd=np.std(np.array(H_res)[:,0])
#Layer 2 Standard Deviations:
H2_sd=np.std(np.array(H_res)[:,1])
print("Layer1 HOTS: "+str(H1)+"+-"+str(H1_sd)) #Mean result +- std
print("Layer2 HOTS: "+str(H2)+"+-"+str(H2_sd)) #Mean result +- std
#%% Kmeans features
# tau_i = 1
# run = 0
# layer = 0
# features = np.reshape(H_kmeansss[tau_i][run].cluster_centers_,\
# [n_clusters[layer],surf_dim[layer],surf_dim[layer]])
# fig, axs = plt.subplots(4, 8)
# for image_number, ax in enumerate(axs.ravel()):
# ax.imshow(features[image_number])
# ax.set_title('feat '+str(image_number))
# fig.suptitle("HOTS features", fontsize=16)