Skip to content

Commit c4ce27d

Browse files
authored
Create MNIST.py
1 parent 5ec30fd commit c4ce27d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

MNIST.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#================================================================================================================#
2+
# Training and Validation
3+
#================================================================================================================#
4+
5+
# Libraries needed:
6+
7+
# - numpy (array)
8+
# - matplotlib (plotting)
9+
# - keras (deep learning) - image, convolution, maxpool, flatten, dense, models
10+
# - os (directory reading)
11+
12+
# `image` = read images
13+
# `model` = compile everything as a model so next time can use
14+
# `flow_from_directory` = read image >> convert to array >> concatenate all images into big array
15+
16+
#----------------------------------------------------------------------------------------------------------------#
17+
18+
import numpy as np
19+
from matplotlib import pyplot as plt
20+
import os
21+
from keras.preprocessing import image
22+
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense
23+
from keras.models import Model
24+
from keras.layers import Input # function of determining the input size in the first layer
25+
26+
directory = '../input/mnistasjpg/trainingSet/trainingSet/'
27+
28+
# image reader
29+
datagen = image.ImageDataGenerator(rescale = 1./255, validation_split = 0.2)
30+
train_set = datagen.flow_from_directory(directory, target_size = (100, 100), batch_size = 16,
31+
class_mode = 'categorical', subset = 'training')
32+
val_set = datagen.flow_from_directory(directory, target_size = (100, 100), batch_size = 16,
33+
class_mode = 'categorical', subset = 'validation')
34+
35+
#----------------------------------------------------------------------------------------------------------------#
36+
37+
def network(nb_class, inputsize): # nb_class = 10, input = (100, 100, 3)
38+
input_img = Input(shape = inputsize)
39+
x = Conv2D(16, (3,3), strides = (1,1), activation = 'relu', padding = 'same', name = 'gkm_conv1')(input_img)
40+
# 1st - number of filters, 2nd - filter size
41+
x = MaxPool2D((3,3), strides = (2,2), padding = 'same', name = 'gkm_maxpool1')(x)
42+
x = Conv2D(32, (3,3), strides = (1,1), activation = 'relu', padding = 'same', name = 'gkm_conv2')(x)
43+
x = MaxPool2D((3,3), strides = (2,2), padding = 'same', name = 'gkm_maxpool2')(x)
44+
x = Flatten(name = 'flatten')(x)
45+
x = Dense(100, activation = 'relu')(x)
46+
x = Dense(nb_class, activation = 'softmax')(x)
47+
model = Model(input_img, x)
48+
return model
49+
50+
model = network(nb_class = 10, inputsize = (100,100,3))
51+
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
52+
model.summary()
53+
54+
# Save the model
55+
model.save('SimpleDL.h5')
56+
57+
#----------------------------------------------------------------------------------------------------------------#
58+
59+
# training and validation
60+
hist = model.fit(train_set, epochs = 20, steps_per_epoch = 25, validation_data = val_set, verbose = 1)
61+
62+
# plot training loss and validation loss
63+
plt.plot(hist.history['loss'])
64+
plt.plot(hist.history['val_loss'])
65+
plt.legend(['training loss', 'validation loss'], loc = 'upper right')
66+
67+
# plot training accuracy and validation accuracy
68+
plt.plot(hist.history['accuracy'])
69+
plt.plot(hist.history['val_accuracy'])
70+
plt.legend(['accuracy', 'validation accuracy'], loc = 'lower right')
71+
72+
73+
#================================================================================================================#
74+
# Testing
75+
#================================================================================================================#
76+
77+
import cv2 # open cv (read images)
78+
79+
# read the picture to be tested
80+
direct = '../input/mnistasjpg/testSet/testSet/img_10.jpg'
81+
img = cv2.imread(direct)
82+
img = cv2.resize(img, (100, 100))
83+
data = np.array(img)/255
84+
85+
# DL trains in 4 dimensions, so data reshaping is needed
86+
data = data.reshape(1, 100, 100, 3)
87+
data.shape
88+
89+
# Get the probability that this image is belong to each class
90+
result = model.predict(data)
91+
result.sum() # this is equal to 1
92+
93+
# take the largest probability among 'result'
94+
output = np.argmax(result)
95+
print(output)
96+
97+
98+
99+

0 commit comments

Comments
 (0)