1
1
# Basic imports
2
2
import os
3
3
import time
4
+ import sys
4
5
import argparse
5
6
import tensorflow as tf
6
7
import numpy as np
21
22
from utils .logger import Logger
22
23
from random import shuffle
23
24
from utils .load_dataset_tfrecords import load_dataset
25
+ from utils .argument_utils import read_json , assign_args
24
26
25
27
26
28
parser = argparse .ArgumentParser ()
27
29
28
- # Model parameters
30
+ # Argument loading
31
+
32
+ parser .add_argument ('--argsFile' , action = 'store' , type = str , default = 'none' ,
33
+ help = 'The name of the file that contains a model\' s arguments. Also requires --model.' )
29
34
30
35
parser .add_argument ('--model' , action = 'store' , required = True ,
31
- help = 'Model architecture (c3d, i3d, tsn, resnet)' )
36
+ help = 'Model architecture (c3d, tsn, i3d, resnet)' )
37
+
38
+ args_init = parser .parse_known_args ()[0 ]
39
+ model_name = args_init .model
40
+ args_file = args_init .argsFile
41
+ args_json = read_json (model_name , args_file )
42
+ json_keys = args_json .keys ()
43
+
32
44
33
- parser .add_argument ('--inputDims' , action = 'store' , required = True , type = int ,
45
+ # Model parameters
46
+
47
+ parser .add_argument ('--inputDims' , action = 'store' , required = 'inputDims' not in json_keys , type = int ,
34
48
help = 'Input Dimensions (Number of frames to pass as input to the model)' )
35
49
36
- parser .add_argument ('--outputDims' , action = 'store' , required = True , type = int ,
50
+ parser .add_argument ('--outputDims' , action = 'store' , required = 'outputDims' not in json_keys , type = int ,
37
51
help = 'Output Dimensions (Number of classes in dataset)' )
38
52
39
- parser .add_argument ('--seqLength' , action = 'store' , required = True , type = int ,
53
+ parser .add_argument ('--seqLength' , action = 'store' , required = 'seqLength' not in json_keys , type = int ,
40
54
help = 'Number of output frames expected from model' )
41
55
42
56
parser .add_argument ('--modelAlpha' , action = 'store' , type = float , default = 1. ,
59
73
60
74
# Experiment parameters
61
75
62
- parser .add_argument ('--dataset' , action = 'store' , required = True ,
76
+ parser .add_argument ('--dataset' , action = 'store' , required = 'dataset' not in json_keys ,
63
77
help = 'Dataset (UCF101, HMDB51)' )
64
78
65
- parser .add_argument ('--loadedDataset' , action = 'store' , required = True ,
79
+ parser .add_argument ('--loadedDataset' , action = 'store' , required = 'loadedDataset' not in json_keys ,
66
80
help = 'Dataset (UCF101, HMDB51)' )
67
81
82
+ parser .add_argument ('--loadedPreproc' , action = 'store' , type = str , default = 'null' ,
83
+ help = 'The preprocessing of the weights to be loaded.' )
84
+
68
85
parser .add_argument ('--numGpus' , action = 'store' , type = int , default = 1 ,
69
86
help = 'Number of Gpus used for calculation' )
70
87
80
97
parser .add_argument ('--loadedCheckpoint' , action = 'store' , type = int , default = - 1 ,
81
98
help = 'Specify the step of the saved model checkpoint that will be loaded for testing. Defaults to most recent checkpoint.' )
82
99
83
- parser .add_argument ('--size' , action = 'store' , required = True , type = int ,
100
+ parser .add_argument ('--size' , action = 'store' , required = 'size' not in json_keys , type = int ,
84
101
help = 'Input frame size' )
85
102
86
- parser .add_argument ('--expName' , action = 'store' , required = True ,
103
+ parser .add_argument ('--expName' , action = 'store' , required = 'expName' not in json_keys ,
87
104
help = 'Unique name of experiment being run' )
88
105
89
- parser .add_argument ('--numVids' , action = 'store' , required = True , type = int ,
106
+ parser .add_argument ('--numVids' , action = 'store' , required = 'numVids' not in json_keys , type = int ,
90
107
help = 'Number of videos to be used for testing' )
91
108
92
109
parser .add_argument ('--split' , action = 'store' , type = int , default = 1 ,
95
112
parser .add_argument ('--baseDataPath' , action = 'store' , default = '/z/dat' ,
96
113
help = 'Path to datasets' )
97
114
98
- parser .add_argument ('--fName' , action = 'store' , required = True ,
115
+ parser .add_argument ('--fName' , action = 'store' , required = 'fName' not in json_keys ,
99
116
help = 'Which dataset list to use (trainlist, testlist, vallist)' )
100
117
101
118
parser .add_argument ('--clipLength' , action = 'store' , type = int , default = - 1 ,
143
160
parser .add_argument ('--topk' , action = 'store' , type = int , default = 3 ,
144
161
help = 'Integer indication top k predictions made (Default 3)' )
145
162
163
+ parser .add_argument ('--save' , action = 'store' , type = int , default = 1 ,
164
+ help = 'Boolean indicating whether to save any metrics, logs, or results. Used for testing if the code runs.' )
165
+
166
+ parser .add_argument ('--reverse' , action = 'store' , type = int , default = 0 ,
167
+ help = 'Boolean indicating whether reverse videos and classify them as a new action class. 0 all videos are forward, 1 randomly reversed videos, 2 all videos are reversed' )
168
+
146
169
147
170
args = parser .parse_args ()
148
171
172
+ args = assign_args (args , args_json , sys .argv )
173
+
174
+
149
175
if args .verbose :
150
176
print "Setup of current experiments"
151
177
print "\n ############################"
154
180
155
181
# END IF
156
182
183
+ loaded_preproc = args .loadedPreproc
184
+ if loaded_preproc == 'null' :
185
+ loaded_preproc = args .preprocMethod
186
+
157
187
model_name = args .model
188
+ save_bool = args .save
158
189
159
190
model = models_import .create_model_object (modelName = model_name ,
160
191
inputAlpha = args .inputAlpha ,
176
207
verbose = args .verbose )
177
208
178
209
179
- def test (model , input_dims , output_dims , seq_length , size , dataset , loaded_dataset , experiment_name , num_vids , split , base_data_path , f_name , load_model , return_layer , clip_length , video_offset , clip_offset , num_clips , clip_stride , metrics_method , batch_size , metrics_dir , loaded_checkpoint , verbose , gpu_list , preproc_method , random_init , avg_clips , use_softmax , preproc_debugging , topk ):
210
+ def test (model , input_dims , output_dims , seq_length , size , dataset , loaded_dataset , experiment_name , num_vids , split , base_data_path , f_name , load_model , return_layer , clip_length , video_offset , clip_offset , num_clips , clip_stride , metrics_method , batch_size , metrics_dir , loaded_checkpoint , verbose , gpu_list , preproc_method , loaded_preproc , random_init , avg_clips , use_softmax , preproc_debugging , reverse , topk ):
180
211
"""
181
212
Function used to test the performance and analyse a chosen model
182
213
Args:
@@ -206,6 +237,7 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
206
237
:verbose: Boolean to indicate if all print statement should be procesed or not
207
238
:gpu_list: List of GPU IDs to be used
208
239
:preproc_method: The preprocessing method to use, default, cvr, rr, sr, or any other custom preprocessing
240
+ :loaded_preproc: Name of preproc method which was used to train the current model
209
241
:random_init: Randomly initialize model weights, not loading from any files (deafult False)
210
242
:avg_clips: Binary boolean indicating whether to average predictions across clips
211
243
:use_softmax: Binary boolean indicating whether to apply softmax to the inference of the model
@@ -226,7 +258,7 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
226
258
# Load pre-trained/saved model
227
259
if load_model :
228
260
try :
229
- ckpt , gs_init , learning_rate_init = load_checkpoint (model .name , loaded_dataset , experiment_name , loaded_checkpoint , preproc_method )
261
+ ckpt , gs_init , learning_rate_init = load_checkpoint (model .name , loaded_dataset , experiment_name , loaded_checkpoint , loaded_preproc )
230
262
if verbose :
231
263
print 'A better checkpoint is found. The global_step value is: ' + str (gs_init )
232
264
@@ -262,7 +294,7 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
262
294
263
295
# Setting up tensors for models
264
296
# input_data_tensor - [batchSize, inputDims, height, width, channels]
265
- input_data_tensor , labels_tensor , names_tensor = load_dataset (model , 1 , batch_size , output_dims , input_dims , seq_length , size , data_path , dataset , istraining , clip_length , video_offset , clip_offset , num_clips , clip_stride , video_step , preproc_debugging , 0 , verbose )
297
+ input_data_tensor , labels_tensor , names_tensor = load_dataset (model , 1 , batch_size , output_dims , input_dims , seq_length , size , data_path , dataset , istraining , clip_length , video_offset , clip_offset , num_clips , clip_stride , video_step , preproc_debugging , 0 , verbose , reverse = reverse )
266
298
267
299
######### GPU list check block ####################
268
300
@@ -303,28 +335,29 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
303
335
304
336
############################################################################################################################################
305
337
338
+ if save_bool :
339
+ ######################### Logger Setup block ######################################
306
340
307
- ######################### Logger Setup block ######################################
341
+ # Logger setup (Name format: Date, month, hour, minute and second, with a prefix of exp_test)
342
+ log_name = ("exp_test_%s_%s_%s_%s_%s" % ( time .strftime ("%d_%m_%H_%M_%S" ),
343
+ dataset , preproc_method , experiment_name , metrics_method ))
308
344
309
- # Logger setup (Name format: Date, month, hour, minute and second, with a prefix of exp_test)
310
- log_name = ("exp_test_%s_%s_%s_%s_%s" % ( time .strftime ("%d_%m_%H_%M_%S" ),
311
- dataset , preproc_method , experiment_name , metrics_method ))
345
+ curr_logger = Logger (os .path .join ('logs' , model .name , dataset , preproc_method , metrics_dir , log_name ))
346
+ make_dir (os .path .join ('results' ,model .name ))
347
+ make_dir (os .path .join ('results' ,model .name , dataset ))
348
+ make_dir (os .path .join ('results' ,model .name , dataset , preproc_method ))
349
+ make_dir (os .path .join ('results' ,model .name , dataset , preproc_method , experiment_name ))
350
+ make_dir (os .path .join ('results' ,model .name , dataset , preproc_method , experiment_name , metrics_dir ))
312
351
313
- curr_logger = Logger (os .path .join ('logs' , model .name , dataset , preproc_method , metrics_dir , log_name ))
314
- make_dir (os .path .join ('results' ,model .name ))
315
- make_dir (os .path .join ('results' ,model .name , dataset ))
316
- make_dir (os .path .join ('results' ,model .name , dataset , preproc_method ))
317
- make_dir (os .path .join ('results' ,model .name , dataset , preproc_method , experiment_name ))
318
- make_dir (os .path .join ('results' ,model .name , dataset , preproc_method , experiment_name , metrics_dir ))
319
-
320
- ###################################################################################
352
+ ###################################################################################
321
353
322
354
# TF session setup
323
355
#sess = tf.Session()
324
356
init = (tf .global_variables_initializer (), tf .local_variables_initializer ())
325
357
coord = tf .train .Coordinator ()
326
358
threads = queue_runner_impl .start_queue_runners (sess = sess , coord = coord )
327
- metrics = Metrics ( output_dims , seq_length , curr_logger , metrics_method , istraining , model .name , experiment_name , preproc_method , dataset , metrics_dir , verbose = verbose , topk = topk )
359
+ if save_bool :
360
+ metrics = Metrics ( output_dims , seq_length , curr_logger , metrics_method , istraining , model .name , experiment_name , preproc_method , dataset , metrics_dir , verbose = verbose , topk = topk )
328
361
329
362
# Variables get randomly initialized into tf graph
330
363
sess .run (init )
@@ -375,7 +408,9 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
375
408
break
376
409
377
410
count += 1
378
- metrics .log_prediction (labels [batch_idx ][0 ], output_predictions [batch_idx ], vid_name , count )
411
+
412
+ if save_bool :
413
+ metrics .log_prediction (labels [batch_idx ][0 ], output_predictions [batch_idx ], vid_name , count )
379
414
380
415
# END IF
381
416
@@ -388,15 +423,17 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
388
423
coord .request_stop ()
389
424
coord .join (threads )
390
425
391
- total_accuracy = metrics .total_classification ()
392
- total_pred = metrics .get_predictions_array ()
393
426
394
- if verbose :
395
- print "Total accuracy : " , total_accuracy
396
- print total_pred
427
+ if save_bool :
428
+ total_accuracy = metrics .total_classification ()
429
+ total_pred = metrics .get_predictions_array ()
430
+
431
+ if verbose :
432
+ print "Total accuracy : " , total_accuracy
433
+ print total_pred
397
434
398
- # Save results in numpy format
399
- np .save (os .path .join ('results' , model .name , dataset , preproc_method , experiment_name , metrics_dir , 'test_predictions_' + dataset + "_" + metrics_method + '.npy' ), np .array (total_pred ))
435
+ # Save results in numpy format
436
+ np .save (os .path .join ('results' , model .name , dataset , preproc_method , experiment_name , metrics_dir , 'test_predictions_' + dataset + "_" + metrics_method + '.npy' ), np .array (total_pred ))
400
437
401
438
402
439
if __name__ == "__main__" :
@@ -427,10 +464,12 @@ def test(model, input_dims, output_dims, seq_length, size, dataset, loaded_datas
427
464
verbose = args .verbose ,
428
465
gpu_list = args .gpuList ,
429
466
preproc_method = args .preprocMethod ,
467
+ loaded_preproc = loaded_preproc ,
430
468
random_init = args .randomInit ,
431
469
avg_clips = args .avgClips ,
432
470
use_softmax = args .useSoftmax ,
433
471
preproc_debugging = args .preprocDebugging ,
472
+ reverse = args .reverse ,
434
473
topk = args .topk )
435
474
436
475
# END IF
0 commit comments