-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMnistCNN.py
More file actions
80 lines (56 loc) · 2.41 KB
/
Copy pathMnistCNN.py
File metadata and controls
80 lines (56 loc) · 2.41 KB
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
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
url_training = 'https://pjreddie.com/media/files/mnist_train.csv'
url_testing = 'https://pjreddie.com/media/files/mnist_test.csv'
df_train = pd.read_csv(url_training, header=None)
df_test = pd.read_csv(url_testing, header=None)
# combine training and testing
data = np.concatenate((df_train, df_test))
# Check if data was correctly combined
#print('data size:', data.shape)
#print('data concatenate:', data)
total_avg = 0
for i in range(100):
np.random.shuffle(data)
x = data[:, 1:]
y = data[:, 0]
#k-10 fold
x_train = x[:63000, :]
# print('x_train_shape', x_train.shape)
y_train = y[:63000]
# print('y_train_shape', y_train.shape)
x_test = x[63000:, :]
y_test = y[63000:]
# Reshape x so instead of having 748 some pixels in a single x row we have a 28 x 28 image
# We also change the type from the origial int value to a float type so we can have ...
# more accurate numbers when we normalize when we divide by 255 from the greyscale it uses
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32')/255
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32')/255
# convert y using one-hot method for mutliclass
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(28, 28, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=30, batch_size=400)
y_loss = history.history['accuracy']
print(model.evaluate(x_test, y_test)[1])
total_avg += model.evaluate(x_test, y_test)[1]
print('total_avg: ', total_avg/100 )