图像真实感评分
更新时间:2026-07-29
简介
ImageRealismScore 是图像真实感评分算子,采用 CLIP zero-shot 方式对比 "真实照片" 与 "AI 生成图像" 两个 prompt 的相似度,输出 0-1 的真实感分数(越大越真实)。
功能描述
• 使用本地 CLIP 权重(默认 openai/clip-vit-base-patch16)做 zero-shot 打分,作为 SILA RealismScore 的开源冷启动替代。
• 初始化时预计算正/负 prompt 的文本特征,推理时仅对图像特征做相似度比较。
• 对每个 batch 的图像特征与正负 prompt 做 softmax,取正向 prompt 概率作为真实感分数。
• 支持 URL / base64 / binary 图像输入,GPU/CPU 均可运行。
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 待评分的图像列,取值由 image_src_type 决定(图像 URL / base64 / 二进制) |
输出
| 输出 | 含义 |
|---|---|
| result | float32,0-1 真实感分数,越大越真实;失败为 None |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| image_src_type | str | "image_url" | 图像输入类型,可选 image_url / image_binary / image_base64 |
| model_path | str | "/opt/aihc/models" | CLIP 权重根目录,与 model_name 拼接为实际模型目录 |
| model_name | str | "openai/clip-vit-base-patch16" | CLIP 模型名(相对 model_path 的子目录) |
| batch_size | int | 32 | 图像推理批大小 |
| rank | int | 0 | 多卡 rank,用于选择 cuda:{rank % cuda_device_count} |
| pos_prompt | str | "a real natural photo taken by a camera" | 正向 prompt(代表真实照片),其概率作为分数 |
| neg_prompt | str | "an AI generated synthetic image" | 负向 prompt(代表 AI 生成图像) |
| fail_on_error | bool | False | 批处理出错时是否抛异常;False 时对应行返回 None |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.image.image_realism_score import ImageRealismScore
10
11IMAGE = "bos://your-bucket/sample.jpg" # MOCK
12MODEL_PATH = "/opt/aihc/models" # MOCK
13
14os.environ.setdefault("BOS_ENDPOINT", "http://bj.bcebos.com")
15os.environ.setdefault("BOS_REGION", "bj")
16
17if __name__ == "__main__":
18 if os.getenv("DAFT_RUNNER", "native") == "ray":
19 import ray
20
21 ray.init(ignore_reinit_error=True)
22 daft.set_runner_ray()
23 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
24
25 samples = {"image": [IMAGE]}
26 ds = daft.from_pydict(samples)
27 ds = ds.with_column(
28 "realism",
29 aihc_udf(
30 ImageRealismScore,
31 construct_args={
32 "image_src_type": "image_url",
33 "model_path": MODEL_PATH,
34 "model_name": "openai/clip-vit-base-patch16",
35 },
36 num_cpus=1,
37 num_gpus=1,
38 concurrency=1,
39 batch_size=1,
40 )(col("image")),
41 )
42 ds.show()
评价此篇文章
