-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar10_bayesian.py
278 lines (237 loc) · 10.4 KB
/
cifar10_bayesian.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
import pickle
import numpy as np
from numpy import cov
import matplotlib.pyplot as plt
from random import random
import pandas as pd
import scipy.stats as stats
from scipy.stats import multivariate_normal
from skimage.transform import rescale, resize, downscale_local_mean
def unpickle(file):
with open(file, 'rb') as f:
dict = pickle.load(f, encoding="latin1")
return dict
def class_acc(pred,gt):
return (np.sum(pred==gt)/len(pred))*100
def cifar10_color(X):
Xf = np.zeros((len(X),3))
for i in range(X.shape[0]):
img = X[i]
img_1x1 = resize(img, (1, 1))
r_vals = img_1x1[:,:,0].reshape(1*1)
g_vals = img_1x1[:,:,1].reshape(1*1)
b_vals = img_1x1[:,:,2].reshape(1*1)
mu_r = r_vals.mean()
mu_g = g_vals.mean()
mu_b = b_vals.mean()
Xf[i,:] = (mu_r, mu_g, mu_b)
return Xf
def cifar10_2X2_color(X):
data=np.array([])
for i in range(X.shape[0]):
img = X[i]
img_2x2 = resize(img, (2, 2))
r_vals = img_2x2[:,:,0].reshape(2*2)
g_vals = img_2x2[:,:,1].reshape(2*2)
b_vals = img_2x2[:,:,2].reshape(2*2)
vector = np.concatenate((np.transpose(r_vals),np.transpose(g_vals),np.transpose(b_vals)))
data=np.append(data,vector)
return data
def cifar10_4X4_color(X):
data=np.array([])
for i in range(X.shape[0]):
img = X[i]
img_4x4 = resize(img, (4, 4))
r_vals = img_4x4[:,:,0].reshape(4*4)
g_vals = img_4x4[:,:,1].reshape(4*4)
b_vals = img_4x4[:,:,2].reshape(4*4)
vector = np.concatenate((np.transpose(r_vals),np.transpose(g_vals),np.transpose(b_vals)))
data=np.append(data,vector)
return data
def cifar10_8X8_color(X):
data=np.array([])
for i in range(X.shape[0]):
img = X[i]
img_8x8 = resize(img, (8, 8))
r_vals = img_8x8[:,:,0].reshape(8*8)
g_vals = img_8x8[:,:,1].reshape(8*8)
b_vals = img_8x8[:,:,2].reshape(8*8)
vector = np.concatenate((np.transpose(r_vals),np.transpose(g_vals),np.transpose(b_vals)))
data=np.append(data,vector)
return data
def cifar10_16X16_color(X):
data=np.array([])
for i in range(X.shape[0]):
img = X[i]
img_16x16 = resize(img, (16, 16))
r_vals = img_16x16[:,:,0].reshape(16*16)
g_vals = img_16x16[:,:,1].reshape(16*16)
b_vals = img_16x16[:,:,2].reshape(16*16)
vector = np.concatenate((np.transpose(r_vals),np.transpose(g_vals),np.transpose(b_vals)))
data=np.append(data,vector)
return data
def cifar_10_naivebayes_learn(Xf,Y):
df_x=pd.DataFrame(data=Xf)
df_y=pd.DataFrame(data=Y)
frames=[df_x,df_y]
data=pd.concat(frames,axis=1)
data.columns=['r','g','b','class']
data=data.sort_values(by=['class'],ascending=True)
mu=np.zeros((10,3))
std=np.zeros((10,3))
cov=np.zeros((10,3,3))
p=np.repeat(0.1,10)
m=0
n=5000
for i in range(0,10):
df_select=data.iloc[m:n, 0:3]
mu[i]=np.array(df_select.mean(axis=0))
std[i]=np.array(df_select.std(axis=0))
cov[i]=np.array(df_select.cov())
m=m+5000
n=n+5000
return mu,std,p,cov
def cifar_10_naivebayes_learn_super_powerful(Xf,Y):
df_x=pd.DataFrame(data=Xf)
df_y=pd.DataFrame(data=Y)
frames=[df_x,df_y]
data=pd.concat(frames,axis=1)
print(data.shape)
t=Xf.shape[1]
print(t)
col_val=[]
for i in range(0,Xf.shape[1]):
col_val.append(str(i))
col_val.append('class')
data.columns=col_val
data=data.sort_values(by='class',ascending=True)
mu=np.zeros((10,t))
cov=np.zeros((10,t,t))
p=np.repeat(0.1,10)
m=0
n=5000
for i in range(0,10):
df_select=data.iloc[m:n, 0:t]
mu[i]=np.array(df_select.mean(axis=0))
cov[i]=np.array(df_select.cov())
m=m+5000
n=n+5000
return mu,p,cov
def cifar10_classifier_naivebayes(x,mu,std,p):
prob_class_0=stats.norm.pdf(x[0],mu[0][0], std[0][0])*stats.norm.pdf(x[1],mu[0][1], std[0][1])*stats.norm.pdf(x[2],mu[0][2], std[0][2])*p[0]
prob_class_1=stats.norm.pdf(x[0],mu[1][0], std[1][0])*stats.norm.pdf(x[1],mu[1][1], std[1][1])*stats.norm.pdf(x[2],mu[1][2], std[1][2])*p[1]
prob_class_2=stats.norm.pdf(x[0],mu[2][0], std[2][0])*stats.norm.pdf(x[1],mu[2][1], std[2][1])*stats.norm.pdf(x[2],mu[2][2], std[2][2])*p[2]
prob_class_3=stats.norm.pdf(x[0],mu[3][0], std[3][0])*stats.norm.pdf(x[1],mu[3][1], std[3][1])*stats.norm.pdf(x[2],mu[3][2], std[3][2])*p[3]
prob_class_4=stats.norm.pdf(x[0],mu[4][0], std[4][0])*stats.norm.pdf(x[1],mu[4][1], std[4][1])*stats.norm.pdf(x[2],mu[4][2], std[4][2])*p[4]
prob_class_5=stats.norm.pdf(x[0],mu[5][0], std[5][0])*stats.norm.pdf(x[1],mu[5][1], std[5][1])*stats.norm.pdf(x[2],mu[5][2], std[5][2])*p[5]
prob_class_6=stats.norm.pdf(x[0],mu[6][0], std[6][0])*stats.norm.pdf(x[1],mu[6][1], std[6][1])*stats.norm.pdf(x[2],mu[6][2], std[6][2])*p[6]
prob_class_7=stats.norm.pdf(x[0],mu[7][0], std[7][0])*stats.norm.pdf(x[1],mu[7][1], std[7][1])*stats.norm.pdf(x[2],mu[7][2], std[7][2])*p[7]
prob_class_8=stats.norm.pdf(x[0],mu[8][0], std[8][0])*stats.norm.pdf(x[1],mu[8][1], std[8][1])*stats.norm.pdf(x[2],mu[8][2], std[8][2])*p[8]
prob_class_9=stats.norm.pdf(x[0],mu[9][0], std[9][0])*stats.norm.pdf(x[1],mu[9][1], std[9][1])*stats.norm.pdf(x[2],mu[9][2], std[9][2])*p[9]
class_predicted=np.argmax([prob_class_0,prob_class_1,prob_class_2,prob_class_3,prob_class_4,prob_class_5,prob_class_6,prob_class_7,prob_class_8,prob_class_9])
return class_predicted
def cifar10_classifier_naivebayes_better_super(x,mu,cov,p):
prob_class_0=multivariate_normal.logpdf(x, mu[0], cov[0])*p[0]
prob_class_1=multivariate_normal.logpdf(x, mu[1], cov[1])*p[1]
prob_class_2=multivariate_normal.logpdf(x, mu[2], cov[2])*p[2]
prob_class_3=multivariate_normal.logpdf(x, mu[3], cov[3])*p[3]
prob_class_4=multivariate_normal.logpdf(x, mu[4], cov[4])*p[4]
prob_class_5=multivariate_normal.logpdf(x, mu[5], cov[5])*p[5]
prob_class_6=multivariate_normal.logpdf(x, mu[6], cov[6])*p[6]
prob_class_7=multivariate_normal.logpdf(x, mu[7], cov[7])*p[7]
prob_class_8=multivariate_normal.logpdf(x, mu[8], cov[8])*p[8]
prob_class_9=multivariate_normal.logpdf(x, mu[9], cov[9])*p[9]
class_predicted=np.argmax([prob_class_0,prob_class_1,prob_class_2,prob_class_3,prob_class_4,prob_class_5,prob_class_6,prob_class_7,prob_class_8,prob_class_9])
return class_predicted
datadict = unpickle('cifar-10-batches-py/test_batch')
X_test = datadict["data"]
Y_test = datadict["labels"]
labeldict = unpickle('cifar-10-batches-py/batches.meta')
label_names = labeldict["label_names"]
X_test= X_test.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("int32")
Y_test = np.array(Y_test)
trainset1=unpickle('cifar-10-batches-py/data_batch_1')
trainset2=unpickle('cifar-10-batches-py/data_batch_2')
trainset3=unpickle('cifar-10-batches-py/data_batch_3')
trainset4=unpickle('cifar-10-batches-py/data_batch_4')
trainset5=unpickle('cifar-10-batches-py/data_batch_5')
X_train1=trainset1["data"]
Y_train1=trainset1["labels"]
X_train2=trainset2["data"]
Y_train2=trainset2["labels"]
X_train3=trainset3["data"]
Y_train3=trainset3["labels"]
X_train4=trainset4["data"]
Y_train4=trainset4["labels"]
X_train5=trainset5["data"]
Y_train5=trainset5["labels"]
X_train = np.concatenate((X_train1,X_train2,X_train3,X_train4,X_train5))
Y_train=np.concatenate((Y_train1,Y_train2,Y_train3,Y_train4,Y_train5))
X_train= X_train.reshape(50000, 3, 32, 32).transpose(0,2,3,1).astype("int32")
#Task 1
Xf_train=cifar10_color(X_train)
[mu,std,p,cov]=cifar_10_naivebayes_learn(Xf_train,Y_train)
Xf_test=cifar10_color(X_test)
y_pred_a=[]
for i in range(0,len(Xf_test)):
y_bayesian_simple=cifar10_classifier_naivebayes(Xf_test[i],mu,std,p)
y_pred_a.append(y_bayesian_simple)
print("Accuracy for simple bayesian classifier:"+str(class_acc(y_pred_a,Y_test))+"%")
#Task 2:
y_pred_b=[]
for i in range(0,len(Xf_test)):
y_bayesian_better=cifar10_classifier_naivebayes_better(Xf_test[i],mu,cov,p)
y_pred_b.append(y_bayesian_better)
print("Accuracy for better bayesian classifier:"+str(class_acc(y_pred_b,Y_test))+"%")
#Task 3
#For 2X2
data=cifar10_2X2_color(X_train)
data=data.reshape(50000,12)
[mu,p,cov]=cifar_10_naivebayes_learn_super_powerful(data,Y_train)
data_test=cifar10_2X2_color(X_test)
data_test=data_test.reshape(10000,12)
y_pred_s=[]
for i in range(0,len(data_test)):
y_bayesian_super=cifar10_classifier_naivebayes_better_super(data_test[i],mu,cov,p)
y_pred_s.append(y_bayesian_super)
print("Accuracy for super powerful bayesian classifier with image size 2X2:"+str(class_acc(y_pred_s,Y_test))+"%")
#For 4X4
data=cifar10_4X4_color(X_train)
data=data.reshape(50000,48)
[mu,p,cov]=cifar_10_naivebayes_learn_super_powerful(data,Y_train)
data_test=cifar10_4X4_color(X_test)
data_test=data_test.reshape(10000,48)
y_pred_q=[]
for i in range(0,len(data_test)):
y_bayesian_super=cifar10_classifier_naivebayes_better_super(data_test[i],mu,cov,p)
y_pred_q.append(y_bayesian_super)
print("Accuracy for super powerful bayesian classifier with image size 4X4:"+str(class_acc(y_pred_q,Y_test))+"%")
#For 8X8
data=cifar10_8X8_color(X_train)
data=data.reshape(50000,192)
[mu,p,cov]=cifar_10_naivebayes_learn_super_powerful(data,Y_train)
data_test=cifar10_8X8_color(X_test)
data_test=data_test.reshape(10000,192)
y_pred_r=[]
for i in range(0,len(data_test)):
y_bayesian_super=cifar10_classifier_naivebayes_better_super(data_test[i],mu,cov,p)
y_pred_r.append(y_bayesian_super)
print("Accuracy for super powerful bayesian classifier with image size 8X8:"+str(class_acc(y_pred_r,Y_test))+"%")
#For 16X16
data=cifar10_16X16_color(X_train)
data=data.reshape(50000,768)
[mu,p,cov]=cifar_10_naivebayes_learn_super_powerful(data,Y_train)
data_test=cifar10_16X16_color(X_test)
data_test=data_test.reshape(10000,768)
y_pred_r=[]
for i in range(0,len(data_test)):
y_bayesian_super=cifar10_classifier_naivebayes_better_super(data_test[i],mu,cov,p)
y_pred_r.append(y_bayesian_super)
print("Accuracy for super powerful bayesian classifier with image size 16X16:"+str(class_acc(y_pred_r,Y_test))+"%")
#Graph
N=[2,4,8,16]
accuracies=[31.05,40.22,41.73,43.46] #Accuracies are obtained after running every task seperately
plt.plot(N,accuracies, 'r-')
plt.xlabel('N')
plt.ylabel('Accuracy')
plt.show()