-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.py
61 lines (48 loc) · 1.75 KB
/
program.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
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras.datasets import mnist
from keras.utils import np_utils as npu
from keras.backend import clear_session
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train.shape
img = x_train[0].shape
print("size of image",img)
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28,28 , 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
y_train = npu.to_categorical(y_train)
y_test = npu.to_categorical(y_test)
model = Sequential()
kernel = 8
filter = 3
pool = 2
model.add(Conv2D(kernel, (filter,filter), input_shape = (28, 28, 1), activation = 'relu'))
model.add(MaxPooling2D(pool_size =(pool,pool)))
model.add(Flatten())
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))
model.compile( optimizer= "Adam" , loss='categorical_crossentropy', metrics=['accuracy'] )
print(model.summary())
batch_size = 128
epochs = 1
history = model.fit(x_train, y_train,
batch_size=batch_size,verbose=1,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True)
model.save("mnist_LeNet.h5")
# Evaluate the performance of our trained model
scores = model.evaluate(x_test, y_test, verbose=False)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
accuracy = scores[1]
f= open("accuracy.txt","w+")
f.write(str(accuracy))
f.close()
print("Accuracy is : " , accuracy ,"%")