-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.2overlap.py
executable file
·176 lines (156 loc) · 4.25 KB
/
p1.2overlap.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
import math
import numpy as np
trainingData = open('trainingimages', 'r');
label_file = open('traininglabels', 'r');
label_array = [] #array consist of training label
count = 0
index = 0
data_dict = {}
temp = []
for line in label_file:
label_array.append(int(line[0:-1]));
total_training_data = len(label_array)
for line in trainingData:
if(count < 28):
temp.append(line[0:-1])
count += 1;
else:
number = label_array[index]
if number in data_dict:
data_dict[number].append(temp)
else:
data_dict[number] = [temp];
count = 0
temp = []
index += 1
temp.append(line[0:-1])
count += 1;
## calculate possibility
# priors
class_possibility = []
for i in range(10):
class_possibility.append(float(len(data_dict[i])) / total_training_data)
# print(class_possibility)
# conditional possibility
m = 4
n = 2
#m*n size block
trained = []
for i in range(10):#For each number class
num_trained = {}#Dict for current class, (x, y, pixelValue):Occurance Time
#Init the num_trained
# for x_idx in range(28):
# for y_idx in range(28):
# for ele in ['#', '+', ' ']:
# num_trained[(x_idx, y_idx, ele)] = 1;
for j in range(len(data_dict[i])):
num_dict = data_dict[i][j]
for x_idx in range(len(num_dict)-(m-1)):
for y_idx in range(len(num_dict[x_idx])-(n-1)):
#obj = (x_idx, y_idx, data_dict[i][j][x_idx][y_idx])
wenwen = ''
c = 0
for x in range(m):
for y in range(n):
#wenwen += data_dict[i][j][x_idx+x][y_idx+y]
if data_dict[i][j][x_idx+x][y_idx+y] == '#' :
wenwen += '#'
if data_dict[i][j][x_idx+x][y_idx+y] == '+' :
wenwen += '#'
if data_dict[i][j][x_idx+x][y_idx+y] == ' ' :
wenwen += ' '
obj = (x_idx, y_idx, wenwen)
if obj in num_trained:
num_trained[obj] += 1;
else:
num_trained[obj] = 1;
for key in num_trained.keys():
print(key)
num_trained[key] = (num_trained[key]) / float(len(data_dict[i]));
trained.append(num_trained);
# print(trained[0])
trainingData.close();
label_file.close();
#Validate
f_testi = open('testimages', 'r');
f_testl = open('testlabels', 'r');
label_arr_valid = []
total_result = []
temp = []
testimage_file = []
count = 0
index = 0
valid_data = {}
correct_count = np.zeros(10);
total_count = np.zeros(10);
confusion_matrix = np.zeros((10,10))
biggest_posterior = np.array([-np.inf]*10)
index_biggest = np.zeros(10)
smallest_posterior = np.array([np.inf]*10)
index_smallest = np.zeros(10)
t_count = 0.0;
c_count = 0.0;
for line in f_testl:
label_arr_valid.append(int(line[0:-1]));
for line in f_testi:
if(count < 28):
temp.append(line[0:-1])
count += 1;
else:#get an image
t_count += 1;
testimage_file.append(temp)
result = []
for k in range(10):
curr_result = np.log2(class_possibility[k]);
for i in range(28-(m-1)):
for j in range(28-(n-1)):
temp_dict = trained[k]
wlb = ''
for x in range(m):
for y in range(n):
#wlb += temp[i+x][j+y]
if temp[i+x][j+y] == '#' :
wlb += '#'
if temp[i+x][j+y] == '+' :
wlb += '#'
if temp[i+x][j+y] == ' ' :
wlb += ' '
if (i,j,wlb) in temp_dict:
t = temp_dict[(i,j,wlb)]
else:
t = 1/float(len(data_dict[k]))
curr_result = curr_result + np.log2(t)
result.append(curr_result);
result_num = np.argmax(result);
true_num = label_arr_valid[index];
# print(result[result_num])
if biggest_posterior[true_num] < result[result_num]:
biggest_posterior[true_num] = result[result_num]
index_biggest[true_num] = index;
if smallest_posterior[true_num] > result[result_num]:
smallest_posterior[true_num] = result[result_num]
index_smallest[true_num] = index;
total_count[true_num] += 1;
if (result_num == label_arr_valid[index]):
c_count += 1;
correct_count[true_num] += 1;
confusion_matrix[true_num,result_num] += 1
# total_result.append(np.argmax(result))
count = 0
index += 1
temp = []
temp.append(line[0:-1])
count += 1;
# print(correct_count)
# print(total_count)
print("Result: ")
print(correct_count/total_count)
for i in range(10):
confusion_matrix[i] /= total_count[i]
# print(confusion_matrix)
# print(index_biggest)
# print(index_smallest)
# for i in range(10):
# print(testimage_file[int(index_biggest[i])])
# print(testimage_file[int(index_smallest[i])])
print(c_count/t_count);