2222from keras .layers .normalization import BatchNormalization
2323from keras .callbacks import LearningRateScheduler
2424from keras .layers .advanced_activations import LeakyReLU
25+ from sklearn .metrics import mean_absolute_error as mae
26+ from plot_results import plot_err_variation
2527from keras .layers import Input
2628from keras .models import Model
2729from config_r16 import *
@@ -285,7 +287,7 @@ def load_to_RAM(frames_source):
285287 im_file = os .path .join (DATA_DIR , filename )
286288 try :
287289 frame = cv2 .imread (im_file , cv2 .IMREAD_COLOR )
288- # frame = cv2.resize (frame, (112, 112), interpolation=cv2.INTER_CUBIC )
290+ frame = cv2 .medianBlur (frame , FILTER_SIZE )
289291 frames [i ] = (frame .astype (np .float32 ) - 127.5 ) / 127.5
290292 j = j + 1
291293 except AttributeError as e :
@@ -314,7 +316,7 @@ def load_X(videos_list, index, data_dir, img_size):
314316 im_file = os .path .join (data_dir , filename )
315317 try :
316318 frame = cv2 .imread (im_file , cv2 .IMREAD_COLOR )
317- # frame = cv2.resize (frame, (112, 112), interpolation=cv2.INTER_LANCZOS4 )
319+ frame = cv2 .medianBlur (frame , FILTER_SIZE )
318320 X [i , j ] = (frame .astype (np .float32 ) - 127.5 ) / 127.5
319321 except AttributeError as e :
320322 print (im_file )
@@ -345,8 +347,8 @@ def get_video_lists(frames_source, stride):
345347
346348def train (BATCH_SIZE , ENC_WEIGHTS , DEC_WEIGHTS ):
347349 print ("Loading data definitions..." )
348- frames_source = hkl .load (os .path .join (DATA_DIR , 'sources_val_208 .hkl' ))
349- videos_list = get_video_lists (frames_source = frames_source , stride = 8 )
350+ frames_source = hkl .load (os .path .join (DATA_DIR , 'sources_train_208 .hkl' ))
351+ videos_list = get_video_lists (frames_source = frames_source , stride = 4 )
350352 n_videos = videos_list .shape [0 ]
351353
352354 # Setup test
@@ -443,7 +445,7 @@ def train(BATCH_SIZE, ENC_WEIGHTS, DEC_WEIGHTS):
443445 # then after each epoch/iteration
444446 avg_loss = sum (loss )/ len (loss )
445447 avg_val_loss = sum (val_loss ) / len (val_loss )
446- logs = {'loss' : avg_loss , 'test_loss ' : avg_val_loss }
448+ logs = {'loss' : avg_loss , 'val_loss ' : avg_val_loss }
447449 TC .on_epoch_end (epoch , logs )
448450
449451 # Log the losses
@@ -477,8 +479,9 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS):
477479 os .mkdir (TEST_RESULTS_DIR + '/pred/' )
478480 if not os .path .exists (TEST_RESULTS_DIR + '/graphs/' ):
479481 os .mkdir (TEST_RESULTS_DIR + '/graphs/' )
480- if not os .path .exists (TEST_RESULTS_DIR + '/gifs/' ):
481- os .mkdir (TEST_RESULTS_DIR + '/gifs/' )
482+ os .mkdir (TEST_RESULTS_DIR + '/graphs/values/' )
483+ # if not os.path.exists(TEST_RESULTS_DIR + '/gifs/'):
484+ # os.mkdir(TEST_RESULTS_DIR + '/gifs/')
482485
483486 print ("Creating models..." )
484487 encoder = encoder_model ()
@@ -489,7 +492,7 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS):
489492
490493 run_utilities (encoder , decoder , autoencoder , ENC_WEIGHTS , DEC_WEIGHTS )
491494
492- NB_TEST_ITERATIONS = int (n_test_videos / BATCH_SIZE )
495+ NB_TEST_ITERATIONS = int (n_test_videos / TEST_BATCH_SIZE )
493496 test_loss = []
494497 for index in range (NB_TEST_ITERATIONS ):
495498 X = load_X (test_videos_list , index , TEST_DATA_DIR , IMG_SIZE )
@@ -513,8 +516,15 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS):
513516 truth_seq = truth_seq * 127.5 + 127.5
514517 pred_seq = pred_seq * 127.5 + 127.5
515518
519+ mae_errors = []
520+ for i in range (int (VIDEO_LENGTH / 2 )):
521+ mae_errors .append (mae (y_test [i ].flatten (), predicted_images [i ].flatten ()))
522+
516523 cv2 .imwrite (os .path .join (TEST_RESULTS_DIR + '/truth/' , str (index ) + "_truth.png" ), truth_seq )
517524 cv2 .imwrite (os .path .join (TEST_RESULTS_DIR + '/pred/' , str (index ) + "_pred.png" ), pred_seq )
525+ np .save (os .path .join (TEST_RESULTS_DIR + '/graphs/values/' , str (index ) + "_mae.npy" ), np .asarray (mae_errors ))
526+ plot_err_variation (mae_errors , index )
527+
518528
519529 # then after each epoch/iteration
520530 avg_test_loss = sum (test_loss ) / len (test_loss )
@@ -527,10 +537,6 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS):
527537 print ("\n Min: " + str (np .min (np .asarray (test_loss ))))
528538
529539
530- def err_variation ()
531- K .abs (y_pred - y_true )
532-
533-
534540def load_X_test (index , data_dir , img_size ):
535541 X = np .zeros ((BATCH_SIZE , int (VIDEO_LENGTH / 2 ),) + img_size )
536542 for i in range (BATCH_SIZE ):
0 commit comments