Skip to content

Commit 5838603

Browse files
committed
Scratch training of classifier
1 parent 632330f commit 5838603

13 files changed

Lines changed: 1668 additions & 83 deletions

code/autoencoder_model/scripts/thesis_scripts/baseline_classifier.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import tb_callback
3939
import lrs_callback
4040
import argparse
41+
import time
4142
import cv2
4243
import os
4344

@@ -824,11 +825,19 @@ def test(CLA_WEIGHTS):
824825
y_test_pred = []
825826
y_test_true = []
826827
test_c_loss = []
828+
iter_loadtime = []
829+
iter_starttime = []
830+
iter_endtime = []
827831
for index in range(NB_TEST_ITERATIONS):
832+
iter_loadtime.append(time.time())
828833
X, y = load_X_y(test_videos_list, index, TEST_DATA_DIR, test_ped_action_classes, batch_size=TEST_BATCH_SIZE)
829834
X_test = X
830835
y_true_class = y[:, CLASS_TARGET_INDEX]
831836

837+
iter_starttime.append(time.time())
838+
test_ped_pred_class = classifier.predict(X_test, verbose=0)
839+
iter_endtime.append(time.time())
840+
832841
test_c_loss.append(classifier.test_on_batch(X_test, y_true_class))
833842
y_test_true.extend(y_true_class)
834843
y_test_pred.extend(classifier.predict(X_test, verbose=0))
@@ -889,6 +898,16 @@ def test(CLA_WEIGHTS):
889898
tn, fp, fn, tp = confusion_matrix(y_test_true, np.round(y_test_pred)).ravel()
890899
print ("TN: %.2f, FP: %.2f, FN: %.2f, TP: %.2f" % (tn, fp, fn, tp))
891900

901+
print("Mean time taken to make " + str(NB_TEST_ITERATIONS) + " predictions: %f"
902+
% (np.mean(np.asarray(iter_endtime) - np.asarray(iter_starttime))))
903+
print("Standard Deviation %f"
904+
% (np.std(np.asarray(iter_endtime) - np.asarray(iter_starttime))))
905+
906+
print("Mean time taken to load and process " + str(NB_TEST_ITERATIONS) + " predictions: %f"
907+
% (np.mean(np.asarray(iter_endtime) - np.asarray(iter_loadtime))))
908+
print("Standard Deviation %f"
909+
% (np.std(np.asarray(iter_endtime) - np.asarray(iter_loadtime))))
910+
892911

893912
def get_args():
894913
parser = argparse.ArgumentParser()

code/autoencoder_model/scripts/thesis_scripts/classifier_all2.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def train(BATCH_SIZE, ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
704704
else:
705705
label_orig = "not crossing"
706706

707-
if y_true_classes[k][0] > 0.5:
707+
if y_true_classes[k][j] > 0.5:
708708
label_true = "crossing"
709709
else:
710710
label_true = "not crossing"
@@ -771,7 +771,7 @@ def train(BATCH_SIZE, ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
771771
else:
772772
label_orig = "not crossing"
773773

774-
if (y_true_classes[k][0] > 0.5):
774+
if (y_true_classes[k][j] > 0.5):
775775
label_true = "crossing"
776776
else:
777777
label_true = "not crossing"
@@ -946,7 +946,7 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
946946
else:
947947
label_orig = "not crossing"
948948

949-
if y_true_classes[k][0] > 0.5:
949+
if y_true_classes[k][j] > 0.5:
950950
label_true = "crossing"
951951
else:
952952
label_true = "not crossing"
@@ -994,7 +994,7 @@ def test(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
994994
print ("Standard Deviation %f"
995995
%(np.std(np.asarray(iter_endtime)-np.asarray(iter_starttime))))
996996

997-
print("Mean time taken to make load and process" + str(NB_TEST_ITERATIONS) + " predictions: %f"
997+
print("Mean time taken to load and process " + str(NB_TEST_ITERATIONS) + " predictions: %f"
998998
% (np.mean(np.asarray(iter_endtime) - np.asarray(iter_loadtime))))
999999
print("Standard Deviation %f"
10001000
% (np.std(np.asarray(iter_endtime) - np.asarray(iter_loadtime))))
@@ -1061,7 +1061,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
10611061
index = index + 1
10621062
continue
10631063
else:
1064-
for fnum in range (VIDEO_LENGTH):
1064+
for fnum in range (int(VIDEO_LENGTH/2)):
10651065

10661066
X, y = load_X_y(test_videos_list, index, TEST_DATA_DIR, test_ped_action_classes,
10671067
batch_size=TEST_BATCH_SIZE)
@@ -1098,7 +1098,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
10981098
else:
10991099
label_orig = "not crossing"
11001100

1101-
if y_true_classes[k][0] > 0.5:
1101+
if y_true_classes[k][j] > 0.5:
11021102
label_true = "crossing"
11031103
else:
11041104
label_true = "not crossing"
@@ -1132,6 +1132,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
11321132
continue
11331133
else:
11341134
print ("Correct prediction. Record data.")
1135+
print (fnum+1)
11351136
tcp_list.append(fnum + 1)
11361137
index = index + int(VIDEO_LENGTH / 2)
11371138
# Break from the for loop

code/autoencoder_model/scripts/thesis_scripts/classifier_all3.py

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from keras.utils.vis_utils import plot_model
1919
from keras.layers.wrappers import TimeDistributed
2020
from keras.layers.convolutional import Conv3D
21+
from keras.layers.convolutional import Conv3DTranspose
2122
from keras.layers.convolutional import Conv2D
2223
from keras.layers.convolutional import UpSampling3D
2324
from keras.layers.convolutional_recurrent import ConvLSTM2D
@@ -50,38 +51,77 @@
5051
import cv2
5152
import os
5253

53-
5454
def encoder_model():
5555
inputs = Input(shape=(int(VIDEO_LENGTH/2), 128, 208, 3))
5656

5757
# 10x128x128
58-
conv_1 = Conv3D(filters=32,
59-
strides=(1, 2, 2),
58+
conv_1 = Conv3D(filters=128,
59+
strides=(1, 4, 4),
6060
dilation_rate=(1, 1, 1),
61-
kernel_size=(3, 3, 3),
61+
kernel_size=(3, 11, 11),
6262
padding='same')(inputs)
6363
x = TimeDistributed(BatchNormalization())(conv_1)
6464
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
6565
out_1 = TimeDistributed(Dropout(0.5))(x)
6666

67+
conv_2a = Conv3D(filters=64,
68+
strides=(1, 1, 1),
69+
dilation_rate=(2, 1, 1),
70+
kernel_size=(2, 5, 5),
71+
padding='same')(out_1)
72+
x = TimeDistributed(BatchNormalization())(conv_2a)
73+
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
74+
out_2a = TimeDistributed(Dropout(0.5))(x)
75+
76+
conv_2b = Conv3D(filters=64,
77+
strides=(1, 1, 1),
78+
dilation_rate=(2, 1, 1),
79+
kernel_size=(2, 5, 5),
80+
padding='same')(out_2a)
81+
x = TimeDistributed(BatchNormalization())(conv_2b)
82+
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
83+
out_2b = TimeDistributed(Dropout(0.5))(x)
84+
85+
conv_2c = TimeDistributed(Conv2D(filters=64,
86+
kernel_size=(1, 1),
87+
strides=(1, 1),
88+
padding='same'))(out_1)
89+
x = TimeDistributed(BatchNormalization())(conv_2c)
90+
out_1_less = TimeDistributed(LeakyReLU(alpha=0.2))(x)
91+
92+
res_1 = add([out_1_less, out_2b])
93+
# res_1 = LeakyReLU(alpha=0.2)(res_1)
94+
6795
conv_3 = Conv3D(filters=64,
6896
strides=(1, 2, 2),
6997
dilation_rate=(1, 1, 1),
70-
kernel_size=(3, 3, 3),
71-
padding='same')(out_1)
98+
kernel_size=(3, 5, 5),
99+
padding='same')(res_1)
72100
x = TimeDistributed(BatchNormalization())(conv_3)
73101
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
74102
out_3 = TimeDistributed(Dropout(0.5))(x)
75103

76104
# 10x16x16
77105
conv_4a = Conv3D(filters=64,
78-
strides=(1, 2, 2),
79-
dilation_rate=(1, 1, 1),
80-
kernel_size=(3, 3, 3),
106+
strides=(1, 1, 1),
107+
dilation_rate=(2, 1, 1),
108+
kernel_size=(2, 3, 3),
81109
padding='same')(out_3)
82110
x = TimeDistributed(BatchNormalization())(conv_4a)
83111
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
84-
z = TimeDistributed(Dropout(0.5))(x)
112+
out_4a = TimeDistributed(Dropout(0.5))(x)
113+
114+
conv_4b = Conv3D(filters=64,
115+
strides=(1, 1, 1),
116+
dilation_rate=(2, 1, 1),
117+
kernel_size=(2, 3, 3),
118+
padding='same')(out_4a)
119+
x = TimeDistributed(BatchNormalization())(conv_4b)
120+
x = TimeDistributed(LeakyReLU(alpha=0.2))(x)
121+
out_4b = TimeDistributed(Dropout(0.5))(x)
122+
123+
z = add([out_3, out_4b])
124+
# res_1 = LeakyReLU(alpha=0.2)(res_1)
85125

86126
model = Model(inputs=inputs, outputs=z)
87127

@@ -101,7 +141,23 @@ def decoder_model():
101141
x = TimeDistributed(BatchNormalization())(convlstm_1)
102142
out_1 = TimeDistributed(Activation('tanh'))(x)
103143

104-
res_1 = UpSampling3D(size=(1, 2, 2))(out_1)
144+
convlstm_2 = ConvLSTM2D(filters=64,
145+
kernel_size=(3, 3),
146+
strides=(1, 1),
147+
padding='same',
148+
return_sequences=True,
149+
recurrent_dropout=0.2)(out_1)
150+
x = TimeDistributed(BatchNormalization())(convlstm_2)
151+
out_2 = TimeDistributed(Activation('tanh'))(x)
152+
153+
# conv_1c = TimeDistributed(Conv2D(filters=64,
154+
# kernel_size=(1, 1),
155+
# strides=(1, 1),
156+
# padding='same'))(inputs)
157+
# x = TimeDistributed(BatchNormalization())(conv_1c)
158+
# res_0_less = TimeDistributed(Activation('tanh'))(x)
159+
res_1 = add([inputs, out_2])
160+
res_1 = UpSampling3D(size=(1, 2, 2))(res_1)
105161

106162
# 10x32x32
107163
convlstm_3a = ConvLSTM2D(filters=64,
@@ -113,10 +169,26 @@ def decoder_model():
113169
x = TimeDistributed(BatchNormalization())(convlstm_3a)
114170
out_3a = TimeDistributed(Activation('tanh'))(x)
115171

116-
res_2 = UpSampling3D(size=(1, 2, 2))(out_3a)
172+
convlstm_3b = ConvLSTM2D(filters=64,
173+
kernel_size=(3, 3),
174+
strides=(1, 1),
175+
padding='same',
176+
return_sequences=True,
177+
recurrent_dropout=0.2)(out_3a)
178+
x = TimeDistributed(BatchNormalization())(convlstm_3b)
179+
out_3b = TimeDistributed(Activation('tanh'))(x)
180+
181+
# conv_3c = TimeDistributed(Conv2D(filters=64,
182+
# kernel_size=(1, 1),
183+
# strides=(1, 1),
184+
# padding='same'))(res_1)
185+
# x = TimeDistributed(BatchNormalization())(conv_3c)
186+
# res_1_less = TimeDistributed(Activation('tanh'))(x)
187+
res_2 = add([res_1, out_3b])
188+
res_2 = UpSampling3D(size=(1, 2, 2))(res_2)
117189

118190
# 10x64x64
119-
convlstm_4a = ConvLSTM2D(filters=32,
191+
convlstm_4a = ConvLSTM2D(filters=16,
120192
kernel_size=(3, 3),
121193
strides=(1, 1),
122194
padding='same',
@@ -125,7 +197,23 @@ def decoder_model():
125197
x = TimeDistributed(BatchNormalization())(convlstm_4a)
126198
out_4a = TimeDistributed(Activation('tanh'))(x)
127199

128-
res_3 = UpSampling3D(size=(1, 2, 2))(out_4a)
200+
convlstm_4b = ConvLSTM2D(filters=16,
201+
kernel_size=(3, 3),
202+
strides=(1, 1),
203+
padding='same',
204+
return_sequences=True,
205+
recurrent_dropout=0.2)(out_4a)
206+
x = TimeDistributed(BatchNormalization())(convlstm_4b)
207+
out_4b = TimeDistributed(Activation('tanh'))(x)
208+
209+
conv_4c = TimeDistributed(Conv2D(filters=16,
210+
kernel_size=(1, 1),
211+
strides=(1, 1),
212+
padding='same'))(res_2)
213+
x = TimeDistributed(BatchNormalization())(conv_4c)
214+
res_2_less = TimeDistributed(Activation('tanh'))(x)
215+
res_3 = add([res_2_less, out_4b])
216+
res_3 = UpSampling3D(size=(1, 2, 2))(res_3)
129217

130218
# 10x128x128
131219
convlstm_5 = ConvLSTM2D(filters=3,
@@ -141,7 +229,6 @@ def decoder_model():
141229
return model
142230

143231

144-
145232
def process_prec3d():
146233
json_file = open(PRETRAINED_C3D, 'r')
147234
loaded_model_json = json_file.read()
@@ -950,7 +1037,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
9501037
n_test_videos = test_videos_list.shape[0]
9511038

9521039
NB_TEST_ITERATIONS = int(n_test_videos / TEST_BATCH_SIZE)
953-
print("Number of iterations %d" % NB_TEST_ITERATIONS)
1040+
print ("Number of iterations %d" %NB_TEST_ITERATIONS)
9541041
# NB_TEST_ITERATIONS = 5
9551042

9561043
if CLASSIFIER:
@@ -967,18 +1054,17 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
9671054
while index < NB_TEST_ITERATIONS:
9681055
X, y = load_X_y(test_videos_list, index, TEST_DATA_DIR, test_ped_action_classes,
9691056
batch_size=TEST_BATCH_SIZE)
970-
# X_test = np.flip(X[:, 0: int(VIDEO_LENGTH / 2)], axis=1)
971-
# y_true_imgs = X[:, int(VIDEO_LENGTH / 2):]
1057+
X_test = np.flip(X[:, 0: int(VIDEO_LENGTH / 2)], axis=1)
1058+
y_true_imgs = X[:, int(VIDEO_LENGTH / 2):]
9721059

9731060
y_past_class = y[:, 0]
974-
975-
y_end_class = y[:, -1]
1061+
y_end_class = y[:,-1]
9761062

9771063
if y_end_class[0] == y_past_class[0]:
9781064
index = index + 1
9791065
continue
9801066
else:
981-
for fnum in range(VIDEO_LENGTH):
1067+
for fnum in range (int(VIDEO_LENGTH/2)):
9821068

9831069
X, y = load_X_y(test_videos_list, index, TEST_DATA_DIR, test_ped_action_classes,
9841070
batch_size=TEST_BATCH_SIZE)
@@ -994,8 +1080,8 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
9941080
y_test_true.extend(y_true_class)
9951081

9961082
# Save generated images to file
997-
z = encoder.predict(X_test)
998-
test_predicted_images = decoder.predict(z)
1083+
z, res = encoder.predict(X_test)
1084+
test_predicted_images = decoder.predict([z, res])
9991085
test_ped_pred_class = sclassifier.predict(X_test, verbose=0)
10001086
pred_seq = arrange_images(np.concatenate((X_test, test_predicted_images), axis=1))
10011087
pred_seq = pred_seq * 127.5 + 127.5
@@ -1015,7 +1101,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
10151101
else:
10161102
label_orig = "not crossing"
10171103

1018-
if y_true_classes[k][0] > 0.5:
1104+
if y_true_classes[k][j] > 0.5:
10191105
label_true = "crossing"
10201106
else:
10211107
label_true = "not crossing"
@@ -1044,16 +1130,18 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
10441130
truth_image)
10451131

10461132
if y_true_class[0] != np.round(y_pred_class[0]):
1047-
print("Wrong prediction! Slide window and try again.")
1133+
print ("Wrong prediction! Slide window and try again.")
10481134
index = index + 1
10491135
continue
10501136
else:
1051-
print("Correct prediction. Record data.")
1137+
print ("Correct prediction. Record data.")
1138+
print (fnum+1)
10521139
tcp_list.append(fnum + 1)
10531140
index = index + int(VIDEO_LENGTH / 2)
10541141
# Break from the for loop
10551142
break
10561143

1144+
10571145
# then after each epoch
10581146
avg_test_c_loss = np.mean(np.asarray(test_c_loss, dtype=np.float32), axis=0)
10591147

@@ -1064,7 +1152,7 @@ def test_mtcp(ENC_WEIGHTS, DEC_WEIGHTS, CLA_WEIGHTS):
10641152
print("\nAvg test_c_loss: " + str(avg_test_c_loss))
10651153
print("Mean time to change prediction: " + str(np.mean(np.asarray(tcp_list))))
10661154
print("Standard Deviation " + str(np.std(np.asarray(tcp_list))))
1067-
print("Number of correct predictions " + str(len(tcp_list)))
1155+
print ("Number of correct predictions " + str(len(tcp_list)))
10681156
print("Test Prec: %.4f, Recall: %.4f, Fbeta: %.4f" % (test_prec, test_rec, test_fbeta))
10691157

10701158
print("Classification Report")

0 commit comments

Comments
 (0)