forked from francescosolera/dreyeving
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
165 lines (135 loc) · 7.6 KB
/
Copy pathutils.py
File metadata and controls
165 lines (135 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from keras.layers import Input, Flatten, merge
from keras.models import Model, Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Convolution3D, MaxPooling3D, Convolution2D, UpSampling2D
from keras.layers.core import Reshape
import os
from os.path import join
import sys
import cv2
import numpy as np
from tqdm import tqdm
import pdb
from collections import deque
# parameters (no need to edit)
t, c, w, h = 16, 3, 112, 112
upsample = 4
def getCoarse2FineModel(summary=True):
# defined input
videoclip_cropped = Input((c, t, h, w), name='input1')
videoclip_original = Input((c, t, h, w), name='input2')
last_frame_bigger = Input((c, h*upsample, w*upsample), name='input3')
# coarse saliency model
coarse_saliency_model = Sequential()
coarse_saliency_model.add(Convolution3D(64, 3, 3, 3, activation='relu', border_mode='same', name='conv1', subsample=(1, 1, 1), input_shape=(c, t, h, w)))
coarse_saliency_model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), border_mode='valid', name='pool1'))
coarse_saliency_model.add(Convolution3D(128, 3, 3, 3, activation='relu', border_mode='same', name='conv2', subsample=(1, 1, 1)))
coarse_saliency_model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), border_mode='valid', name='pool2'))
coarse_saliency_model.add(Convolution3D(256, 3, 3, 3, activation='relu', border_mode='same', name='conv3a', subsample=(1, 1, 1)))
coarse_saliency_model.add(Convolution3D(256, 3, 3, 3, activation='relu', border_mode='same', name='conv3b', subsample=(1, 1, 1)))
coarse_saliency_model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), border_mode='valid', name='pool3'))
coarse_saliency_model.add(Convolution3D(512, 3, 3, 3, activation='relu', border_mode='same', name='conv4a', subsample=(1, 1, 1)))
coarse_saliency_model.add(Convolution3D(512, 3, 3, 3, activation='relu', border_mode='same', name='conv4b', subsample=(1, 1, 1)))
coarse_saliency_model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(4, 2, 2), border_mode='valid', name='pool4'))
coarse_saliency_model.add(Reshape((512, 7, 7)))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(256, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(128, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(64, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(32, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(16, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
coarse_saliency_model.add(BatchNormalization())
coarse_saliency_model.add(Convolution2D(1, 3, 3, init='glorot_uniform', border_mode='same'))
coarse_saliency_model.add(LeakyReLU(alpha=.001))
# loss on cropped image
coarse_saliency_cropped = coarse_saliency_model(videoclip_cropped)
cropped_output = Flatten(name='cropped_output')(coarse_saliency_cropped)
# coarse-to-fine saliency model and loss
coarse_saliency_original = coarse_saliency_model(videoclip_original)
x = UpSampling2D((upsample, upsample), name='coarse_saliency_upsampled')(coarse_saliency_original) # 112 x 4 = 448
x = merge([x, last_frame_bigger], mode='concat', concat_axis=1) # merge the last RGB frame
x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
x = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(x)
x = LeakyReLU(alpha=.001)(x)
x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
x = LeakyReLU(alpha=.001)(x)
x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
x = LeakyReLU(alpha=.001)(x)
x = Convolution2D(16, 3, 3, border_mode='same', init='he_normal')(x)
x = LeakyReLU(alpha=.001)(x)
x = Convolution2D(4, 3, 3, border_mode='same', init='he_normal')(x)
x = LeakyReLU(alpha=.001)(x)
fine_saliency_model = Convolution2D(1, 3, 3, border_mode='same', activation='relu')(x)
# loss on full image
full_fine_output = Flatten(name='full_fine_output')(fine_saliency_model)
final_model = Model(input=[videoclip_cropped, videoclip_original, last_frame_bigger],
output=[cropped_output, full_fine_output])
if summary:
print final_model.summary()
return final_model
def predict_folder(model, folder_in, output_path, mean_frame_path):
#parameters
sample_rate = 3
frame_rate = 25
# load frames to predict
frames = []
frame_list = os.listdir(folder_in)
if '_' in frame_list[0]:
video_set = set([f.split('_')[0] for f in frame_list])
for video in video_set:
sub_list = [f for f in frame_list if f.startswith(video)]
sub_list.sort()
#get sampled frames
end_time = int(sub_list[-1].split('_')[1].split('.')[0]) + 1
suffix = sub_list[0].split('.')[1]
sample_times = np.arange(0, end_time, 1000.0/sample_rate).astype(int)
sample_index = (np.around(sample_times/(1000.0/frame_rate))).astype(int)
predict_video(model, folder_in, sub_list, sample_index, output_path, mean_frame_path)
else:
frame_list.sort()
predict_video(model, folder_in, frame_list, np.arange(len(frame_list)), output_path, mean_frame_path)
def predict_video(model, folder_in, frame_list, sample_index, output_path, mean_frame_path):
mean_frame = cv2.imread(mean_frame_path)
#prepare output path
if not os.path.isdir(output_path):
os.makedirs(output_path)
# start of prediction
for i in tqdm(sample_index):
if i < t:
continue
# loading videoclip of t frames
frames = []
for frame_name in frame_list[i-t:i]:
frame = cv2.imread(join(folder_in, frame_name))
frames.append(frame.astype(np.float32) - mean_frame)
sys.stdout.write('\r{0}: predicting on frame {1}...'.format(folder_in, frame_list[i]))
# convert to array
x = np.array(frames)
x_last_bigger = cv2.resize(x[-1, :, :, :], (h*upsample,w*upsample))
x_last_bigger = x_last_bigger.transpose(2, 0, 1)
x_last_bigger = x_last_bigger[None, :]
x = np.array([cv2.resize(f, (h, w)) for f in x])
x = x[None, :]
x = x.transpose(0, 4, 1, 2, 3).astype(np.float32)
# predict attentional map on last frame of the videoclip
res = model.predict_on_batch([x, x, x_last_bigger])
res = res[1] # keep only fine output
res = np.clip(res, a_min=0, a_max=255)
# normalize attentional map between 0 and 1
res_norm = ((res / res.max()) * 255).astype(np.uint8)
res_norm = np.reshape(res_norm, (h*upsample,w*upsample))
cv2.imwrite(join(output_path, frame_list[i]), res_norm)