-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathXception_eval.py
289 lines (248 loc) · 10 KB
/
Xception_eval.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import print_function
import numpy as np
import tensorflow as tf
import h5py
import gc
#from keras.applications.xception import Xception
# from keras.models import Sequential
# from keras.layers import advanced_activations
# from keras.layers.pooling import MaxPooling2D
#from keras.optimizers import RMSprop, Adadelta, Adam
#from keras.layers.convolutional import Convolution2D
#from keras.layers.core import Dense, Activation, Dropout, Flatten
#from keras.layers import Conv2D, MaxPooling2D
#from keras.layers.wrappers import TimeDistributed
from keras.utils import np_utils
#from keras import backend as K
# from keras import optimizers
#from keras.preprocessing.image import ImageDataGenerator
# import Dataset_loader
# from keras.callbacks import ModelCheckpoint
from keras.models import load_model
# from keras.backend.tensorflow_backend import dtype
from keras.utils.io_utils import HDF5Matrix
from Dataset_loader import getNumSamples
from scipy import misc
# from AlexNet import y_tr, y_t
# Parameters
DATASET_COMMON_FOLDER = '/home/alex/Datasets/Yuan/'
NET_NAME = 'Xception'
BATCH_SIZE = 10
EPOCHS = 10
NUMBER_OF_CLASSES = 2
INPUT_FRAME_SIZE = 299
def generate_arrays(train_filename, batch_size, max_sample, new_size):
batch_features = np.zeros((batch_size, new_size, new_size, 3))
batch_labels = np.zeros((batch_size,1))
current_sample_idx = 0
combined_num = 0
print('GENERATOR: Train file = {}, batch = {}, total samples = {}'.format(train_filename, batch_size, max_sample))
while 1:
reached_end = False
start_idx = current_sample_idx
end_idx = batch_size + start_idx
if (end_idx > max_sample):
end_idx = batch_size
reached_end = True
print('GENERATOR: Start idx = {}, end_idx = {}, total samples = {}'.format(start_idx, end_idx, max_sample))
x = HDF5Matrix(train_filename, 'data', start=start_idx, end=end_idx)
y = HDF5Matrix(train_filename, 'labels', start=start_idx, end=end_idx)
x = np.array(x)
y = np.array(y)
y = np_utils.to_categorical(y, NUMBER_OF_CLASSES)
current_sample_idx = end_idx
if reached_end:
current_sample_idx = 0
print("Shapes. x = {}, y = {}".format(x.shape, y.shape))
#batch_labels = np_utils.to_categorical(batch_labels, NUMBER_OF_CLASSES)
yield(x,y)
print("Starting Xception")
variant1 = ['set1_{}.h5'.format(INPUT_FRAME_SIZE), 'set2_{}.h5'.format(INPUT_FRAME_SIZE)]
variant2 = ['set2_{}.h5'.format(INPUT_FRAME_SIZE), 'set1_{}.h5'.format(INPUT_FRAME_SIZE)]
variant3 = ['set3_{}.h5'.format(INPUT_FRAME_SIZE), 'set4_{}.h5'.format(INPUT_FRAME_SIZE)]
variant4 = ['set4_{}.h5'.format(INPUT_FRAME_SIZE), 'set3_{}.h5'.format(INPUT_FRAME_SIZE)]
variants = []
variants.append(variant1)
# variants.append(variant2)
# variants.append(variant3)
# variants.append(variant4)
for num_variant in range(len(variants)):
TRAIN_SET = DATASET_COMMON_FOLDER + variants[num_variant][0]
TEST_SET = DATASET_COMMON_FOLDER + variants[num_variant][1]
print('Frame size = {}'.format(INPUT_FRAME_SIZE))
print('Workings with sets: {} and {}'.format(TRAIN_SET, TEST_SET))
# # Load dataset
# x_tr_temp = HDF5Matrix(TRAIN_SET, 'data')
# x_train = np.array(x_tr_temp)
# del x_tr_temp
# gc.collect()
#
#
# y_tr_temp = HDF5Matrix(TRAIN_SET, 'labels')
# y_tr = np.array(y_tr_temp)
# del y_tr_temp
# gc.collect()
#
# y_train = np_utils.to_categorical(y_tr, NUMBER_OF_CLASSES)
# del y_tr
# gc.collect()
#
#
# x_test_temp = HDF5Matrix(TEST_SET, 'data')
# x_test = np.array(x_test_temp)
# del x_test_temp
# gc.collect()
#
# y_t_temp = HDF5Matrix(TEST_SET, 'labels')
# y_t = np.array(y_t_temp)
# del y_t_temp
# gc.collect()
#
# y_test = np_utils.to_categorical(y_t, NUMBER_OF_CLASSES)
# del y_t
# gc.collect()
#
#
#
# total_samples_test = getNumSamples(variants[num_variant][1][0:4]+'.h5')
#
# #x_test = np.zeros((total_samples_test, INPUT_FRAME_SIZE, INPUT_FRAME_SIZE, 3), dtype='float16')
# #y_test = np.zeros((total_samples_test,1), dtype='float16')
#
#
# print('Test dataset loaded')
#
# print('Testing dataset size = ', x_test.shape)
#
# print('Loading model')
# model = Xception(weights=None, include_top=True, classes=NUMBER_OF_CLASSES)
# rms_prop = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
# model.compile(loss='categorical_crossentropy',
# optimizer=rms_prop,
# metrics=['accuracy', 'mse'])
# print("Model loaded")
#
# # autosave best Model
best_model_file = '{}_{}_{}_B{}_E{}_F{}.h5'.format(NET_NAME, TRAIN_SET[-11:-7], TEST_SET[-11:-7], BATCH_SIZE, EPOCHS, INPUT_FRAME_SIZE)
# best_model = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=1, save_best_only=True)
#
# print("*************************************")
# print("Fitting model")
# print("*************************************")
#
# model.fit(x_train, y_train,
# batch_size=BATCH_SIZE,
# epochs=EPOCHS,
# verbose=1,
# callbacks=[best_model], # this callback can be de-activated
# validation_data=(x_test, y_test))
#
# # 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, # randomly shift images horizontally (fraction of total width)
# # height_shift_range=0, # randomly shift images vertically (fraction of total height)
# # horizontal_flip=False, # randomly flip images
# # vertical_flip=False) # randomly flip images
# #
# # # Compute quantities required for feature-wise normalization
# # # (std, mean, and principal components if ZCA whitening is applied).
# # datagen.fit(x_train)
#
# # total_samples_train = getNumSamples(variants[num_variant][0][0:4]+'.h5')
# # model.fit_generator(generate_arrays(TRAIN_SET,
# # batch_size=BATCH_SIZE,
# # max_sample=total_samples_train,
# # new_size=INPUT_FRAME_SIZE),
# # BATCH_SIZE, EPOCHS,
# # verbose=2,
# # callbacks=[best_model],
# # validation_data=(x_test, y_test))
# print("Finished fitting model")
# score = model.evaluate(x_test, y_test, verbose=1)
# print('Test loss:', score[0])
# print('Test accuracy:', score[1])
# print('All metrics', score)
#
#x_train = HDF5Matrix(TRAIN_SET, 'data')
#y_train = HDF5Matrix(TRAIN_SET, 'labels')
# res = model.predict(x_test)
# res_label = np.argmax(res,1)
# print('\ntest:', sum(res_label==y_t)/float(len(y_t))*100)
#
#
# res = model.predict(x_train)
# res_label = np.argmax(res,1)
# print('train:', sum(res_label==y_tr)/float(len(y_tr))*100)
# print("Update '{}'".format(best_model_file))
# with h5py.File(best_model_file, 'a') as f:
# if 'optimizer_weights' in f.keys():
# del f['optimizer_weights']
print('result on best model')
print('Loading the best model...')
model = load_model(best_model_file)
print('Best Model loaded!')
from keras.utils import plot_model
plot_model(model, to_file='model.png')
# print('Loading test dataset')
# x_test_temp = HDF5Matrix(TEST_SET, 'data')
# print('Copying to Numpy array')
# x_test = np.array(x_test_temp)
# print('Deleting temp data')
# del x_test_temp
# print('GC!')
# gc.collect()
# print('Loading labels')
# y_t_temp = HDF5Matrix(TEST_SET, 'labels')
# print('Copying to Numpy array')
# y_t = np.array(y_t_temp)
# print('Deleting temp data')
# del y_t_temp
# print('GC!')
# gc.collect()
# print('Predicting')
# res = model.predict(x_test)
# res_label = np.argmax(res,1)
# acc_test = sum(res_label==y_t)/float(len(y_t))*100
# print('test:', sum(res_label==y_t)/float(len(y_t))*100)
# del x_test
# del y_t
# gc.collect()
#
# results_filename = '{}_{}_{}_B{}_E{}_F{}_test.txt'.format(NET_NAME, TRAIN_SET[-11:-7], TEST_SET[-11:-7], BATCH_SIZE, EPOCHS, INPUT_FRAME_SIZE)
# f = open(results_filename, 'wb')
# data = 'Test accuracy = {}'.format(acc_test)
# f.write(data)
# f.close()
#
# print('Loading labels')
# x_train_temp = HDF5Matrix(TRAIN_SET, 'data')
# print('Copying to Numpy array')
# x_train = np.array(x_train_temp)
# print('Deleting temp data')
# del x_train_temp
# print('GC!')
# gc.collect()
# print('Loading labels')
# y_tr_temp = HDF5Matrix(TRAIN_SET, 'labels')
# print('Copying to Numpy array')
# y_tr = np.array(y_tr_temp)
# print('Deleting temp data')
# del y_tr_temp
# print('GC!')
# gc.collect()
# print('Predicting')
# res = model.predict(x_train)
# res_label = np.argmax(res,1)
# acc_train = sum(res_label==y_tr)/float(len(y_tr))*100
# print('train:', sum(res_label==y_tr)/float(len(y_tr))*100)
#
# results_filename = '{}_{}_{}_B{}_E{}_F{}_train.txt'.format(NET_NAME, TRAIN_SET[-11:-7], TEST_SET[-11:-7], BATCH_SIZE, EPOCHS, INPUT_FRAME_SIZE)
# f = open(results_filename, 'wb')
# data = 'Train accuracy = {}'.format(acc_train)
# f.write(data)
# f.close()