Skip to content

Commit 51e8b23

Browse files
committed
Directory restructuring
1 parent bfd1a0f commit 51e8b23

17 files changed

Lines changed: 3702 additions & 13 deletions
16 KB
Binary file not shown.

code/autoencoder_model/scripts/config_r16.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
print ("Config file: " + str(__name__))
7676

7777
BATCH_SIZE = 9
78+
TEST_BATCH_SIZE = 1
7879
NB_EPOCHS_AUTOENCODER = 30
7980

8081
# OPTIM_A = Adam(lr=0.0001, beta_1=0.5)

code/autoencoder_model/scripts/plot_heatmap.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import cv2
1313
import ast
1414
import os
15-
from config_aa import TEST_RESULTS_DIR
15+
from config_r16 import TEST_RESULTS_DIR
1616

1717
def plot_heatmap(attn_layer, epoch, vid_num, file):
1818
print (attn_layer)
@@ -40,6 +40,14 @@ def plot_heatmap(attn_layer, epoch, vid_num, file):
4040
transparent=True)
4141

4242

43+
def plot_err_variation(values, index):
44+
plt.clf()
45+
plt.plot(values)
46+
plt.axis('off')
47+
plt.savefig(os.path.join(TEST_RESULTS_DIR + '/graphs/', 'plot_' + str(index) + '.png'),
48+
transparent=True)
49+
50+
4351
def get_args():
4452
parser = argparse.ArgumentParser()
4553
parser.add_argument("--file", type=str, default="None")

code/autoencoder_model/scripts/res_16.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from keras.layers.normalization import BatchNormalization
2323
from keras.callbacks import LearningRateScheduler
2424
from keras.layers.advanced_activations import LeakyReLU
25+
from sklearn.metrics import mean_absolute_error as mae
26+
from plot_results import plot_err_variation
2527
from keras.layers import Input
2628
from keras.models import Model
2729
from 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

346348
def 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-
534540
def 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):
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
from keras.optimizers import SGD
6+
from keras.optimizers import Adam
7+
from keras.optimizers import adadelta
8+
from keras.optimizers import rmsprop
9+
from keras.layers import Layer
10+
from keras import backend as K
11+
K.set_image_dim_ordering('tf')
12+
import socket
13+
import os
14+
15+
# -------------------------------------------------
16+
# Background config:
17+
hostname = socket.gethostname()
18+
if hostname == 'baymax':
19+
path_var = 'baymax/'
20+
elif hostname == 'walle':
21+
path_var = 'walle/'
22+
elif hostname == 'bender':
23+
path_var = 'bender/'
24+
else:
25+
path_var = 'zhora/'
26+
27+
DATA_DIR= '/local_home/JAAD_Dataset/iros/resized_imgs_208_thesis/train/'
28+
29+
VAL_DATA_DIR= '/local_home/JAAD_Dataset/iros/resized_imgs_208_thesis/val/'
30+
31+
TEST_DATA_DIR= '/local_home/JAAD_Dataset/iros/resized_imgs_208_thesis/test/'
32+
# TEST_DATA_DIR= '/local_home/JAAD_Dataset/fun_experiments/resized/'
33+
34+
RESULTS_DIR = '/local_home/JAAD_Dataset/thesis/results/DRRK16/'
35+
36+
# MODEL_DIR = './../' + path_var + 'models'
37+
MODEL_DIR = RESULTS_DIR + 'models/'
38+
if not os.path.exists(MODEL_DIR):
39+
os.mkdir(MODEL_DIR)
40+
41+
# CHECKPOINT_DIR = './../' + path_var + 'checkpoints'
42+
CHECKPOINT_DIR = RESULTS_DIR + 'checkpoints/'
43+
if not os.path.exists(CHECKPOINT_DIR):
44+
os.mkdir(CHECKPOINT_DIR)
45+
46+
GEN_IMAGES_DIR = RESULTS_DIR + 'generated_images/'
47+
if not os.path.exists(GEN_IMAGES_DIR):
48+
os.mkdir(GEN_IMAGES_DIR)
49+
50+
LOG_DIR = RESULTS_DIR + 'logs/'
51+
if not os.path.exists(LOG_DIR):
52+
os.mkdir(LOG_DIR)
53+
54+
TF_LOG_DIR = RESULTS_DIR + 'tf_logs/'
55+
if not os.path.exists(TF_LOG_DIR):
56+
os.mkdir(TF_LOG_DIR)
57+
58+
TEST_RESULTS_DIR = RESULTS_DIR + 'test_results/'
59+
if not os.path.exists(TEST_RESULTS_DIR):
60+
os.mkdir(TEST_RESULTS_DIR)
61+
62+
PRINT_MODEL_SUMMARY = True
63+
SAVE_MODEL = True
64+
PLOT_MODEL = True
65+
SAVE_GENERATED_IMAGES = True
66+
SHUFFLE = True
67+
VIDEO_LENGTH = 32
68+
IMG_SIZE = (128, 208, 3)
69+
RAM_DECIMATE = True
70+
REVERSE = True
71+
FILTER_SIZE = 3
72+
73+
74+
# -------------------------------------------------
75+
# Network configuration:
76+
print ("Loading network/training configuration.")
77+
print ("Config file: " + str(__name__))
78+
79+
BATCH_SIZE = 9
80+
TEST_BATCH_SIZE = 1
81+
NB_EPOCHS_AUTOENCODER = 30
82+
83+
# OPTIM_A = Adam(lr=0.0001, beta_1=0.5)
84+
OPTIM_A = rmsprop(lr=0.0001, rho=0.9)
85+
OPTIM_B = rmsprop(lr=0.00001, rho=0.9)
86+
# OPTIM_A = SGD(lr=0.000001, momentum=0.5, nesterov=True)
87+
88+
lr_schedule = [7, 14, 20, 30] # epoch_step
89+
90+
def schedule(epoch_idx):
91+
if (epoch_idx) <= lr_schedule[0]:
92+
return 0.001
93+
elif (epoch_idx) <= lr_schedule[1]:
94+
return 0.0001 # lr_decay_ratio = 10
95+
elif (epoch_idx) <= lr_schedule[2]:
96+
return 0.00001 # lr_decay_ratio = 10
97+
elif (epoch_idx) <= lr_schedule[3]:
98+
return 0.00001
99+
return 0.00001
100+
101+
102+
2.77 KB
Binary file not shown.

0 commit comments

Comments
 (0)