File size: 10,790 Bytes
9d38064 bd72620 2b91970 cd45874 9d38064 |
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 |
from vision_encoders.builder import build_vision_tower_aux_list
from transformers import Qwen2Config
from typing import Optional, List, Tuple
import torch
import json
from transformers import BaseImageProcessor
class CambrianConfig(Qwen2Config):
model_type = "cambrian_qwen"
debug = "debug"
def __init__(
self,
**kwargs
) -> None:
super().__init__(**kwargs)
for key, value in kwargs.items():
setattr(self, key, value)
@classmethod
def from_json_file(cls, json_file_path):
"""Load a config from a json file."""
with open(json_file_path, "r") as f:
config_dict = json.load(f)
return cls(**config_dict)
class CambrianEncoders:
def __init__(
self,
config: CambrianConfig
) -> None:
self.config: CambrianConfig = config
self.vision_tower_aux_list = build_vision_tower_aux_list(config, delay_load=True)
def encode_images(self, image_aux_list, encode_type=None):
vision_tower_aux_list = self.vision_tower_aux_list
image_aux_features_list = []
chunk_size = 64
if encode_type == "dino":
image_aux = image_aux_list[-1]
vision_tower_aux = vision_tower_aux_list[-1]
if image_aux.shape[0] > chunk_size:
image_aux_features_chunks = []
for start_idx in range(0, image_aux.shape[0], chunk_size):
end_idx = min(start_idx + chunk_size, image_aux.shape[0])
chunk = image_aux[start_idx:end_idx]
image_aux_features_chunk = vision_tower_aux(chunk)
image_aux_features_chunks.append(image_aux_features_chunk)
image_aux_features = torch.cat(image_aux_features_chunks, dim=0)
else:
image_aux_features = vision_tower_aux(image_aux)
return image_aux_features
elif encode_type == "siglip":
image_aux = image_aux_list[0]
vision_tower_aux = vision_tower_aux_list[0]
if image_aux.shape[0] > chunk_size:
image_aux_features_chunks = []
for start_idx in range(0, image_aux.shape[0], chunk_size):
end_idx = min(start_idx + chunk_size, image_aux.shape[0])
chunk = image_aux[start_idx:end_idx]
image_aux_features_chunk = vision_tower_aux(chunk)
image_aux_features_chunks.append(image_aux_features_chunk)
image_aux_features = torch.cat(image_aux_features_chunks, dim=0)
else:
image_aux_features = vision_tower_aux(image_aux)
return image_aux_features
else:
for image_aux, vision_tower_aux in zip(
image_aux_list, vision_tower_aux_list
):
if image_aux.shape[0] > chunk_size:
image_aux_features_chunks = []
for start_idx in range(0, image_aux.shape[0], chunk_size):
end_idx = min(start_idx + chunk_size, image_aux.shape[0])
chunk = image_aux[start_idx:end_idx]
image_aux_features_chunk = vision_tower_aux(chunk)
image_aux_features_chunks.append(image_aux_features_chunk)
image_aux_features = torch.cat(image_aux_features_chunks, dim=0)
else:
image_aux_features = vision_tower_aux(image_aux)
image_aux_features_list.append(image_aux_features)
return image_aux_features_list
def select_frame(
self,
feature_list,
split_sizes,
new_image_aux_list,
image_sizes,
window_size=16,
threshold=0.83,
):
dino_features_batch = torch.split(feature_list, split_sizes, dim=0)
new_image_aux_batch_0 = torch.split(new_image_aux_list[0], split_sizes, dim=0)
new_image_aux_batch_1 = torch.split(new_image_aux_list[1], split_sizes, dim=0)
new_split_sizes = []
selected_frames_all_0 = []
selected_frames_all_1 = []
selected_frames_feature_all = []
selected_frame_indices_all = []
for i_batch, frame_features in enumerate(dino_features_batch):
original_width, original_height = image_sizes[i_batch]
if getattr(self.get_model().config, "highres", False):
token_per_frame = self.config.lowres_token ** 2
else:
token_per_frame = self.config.image_token_len
max_num_frames = max(
1,
(
self.config.tokenizer_model_max_length
- getattr(self.config, "inference_max_length", 16)
)
// token_per_frame,
)
if len(frame_features) < max_num_frames:
selected_frames_all_0.append(new_image_aux_batch_0[i_batch])
selected_frames_all_1.append(new_image_aux_batch_1[i_batch])
selected_frames_feature_all.append(frame_features)
new_split_sizes.append(len(frame_features))
selected_frame_indices_all.append(torch.arange(len(frame_features)))
continue
num_segments = len(frame_features) // window_size
if num_segments == 0:
query_feature = frame_features.flatten(1, 2)
query_feature = query_feature / torch.norm(
(query_feature), dim=1, keepdim=True
)
similarities = torch.mean(query_feature @ query_feature.T, dim=1)
similarities[len(frame_features) // 2] = 0
indices = torch.where(similarities < threshold)[0]
selected_frame_indices_all.append(indices)
selected_frames_all_0.append(new_image_aux_batch_0[i_batch][indices])
selected_frames_all_1.append(new_image_aux_batch_1[i_batch][indices])
selected_frames_feature_all.append(frame_features[indices])
new_split_sizes.append(len(indices))
continue
segments_frames_0 = []
segments_frames_1 = []
segments_features = []
for start_idx in range(0, len(frame_features), window_size):
end_idx = min(start_idx + window_size, len(frame_features))
segments_frames_0.append(
new_image_aux_batch_0[i_batch][start_idx:end_idx]
)
segments_frames_1.append(
new_image_aux_batch_1[i_batch][start_idx:end_idx]
)
segments_features.append(frame_features[start_idx:end_idx])
selected_frames_0 = []
selected_frames_1 = []
selected_features = []
selected_frame_indices = []
for i, segment in enumerate(segments_features):
query_feature = segment.flatten(1, 2)
query_feature = query_feature / torch.norm(
(query_feature), dim=1, keepdim=True
)
similarities = torch.mean(query_feature @ query_feature.T, dim=1)
similarities[len(segment) // 2] = 0
indices = torch.where(similarities < threshold)[0]
selected_frames_0.append(segments_frames_0[i][indices])
selected_frames_1.append(segments_frames_1[i][indices])
selected_features.append(segment[indices])
selected_frame_indices.extend(indices + i * window_size)
selected_frames_0 = torch.cat(selected_frames_0, dim=0)
selected_frames_1 = torch.cat(selected_frames_1, dim=0)
selected_features = torch.cat(selected_features, dim=0)
selected_frame_indices = torch.tensor(selected_frame_indices)
# ablation
max_num_frames = 400 # in case of OOM
if len(selected_frames_0) > max_num_frames:
interval = len(selected_frames_0) / float(max_num_frames)
indices = [int(interval * i) for i in range(max_num_frames)]
new_split_sizes.append(len(indices))
selected_frames_all_0.append(selected_frames_0[indices])
selected_frames_all_1.append(selected_frames_1[indices])
selected_frames_feature_all.append(selected_features[indices])
selected_frame_indices = selected_frame_indices[indices]
else:
new_split_sizes.append(len(selected_frames_0))
selected_frames_all_0.append(selected_frames_0)
selected_frames_all_1.append(selected_frames_1)
selected_frames_feature_all.append(selected_features)
selected_frame_indices_all.append(selected_frame_indices)
selected_frames_all_0 = torch.cat(selected_frames_all_0, dim=0)
selected_frames_all_1 = torch.cat(selected_frames_all_1, dim=0)
selected_frames_feature_all = torch.cat(selected_frames_feature_all, dim=0)
return (
selected_frames_feature_all,
new_split_sizes,
[selected_frames_all_0, selected_frames_all_1],
selected_frame_indices_all,
)
def prepare_mm_features(
self,
images: List[torch.Tensor],
image_sizes: List[Tuple[int, int]],
):
image_aux_list = images
split_sizes_ori = [
1 if image.ndim == 3 else image.shape[0] for image in image_aux_list[0]
]
new_image_aux_list = []
for image_aux in image_aux_list:
if type(image_aux) is list:
image_aux = [
x.unsqueeze(0) if x.ndim == 3 else x for x in image_aux
]
concat_image_aux = torch.cat([image for image in image_aux], dim=0)
new_image_aux_list.append(concat_image_aux)
image_aux_features_dino = self.encode_images(
new_image_aux_list, encode_type="dino"
)
(
image_aux_features_dino,
split_sizes,
new_image_aux_list,
selected_frame_indices_all,
) = self.select_frame(
image_aux_features_dino,
split_sizes_ori,
new_image_aux_list,
image_sizes,
threshold=getattr(self.config, "dino_threshold", 0.83),
)
image_aux_features_siglip = self.encode_images(
new_image_aux_list, encode_type="siglip"
)
image_aux_features_list = [
image_aux_features_siglip,
image_aux_features_dino,
]
return image_aux_features_list |