模块更新于 2026-11-21· 浏览 2· 下载 0
IFAdapter 实例特征控制
在扩散交叉注意力中注入实例级外观 token 与实例语义图,按边界框控制每个实例的细粒度特征。
结构图

在扩散交叉注意力中注入实例级外观 token 与实例语义图,按边界框控制每个实例的细粒度特征。
论文信息
- 论文:IFAdapter: Instance Feature Control for Grounded Text-to-Image Generation
- 出处:ICCV 2025
核心痛点
布局到图像只解决位置,文本提示又缺少实例级细节,多实例生成时属性容易串到错误实例上。
模块亮点
- 实例特征适配器:额外的 appearance token 与 Instance Semantic Map,把实例级特征对齐到对应区域。
- 只在部分交叉注意力层注入,保持即插即用,可迁移到 SDXL、PixArt 等社区模型。
- 配套 COCO-IFG 基准与 VLM 验证管线,同时评估位置和实例特征准确度。
适用场景
适合带边界框或布局的实例级 T2I 生成,尤其是多实例、需要精细外观控制的场景。
插入位置建议
- 接入扩散模型的交叉注意力层,注入实例特征与语义图;需文本、实例特征、归一化框和每样本实例数。
- 核对 token 空间网格、实例数与掩码;只在部分层注入以保持泛化。
适用任务
- 实例特征生成
- 布局到图像
- 多实例可控生成
完整代码
ifadapter.py
"""55_IFAdapter_ICCV2025: 官方核心模块的单文件提取。
官方源码: https://github.com/WUyinwei-hah/IFAdapter/blob/965cd1e006691b85a996017b92abcee393012188/if_adapter/attention_processor.py
源码 SHA256: 768b82a276bf60dfb4893e26ee66c1b7c856ef92a639263754db6b5ac1430806
提取核心: exists, default, SAM, CAM, CBAM, GemmaRMSNorm, IFAttnProcessor2_0
整理说明: 提取真正的实例特征注意力处理器,而非原文件中的通用 resampler。保留框空间掩码、实例注意力、归一化、CBAM 和实例融合;需文本、实例特征、归一化框与每样本实例数量。
形状验证只说明接口可用,不代表已复现论文指标。初始化/辅助输入遵循原仓库;
需要语义特征、掩码、时间等条件的模块不能省略对应条件。
"""
from __future__ import annotations
import torch
from torch import nn
import torch.nn.functional as F
from diffusers.models.attention_processor import Attention
def exists(v):
return v is not None
def default(v, d):
return v if exists(v) else d
class SAM(nn.Module):
def __init__(self, bias=False):
super(SAM, self).__init__()
self.bias = bias
self.conv = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3, dilation=1, bias=self.bias)
def forward(self, x):
max = torch.max(x, 1)[0].unsqueeze(1)
avg = torch.mean(x, 1).unsqueeze(1)
concat = torch.cat((max, avg), dim=1)
output = self.conv(concat)
output = F.sigmoid(output) * x
return output
class CAM(nn.Module):
def __init__(self, channels, r):
super(CAM, self).__init__()
self.channels = channels
self.r = r
self.linear = nn.Sequential(nn.Linear(in_features=self.channels, out_features=self.channels // self.r, bias=True), nn.ReLU(inplace=True), nn.Linear(in_features=self.channels // self.r, out_features=self.channels, bias=True))
def forward(self, x):
max = F.adaptive_max_pool2d(x, output_size=1)
avg = F.adaptive_avg_pool2d(x, output_size=1)
b, c, _, _ = x.size()
linear_max = self.linear(max.view(b, c)).view(b, c, 1, 1)
linear_avg = self.linear(avg.view(b, c)).view(b, c, 1, 1)
output = linear_max + linear_avg
output = F.sigmoid(output) * x
return output
class CBAM(nn.Module):
def __init__(self, channels, r):
super(CBAM, self).__init__()
self.channels = channels
self.r = r
self.sam = SAM(bias=False)
self.cam = CAM(channels=self.channels, r=self.r)
def forward(self, x):
output = self.cam(x)
output = self.sam(output)
return output
class GemmaRMSNorm(nn.Module):
def __init__(self, dim: int, eps: float=1e-06):
super().__init__()
self.weight = nn.Parameter(torch.zeros(dim))
self.eps = eps
def _norm(self, x: torch.Tensor) -> torch.Tensor:
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x: torch.Tensor) -> torch.Tensor:
output = self._norm(x.float())
output = output * (1.0 + self.weight.float())
return output.type_as(x)
class IFAttnProcessor2_0(torch.nn.Module):
"""
Attention processor for IP-Adapater for PyTorch 2.0.
Args:
hidden_size (`int`):
The hidden size of the attention layer.
cross_attention_dim (`int`):
The number of channels in the `encoder_hidden_states`.
scale (`float`, defaults to 1.0):
the weight scale of image prompt.
num_tokens (`int`, defaults to 4 when do ip_adapter_plus it should be 16):
The context length of the image features.
"""
def __init__(self, hidden_size, cross_attention_dim=None, num_heads=None, scale=1.0, num_tokens=4, enable_alpha=False, use_gate=False, max_obj=8):
super().__init__()
if not hasattr(F, 'scaled_dot_product_attention'):
raise ImportError('AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.')
self.hidden_size = hidden_size
self.cross_attention_dim = cross_attention_dim
self.scale = scale
self.num_tokens = num_tokens
self.use_gate = use_gate
self.max_obj = max_obj
self.to_k_ap = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
self.to_v_ap = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
self.q_norm = GemmaRMSNorm(hidden_size // num_heads)
self.k_norm = GemmaRMSNorm(hidden_size // num_heads)
self.cbam = CBAM(hidden_size, 16)
self.conv = nn.Conv2d(hidden_size, 1, 1, 1)
self.enable_alpha = enable_alpha
if enable_alpha:
self.alpha = nn.Parameter(torch.tensor(0.0))
def map_construction(self, bboxes_list, area, batch_size, num_heads, head_dim, dtype, device):
"""
input:
output: (B, heads, L, n_tokens, dim)
"""
num_block_row_col = int(area ** 0.5)
all_sample_background_mask_list = []
all_sample_cond_mask_list = []
mask_scale = []
for bboxes in bboxes_list:
single_sample_cond_mask = []
single_sample_mask_scale = []
for i in range(bboxes.shape[0]):
single_box_cond_mask = torch.zeros((1, num_block_row_col, num_block_row_col), dtype=dtype, device=device)
current_box_start = torch.floor(bboxes[i, 0:2] * num_block_row_col)
current_box_end = torch.ceil(bboxes[i, 2:4] * num_block_row_col)
top_left_x = int(current_box_start[0])
top_left_y = int(current_box_start[1])
bottom_right_x = int(current_box_end[0])
bottom_right_y = int(current_box_end[1])
single_box_cond_mask[:, top_left_y:bottom_right_y, top_left_x:bottom_right_x] = 1
single_sample_cond_mask.append(single_box_cond_mask)
single_sample_cond_mask_torch = torch.concat(single_sample_cond_mask)
background_map = (single_sample_cond_mask_torch.sum(axis=0) < 1).int()
box_areas = single_sample_cond_mask_torch.sum(axis=[1, 2])
box_area_scale = (area - background_map.sum()) / box_areas
mask_scale.append(box_area_scale)
all_sample_background_mask_list.append(background_map[None, :, :])
all_sample_cond_mask_list.append(single_sample_cond_mask_torch)
return (all_sample_cond_mask_list, all_sample_background_mask_list, mask_scale)
def __call__(self, attn, hidden_states, encoder_hidden_states=None, attention_mask=None, temb=None, phrase_num_arr=None, ap_tokens=None, boxes=None, use_cond=True, *args, **kwargs):
residual = hidden_states
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
batch_size, sequence_length, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
if attention_mask is not None:
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
if attn.group_norm is not None:
hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
query = attn.to_q(hidden_states)
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
if attn.norm_cross:
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
key = attn.to_k(encoder_hidden_states)
value = attn.to_v(encoder_hidden_states)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
hidden_states = F.scaled_dot_product_attention(query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False)
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype)
ap_key = self.to_k_ap(ap_tokens)
ap_value = self.to_v_ap(ap_tokens)
bboxes_list = list(torch.split(boxes, phrase_num_arr, dim=0))
ap_key = ap_key.view(ap_key.shape[0], -1, attn.heads, head_dim).transpose(1, 2)
ap_key = self.k_norm(ap_key)
ap_value = ap_value.view(ap_value.shape[0], -1, attn.heads, head_dim).transpose(1, 2)
all_sample_cond_mask_list, all_sample_background_mask_list, mask_scale = self.map_construction(bboxes_list, hidden_states.shape[1], hidden_states.shape[0], attn.heads, head_dim, ap_key.dtype, hidden_states.device)
ap_key_list = list(torch.split(ap_key, phrase_num_arr, dim=0))
ap_value_list = list(torch.split(ap_value, phrase_num_arr, dim=0))
ap_key_padded = []
ap_value_padded = []
num_tokens = ap_key.shape[2]
padded_key_list = []
padded_value_list = []
cond_mask_list = []
mask_scale_list = []
for ap_key_one_sample, ap_value_one_sample, cond_mask_one_sample, m_scale in zip(ap_key_list, ap_value_list, all_sample_cond_mask_list, mask_scale):
padded_key = torch.zeros(1, self.max_obj, attn.heads, num_tokens, head_dim).to(ap_key_one_sample)
padded_value = torch.zeros(1, self.max_obj, attn.heads, num_tokens, head_dim).to(ap_value_one_sample)
padded_mask = torch.zeros(1, self.max_obj, cond_mask_one_sample.shape[1], cond_mask_one_sample.shape[2]).to(cond_mask_one_sample)
padded_mask_scale = torch.zeros(1, self.max_obj).to(m_scale)
padded_key[:, :ap_key_one_sample.shape[0], :, :, :] = padded_key[:, :ap_key_one_sample.shape[0], :, :, :] + ap_key_one_sample
padded_value[:, :ap_value_one_sample.shape[0], :, :, :] = padded_value[:, :ap_value_one_sample.shape[0], :, :, :] + ap_value_one_sample
padded_mask[:, :cond_mask_one_sample.shape[0], :, :] = padded_mask[:, :cond_mask_one_sample.shape[0], :, :] + cond_mask_one_sample
padded_mask_scale[:, :m_scale.shape[0]] = padded_mask_scale[:, :m_scale.shape[0]] + m_scale
padded_key_list.append(padded_key)
padded_value_list.append(padded_value)
cond_mask_list.append(padded_mask)
mask_scale_list.append(padded_mask_scale)
padded_key = torch.concat(padded_key_list)
padded_value = torch.concat(padded_value_list)
cond_mask = torch.concat(cond_mask_list)
mask_scale = torch.concat(mask_scale_list)
query = self.q_norm(query)
query = query.unsqueeze(1).repeat(1, self.max_obj, 1, 1, 1)
cond_mask = cond_mask.view(batch_size, self.max_obj, -1, 1)
attn_mask = cond_mask.view(batch_size, self.max_obj, 1, -1, 1).repeat(1, 1, attn.heads, 1, num_tokens).to(torch.bool)
attn_mask = attn_mask.to(dtype=ap_key.dtype)
attn_mask = (1.0 - attn_mask) * torch.finfo(ap_key.dtype).min
ap_hidden_states = F.scaled_dot_product_attention(query, padded_key, padded_value, attn_mask=attn_mask, dropout_p=0.0, is_causal=False)
ap_hidden_states = ap_hidden_states.transpose(2, 3).reshape(batch_size, self.max_obj, -1, attn.heads * head_dim)
ap_hidden_states = ap_hidden_states * cond_mask
cond_scale = ap_hidden_states.view(batch_size * self.max_obj, -1, attn.heads * head_dim).transpose(1, 2)
num_blocks = int(hidden_states.shape[1] ** 0.5)
cond_scale = cond_scale.view(-1, attn.heads * head_dim, num_blocks, num_blocks)
cond_scale = self.cbam(cond_scale)
cond_scale = self.conv(cond_scale)
mask_scale = mask_scale.view(-1, 1, 1, 1)
cond_scale = (cond_scale * F.sigmoid(mask_scale)).view(batch_size, self.max_obj, hidden_states.shape[1]).softmax(dim=1)
ap_hidden_states = (ap_hidden_states * cond_scale.unsqueeze(-1)).sum(dim=1)
all_sample_background_mask = (1 - torch.concat(all_sample_background_mask_list)).view(batch_size, -1, 1)
ap_hidden_states = ap_hidden_states * all_sample_background_mask
ap_hidden_states = ap_hidden_states.to(query.dtype)
if use_cond:
if self.enable_alpha:
hidden_states = hidden_states + torch.tanh(self.alpha) * self.scale * ap_hidden_states
else:
hidden_states = hidden_states + self.scale * ap_hidden_states
hidden_states = attn.to_out[0](hidden_states)
hidden_states = attn.to_out[1](hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if attn.residual_connection:
hidden_states = hidden_states + residual
hidden_states = hidden_states / attn.rescale_output_factor
return hidden_states
def main():
"""最小使用示例:创建模块,验证输入与输出形状。"""
torch.manual_seed(0)
module = Attention(query_dim=32, cross_attention_dim=32, heads=4, dim_head=8)
processor = IFAttnProcessor2_0(32,32,num_heads=4,num_tokens=4,max_obj=2)
module.set_processor(processor)
input = torch.randn(2,16,32)
text = torch.randn(2,5,32)
ap_tokens = torch.randn(2,4,32)
boxes = torch.tensor([[0.0,0.0,0.75,0.75],[0.25,0.25,1.0,1.0]])
output = module(input, encoder_hidden_states=text, ap_tokens=ap_tokens, boxes=boxes, phrase_num_arr=[1,1])
assert input.shape == output.shape, (input.shape, output.shape)
assert torch.isfinite(output).all(), "output contains NaN/Inf"
print("input shape:", tuple(input.shape))
print("output shape:", tuple(output.shape))
return module, input, output
if __name__ == "__main__":
main()
环境与依赖
Python 3.9+
继续浏览
下一篇IRM InvResMLP 逆向残差 MLP仅优化训练策略(数据增强+优化器配置),不改PointNet++任何架构,即可在ScanObjectNN数据集涨8.2%OA至86.1%,直接超过此前SOTA PointMLP相关资源
模块2026-11-21
Att-Adapter CVAE 属性条件编码
用 CVAE 编码连续多属性条件,经重参数化与条件解码注入扩散模型,实现细粒度、可解耦的属性控制。
文本生图扩散模型属性控制
计算机视觉MIT
模块2026-11-20
IQA-Adapter 图像质量适配器
分数条件化:把 IQA/IAA 模型输出的质量分数(可多指标拼成向量)经轻量投影变成与文本 token 同维的质量 token,经交叉注意力注入 U-Net,SDXL 权重全程冻结,开销小
扩散模型文生图IQAAdapter
计算机视觉MIT
模块2026-11-20
D2ST-Adapter 解耦可变形时空适配器
双路径解耦编码:瓶颈结构下采样后分空间、时序两条路径并行适配,空间抓外观语义、时序学动态变化,融合后上采样,保持单流结构
计算机视觉动作识别D2STAdapter
计算机视觉MIT