This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_features.py
176 lines (140 loc) · 4.72 KB
/
extract_features.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
import os
import argparse
import time
import torch
import torchvision
from omegaconf import OmegaConf
from tqdm import tqdm
from dataset.dataset import VideoDataSet
from models.models import TBN
from dataset.transforms import (
GroupScale,
GroupCenterCrop,
Stack,
ToTorchFormatTensor,
GroupNormalize,
)
import pickle
def parse_args():
parser = argparse.ArgumentParser(
description="Video feature extractor using the BNInceptopn Epic Fusion model"
)
parser.add_argument("cfg", type=str, help="config file")
return parser.parse_args()
def get_time_diff(start_time, end_time):
"""
Helper function to calculate time difference
Args
----------
start_time: float
Start time in seconds since January 1, 1970, 00:00:00 (UTC)
end_time: float
End time in seconds since January 1, 1970, 00:00:00 (UTC)
Returns
----------
hours: int
Difference of hours between start and end time
minutes: int
Difference of minutes between start and end time
seconds: int
Difference of seconds between start and end time
"""
hours = int((end_time - start_time) / 3600)
minutes = int((end_time - start_time) / 60) - (hours * 60)
seconds = round((end_time - start_time) % 60)
return (hours, minutes, seconds)
def extract_features(cfg):
"""
Helper function to extract and save features from epic fusion tbn model
Args
----------
cfg: OmegaConf
Config dictionary of parameters
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
modality = cfg.DATA.MODALITY
out_dir = cfg.DATA.OUT_DIR
os.makedirs(out_dir, exist_ok=True)
num_classes = tuple(cfg.MODEL.NUM_CLASSES)
net = TBN(
num_classes,
1,
cfg.DATA.MODALITY,
base_model=cfg.MODEL.ARCH,
consensus_type="avg",
dropout=0.5,
midfusion="concat",
)
checkpoint = torch.load(cfg.MODEL.CHECKPOINT)
base_dict = {
".".join(k.split(".")[1:]): v for k, v in list(checkpoint["state_dict"].items())
}
net.load_state_dict(base_dict)
print("Pretrained weights loaded from {}".format(cfg.MODEL.CHECKPOINT))
print("----------------------------------------------------------")
# TODO
# if device.type == "cuda" and torch.cuda.device_count() > 1:
# net = torch.nn.DataParallel(net)
# else:
# net = net.to(device)
net = net.to(device)
print("Model loaded to {}".format(device))
print("----------------------------------------------------------")
with open(cfg.DATA.VID_LIST) as f:
vid_list = [x.strip() for x in f.readlines() if len(x.strip()) > 0]
print("Video List loaded.")
print("----------------------------------------------------------")
transform = {}
for m in modality:
if m != "Spec":
cropping = torchvision.transforms.Compose(
[GroupScale(net.scale_size[m]), GroupCenterCrop(net.input_size[m]),]
)
transform[m] = torchvision.transforms.Compose(
[
cropping,
Stack(roll=cfg.MODEL.ARCH == "BNInception"),
ToTorchFormatTensor(div=cfg.MODEL.ARCH != "BNInception"),
GroupNormalize(net.input_mean[m], net.input_std[m]),
]
)
else:
transform[m] = torchvision.transforms.Compose(
[
Stack(roll=cfg.MODEL.ARCH == "BNInception"),
ToTorchFormatTensor(div=False),
]
)
start = time.time()
for vid_id in vid_list:
print("Processing {}...".format(vid_id))
dataset = VideoDataSet(cfg, vid_id, modality, transform=transform,)
# TODO Implement multi batch processing
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=1, shuffle=False, num_workers=cfg.NUM_WORKERS
)
with torch.no_grad():
net.eval()
feat_dict = {}
for data in tqdm(dataloader):
for m in modality:
data[m] = data[m].to(device)
feat = net(data)
feat_dict[str(data["frame_idx"].item())] = feat
torch.save(
feat_dict,
os.path.join(out_dir, "{}_{}.pkl".format(vid_id, cfg.DATA.OUT_FPS)),
)
print(
"Done. Total time taken (HH:MM:SS): {}".format(
get_time_diff(start, time.time())
)
)
def main():
args = parse_args()
cfg = OmegaConf.load(args.cfg)
print(cfg.pretty())
print("----------------------------------------------------------")
extract_features(cfg)
if __name__ == "__main__":
main()