文本绘制扩散模型TextDiffuser
扩散模型(Diffusion Models)目前无法在图像中绘制准确清晰的文字内容,而TextDiffuser专注于解决该问题,可以生成与背景融合的带文字的图像。TextDiffuser有三种使用方式,既可以通过输入提示词,在自动预测的位置上渲染文本,也可以通过输入模板图像在指定位置上渲染文本,还可以输入一张图像后对文本内容进行编辑。
模型介绍
本模型共有两个阶段,首先是布局生成(Layout Generation)阶段,通过对输入的提示词做分词后将待绘制的文本内容输入给一个Transformer模型,经过布局预测并生成字符级的分割掩码;然后是图像生成(Image Generation)阶段,采用Stable Diffusion v1.5模型并将输入通道数由4扩充到17,分别包含了加噪后的图像、字符分割掩码、编辑区域掩码、非编辑区域图像,通过开关切换文字生成或编辑修复两个分支的训练,并在损失函数中使用一个Unet分割模型对生成文字做字符级别的监督,提升文字准确率。
期望模型使用方式以及使用范围
本模型使用范围较广,能基于输入的提示词生成包含英文字符的图像,或对图像中的文本进行编辑或修复。
环境配置
# 安装git(如有请跳过)
conda install -c anaconda git
# 克隆textdiffuser仓库
git clone https://github.com/microsoft/unilm.git
cd unilm/textdiffuser/
# 创建环境
conda create -n textdiffuser python=3.8
conda activate textdiffuser
pip install -r requirements.txt
pip install Pillow==9.5.0
# 安装diffusers并替换部分文件
git clone https://github.com/JingyeChen/diffusers
cp ./assets/files/scheduling_ddpm.py ./diffusers/src/diffusers/schedulers/scheduling_ddpm.py
cp ./assets/files/unet_2d_condition.py ./diffusers/src/diffusers/models/unet_2d_condition.py
cp ./assets/files/modeling_utils.py ./diffusers/src/diffusers/models/modeling_utils.py
cd diffusers && pip install -e . && cd ..
示例代码
请参照如下示例代码,以三种方式使用TextDiffuser生成带文字的图像。(首次执行需要下载多个模型文件至缓存,如遇报错请重启ipython环境再次执行)
from modelscope.pipelines import pipeline
pipe = pipeline('my-textdiffuser-task', model='damo/TextDiffuser_en', model_revision='v1.0.0')
params = {
"sample_num": 4,
"sample_steps": 50,
"classifier_free_scale": 7.5,
"use_pillow_segmentation_mask": True,
"binarization": False,
}
# 1. text-to-image
input_data_t2i = {
"prompt": "'test if textdiffuser works' written in autumn leaves floating on a lake"
}
mode = 'text-to-image'
output = pipe(input_data_t2i, mode=mode, **params)
for idx, img in enumerate(output):
img.save(f'{mode}_{idx}.jpg')
# 2. text-to-image-with-template
input_data_t2it = {
"prompt": "a poster of monkey music festival",
"template_image": "assets/examples/text-to-image-with-template/case2.jpg",
}
mode = 'text-to-image-with-template'
output = pipe(input_data_t2it, mode=mode, **params)
for idx, img in enumerate(output):
img.save(f'{mode}_{idx}.jpg')
# 3. text-inpainting
input_data_ti = {
"prompt": "a boy draws good morning on a board",
"original_image": "assets/examples/text-inpainting/case2.jpg",
"text_mask": "assets/examples/text-inpainting/case2_mask.jpg"
}
mode = 'text-inpainting'
output = pipe(input_data_ti, mode=mode, **params)
for idx, img in enumerate(output):
img.save(f'{mode}_{idx}.jpg')
模型局限性以及可能得偏差
- 模型目前只支持英文大小写字符的书写,不支持标点符号,不支持其他语言(如中文);
- 使用文生图模式时,写入多行可能会存在文字重叠现象;
- 一定概率下会产生文本模糊、扭曲、拼写错误等现象;
- 基于互联网公开数据的训练,生成结果与训练数据分布存在较大关联;
训练数据介绍
本模型的训练数据集采用MARIO-10M,主要来源于LAION-400M,TMDB,Open Library三个互联网开源数据集,包含1000万高质量的图文对,涵盖自然图像、电影海报、书籍封面等各类场景。
模型评测指标
与现有方法相比,TextDiffuser在CLIPScore和OCR的性能指标SOTA,FID方面指标也具有相当的竞争力:
相关论文及引用信息
@article{chen2023textdiffuser,
title={TextDiffuser: Diffusion Models as Text Painters},
author={Chen, Jingye and Huang, Yupan and Lv, Tengchao and Cui, Lei and Chen, Qifeng and Wei, Furu},
journal={arXiv preprint arXiv:2305.10855},
year={2023}
}
评论