1818from keras .utils .vis_utils import plot_model
1919from keras .layers .wrappers import TimeDistributed
2020from keras .layers .convolutional import Conv3D
21+ from keras .layers .convolutional import Conv3DTranspose
2122from keras .layers .convolutional import Conv2D
2223from keras .layers .convolutional import UpSampling3D
2324from keras .layers .convolutional_recurrent import ConvLSTM2D
5051import cv2
5152import os
5253
53-
5454def 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-
145232def 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 ("\n Avg 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