Spaces:
Build error
Build error
File size: 13,375 Bytes
8fbac9e 2145817 8fbac9e 4181a28 af7b806 8fbac9e 5cbb0f4 8fbac9e |
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
from .clip import *
from .clip_gradcam import ClipGradcam
import torch
import numpy as np
from PIL import Image
import torchvision
from functools import reduce
def factors(n):
return set(
reduce(
list.__add__,
([i, n // i] for i in range(1, int(n**0.5) + 1) if n % i == 0),
)
)
saliency_configs = {
"ours": lambda img_dim: {
"distractor_labels": {},
"horizontal_flipping": True,
"augmentations": 5,
"imagenet_prompt_ensemble": False,
"positive_attn_only": True,
"cropping_augmentations": [
{"tile_size": img_dim, "stride": img_dim // 4},
{"tile_size": int(img_dim * 2 / 3), "stride": int(img_dim * 2 / 3) // 4},
{"tile_size": img_dim // 2, "stride": (img_dim // 2) // 4},
{"tile_size": img_dim // 4, "stride": (img_dim // 4) // 4},
],
},
"ours_fast": lambda img_dim: {
"distractor_labels": {},
"horizontal_flipping": True,
"augmentations": 2,
"imagenet_prompt_ensemble": False,
"positive_attn_only": True,
"cropping_augmentations": [
{"tile_size": img_dim, "stride": img_dim // 4},
{"tile_size": int(img_dim * 2 / 3), "stride": int(img_dim * 2 / 3) // 4},
{"tile_size": img_dim // 2, "stride": (img_dim // 2) // 4},
],
},
"chefer_et_al": lambda img_dim: {
"distractor_labels": {},
"horizontal_flipping": False,
"augmentations": 0,
"imagenet_prompt_ensemble": False,
"positive_attn_only": True,
"cropping_augmentations": [{"tile_size": img_dim, "stride": img_dim // 4}],
},
}
class ClipWrapper:
# SINGLETON WRAPPER
clip_model = None
clip_preprocess = None
clip_gradcam = None
lavt = None
device = None
jittering_transforms = None
def __init__(self, clip_model_type, device, **kwargs):
ClipWrapper.device = device
ClipWrapper.jittering_transforms = torchvision.transforms.ColorJitter(
brightness=0.6, contrast=0.6, saturation=0.6, hue=0.1
)
ClipWrapper.clip_model, ClipWrapper.clip_preprocess = load(
clip_model_type, ClipWrapper.device, **kwargs
)
ClipWrapper.clip_gradcam = ClipGradcam(
clip_model_name=clip_model_type,
classes=[""],
templates=["{}"],
device=ClipWrapper.device,
**kwargs
)
@classmethod
def check_initialized(cls, clip_model_type="ViT-B/32", **kwargs):
if cls.clip_gradcam is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
ClipWrapper(clip_model_type=clip_model_type, device=device, **kwargs)
print("using", device)
@classmethod
def get_clip_text_feature(cls, string):
ClipWrapper.check_initialized()
with torch.no_grad():
return (
cls.clip_model.encode_text(
tokenize(string, context_length=77).to(cls.device)
)
.squeeze()
.cpu()
.numpy()
)
@classmethod
def get_visual_feature(cls, rgb, tile_attn_mask, device=None):
if device is None:
device = ClipWrapper.device
ClipWrapper.check_initialized()
rgb = ClipWrapper.clip_preprocess(Image.fromarray(rgb)).unsqueeze(0)
with torch.no_grad():
clip_feature = ClipWrapper.clip_model.encode_image(
rgb.to(ClipWrapper.device), tile_attn_mask=tile_attn_mask
).squeeze()
return clip_feature.to(device)
@classmethod
def get_clip_saliency(
cls,
img,
text_labels,
prompts,
distractor_labels=set(),
use_lavt=False,
**kwargs
):
cls.check_initialized()
if use_lavt:
return cls.lavt.localize(img=img, prompts=text_labels)
cls.clip_gradcam.templates = prompts
cls.clip_gradcam.set_classes(text_labels)
text_label_features = torch.stack(
list(cls.clip_gradcam.class_to_language_feature.values()), dim=0
)
text_label_features = text_label_features.squeeze(dim=-1).cpu()
text_maps = cls.get_clip_saliency_convolve(
img=img, text_labels=text_labels, **kwargs
)
if len(distractor_labels) > 0:
distractor_labels = set(distractor_labels) - set(text_labels)
cls.clip_gradcam.set_classes(list(distractor_labels))
distractor_maps = cls.get_clip_saliency_convolve(
img=img, text_labels=list(distractor_labels), **kwargs
)
text_maps -= distractor_maps.mean(dim=0)
text_maps = text_maps.cpu()
return text_maps, text_label_features.squeeze(dim=-1)
@classmethod
def get_clip_saliency_convolve(
cls,
text_labels,
horizontal_flipping=False,
positive_attn_only: bool = False,
tile_batch_size=32,
prompt_batch_size=32,
tile_interpolate_batch_size=16,
**kwargs
):
cls.clip_gradcam.positive_attn_only = positive_attn_only
tiles, tile_imgs, counts, tile_sizes = cls.create_tiles(**kwargs)
outputs = {
k: torch.zeros(
[len(text_labels)] + list(count.shape), device=cls.device
).half()
for k, count in counts.items()
}
tile_gradcams = torch.cat(
[
torch.cat(
[
cls.clip_gradcam(
x=tile_imgs[tile_idx : tile_idx + tile_batch_size],
o=text_labels[prompt_idx : prompt_idx + prompt_batch_size],
)
for tile_idx in np.arange(0, len(tile_imgs), tile_batch_size)
],
dim=1,
)
for prompt_idx in np.arange(0, len(text_labels), prompt_batch_size)
],
dim=0,
)
if horizontal_flipping:
flipped_tile_imgs = tile_imgs[
..., torch.flip(torch.arange(0, tile_imgs.shape[-1]), dims=[0])
]
flipped_tile_gradcams = torch.cat(
[
torch.cat(
[
cls.clip_gradcam(
x=flipped_tile_imgs[
tile_idx : tile_idx + tile_batch_size
],
o=text_labels[
prompt_idx : prompt_idx + prompt_batch_size
],
)
for tile_idx in np.arange(
0, len(tile_imgs), tile_batch_size
)
],
dim=1,
)
for prompt_idx in np.arange(0, len(text_labels), prompt_batch_size)
],
dim=0,
)
with torch.no_grad():
flipped_tile_gradcams = flipped_tile_gradcams[
...,
torch.flip(
torch.arange(0, flipped_tile_gradcams.shape[-1]), dims=[0]
),
]
tile_gradcams = (tile_gradcams + flipped_tile_gradcams) / 2
del flipped_tile_gradcams
with torch.no_grad():
torch.cuda.empty_cache()
for tile_size in np.unique(tile_sizes):
tile_size_mask = tile_sizes == tile_size
curr_size_grads = tile_gradcams[:, tile_size_mask]
curr_size_tiles = tiles[tile_size_mask]
for tile_idx in np.arange(
0, curr_size_grads.shape[1], tile_interpolate_batch_size
):
resized_tiles = torch.nn.functional.interpolate(
curr_size_grads[
:, tile_idx : tile_idx + tile_interpolate_batch_size
],
size=tile_size,
mode="bilinear",
align_corners=False,
)
for tile_idx, tile_slice in enumerate(
curr_size_tiles[
tile_idx : tile_idx + tile_interpolate_batch_size
]
):
outputs[tile_size][tile_slice] += resized_tiles[
:, tile_idx, ...
]
output = sum(
output.float() / count
for output, count in zip(outputs.values(), counts.values())
) / len(counts)
del outputs, counts, tile_gradcams
output = output.cpu()
return output
@classmethod
def create_tiles(cls, img, augmentations, cropping_augmentations, **kwargs):
assert type(img) == np.ndarray
images = []
cls.check_initialized()
# compute image crops
img_pil = Image.fromarray(img)
images.append(np.array(img_pil))
for _ in range(augmentations):
images.append(np.array(cls.jittering_transforms(img_pil)))
# for taking average
counts = {
crop_aug["tile_size"]: torch.zeros(img.shape[:2], device=cls.device).float()
+ 1e-5
for crop_aug in cropping_augmentations
}
tiles = []
tile_imgs = []
tile_sizes = []
for img in images:
for crop_aug in cropping_augmentations:
tile_size = crop_aug["tile_size"]
stride = crop_aug["stride"]
for y in np.arange(0, img.shape[1] - tile_size + 1, stride):
if y >= img.shape[0]:
continue
for x in np.arange(0, img.shape[0] - tile_size + 1, stride):
if x >= img.shape[1]:
continue
tile = (
slice(None, None),
slice(x, x + tile_size),
slice(y, y + tile_size),
)
tiles.append(tile)
counts[tile_size][tile[1:]] += 1
tile_sizes.append(tile_size)
# this is currently biggest bottle neck
tile_imgs.append(
cls.clip_gradcam.preprocess(
Image.fromarray(img[tiles[-1][1:]])
)
)
tile_imgs = torch.stack(tile_imgs).to(cls.device)
return np.array(tiles), tile_imgs, counts, np.array(tile_sizes)
imagenet_templates = [
"a bad photo of a {}.",
"a photo of many {}.",
"a sculpture of a {}.",
"a photo of the hard to see {}.",
"a low resolution photo of the {}.",
"a rendering of a {}.",
"graffiti of a {}.",
"a bad photo of the {}.",
"a cropped photo of the {}.",
"a tattoo of a {}.",
"the embroidered {}.",
"a photo of a hard to see {}.",
"a bright photo of a {}.",
"a photo of a clean {}.",
"a photo of a dirty {}.",
"a dark photo of the {}.",
"a drawing of a {}.",
"a photo of my {}.",
"the plastic {}.",
"a photo of the cool {}.",
"a close-up photo of a {}.",
"a black and white photo of the {}.",
"a painting of the {}.",
"a painting of a {}.",
"a pixelated photo of the {}.",
"a sculpture of the {}.",
"a bright photo of the {}.",
"a cropped photo of a {}.",
"a plastic {}.",
"a photo of the dirty {}.",
"a jpeg corrupted photo of a {}.",
"a blurry photo of the {}.",
"a photo of the {}.",
"a good photo of the {}.",
"a rendering of the {}.",
"a {} in a video game.",
"a photo of one {}.",
"a doodle of a {}.",
"a close-up photo of the {}.",
"a photo of a {}.",
"the origami {}.",
"the {} in a video game.",
"a sketch of a {}.",
"a doodle of the {}.",
"a origami {}.",
"a low resolution photo of a {}.",
"the toy {}.",
"a rendition of the {}.",
"a photo of the clean {}.",
"a photo of a large {}.",
"a rendition of a {}.",
"a photo of a nice {}.",
"a photo of a weird {}.",
"a blurry photo of a {}.",
"a cartoon {}.",
"art of a {}.",
"a sketch of the {}.",
"a embroidered {}.",
"a pixelated photo of a {}.",
"itap of the {}.",
"a jpeg corrupted photo of the {}.",
"a good photo of a {}.",
"a plushie {}.",
"a photo of the nice {}.",
"a photo of the small {}.",
"a photo of the weird {}.",
"the cartoon {}.",
"art of the {}.",
"a drawing of the {}.",
"a photo of the large {}.",
"a black and white photo of a {}.",
"the plushie {}.",
"a dark photo of a {}.",
"itap of a {}.",
"graffiti of the {}.",
"a toy {}.",
"itap of my {}.",
"a photo of a cool {}.",
"a photo of a small {}.",
"a tattoo of the {}.",
]
__all__ = ["ClipWrapper", "imagenet_templates"]
|