-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMnist_dffnn.py
158 lines (117 loc) · 4.42 KB
/
Mnist_dffnn.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
158
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 14 00:17:58 2019
@author: sidistic
"""
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 21:58:35 2019
@author: sidistic
"""
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 21:58:35 2019
@author: sidistic
"""
import os
import keras
from keras.datasets import mnist
#from mnist import MNIST
import matplotlib.pyplot as plt
from keras.models import Sequential
import numpy as np
from sklearn.utils import shuffle
import h5py
#from __future__ import print_function
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
###############################################################################################################3
# input image dimensions
img_rows, img_cols = 28, 28
cl1, cl2 = 0, 8
#mndata = MNIST('/home/sidistic/MNIST_data')
###############################################################################################################
#images, labels = mndata.load_training()
(x_train, y_train), (x_test, y_test) = mnist.load_data()
###############################################################################################################33
i, = np.where(y_train == cl1)
j, = np.where(y_train == cl2)
cl1_train=x_train[i,:,:]
cl1_label=y_train[i]
cl2_train=x_train[j,:,:]
cl2_label=y_train[j]
train_com = np.concatenate((cl1_train,cl2_train),axis=0)
train_lab=np.concatenate((cl1_label,cl2_label),axis=0)
[train_sff,train_labs]=shuffle(train_com,train_lab)
fig = plt.figure()
for i in range(16):
plt.subplot(4,4,i+1)
plt.tight_layout()
plt.imshow(train_sff[i], cmap='gray', interpolation='none')
plt.title("Digit: {}".format(train_labs[i]))
plt.xticks([])
plt.yticks([])
fig
np.place(train_labs, train_labs==cl1, [0])
np.place(train_labs, train_labs==cl2, [1])
############################################################################################################
train_labs_cat = keras.utils.to_categorical(train_labs, 2)
#
train_sff = train_sff.astype('float32')
train_sff /= 255
ftrain_sff=train_sff.reshape(train_labs_cat.shape[0],img_rows*img_cols)
################################################################################################################
model = Sequential()
model.add(Dense(1024, input_dim=784, activation='sigmoid'))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(256, activation='sigmoid'))
model.add(Dense(64, activation='sigmoid'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model_log=model.fit(ftrain_sff, train_labs_cat, epochs=30, batch_size=200, validation_split=0.10)
###############################################################################################################
i, = np.where(y_test == cl1)
j, = np.where(y_test == cl2)
cl1_test=x_test[i,:,:]
cl1_label=y_test[i]
cl2_test=x_test[j,:,:]
cl2_label=y_test[j]
test_com = np.concatenate((cl1_test,cl2_test),axis=0)
test_lab=np.concatenate((cl1_label,cl2_label),axis=0)
np.place(test_lab, test_lab==cl1, [0])
np.place(test_lab, test_lab==cl2, [1])
test_com = test_com.astype('float32')
test_com /= 255
ftest_com=test_com.reshape(test_lab.shape[0],img_rows*img_cols)
test_lab_cat = keras.utils.to_categorical(test_lab, 2)
score = model.evaluate(ftest_com, test_lab_cat, verbose=1)
########################################################################################################################
inp = model.input
outputs = [model.layers[3].output]
functor = K.function([inp]+ [K.learning_phase()], outputs )
layer_outs = functor([ftest_com, 0.])
####################################################################################################################
####################################################################################################################
print('Test loss:', score[0])
print('Test accuracy:', score[1])# plotting the metrics
model.summary()
fig = plt.figure()
plt.subplot(2,1,1)
plt.plot(model_log.history['acc'])
plt.plot(model_log.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='lower right')
plt.subplot(2,1,2)
plt.plot(model_log.history['loss'])
plt.plot(model_log.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.tight_layout()
fig
#i, = np.where(y_test == 1)
#j, = np.where(y_test == 8)