-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
256 lines (216 loc) · 8.15 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import clip
import torch
import torch.nn as nn
import torchvision.transforms as trans
from lavis.models import load_model_and_preprocess
from torchvision.models.resnet import ResNet101_Weights, resnet101
from superpixels import (
_extract_pixels_from_bounding_boxes,
_get_bounding_boxes,
)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = None
preprocess = None
def _get_resnet():
global model, preprocess
if model is not None and preprocess is not None:
return model, preprocess
model = resnet101(ResNet101_Weights.IMAGENET1K_V1)
model = nn.Sequential(*list(model.children())[:-1])
model.eval()
model.to(DEVICE)
preprocess = trans.Compose(
[
trans.Resize((224, 224)),
trans.CenterCrop((224, 224)),
trans.Normalize(
mean=(0.48145466, 0.4578275, 0.40821073),
std=(0.26862954, 0.26130258, 0.27577711),
),
]
)
return model, preprocess
def get_resnet_superpixel_features(
img: torch.Tensor,
super_pixel_masks: torch.Tensor,
feat_resize_dim: int = 2048,
) -> torch.Tensor:
"""
Given an image, create superpixel features using SLIC and ResNet101
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of superpixel features of shape (b, n_segments, 2048)
"""
model, preprocess = _get_resnet()
# Add batch dimension
img = img.unsqueeze(0).to(DEVICE)
super_pixel_masks = super_pixel_masks.unsqueeze(0).to(DEVICE)
bounding_boxes = _get_bounding_boxes(img, super_pixel_masks)
print(bounding_boxes.shape)
pixels = _extract_pixels_from_bounding_boxes(img, bounding_boxes).to(DEVICE)
pixels = pixels.reshape(-1, 3, 224, 224)
pixels = preprocess(pixels)
with torch.no_grad():
features = model(pixels).squeeze(-1)
features = features.reshape(-1, bounding_boxes.shape[1], feat_resize_dim)
return features, bounding_boxes
def get_resnet_patch_features(patches, feat_resize_dim: int = 2048):
"""
Given an image, create patch features using ResNet101
:param patches: Patches tensor of shape (b, n_patches, c, h, w)
:return: Tensor of patch features of shape (b, n_patches, 2048)
"""
model, preprocess = _get_resnet()
patches = patches.to(DEVICE)
patches = preprocess(patches)
with torch.no_grad():
features = model(patches).squeeze(-1)
features = features.reshape(-1, patches.shape[0], feat_resize_dim)
return features
def get_resnet_whole_img_features(
img: torch.Tensor,
) -> torch.Tensor:
"""
Given an image, create whole image features using ResNet101
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of whole image features of shape (b, 2048)
"""
model, preprocess = _get_resnet()
img = preprocess(img).unsqueeze(0).to(DEVICE)
with torch.no_grad():
features = model(img).squeeze(-1)
return features.squeeze(-1)
########################################################################################
def _get_clip():
global model, preprocess
if model is not None and preprocess is not None:
return model, preprocess
model_id = "ViT-B/32"
model, _ = clip.load(model_id, device=DEVICE)
preprocess = trans.Compose(
[
trans.Resize((224, 224)),
trans.CenterCrop((224, 224)),
trans.Normalize(
mean=(0.48145466, 0.4578275, 0.40821073),
std=(0.26862954, 0.26130258, 0.27577711),
),
]
)
model.eval()
model = model.encode_image
return model, preprocess
def get_clip_superpixel_features(
img: torch.Tensor,
super_pixel_masks: torch.Tensor,
feat_resize_dim: int = 2048,
) -> torch.Tensor:
"""
Given an image, create superpixel features using SLIC and ResNet101
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of superpixel features of shape (b, n_segments, 2048)
"""
model, preprocess = _get_clip()
# Add batch dimension
img = img.unsqueeze(0).to(DEVICE)
super_pixel_masks = super_pixel_masks.unsqueeze(0).to(DEVICE)
bounding_boxes = _get_bounding_boxes(img, super_pixel_masks)
pixels = _extract_pixels_from_bounding_boxes(img, bounding_boxes).to(DEVICE)
pixels = pixels.reshape(-1, 3, 224, 224)
pixels = preprocess(pixels)
with torch.no_grad():
features = model(pixels).squeeze(-1)
features = features.reshape(-1, bounding_boxes.shape[1], feat_resize_dim)
return features, bounding_boxes
def get_clip_patch_features(patches, feat_resize_dim: int = 2048):
"""
Given an image, create patch features using CLIP
:param patches: Patches tensor of shape (b, n_patches, c, h, w)
:return: Tensor of patch features of shape (b, n_patches, 2048)
"""
model, preprocess = _get_clip()
patches = patches.to(DEVICE)
patches = preprocess(patches)
with torch.no_grad():
features = model(patches).squeeze(-1)
features = features.reshape(-1, patches.shape[0], feat_resize_dim)
return features
def get_clip_whole_img_features(
img: torch.Tensor,
) -> torch.Tensor:
"""
Given an image, create whole image features using CLIP
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of whole image features of shape (b, 512)
"""
model, preprocess = _get_clip()
img = preprocess(img).unsqueeze(0).to(DEVICE)
with torch.no_grad():
features = model(img).squeeze(-1)
return features
########################################################################################
def _get_blip():
global model, preprocess
if model is not None and preprocess is not None:
return model, preprocess
model, vis_processors, _ = load_model_and_preprocess(
name="blip_feature_extractor",
model_type="base",
is_eval=True,
device=DEVICE,
)
preprocess = vis_processors["eval"]
preprocess.transform.transforms.remove(preprocess.transform.transforms[1])
return model, preprocess
def get_blip_superpixel_features(
img: torch.Tensor,
super_pixel_masks: torch.Tensor,
feat_resize_dim: int = 2048,
) -> torch.Tensor:
"""
Given an image, create superpixel features using SLIC and ResNet101
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of superpixel features of shape (b, n_segments, 2048)
"""
model, preprocess = _get_blip()
# Add batch dimension
img = img.unsqueeze(0).to(DEVICE)
super_pixel_masks = super_pixel_masks.unsqueeze(0).to(DEVICE)
bounding_boxes = _get_bounding_boxes(img, super_pixel_masks)
pixels = _extract_pixels_from_bounding_boxes(img, bounding_boxes).to(DEVICE)
pixels = pixels.reshape(-1, 3, 224, 224)
pixels = preprocess(pixels)
sample = {"image": pixels}
with torch.no_grad():
features = model.extract_features(sample, mode="image")
assert (
features.image_embeds[:, 0, :].unsqueeze(0).shape[1] == bounding_boxes.shape[1]
), "Mismatch in number of superpixels and bboxes"
return features.image_embeds[:, 0, :].unsqueeze(0), bounding_boxes
def get_blip_patch_features(patches, feat_resize_dim: int = 2048):
"""
Given an image, create patch features using BLIP
:param patches: Patches tensor of shape (b, n_patches, c, h, w)
:return: Tensor of patch features of shape (b, n_patches, 2048)
"""
model, preprocess = _get_blip()
patches = patches.to(DEVICE)
patches = preprocess(patches)
sample = {"image": patches}
with torch.no_grad():
features = model.extract_features(sample, mode="image")
features = features.image_embeds[:, 0, :].unsqueeze(0)
return features
def get_blip_whole_img_features(
img: torch.Tensor,
) -> torch.Tensor:
"""
Given an image, create whole image features using BLIP
:param img: Image tensor of shape (b, c, h, w)
:return: Tensor of whole image features of shape (b, 768)
"""
model, preprocess = _get_blip()
img = preprocess(img).unsqueeze(0).to(DEVICE)
sample = {"image": img}
with torch.no_grad():
features = model.extract_features(sample, mode="image")
return features.image_embeds[0, 0, :].unsqueeze(0)