-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathloadData.py
More file actions
executable file
·235 lines (176 loc) · 8.96 KB
/
loadData.py
File metadata and controls
executable file
·235 lines (176 loc) · 8.96 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
"""
The main file for load models
author: Xiaowei Huang
"""
import sys
sys.path.append('networks')
sys.path.append('safety_check')
sys.path.append('adversary_generation')
import time
import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
from pylab import *
# keras
from keras.models import Model, Sequential
from keras.layers import Input, Dense
import keras.optimizers
# visualisation
from keras.utils.visualize_util import plot
#
from keras.datasets import mnist
from keras.utils import np_utils
# for training cifar10
from keras.preprocessing.image import ImageDataGenerator
from configuration import *
# training the model from data
# or read the model from saved data file
# then start analyse the model
def loadData():
# construct model according to the flage
# whichMode == "read" read from saved file
# whichMode == "train" training from the beginning
if whichMode == "train" and dataset == "mnist":
(X_train, Y_train, X_test, Y_test, batch_size, nb_epoch) = NN.read_mnist_dataset()
print "Building network model ......"
model = NN.build_mnist_model()
start_time = time.time()
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print("Fitting time: --- %s seconds ---" % (time.time() - start_time))
print("Training finished!")
# save model
json_string = model.to_json()
open('%s/mnist.json'%(directory_model_string), 'w').write(json_string)
model.save_weights('%a/mnist.h5'%directory_model_string, overwrite=True)
sio.savemat('%s/mnist.mat'%directory_model_string, {'weights': model.get_weights()})
print("Model saved!")
elif whichMode == "read" and dataset == "mnist":
print("Start loading model ... ")
model = NN.read_model_from_file('%s/mnist.mat'%directory_model_string,'%s/mnist.json'%directory_model_string)
print("Model loaded!")
#test(model)
elif whichMode == "train" and dataset == "cifar10":
(X_train,Y_train,X_test,Y_test, img_channels, img_rows, img_cols, batch_size, nb_classes, nb_epoch, data_augmentation) = NN.read_dataset()
print "Building network model ......"
model = NN.build_model(img_channels, img_rows, img_cols, nb_classes)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
start_time = time.time()
if not data_augmentation:
print('Not using data augmentation.')
model.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test),
shuffle=True)
else:
print('Using real-time data augmentation.')
# this will do preprocessing and realtime data augmentation
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)
# fit the model on the batches generated by datagen.flow()
model.fit_generator(datagen.flow(X_train, Y_train,
batch_size=batch_size),
samples_per_epoch=X_train.shape[0],
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print("Fitting time: --- %s seconds ---" % (time.time() - start_time))
print("Training finished!")
# save model
json_string = model.to_json()
open('%s/cifar10.json'%directory_model_string, 'w').write(json_string)
model.save_weights('%s/cifar10.h5'%directory_model_string, overwrite=True)
sio.savemat('%s/cifar10.mat'%directory_model_string, {'weights': model.get_weights()})
print("Model saved!")
elif whichMode == "read" and dataset == "cifar10":
print("Start loading model ... ")
(X_train,Y_train,X_test,Y_test, img_channels, img_rows, img_cols, batch_size, nb_classes, nb_epoch, data_augmentation) = NN.read_dataset()
model = NN.read_model_from_file(img_channels, img_rows, img_cols, nb_classes, '%s/cifar10.mat'%directory_model_string,'%s/cifar10.json'%directory_model_string)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
print("Model loaded!")
elif whichMode == "train" and dataset == "imageNet":
(img_channels, img_rows, img_cols, batch_size, nb_classes, nb_epoch, data_augmentation) = NN.read_dataset()
print "Building network model ......"
model = NN.build_model(img_channels, img_rows, img_cols, nb_classes)
# load weights
model.load_weights('imageNet/imageNet.h5')
# save model
json_string = model.to_json()
open('%s/imageNet.json'%directory_model_string, 'w').write(json_string)
model.save_weights('%s/imageNet.h5'%directory_model_string, overwrite=True)
sio.savemat('%s/imageNet.mat'%directory_model_string, {'weights': model.get_weights()})
print("Model saved!")
elif whichMode == "read" and dataset == "imageNet":
print("Start loading model ... ")
(img_channels, img_rows, img_cols, batch_size, nb_classes, nb_epoch, data_augmentation) = NN.read_dataset()
model = NN.read_model_from_file(img_channels, img_rows, img_cols, nb_classes, '%s/imageNet.mat'%directory_model_string,'%s/imageNet.json'%directory_model_string)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
print("Model loaded!")
elif whichMode == "train" and dataset == "twoDcurve":
# define and construct model
# load data
N_samples = 5000
N_tests = 1000
x_train, y_train, x_test, y_test = NN.load_data(N_samples,N_tests)
print "Building network model ......"
model = NN.build_model()
plot(model, to_file='twoDcurve_pic/model.png')
# visualisation
# configure learning process
sgd = keras.optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss={'output': 'mse'})
model.summary()
start_time = time.time()
model.fit({'data': x_train}, {'output': y_train}, nb_epoch=3000, validation_split=0.1, verbose=0)
print("Fitting time: --- %s seconds ---" % (time.time() - start_time))
print("Training finished!")
# save model
json_string = model.to_json()
open('%s/MLP.json'%directory_model_string, 'w').write(json_string)
model.save_weights('%s/MLP.h5'%directory_model_string, overwrite=True)
sio.savemat('%s/MLP.mat'%directory_model_string, {'weights': model.get_weights()})
print("Model saved!")
elif whichMode == "read" and dataset == "twoDcurve":
print("Start loading model ... ")
model = NN_twoDcurve.read_model_from_file('%s/MLP.mat'%directory_model_string,'%s/MLP.json'%directory_model_string)
#model.summary()
print("Model loaded!")
return (model)
"""
validate the model by the test data from the package
"""
def test(model):
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_test = X_test.astype('float32')
X_test = X_test.astype('float32')
X_test /= 255
Y_test = np_utils.to_categorical(y_test, nb_classes)
print("Start testing model ... ")
# prediction after training
start_time = time.time()
y_predicted = model.predict(X_test)
print y_predicted
print("Testing time: --- %s seconds ---" % (time.time() - start_time))