-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_autoencoder_1.py
178 lines (135 loc) · 5.07 KB
/
mnist_autoencoder_1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import print_function
import os
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD, RMSprop, Adam, Adagrad, Adadelta, Adamax, Nadam
from keras import backend as K
from keras.utils import np_utils
from keras.models import Model
from keras import backend as K
K.set_image_dim_ordering('tf')
np.set_printoptions(precision=4)
#from sklearn.cross_validation import train_test_split #will remove it
batch_size = 32
num_classes = 4
# epochs = 20
#epochs = 5
img_rows = 20
img_cols = 10
num_channel = 1
num_epoch = 5
# the data, shuffled and split between train and test sets
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
#x_train = x_train.reshape(60000, 784)
#x_test = x_test.reshape(10000, 784)
#x_train = x_train.astype('float32')
#x_test = x_test.astype('float32')
#x_train /= 255
#x_test /= 255
#print('x_train shape:', x_train.shape)
#print('x_test shape:', x_test.shape)
#num_files = 8800*num_classes
Dir1 = '/users/hpcusers/dumpCaloCells/images2'
data_path = Dir1 + 'TrainingData/'
names = ['electronelectron', 'muonmuon', 'tautau']
img_data_list = []
labels = []
# for name in names:
for labelID in [0, 1, 2]:
name = names[labelID]
for img_ind in range(num_files / num_classes):
fileIn = data_path + '/' + name + '/' + name + str(img_ind) + '.data'
input_img = np.load(fileIn)
if np.isnan(input_img).any():
print (labelID, img_ind, ' -- ERROR: NaN')
else:
img_data = np.resize(input_img,(20,10,2))
img_data_list.append(input_img[:,:,0])
img_data_list.append(input_img[:,:,1])
labels.append(labelID)
img_data = np.array(img_data_list)
#img_data = img_data.astype('float32')
#img_data = 255.*(img_data - img_data.min()) / (img_data.max() - img_data.min())
#img_data /= 255
print (img_data.shape)
'''
if num_channel == 1:
if K.image_dim_ordering() == 'th':
img_data = np.expand_dims(img_data, axis=1)
print (img_data.shape)
else:
img_data = np.expand_dims(img_data, axis=4)
print (img_data.shape)
else:
if K.image_dim_ordering() == 'th':
img_data = np.rollaxis(img_data, 3, 1)
print (img_data.shape)
'''
#X= img_data
#y=np_utils.to_categorical(labels, num_classes)
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4) #will remove it
X_train = img_data
y_train = np_utils.to_categorical(labels, num_classes)
np.random.seed(123)
shuffleOrder = np.arange(X_train.shape[2])
np.random.shuffle(shuffleOrder)
X_train = X_train[shuffleOrder]
y_train = y_train[shuffleOrder]
print(X_train.shape, 'train samples shape')
print(y_train.shape, 'test samples shape')
input_shape = X_train[0].shape
# convert class vectors to binary class matrices
# y_train = keras.utils.to_categorical(y_train, num_classes)
# y_test = keras.utils.to_categorical(y_test, num_classes)
# %matplotlib inline
# this is the size of our encoded representations
encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
# this is our input placeholder
input_img = Input(shape=(200,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(200, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input=input_img, output=decoded)
print("autoencoder model created")
# this model maps an input to its encoded representation
encoder = Model(input=input_img, output=encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
autoencoder.summary()
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
history = autoencoder.fit(X_train, X_train,
batch_size=batch_size, epochs=num_epoch,
verbose=1, validation_split=0.2)
# encode and decode some digits
# note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
n = 10 # how many digits we will display
fig = plt.figure(figsize=(20, 4))
for i in range(10):
# display original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# plt.show()
fig.savefig("autoencoded_digits.png")