-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunSvmSoftmax.py
252 lines (225 loc) · 10.7 KB
/
runSvmSoftmax.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
import os
import time
import numpy as np
# Library for plot the output and save to file
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
# Load the CIFAR10 dataset
from keras.datasets import cifar10
baseDir = os.path.dirname(os.path.abspath('__file__')) + '/'
classesName = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
(xTrain, yTrain), (xTest, yTest) = cifar10.load_data()
xVal = xTrain[49000:, :].astype(np.float)
yVal = np.squeeze(yTrain[49000:, :])
xTrain = xTrain[:49000, :].astype(np.float)
yTrain = np.squeeze(yTrain[:49000, :])
yTest = np.squeeze(yTest)
xTest = xTest.astype(np.float)
# Show dimension for each variable
print ('Train image shape: {0}'.format(xTrain.shape))
print ('Train label shape: {0}'.format(yTrain.shape))
print ('Validate image shape: {0}'.format(xVal.shape))
print ('Validate label shape: {0}'.format(yVal.shape))
print ('Test image shape: {0}'.format(xTest.shape))
print ('Test label shape: {0}'.format(yTest.shape))
# Show some CIFAR10 images
plt.subplot(221)
plt.imshow(xTrain[0])
plt.axis('off')
plt.title(classesName[yTrain[0]])
plt.subplot(222)
plt.imshow(xTrain[1])
plt.axis('off')
plt.title(classesName[yTrain[1]])
plt.subplot(223)
plt.imshow(xVal[0])
plt.axis('off')
plt.title(classesName[yVal[1]])
plt.subplot(224)
plt.imshow(xTest[0])
plt.axis('off')
plt.title(classesName[yTest[0]])
plt.savefig(baseDir+'svm0.png')
plt.clf()
# Pre processing data
# Normalize the data by subtract the mean image
meanImage = np.mean(xTrain, axis=0)
xTrain -= meanImage
xVal -= meanImage
xTest -= meanImage
# Reshape data from channel to rows
xTrain = np.reshape(xTrain, (xTrain.shape[0], -1))
xVal = np.reshape(xVal, (xVal.shape[0], -1))
xTest = np.reshape(xTest, (xTest.shape[0], -1))
# Add bias dimension columns
xTrain = np.hstack([xTrain, np.ones((xTrain.shape[0], 1))])
xVal = np.hstack([xVal, np.ones((xVal.shape[0], 1))])
xTest = np.hstack([xTest, np.ones((xTest.shape[0], 1))])
print ('Train image shape after add bias column: {0}'.format(xTrain.shape))
print ('Val image shape after add bias column: {0}'.format(xVal.shape))
print ('Test image shape after add bias column: {0}'.format(xTest.shape))
print ('\n##############################################################################################')
######################################################################################################
# SVM CLASSIFIER #
######################################################################################################
from svm import Svm
numClasses = np.max(yTrain) + 1
print ('Start training Svm classifier')
classifier = Svm(xTrain.shape[1], numClasses)
# Show weight for each class before training
if classifier.W is not None:
tmpW = classifier.W[:-1, :]
tmpW = tmpW.reshape(32, 32, 3, 10)
tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
for i in range(numClasses):
plt.subplot(2, 5, i+1)
plt.title(classesName[i])
wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax - tmpWMin)
plt.imshow(wPlot.astype('uint8'))
plt.savefig(baseDir+'svm1.png')
plt.clf()
# Training classifier
startTime = time.time()
classifier.train(xTrain, yTrain, lr=1e-7, reg=5e4, iter=1500 ,verbose=True)
print ('Training time: {0}'.format(time.time() - startTime))
# Calculate accuracy (Should get around this)
# Training acc: 37.61%
# Validating acc: 37.0%
# Testing acc: 37.38%
print ('Training acc: {0}%'.format(classifier.calAccuracy(xTrain, yTrain)))
print ('Validating acc: {0}%'.format(classifier.calAccuracy(xVal, yVal)))
print ('Testing acc: {0}%'.format(classifier.calAccuracy(xTest, yTest)))
# Show some weight for each class after training
if classifier.W is not None:
tmpW = classifier.W[:-1, :]
tmpW = tmpW.reshape(32, 32, 3, 10)
tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
for i in range(numClasses):
plt.subplot(2, 5, i+1)
plt.title(classesName[i])
# Scale weight to 0 - 255
wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax - tmpWMin)
plt.imshow(wPlot.astype('uint8'))
plt.savefig(baseDir+'svm2.png')
plt.clf()
# Tuneup hyper parameters (regularization strength, learning rate) by using validation data set,
# and random search technique to find the best set of parameters.
learn_rates = [0.5e-7, 1e-7, 2e-7, 6e-7]
reg_strengths = [500,5000,18000]
bestParameters = [0, 0]
bestAcc = -1
bestModel = None
print ('\nFinding best model for Svm classifier')
################################################################################
# TODO: 5 points #
# Tuneup hyper parameters by using validation set. #
# - Store the best variables in parameters #
# - Store the best model in bestSoftmax #
# - Store the best accuracy in bestAcc #
################################################################################
for rs in reg_strengths:
for lr in learn_rates:
#print(str(lr)+" "+str(rs))
classifier = Svm(xTrain.shape[1], numClasses)
classifier.train(xTrain, yTrain, lr, rs, iter=1500 ,verbose=False)
valAcc = classifier.calAccuracy(xVal, yVal)
if valAcc > bestAcc:
bestAcc = valAcc
bestModel = classifier
bestParameters = [lr,rs]
pass
################################################################################
# END OF YOUR CODE #
################################################################################
print ('Best validation accuracy: {0}'.format(bestAcc))
# Predict with best model
if bestModel is not None:
print ('Best Model parameter, lr = {0}, reg = {1}'.format(bestParameters[0], bestParameters[1]))
print ('Training acc: {0}%'.format(bestModel.calAccuracy(xTrain, yTrain)))
print ('Validating acc: {0}%'.format(bestModel.calAccuracy(xVal, yVal)))
print ('Testing acc: {0}%'.format(bestModel.calAccuracy(xTest, yTest)))
print ('\n##############################################################################################')
######################################################################################################
# END OF SVM CLASSIFIER #
######################################################################################################
######################################################################################################
# SOFTMAX CLASSIFIER #
######################################################################################################
from softmax import Softmax
numClasses = np.max(yTrain) + 1
print ('Start training Softmax classifier')
classifier = Softmax(xTrain.shape[1], numClasses)
# Show weight for each class before training
if classifier.W is not None:
tmpW = classifier.W[:-1, :]
tmpW = tmpW.reshape(32, 32, 3, 10)
tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
for i in range(numClasses):
plt.subplot(2, 5, i+1)
plt.title(classesName[i])
wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax - tmpWMin)
plt.imshow(wPlot.astype('uint8'))
plt.savefig(baseDir+'softmax1.png')
plt.clf()
# Training classifier
startTime = time.time()
classifier.train(xTrain, yTrain, lr=1e-7, reg=5e4, iter=1500 ,verbose=True)
print ('Training time: {0}'.format(time.time() - startTime))
# Calculate accuracy (Should get around this)
# Training acc: 33.03%
# Validating acc: 33.7%
# Testing acc: 33.12%
print ('Training acc: {0}%'.format(classifier.calAccuracy(xTrain, yTrain)))
print ('Validating acc: {0}%'.format(classifier.calAccuracy(xVal, yVal)))
print ('Testing acc: {0}%'.format(classifier.calAccuracy(xTest, yTest)))
# Show some weight for each class after training
if classifier.W is not None:
tmpW = classifier.W[:-1, :]
tmpW = tmpW.reshape(32, 32, 3, 10)
tmpWMin, tmpWMax = np.min(tmpW), np.max(tmpW)
for i in range(numClasses):
plt.subplot(2, 5, i+1)
plt.title(classesName[i])
# Range 0 - 255
wPlot = 255.0 * (tmpW[:, :, :, i].squeeze() - tmpWMin) / (tmpWMax - tmpWMin)
plt.imshow(wPlot.astype('uint8'))
plt.savefig(baseDir+'softmax2.png')
plt.clf()
# Tuneup hyper parameters (regularization strength, learning rate) by using validation data set,
# and random search technique to find the best set of parameters.
learn_rates = [0.5e-7,4e-7, 8e-7]
reg_strengths = [ 500,1500,7500,12000]
bestParameters = [0, 0]
bestAcc = -1
bestModel = None
print ('\nFinding best model for Softmax classifier')
################################################################################
# TODO: 5 points #
# Tuneup hyper parameters by using validation set. #
# - Store the best variables in parameters #
# - Store the best model in bestSoftmax #
# - Store the best accuracy in bestAcc #
################################################################################
for rs in reg_strengths:
for lr in learn_rates:
classifier = Softmax(xTrain.shape[1], numClasses)
classifier.train(xTrain, yTrain, lr, rs, iter=1500 ,verbose=False)
valAcc = classifier.calAccuracy(xVal, yVal)
if valAcc > bestAcc:
bestAcc = valAcc
bestModel = classifier
bestParameters = [lr,rs]
################################################################################
# END OF YOUR CODE #
################################################################################
print ('Best validation accuracy: {0}'.format(bestAcc))
# Predict with best model
if bestModel is not None:
print ('Best Model parameter, lr = {0}, reg = {1}'.format(bestParameters[0], bestParameters[1]))
print ('Training acc: {0}%'.format(bestModel.calAccuracy(xTrain, yTrain)))
print ('Validating acc: {0}%'.format(bestModel.calAccuracy(xVal, yVal)))
print ('Testing acc: {0}%'.format(bestModel.calAccuracy(xTest, yTest)))
######################################################################################################
# END OF SOFTMAX CLASSIFIER #
######################################################################################################