Spaces:
Running
on
Zero
Running
on
Zero
File size: 6,050 Bytes
7c34c28 |
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 |
from typing import Dict, List, Tuple, Optional
import torch
import torch.nn as nn
from transformers import CLIPVisionModel, CLIPImageProcessor
from PIL import Image
from multi_token.modalities.base_modality import Modality
from multi_token.modalities.projectors import (
build_patch_mlp_projector,
build_mlp_vector_projector,
)
from multi_token.data_tools import load_image
PATCH_LAYER = -2
OUTPUT_LAYER = -1
OUTPUT_EMB_SIZE = 1024
class CLIPVisionModule(nn.Module):
def __init__(self, model_name_or_path: str, feature_layer: int = PATCH_LAYER):
super().__init__()
self.feature_layer = feature_layer
self.model_name_or_path = model_name_or_path
self.image_processor = None
self.image_model = None
self.load_model()
def load_model(self):
self.image_processor = CLIPImageProcessor.from_pretrained(
self.model_name_or_path
)
self.image_model = CLIPVisionModel.from_pretrained(self.model_name_or_path)
self.image_model.requires_grad_(False)
@torch.no_grad()
def forward(self, images) -> torch.Tensor:
if self.feature_layer == PATCH_LAYER:
image_forward_outs = self.image_model(
images.to(device=self.device, dtype=self.dtype),
output_hidden_states=True,
)
image_features = image_forward_outs.hidden_states[self.feature_layer]
image_features = image_features[:, 1:].to(images.dtype)
else:
image_forward_outs = self.image_model(
images.to(device=self.device, dtype=self.dtype),
)
image_features = image_forward_outs.pooler_output.to(images.dtype).view(
-1, 1, OUTPUT_EMB_SIZE
)
return image_features
@property
def dtype(self):
return self.image_model.dtype
@property
def device(self):
return self.image_model.device
@property
def config(self):
return self.image_model.config
@property
def hidden_size(self):
return self.config.hidden_size
@property
def num_patches(self):
return (self.config.image_size // self.config.patch_size) ** 2
def _expand2square(pil_img: Image, background_color: Tuple) -> Image:
width, height = pil_img.size
if width == height:
return pil_img
elif width > height:
result = Image.new(pil_img.mode, (width, width), background_color)
result.paste(pil_img, (0, (width - height) // 2))
return result
else:
result = Image.new(pil_img.mode, (height, height), background_color)
result.paste(pil_img, ((height - width) // 2, 0))
return result
class CLIPVisionModality(Modality):
def __init__(
self,
model_name_or_path: str = "openai/clip-vit-large-patch14-336",
pad_non_square_images: bool = False,
num_projector_layers: int = 2,
feature_layer: int = PATCH_LAYER,
num_tokens_output: Optional[int] = None,
):
if feature_layer not in [PATCH_LAYER, OUTPUT_LAYER]:
raise ValueError(
f"feature_layer must be one of {PATCH_LAYER} or {OUTPUT_LAYER}"
)
if (feature_layer == PATCH_LAYER) != (num_tokens_output is None):
raise ValueError(
"num_tokens_output must be None if feature_layer is PATCH_LAYER"
)
self.model_name_or_path = model_name_or_path
self.module = CLIPVisionModule(
model_name_or_path=self.model_name_or_path, feature_layer=feature_layer
)
self.pad_non_square_images = pad_non_square_images
self.num_projector_layers = num_projector_layers
self.num_tokens_output = num_tokens_output
def build_projector(self, lm_hidden_size: int) -> nn.Module:
if self.module.feature_layer == PATCH_LAYER:
return build_patch_mlp_projector(
self.module.hidden_size,
lm_hidden_size,
num_layers=self.num_projector_layers,
)
else:
return build_mlp_vector_projector(
input_hidden_size=OUTPUT_EMB_SIZE,
lm_hidden_size=lm_hidden_size,
num_layers=self.num_projector_layers,
num_tokens=self.num_tokens_output,
)
@property
def name(self) -> str:
return "vision_clip"
@property
def token(self) -> str:
return "<image>"
@property
def data_key(self) -> str:
return "images"
@property
def token_width(self) -> int:
if self.module.feature_layer == PATCH_LAYER:
return self.module.num_patches
else:
return self.num_tokens_output
def to(self, dtype: torch.dtype, device: torch.device) -> "CLIPVisionModality":
self.module.to(dtype=dtype, device=device)
return self
def preprocess_rows(self, rows: List[Dict]) -> List[Optional[torch.Tensor]]:
row_values = []
for row in rows:
images = []
for image_fn in row[self.data_key]:
image_obj = load_image(image_fn)
if self.pad_non_square_images:
image_obj = _expand2square(
image_obj,
tuple(
int(x * 255) for x in self.module.image_processor.image_mean
),
)
image = self.module.image_processor.preprocess(
image_obj, return_tensors="pt"
)["pixel_values"][0]
images.append(image)
row_values.append(torch.stack(images) if len(images) > 0 else None)
return row_values
@torch.no_grad()
def forward(self, encoded_values: List[torch.Tensor]) -> List[torch.Tensor]:
image_features = []
for image_batch in encoded_values:
image_features.append(self.module.forward(image_batch))
return image_features
|