模块更新于 2026-11-20· 浏览 2· 下载 2
IQA-Adapter 图像质量适配器
分数条件化:把 IQA/IAA 模型输出的质量分数(可多指标拼成向量)经轻量投影变成与文本 token 同维的质量 token,经交叉注意力注入 U-Net,SDXL 权重全程冻结,开销小
结构图

分数条件化:把 IQA/IAA 模型输出的质量分数(可多指标拼成向量)经轻量投影变成与文本 token 同维的质量 token,经交叉注意力注入 U-Net,SDXL 权重全程冻结,开销小
论文信息
- 论文:IQA-Adapter: Exploring Knowledge Transfer from Image Quality Assessment to Diffusion-based Generative Models
- 出处:ICCV 2025
核心痛点
扩散模型画质已经很强,但生成器本身"不懂"感知质量,想提质只能靠改提示词、RL 微调或事后增强;推理时拿 IQA 分数做梯度引导又会钻 IQA 模型的空子,其他指标不涨,还出现视觉伪影。
模块亮点
- 分数条件化:把 IQA/IAA 模型输出的质量分数(可多指标拼成向量)经轻量投影变成与文本 token 同维的质量 token,经交叉注意力注入 U-Net,SDXL 权重全程冻结,开销小
- 质量文本解耦:Separate Qualitative Attention 给每个文本交叉注意力配独立的质量注意力分支,共享 Q、分开算 K/V,质量知识内容无关地泛化;推理时用 λ 调强度
- 涨点实打实:高质量条件让多个客观指标最高提升 10%,单个 IQA 模型训练平均提升 7-9%;主观研究中对 SDXL 基线胜率 60% vs 32%;反向给低分还能当退化模型,参考图迁移失真 SROCC 0.80(IP-Adapter 仅 0.41)
适用场景
领域为文生图 / 扩散模型可控生成;原文在 SDXL 上验证了高质量条件生成、按百分位分级调控质量、参考图失真迁移;也可当退化模型造失真数据。
插入位置建议
- 挂在 SDXL 等扩散 U-Net 的每个文本交叉注意力层旁,作独立质量分支(原文用法)
- 把单个 IQA 分数换成多指标向量输入,如原文 TOPIQ + LAION-AES 组合
- 参考图场景改用 IQA 模型(如 ARNIQA)的中间层激活作质量嵌入
适用任务
- 文生图
- 扩散模型可控生成
完整代码
iqa-adapter.py
"""38_IQAAdapter_ICCV2025: 官方核心模块的单文件提取。
官方源码: https://github.com/X1716/IQA-Adapter/blob/cf356688934c3f5bbad3b26956a1070c1bd16380/iqa_adapter/iqa_adapter.py
源码 SHA256: 259b75a7fc18aaf36e3174660db9875261e1e9116cd391494392b9b50c728795
提取核心: MLPProjModel
整理说明: 保留官方质量分数 MLP 投影与质量 token 解耦交叉注意力;包装类按原 generate 的拼接方式调用处理器。需要质量分数及文本特征,默认不下载扩散模型。
形状验证只说明接口可用,不代表已复现论文指标。初始化/辅助输入遵循原仓库;
需要语义特征、掩码、时间等条件的模块不能省略对应条件。
"""
from __future__ import annotations
import torch
from torch import nn
import torch.nn.functional as F
from diffusers.models.attention_processor import Attention
class IPAttnProcessor2_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, scale=1.0, num_tokens=4):
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.to_k_ip = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
self.to_v_ip = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
def __call__(self, attn, hidden_states, encoder_hidden_states=None, attention_mask=None, temb=None, *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
else:
end_pos = encoder_hidden_states.shape[1] - self.num_tokens
encoder_hidden_states, ip_hidden_states = (encoder_hidden_states[:, :end_pos, :], encoder_hidden_states[:, end_pos:, :])
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)
ip_key = self.to_k_ip(ip_hidden_states)
ip_value = self.to_v_ip(ip_hidden_states)
ip_key = ip_key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
ip_value = ip_value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
ip_hidden_states = F.scaled_dot_product_attention(query, ip_key, ip_value, attn_mask=None, dropout_p=0.0, is_causal=False)
with torch.no_grad():
self.attn_map = query @ ip_key.transpose(-2, -1).softmax(dim=-1)
ip_hidden_states = ip_hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
ip_hidden_states = ip_hidden_states.to(query.dtype)
hidden_states = hidden_states + self.scale * ip_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
class MLPProjModel(torch.nn.Module):
"""SD model with image prompt"""
def __init__(self, cross_attention_dim=1024, input_dim=1, inner_dim=128, clip_extra_context_tokens=2):
super().__init__()
self.cross_attention_dim = cross_attention_dim
self.clip_extra_context_tokens_num = clip_extra_context_tokens
self.proj = torch.nn.Sequential(torch.nn.Linear(input_dim, inner_dim), torch.nn.GELU(), torch.nn.Linear(inner_dim, cross_attention_dim * clip_extra_context_tokens))
self.norm = torch.nn.LayerNorm(cross_attention_dim)
def forward(self, image_embeds):
embeds = image_embeds
clip_extra_context_tokens = self.proj(embeds).reshape(-1, self.clip_extra_context_tokens_num, self.cross_attention_dim)
clip_extra_context_tokens = self.norm(clip_extra_context_tokens)
return clip_extra_context_tokens
class IQAFeatureAdapter(nn.Module):
"""复用官方质量分数投影、token 拼接与解耦注意力。"""
def __init__(self, channels=32, quality_dim=1, num_tokens=4, heads=4):
super().__init__()
self.projection = MLPProjModel(channels, quality_dim, 32, num_tokens)
self.attention = Attention(query_dim=channels, cross_attention_dim=channels, heads=heads, dim_head=channels//heads)
self.attention.set_processor(IPAttnProcessor2_0(channels, channels, num_tokens=num_tokens))
def forward(self, x, quality, text_features):
tokens = self.projection(quality)
context = torch.cat([text_features, tokens], dim=1)
return self.attention(x, encoder_hidden_states=context)
def main():
"""最小使用示例:创建模块,验证输入与输出形状。"""
torch.manual_seed(0)
module = IQAFeatureAdapter()
input = torch.randn(2,16,32)
quality = torch.tensor([[0.8],[0.5]])
text_features = torch.randn(2,5,32)
output = module(input,quality,text_features)
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+
继续浏览
下一篇IQPIR 图像质量先验条件模块分数即条件,免改结构:用预训练 NR-IQA 模型给图像打质量分,分数离散化后经 Embedding 变成与特征图同尺寸的向量相加,再送入码预测 Transformer;推理时把分数拉满即输出最高感知质量,无需改结构相关资源
模块2026-11-20
HOIST 人-物交互空间Transformer
三元组标记化:把每组〈主体、动作、客体〉连同边界框编成三个 token,主体/客体用 CLIP 文本嵌入加傅里叶框嵌入融合,动作走单独 MLP;再用实例嵌入聚组、角色嵌入分角色,多组交互同框也能分清谁在对谁做什么
AIGC文生图扩散模型
计算机视觉MIT
模块2026-11-21
Att-Adapter CVAE 属性条件编码
用 CVAE 编码连续多属性条件,经重参数化与条件解码注入扩散模型,实现细粒度、可解耦的属性控制。
文本生图扩散模型属性控制
计算机视觉MIT
模块2026-11-21
IFAdapter 实例特征控制
在扩散交叉注意力中注入实例级外观 token 与实例语义图,按边界框控制每个实例的细粒度特征。
文本生图扩散模型实例特征
计算机视觉MIT