混合分辨率:多尺度处理
混合分辨率的核心动机 在传统的Transformer中,所有token都被平等对待——无论这个token是高频细节(如图像中的边缘像素)还是低频概览(如文档的整体结构)。混合分辨率(Mixture of Resolutions)打破了这一平等假设:不同的token使用不同的空间/时间分辨率进行处理。 这一思想在视觉-语言模型(VLM)中尤为重要。图像中的不同区域对理解的重要性不同——主体区域需要高分辨率,背景区域可以使用低分辨率。 多尺度Transformer的基本架构 分层Tokenization 将输入转换为不同尺度的token序列: def multi_scale_tokenize(image, scales=[1, 0.5, 0.25]): """ 将图像编码为多个尺度的token序列 scales: 不同尺度的缩放因子 """ token_sequences = [] for scale in scales: # 缩放图像 scaled = F.interpolate(image, scale_factor=scale) # 分块编码 patches = patchify(scaled, patch_size=16) tokens = vision_encoder(patches) token_sequences.append(tokens) # 拼接所有尺度的token return torch.cat(token_sequences, dim=1) 分辨率路由器 使用路由器将token分配到不同分辨率处理路径: class ResolutionRouter(nn.Module): def __init__(self, d_model, n_resolutions=3): super().__init__() self.router = nn.Linear(d_model, n_resolutions) self.resolution_embeddings = nn.Parameter( torch.randn(n_resolutions, d_model) ) def forward(self, x): """ x: [batch, seq_len, d_model] """ # 路由分数 router_logits = self.router(x) # [batch, seq_len, n_res] router_probs = F.softmax(router_logits, dim=-1) # 选择分辨率 resolution_ids = router_probs.argmax(dim=-1) # [batch, seq_len] # 添加分辨率嵌入 resolution_emb = self.resolution_embeddings[resolution_ids] x = x + resolution_emb return x, resolution_ids 视觉-语言模型中的应用 高分辨率区域识别 在VLM中,系统需要识别图像中哪些区域需要高分辨率处理: ...