-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenet5_mnist_zeropadding.py
157 lines (109 loc) · 5.01 KB
/
lenet5_mnist_zeropadding.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
# -*- coding: utf-8 -*-
from requests import get
def download_file(url, file_name):
with open(file_name, "wb") as file:
response = get(url)
file.write(response.content)
# Commented out IPython magic to ensure Python compatibility.
import gzip
import numpy as np
import pandas as pd
from time import time
from sklearn.model_selection import train_test_split
import tensorflow as tf
import keras
import keras.layers as layers
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.callbacks import TensorBoard
# %matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
def read_mnist(images_path: str, labels_path: str):
with gzip.open(labels_path, 'rb') as labelsFile:
labels = np.frombuffer(labelsFile.read(), dtype=np.uint8, offset=8)
with gzip.open(images_path,'rb') as imagesFile:
length = len(labels)
# Load flat 28x28 px images (784 px), and convert them to 28x28 px
features = np.frombuffer(imagesFile.read(), dtype=np.uint8, offset=16) \
.reshape(length, 784) \
.reshape(length, 28, 28, 1)
return features, labels
train = {}
test = {}
train['features'], train['labels'] = read_mnist('train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz')
test['features'], test['labels'] = read_mnist('t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz')
print('# of training images:', train['features'].shape[0])
print('# of test images:', test['features'].shape[0])
"""### Split training data into training and validation"""
validation = {}
train['features'], validation['features'], train['labels'], validation['labels'] = train_test_split(train['features'], train['labels'], test_size=0.2, random_state=0)
print('# of training images:', train['features'].shape[0])
print('# of validation images:', validation['features'].shape[0])
"""## Zero Padding
The LeNet architecture accepts a 32x32 pixel images as input, mnist data is 28x28 pixels.
"""
# Pad images with 0s
train['features'] = np.pad(train['features'], ((0,0),(2,2),(2,2),(0,0)), 'constant')
validation['features'] = np.pad(validation['features'], ((0,0),(2,2),(2,2),(0,0)), 'constant')
test['features'] = np.pad(test['features'], ((0,0),(2,2),(2,2),(0,0)), 'constant')
print("Updated Image Shape: {}".format(train['features'][0].shape))
model = keras.Sequential()
model.add(layers.Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(32,32,1)))
model.add(layers.AveragePooling2D())
model.add(layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
model.add(layers.AveragePooling2D())
model.add(layers.Flatten())
model.add(layers.Dense(units=120, activation='relu'))
model.add(layers.Dense(units=84, activation='relu'))
model.add(layers.Dense(units=10, activation = 'softmax'))
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
EPOCHS = 40
BATCH_SIZE = 128
X_train, y_train = train['features'], to_categorical(train['labels'])
X_validation, y_validation = validation['features'], to_categorical(validation['labels'])
train_generator = ImageDataGenerator().flow(X_train, y_train, batch_size=BATCH_SIZE)
validation_generator = ImageDataGenerator().flow(X_validation, y_validation, batch_size=BATCH_SIZE)
print('# of training images:', train['features'].shape[0])
print('# of validation images:', validation['features'].shape[0])
steps_per_epoch = X_train.shape[0]//BATCH_SIZE
validation_steps = X_validation.shape[0]//BATCH_SIZE
tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
history = model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=EPOCHS,
validation_data=validation_generator, validation_steps=validation_steps,
shuffle=True, callbacks=[tensorboard])
score = model.evaluate(test['features'], to_categorical(test['labels']))
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# Plot training & validation accuracy values
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
"""# **Confusion Matrix**"""
#Confusion Matrix on training:
from sklearn.metrics import confusion_matrix
y_pred = model.predict_classes(X_train)
rounded_y_train=np.argmax(y_train, axis=1)
CM_train = confusion_matrix(rounded_y_train, y_pred )
print(CM_train)
#Confusion Matrix on validation:
from sklearn.metrics import confusion_matrix
y_pred_test = model.predict_classes(X_validation)
rounded_y_test=np.argmax(y_validation, axis=1)
CM_test = confusion_matrix(rounded_y_test, y_pred_test )
print(CM_test)