-
Notifications
You must be signed in to change notification settings - Fork 1
/
spatial_feature_extract_ugc.py
181 lines (146 loc) · 6.16 KB
/
spatial_feature_extract_ugc.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import torch
from modules.network import get_network
from modules.CONTRIQUE_model import CONTRIQUE_model
from torchvision import transforms
import numpy as np
import os
import argparse
from PIL import Image
import skvideo.io
import pandas as pd
import scipy.io
class torch_transform:
def __init__(self, size):
self.transform1 = transforms.Compose(
[
transforms.Resize((size[0],size[1])),
transforms.ToTensor(),
]
)
self.transform2 = transforms.Compose(
[
transforms.Resize((size[0] // 2, size[1] // 2)),
transforms.ToTensor(),
]
)
def __call__(self, x):
return self.transform1(x), self.transform2(x)
def rescale_video(vid, min_val, max_val):
vid = (vid - min_val)/(max_val - min_val + 1e-3)
return vid
def temporal_filt(vid, filt_choice):
# choose temporal filter (haar, db2, bior22)
if filt_choice == 3:
return vid
filters = ['haar','db2','bior22']
num_levels = 3
filt_choice = np.random.choice([0,1,2],1)[0]
#load filter
filt_path = 'WPT_Filters/' + filters[filt_choice] + '_wpt_' \
+ str(num_levels) + '.mat'
wfun = scipy.io.loadmat(filt_path)
wfun = wfun['wfun']
#choose subband
subband_choice = np.random.choice(np.arange(len(wfun)),1)[0]
#Temporal Filtering
frame_data = vid.numpy()
dpt_filt = np.zeros_like(frame_data)
for ch in range(3):
inp = frame_data[:,ch,:,:].astype(np.float32)
out = scipy.ndimage.filters.convolve1d(inp,\
wfun[subband_choice,:],axis=0,mode='constant')
dpt_filt[:,ch,:,:] = out.astype(np.float16)
min_val, max_val = np.min(dpt_filt), np.max(dpt_filt)
dpt_filt = torch.from_numpy(dpt_filt)
dpt_filt = rescale_video(dpt_filt, min_val, max_val)
return dpt_filt
def create_data_loader(image, image_2, batch_size):
train = torch.utils.data.TensorDataset(image, image_2)
loader = torch.utils.data.DataLoader(
train,
batch_size=batch_size,
drop_last=True,
num_workers=4,
sampler=None,
shuffle=False
)
return loader
def extract_features(model, loader):
feat = []
model.eval()
for step, (batch_im, batch_im_2) in enumerate(loader):
batch_im = batch_im.type(torch.float32)
batch_im_2 = batch_im_2.type(torch.float32)
batch_im = batch_im.cuda(non_blocking=True)
batch_im_2 = batch_im_2.cuda(non_blocking=True)
with torch.no_grad():
_,_, _, _, model_feat, model_feat_2, _, _ = model(batch_im, batch_im_2)
feat_ = np.hstack((model_feat.detach().cpu().numpy(),\
model_feat_2.detach().cpu().numpy()))
feat.extend(feat_)
return np.array(feat)
def main(args):
# load CONTRIQUE Model
encoder = get_network('resnet50', pretrained=False)
model = CONTRIQUE_model(args, encoder, 2048)
model.load_state_dict(torch.load(args.model_path, map_location=args.device.type))
model = model.to(args.device)
write_fdr = args.write_fdr
fls = pd.read_csv(args.csv_file)
fls = fls.loc[:,'File_names'].tolist()
for step,vid_path in enumerate(fls):
# extract features for all temporal transforms
for temporal in range(4):
name = vid_path.split('/')[-1]
content = vid_path.split('/')[-2]
if not os.path.isdir(write_fdr + content):
os.system('mkdir -p ' + write_fdr + content)
os.system('chmod -R 777 ' + write_fdr + content)
write_name = write_fdr + content + '/' + \
name.split('.')[0] + '_temp' + str(temporal) + '.npy'
print(write_name)
if os.path.exists(write_name):
continue
path = 'training_data/' + vid_path
vid_raw = skvideo.io.FFmpegReader(path)
T, H, W, C = vid_raw.getShape()
dist_frames = torch.zeros((T,3,H,W))
dist_frames = dist_frames.type(torch.float16)
dist_frames_2 = torch.zeros((T,3,H//2,W//2))
dist_frames_2 = dist_frames_2.type(torch.float16)
transform = torch_transform((H,W))
for frame_ind in range(T):
frame = Image.fromarray(next(vid_raw))
# resize to source spatial resolution
frame = frame.resize((W, H), Image.LANCZOS)
frame, frame_2 = transform(frame)
dist_frames[frame_ind],dist_frames_2[frame_ind] = \
frame.type(torch.float16), frame_2.type(torch.float16)
# temporal transforms
dist_frames = temporal_filt(dist_frames, temporal)
dist_frames_2 = temporal_filt(dist_frames_2, temporal)
loader = create_data_loader(dist_frames, dist_frames_2, args.batch_size)
video_feat = extract_features(model, loader)
video_feat = video_feat.astype(np.float16)
np.save(write_name, video_feat)
vid_raw.close()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, \
default='models/CONTRIQUE_checkpoint25.tar', \
help='Path to trained CONTRIQUE model', metavar='')
parser.add_argument('--batch_size', type=int, \
default=16, \
help='batch size', metavar='')
parser.add_argument('--csv_file', type=str, \
default='csv_files/file_names_ugc.csv', \
help='path for csv file with filenames', metavar='')
parser.add_argument('--write_fdr', type=str, \
default='training_data/kinetics_feat/', \
help='write folder', metavar='')
args = parser.parse_args()
args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
return args
if __name__ == '__main__':
args = parse_args()
main(args)