-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
131 lines (96 loc) · 4 KB
/
main.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
import numpy as np
import keras
from keras import layers
def clean_data(data):
(X_train, Y_train), (X_test, Y_test) = data
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
X_train = np.where(X_train >= 0.5, 1, 0).astype('int8') # (60000 x 784) binary values
X_test = np.where(X_test >= 0.5, 1, 0).astype('int8') # (10000 x 784) binary values
Y_train = keras.utils.to_categorical(Y_train, 10) # (60000 x 10) 1-hot encoded
Y_test = keras.utils.to_categorical(Y_test, 10) # (10000 x 10) 1-hot encoded
return X_train, Y_train, X_test, Y_test
def train_new_model(model_name):
X_train, Y_train, X_test, Y_test = clean_data(keras.datasets.mnist.load_data())
model = keras.Sequential(
[
keras.Input(shape=(784,)),
layers.Dense(10),
layers.Activation('relu'),
layers.Dense(10),
layers.Activation('softmax')
]
)
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(X_train, Y_train, batch_size=128, epochs=15, validation_split=0.1)
model.save(model_name)
loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2)
print("Test Loss", loss_and_metrics[0])
print("Test Accuracy", loss_and_metrics[1])
def check_overflow(x, num_bits):
if (x > (2 ** (num_bits-1) - 1)) or (x < -(2 ** (num_bits-1))):
print('overflow detected')
def forward(model_name, iterations=10000):
X_train, Y_train, X_test, Y_test = clean_data(keras.datasets.mnist.load_data())
model = keras.saving.load_model(model_name)
weights1 = model.layers[0].get_weights()[0]
biases1 = model.layers[0].get_weights()[1]
weights2 = model.layers[2].get_weights()[0]
biases2 = model.layers[2].get_weights()[1]
def to_fixed(float_value, bits_past_radix=2):
a = float_value * (2 ** bits_past_radix)
b = int(round(a))
if a < 0:
b = ~(abs(b)) + 1
return b
weights1 = np.vectorize(to_fixed)(weights1).astype('int8')
biases1 = np.vectorize(to_fixed)(biases1).astype('int8')
weights2 = np.vectorize(to_fixed)(weights2).astype('int8')
biases2 = np.vectorize(to_fixed)(biases2).astype('int8')
count = 0
total = 0
for X, Y in zip(X_test, Y_test):
# HIDDEN LAYER
output = [0] * 10
for neuron in range(10):
weights = weights1.T[neuron]
weight = 0
for index, pixel in enumerate(X):
if pixel == 1:
weight += weights[index]
check_overflow(weight, 8)
weight += biases1.T[neuron]
check_overflow(weight, 8)
output[neuron] = int(weight)
hidden_out = np.array(output).astype('int8')
# RELU
hidden_out = np.maximum(0, hidden_out).astype('int8')
# OUTPUT LAYER
output = [0] * 10
for neuron in range(10):
weights = weights2.T[neuron]
weight = 0
for index, value in enumerate(hidden_out):
weight += weights[index] * np.int16(value)
check_overflow(weight, 16)
weight += biases2.T[neuron]
check_overflow(weight, 16)
output[neuron] = int(weight)
output_out = np.array(output).astype('int16')
prediction = np.where(output_out == np.max(output_out), 1, 0).astype('int16')
if np.array_equal(prediction, Y):
count += 1
total+=1
if total >= iterations:
break
if total % 100 == 0:
print(total)
print(f'Accuracy: {count} / {total} = {count / total * 100}%')
def main():
model_name = 'mnist_model.keras'
# train_new_model(model_name)
forward(model_name, 10000)
if __name__ == '__main__':
main()