-
Notifications
You must be signed in to change notification settings - Fork 1
/
feat_extract_visual.py
158 lines (140 loc) · 3.74 KB
/
feat_extract_visual.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
"""
Project: Advancing Surgical VQA with Scene Graph Knowledge
-----
Copyright (c) University of Strasbourg, All Rights Reserved.
"""
import os
import sys
import h5py
import argparse
import torch
import torchvision.models
import torchvision.transforms as transforms
import numpy as np
from PIL import Image
from glob import glob
import torch
from torch import nn
from torchvision import models
class FeatureExtractor(nn.Module):
def __init__(self, patch_size=4):
super(FeatureExtractor, self).__init__()
# visual feature extraction
self.img_feature_extractor = models.resnet18(pretrained=True)
self.img_feature_extractor = torch.nn.Sequential(
*(list(self.img_feature_extractor.children())[:-2])
)
self.resize_dim = nn.AdaptiveAvgPool2d((patch_size, patch_size))
def forward(self, img):
outputs = self.resize_dim(self.img_feature_extractor(img))
return outputs
# input data and IO folder location
filenames = []
seq = [
"1",
"2",
"3",
"4",
"6",
"7",
"8",
"9",
"10",
"13",
"14",
"15",
"16",
"18",
"20",
"5",
"11",
"12",
"17",
"19",
"26",
"27",
"31",
"21",
"22",
"23",
"24",
"25",
"28",
"29",
"30",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
]
folder_head = "/media/mobarak/data/lalith_dataset/Cholec80-VQA/cropped_image/"
folder_tail = "/*.png"
for curr_seq in seq:
filenames = filenames + glob(folder_head + str(curr_seq) + folder_tail)
new_filenames = []
for filename in filenames:
frame_num = int(filename.split("/")[-1].split(".")[0])
if frame_num % 25 == 0:
new_filenames.append(filename)
transform = transforms.Compose(
[
transforms.Resize((300, 256)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
# arguments
parser = argparse.ArgumentParser(description="feature extractor")
parser.add_argument("--patch_size", type=int, default=1, help="")
args = parser.parse_args()
# declare fearure extraction model
feature_network = FeatureExtractor(patch_size=args.patch_size)
# Set data parallel based on GPU
num_gpu = torch.cuda.device_count()
if num_gpu > 0:
device_ids = np.arange(num_gpu).tolist()
feature_network = nn.DataParallel(feature_network, device_ids=device_ids)
# Use Cuda
feature_network = feature_network.cuda()
feature_network.eval()
for img_loc in new_filenames:
# get visual features
print(img_loc)
img = Image.open(img_loc)
img = transform(img)
img = torch.unsqueeze(img, 0)
with torch.no_grad():
visual_features = feature_network(img)
visual_features = torch.flatten(visual_features, start_dim=2)
visual_features = visual_features.permute((0, 2, 1))
visual_features = visual_features.squeeze(0)
visual_features = visual_features.data.cpu().numpy()
# save extracted features
img_loc = img_loc.split("/")
save_dir = "/" + os.path.join(
img_loc[0],
img_loc[1],
img_loc[2],
img_loc[3],
img_loc[4],
img_loc[5],
img_loc[6],
img_loc[7],
"vqa/img_features",
(str(args.patch_size) + "x" + str(args.patch_size)),
)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# save to file
hdf5_file = h5py.File(
os.path.join(save_dir, "{}.hdf5".format(img_loc[-1].split(".")[0])), "w"
)
print(os.path.join(save_dir, "{}.hdf5".format(img_loc[-1].split(".")[0])))
hdf5_file.create_dataset("visual_features", data=visual_features)
hdf5_file.close()
print("save_dir: ", save_dir, " | visual_features: ", visual_features.shape)