-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_compare.py
303 lines (236 loc) · 9.75 KB
/
test_compare.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
290
291
292
293
294
295
296
297
298
299
# coding: utf-8
# In[60]:
import matplotlib.pyplot as plt
import os
import glob
import h5py
import numpy as np
import scipy.io as sio
import cv2
import pickle
import time
import random
import sys
from collections import namedtuple as nt
def sample_result_image_name(count):
return str(("%04d") % count) + '.jpg'
# In[61]:
os.environ['KERAS_BACKEND'] = 'tensorflow'
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# In[62]:
from keras.models import Sequential
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.models import Model
from keras.models import model_from_json
from keras.layers import Input
from keras.utils import data_utils, plot_model
from keras import backend as K
from keras.models import model_from_json
from keras.callbacks import TensorBoard
K.clear_session()
# In[63]:
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "0"
set_session(tf.Session(config=config))
TYNAMO_HOME_DIR = '/nfs/tynamo/home/data/vision7/gdhody/chrono/'
BLITZLE_HOME_DIR = '/nfs/blitzle/home/data/vision5/gdhody/chrono/'
HOME_DIR = TYNAMO_HOME_DIR
# In[65]:
EPOCH = str(80)
BATCH_SIZE = 128
LR, LR_DECAY = 0.01, 0.0000625
LOSS = 'mean_squared_error'
MODEL_NAME = 'URLSS'
MODEL_PATH = os.path.join(HOME_DIR, 'models', MODEL_NAME, 'model.json')
MODEL_WEIGHTS = os.path.join(HOME_DIR, 'models', MODEL_NAME, 'weights', 'EPOCH_' + EPOCH + '.h5')
HDF5_PATH = os.path.join(HOME_DIR, 'patch.hdf5')
PICKLE_PATH = os.path.join(HOME_DIR, 'patch.pkl')
# In[66]:
# %run model_and_callback.ipynb
# %run patch_DataLoad_Binary.ipynb
from compare_model_and_callback import lstm_model, EpochCallback
from patch_DataLoad_Binary_test import ChronoDataSet
# In[67]:
model_input, model_output = lstm_model(
input_shape=(4, 80, 80, 3),
resnet=False,
dense_feature_to_compare=0,
drop_feature=0,
compare_layer=True,
compare_layer_dense=512,
drop_compare_dense=0,
lstm_cells=[(0, True, 'sum')],
many_to_many=False,
dense_layer_after_lstm=0,
multi_output=False,
number_of_classes=1,
)
model = Model(inputs=[model_input], outputs=[model_output])
# loaded_model_json = None
# with open(MODEL_PATH, 'r') as f:
# loaded_model_json = f.read()
# model = model_from_json(loaded_model_json)
# # load weights into new model
output_layer_name = model.layers[-1].name
print model.summary()
sgd = SGD(lr=LR, decay=LR_DECAY, momentum=0.9, nesterov=True)
model.compile(
loss={output_layer_name : LOSS},
optimizer=sgd,
metrics=['accuracy']
)
# In[70]:
model.load_weights(MODEL_WEIGHTS, by_name=True)
print "Weights from", str(EPOCH), "epoch Loaded", MODEL_WEIGHTS
# In[ ]:
print "TESTING"
print model.metrics_names
test_generator = ChronoDataSet(
PICKLE_PATH,
HDF5_PATH,
batch_size=BATCH_SIZE,
start_epoch=0,
mode='TEST',
dataset='UCF'
)
print "Test Data Generator Ready"
print model.evaluate_generator(
generator=test_generator,
steps=test_generator.getBatchRunsPerEpoch(),
max_queue_size=9,
workers=3,
use_multiprocessing=True
)
# In[74]:
# '''
# Output File Code
# '''
# TRAIN_UCF, TRAIN_HMDB = None, None
# with open(PICKLE_PATH, 'rb') as f:
# TRAIN_UCF, TRAIN_HMDB = pickle.load(f)
# print "Dataset Opened"
# train_samples_ucf, validation_samples_ucf = (70 * len(TRAIN_UCF)) / 100, len(TRAIN_UCF) / 10
# test_samples_ucf = len(TRAIN_UCF) - train_samples_ucf - validation_samples_ucf
# TEST_DATA = TRAIN_UCF[train_samples_ucf + validation_samples_ucf:]
# test_generator = ChronoDataSet(
# PICKLE_PATH,
# HDF5_PATH,
# batch_size=BATCH_SIZE,
# start_epoch=0,
# mode='TEST',
# dataset='UCF'
# )
# ITERAATIONS = test_generator.getBatchRunsPerEpoch()
# TRUE, FALSE = [], []
# for idx in xrange(ITERAATIONS):
# image_batch = test_generator.__getitem__(idx)
# true_false = np.squeeze(model.predict_on_batch(image_batch[0])) >= 0.5
# assert len(true_false) == len(TEST_DATA[idx * BATCH_SIZE:(idx+1) * BATCH_SIZE])
# for value, data in zip(true_false, TEST_DATA[idx * BATCH_SIZE:(idx+1) * BATCH_SIZE]):
# if value:
# TRUE.append(data)
# continue
# FALSE.append(data)
# # if len(TRUE) == 10:
# # break
# POS_IMG_SAMPLES = os.path.join(HOME_DIR, 'image_results', 'pos')
# NEG_IMG_SAMPLES = os.path.join(HOME_DIR, 'image_results', 'neg')
# if not os.path.exists(POS_IMG_SAMPLES):
# os.makedirs(POS_IMG_SAMPLES)
# if not os.path.exists(NEG_IMG_SAMPLES):
# os.makedirs(NEG_IMG_SAMPLES)
# print "Finished finding results", len(TRUE), len(FALSE)
# file_no_per_class = {}
# correct_per_class = {}
# for index, pos_sample in enumerate(TRUE):
# if index and not index % 500:
# print "Correct processed", index, '|', len(TRUE)
# im_file_name, crop, frames = pos_sample
# image_class = im_file_name.split('/')[-1].split('_')[1].upper()
# if np.random.randint(5) - 4 == 0:
# im_file_paths = [os.path.join(HOME_DIR, im_file_name, 'Image' + str(frame) + '.jpg') for frame in frames]
# ims = [cv2.imread(im_path) for im_path in im_file_paths]
# save_image = np.zeros((ims[0].shape[0], ims[0].shape[1] * 4, 3))
# save_image[:,:ims[0].shape[1],:] = ims[0]
# save_image[:,ims[0].shape[1]:ims[0].shape[1]*2,:] = ims[1]
# save_image[:,ims[0].shape[1]*2:ims[0].shape[1]*3,:] = ims[2]
# save_image[:,ims[0].shape[1]*3:ims[0].shape[1]*4,:] = ims[3]
# if image_class not in file_no_per_class:
# file_no_per_class[image_class] = 1
# save_image_path = os.path.join(POS_IMG_SAMPLES, image_class)
# if not os.path.exists(save_image_path):
# os.makedirs(save_image_path)
# save_image_path = os.path.join(save_image_path, sample_result_image_name(file_no_per_class[image_class]))
# cv2.imwrite(save_image_path, save_image)
# file_no_per_class[image_class] += 1
# if image_class not in correct_per_class:
# correct_per_class[image_class] = 0
# correct_per_class[image_class] += 1
# print "Correct Samples stored"
# file_no_per_class = {}
# wrong_per_class = {}
# for index, pos_sample in enumerate(FALSE):
# if index and not index % 500:
# print "Wrong processed", index, '|', len(FALSE)
# im_file_name, crop, frames = pos_sample
# image_class = im_file_name.split('/')[-1].split('_')[1].upper()
# if np.random.randint(5) - 4 == 0:
# im_file_paths = [os.path.join(HOME_DIR, im_file_name, 'Image' + str(frame) + '.jpg') for frame in frames]
# ims = [cv2.imread(im_path) for im_path in im_file_paths]
# save_image = np.zeros((ims[0].shape[0], ims[0].shape[1] * 4, 3))
# save_image[:,:ims[0].shape[1],:] = ims[0]
# save_image[:,ims[0].shape[1]:ims[0].shape[1]*2,:] = ims[1]
# save_image[:,ims[0].shape[1]*2:ims[0].shape[1]*3,:] = ims[2]
# save_image[:,ims[0].shape[1]*3:ims[0].shape[1]*4,:] = ims[3]
# if image_class not in file_no_per_class:
# file_no_per_class[image_class] = 1
# save_image_path = os.path.join(NEG_IMG_SAMPLES, image_class)
# if not os.path.exists(save_image_path):
# os.makedirs(save_image_path)
# save_image_path = os.path.join(save_image_path, sample_result_image_name(file_no_per_class[image_class]))
# cv2.imwrite(save_image_path, save_image)
# file_no_per_class[image_class] += 1
# if image_class not in wrong_per_class:
# wrong_per_class[image_class] = 0
# wrong_per_class[image_class] += 1
# print "Wrong Samples stored"
# plot = {}
# for k,v in correct_per_class.items():
# if k in wrong_per_class:
# plot[k] = [v, wrong_per_class[k]]
# else:
# plot[k] = [v, 0]
# with open('store_test.pkl', 'wb') as f:
# pickle.dump([plot], f, pickle.HIGHEST_PROTOCOL)
# # data to plot
# n_groups = len(plot)
# correct_bar = map(lambda x : x[1][0], plot.items())
# wrong_bar = map(lambda x : x[1][1], plot.items())
# # create plot
# fig = plt.figure()
# ax = plt.subplot(111)
# index = np.arange(n_groups)
# bar_width = 0.35
# opacity = 0.8
# rects1 = ax.bar(index, correct_bar, bar_width,
# alpha=opacity,
# color='r',
# label='Wrong')
# rects2 = ax.bar(index + bar_width, wrong_bar, bar_width,
# alpha=opacity,
# color='g',
# label='Correct')
# print "Plotted graphs for both correct and wrong"
# ax.xlabel('Class')
# ax.ylabel('Samples')
# ax.title('Correct Wrong')
# ax.xticks(index + bar_width, tuple(plot.keys()))
# ax.legend()
# fig.savefig('abc.jpg')
'''
Output File Code Finish
'''