-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
193 lines (163 loc) · 5.43 KB
/
run.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
# Cpu imports.
from cpu.Layers import *
from cpu.Optimization import *
import cpu.NeuralNetwork as net
import DataHandler
from Gpu import basicCNN
import numpy as np
import pandas as pd
import time
import matplotlib.pyplot as plt
import os
dataPath = 'data/mnist.pkl.gz'
def CNN_Gpu():
model = basicCNN()
model.data_Layer = DataHandler.MnistData(batch_size=batch_size, num_classes=num_classes, path=dataPath,
image_format="channel_last",input_shape=(28,28,1))
model.createModel(architecture)
start = time.time()
hist_dict = model.train(epochs)
end = time.time()
print("total training time = ", end - start)
testResult = model.test()
return hist_dict, [testResult[0], testResult[1], end - start]
def CNN_Cpu():
model = net.NeuralNetwork(optimizer="Adam",weights_initializer="He",bias_initializer="Constant", image_format = "channel_last")
model.data_layer = DataHandler.MnistData(batch_size=batch_size, num_classes=num_classes, path=dataPath,
image_format="channel_last", input_shape=(28,28,1))
model.createModel(architecture)
start = time.time()
hist_dict = model.train(epochs)
end = time.time()
print("total training time = ", end - start)
testResult = model.test()
return hist_dict, [testResult[0], testResult[1], end - start]
def FC_Cpu():
model = net.NeuralNetwork(optimizer="Adam",weights_initializer="He",bias_initializer="Constant", image_format = "channel_last")
model.data_layer = DataHandler.MnistData(batch_size=batch_size, num_classes=num_classes, path=dataPath,
image_format="channel_last", input_shape=(784,))
model.createModel(FullyConnected)
start = time.time()
hist_dict = model.train(epochs)
end = time.time()
print("total training time = ", end - start)
testResult = model.test()
return hist_dict, [testResult[0], testResult[1], end - start]
batch_size = 128
num_classes = 10
epochs = 12
#structue of the architecture - used for both CPU and GPU
architecture = {
"layers": [
{
"name": "CNN",
"filters":5,
"kernel_size": (5,5),
"strides": (2,2),
"padding": "valid",
"activation": "relu",
"input_shape": (28,28,1),
"image_channels":1
},
{
"name": "CNN",
"filters":12,
"kernel_size": (3,3),
"strides": (1,1),
"padding": "valid",
"activation": "relu",
"input_shape": (1,28,28),
"image_channels":1
},
{
"name": "pool",
"pool_size": (2,2),
"strides": (1,1),
"input_shape": (1,28,28)
},
{
"name": "dropout",
"dropoutRate": 0.25
},
{
"name": "Flatten"
},
{
"name": "FC",
"input_shape": 972,
"outputSize": 128,
"activation": "relu"
},
{
"name": "dropout",
"dropoutRate": 0.5
},
{
"name": "FC",
"input_shape": 128,
"outputSize": num_classes,
"activation": "softmax"
},
],
}
FullyConnected = {
"layers": [
{
"name": "FC",
"input_shape": 784,
"outputSize": 720,
"activation": "relu"
},
{
"name": "FC",
"input_shape": 720,
"outputSize": 1200,
"activation": "relu"
},
{
"name": "dropout",
"dropoutRate": 0.25
},
{
"name": "FC",
"input_shape": 1200,
"outputSize": 128,
"activation": "relu"
},
{
"name": "dropout",
"dropoutRate": 0.5
},
{
"name": "FC",
"input_shape": 128,
"outputSize": num_classes,
"activation": "softmax"
},
],
}
resultsFolder = "results/"
if not os.path.exists(resultsFolder):
os.makedirs(resultsFolder)
fnLisit = [CNN_Gpu,CNN_Cpu,FC_Cpu]
MetricDict = {}
for fn in fnLisit:
hist_dict, metric = fn()
MetricDict[fn.__name__] = metric
fig = plt.figure()
fig.gca().plot(np.arange(epochs),hist_dict["loss"],'X-', label='training loss', linewidth=1.0)
fig.gca().plot(np.arange(epochs),hist_dict["val_loss"],'o-', label='validation loss', linewidth=1.0)
fig.gca().set_xlim(right = epochs+1)
fig.gca().grid(which='minor', linestyle='--')
fig.gca().set_xlabel('epoch')
fig.gca().set_ylabel('loss')
fig.gca().legend(loc = "upper right", fontsize = 18)
fig.gca().set_title(fn.__name__, fontsize = 20)
fig.gca().minorticks_on()
fig.gca().grid(which='minor', linestyle='--')
fig.tight_layout()
fig.savefig(resultsFolder+fn.__name__+".png",dpi = 300)
index = ["Loss", "Accuracy", "Time"]
df = pd.DataFrame(data=MetricDict, index=index)
df = df.T
df.to_csv(resultsFolder+"testResults.csv")