daotthuanSE commited on
Commit
e0271b4
·
verified ·
1 Parent(s): c989635

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +137 -3
README.md CHANGED
@@ -1,3 +1,137 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - text-to-image
5
+ - text-generation
6
+ - text-editing
7
+ ---
8
+ # AnyText多语言文字生成与编辑
9
+
10
+ 近年来,随着AIGC的爆火,图片生成技术得到飞速发展,当前AI生成的图片已达到真假难辨的高保真度。不过,当合成图片中出现文字内容时,仍能够使AI露出马脚,因为当前主流方法尚无法在图片中生成准确可读的字符。最近半年来已有学者开始研究文本生成的问题,但这些方法大多以英文为主,无法解决中文这种字形繁杂、字符数以万计的文字生成。因此,我们提出了一种新颖的文字生成方法AnyText,通过创新性的算法设计,可以支持中文、英语、日语、韩语等多语言的文字生成,还支持对输入图片中的文字内容进行编辑。本模型所涉及的文字生成技术为电商海报、Logo设计、创意涂鸦、表情包等新型AIGC应用提供了可能性。
11
+
12
+ 代码链接:https://github.com/tyxsspa/AnyText
13
+
14
+ 论文链接:https://arxiv.org/abs/2311.03054
15
+
16
+ 在线Demo:[创空间](https://modelscope.cn/studios/damo/studio_anytext)(推荐), [HuggingFace](https://huggingface.co/spaces/modelscope/AnyText)
17
+
18
+ ![anytext](description/sample.jpg)
19
+
20
+ ## 模型介绍
21
+
22
+ AnyText主要基于扩散(Diffusion)模型,包含两个核心模块:隐空间辅助模块(Auxiliary Latent Module)和文本嵌入模块(Text Embedding Module)。其中,隐空间辅助模块对三类辅助信息(字形、文字位置和掩码图像)进行编码并构建隐空间特征图像,用来辅助视觉文字的生成;文本嵌入模块则将描述词中的语义部分与待生成文本的字形部分解耦,使用图像编码模块单独提取字形信息后再与语义信息做融合,既有助于文字的书写精度,也有利于提升文字与背景的一致性。训练阶段,除了使用扩散模型常用的噪声预测损失,我们还增加了文本感知损失,在图像空间对每个生成文本区域进行像素级的监督,以进一步提升文字书写精度。
23
+ ![framework](description/framework.jpg)
24
+
25
+ ## 期望模型使用方式以及使用范围
26
+
27
+ 本模型使用范围较广,能基于输入的提示词以及指定的位置生成包含多语言字符的图像,或对输入图像中的文本进行编辑或修复。
28
+
29
+ ### 环境配置
30
+
31
+ ```shell
32
+ # 安装git(如有请跳过)
33
+ conda install -c anaconda git
34
+ # 克隆anytext仓库
35
+ git clone https://github.com/tyxsspa/AnyText.git
36
+ cd AnyText
37
+ # 准备字库文件(推荐Arial Unicode MS,需自行下载)
38
+ mv your/path/to/arialuni.ttf ./font/Arial_Unicode.ttf
39
+ # 方式一:如果使用modelscope notebook最新镜像(ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.10.0),直接安装如下包即可
40
+ pip install Pillow==9.5.0
41
+ pip install gradio==3.50.0
42
+ # 方式二:重新创建一个独立虚拟环境(耗时较长)
43
+ conda env create -f environment.yaml
44
+ conda activate anytext
45
+ ```
46
+
47
+ ### 示例代码
48
+
49
+ 参照如下示例代码,使用anytext进行文字生成或文字编辑
50
+
51
+ (首次运行时,会下载模型文件至:~/.cache/modelscope/hub中,请耐心等待;如果需要修改下载目录,可以手动指定环境变量:MODELSCOPE_CACHE)
52
+
53
+ ```python
54
+ from modelscope.pipelines import pipeline
55
+ from util import save_images
56
+ pipe = pipeline('my-anytext-task', model='damo/cv_anytext_text_generation_editing', model_revision='v1.1.1', use_fp16=True, use_translator=False)
57
+ img_save_folder = "SaveImages"
58
+ params = {
59
+ "show_debug": True,
60
+ "image_count": 2,
61
+ "ddim_steps": 20,
62
+ }
63
+
64
+ # 1. text generation
65
+ mode = 'text-generation'
66
+ input_data = {
67
+ "prompt": 'photo of caramel macchiato coffee on the table, top-down perspective, with "Any" "Text" written on it using cream',
68
+ "seed": 66273235,
69
+ "draw_pos": 'example_images/gen9.png'
70
+ }
71
+ results, rtn_code, rtn_warning, debug_info = pipe(input_data, mode=mode, **params)
72
+ if rtn_code >= 0:
73
+ save_images(results, img_save_folder)
74
+ # 2. text editing
75
+ mode = 'text-editing'
76
+ input_data = {
77
+ "prompt": 'A cake with colorful characters that reads "EVERYDAY"',
78
+ "seed": 8943410,
79
+ "draw_pos": 'example_images/edit7.png',
80
+ "ori_image": 'example_images/ref7.jpg'
81
+ }
82
+ results, rtn_code, rtn_warning, debug_info = pipe(input_data, mode=mode, **params)
83
+ if rtn_code >= 0:
84
+ save_images(results, img_save_folder)
85
+ print(f'Done, result images are saved in: {img_save_folder}')
86
+ ```
87
+
88
+ ### 交互式Demo[推荐]
89
+ 使用基于gradio搭建的交互式demo,内含使用说明、操作界面及丰富的示例,可完整体验AnyText,执行如下语句启动:
90
+
91
+ ```bash
92
+ export CUDA_VISIBLE_DEVICES=0 && python demo.py
93
+ ```
94
+ 默认情况下使用FP16推理,并预加载一个中译英的模型用于直接输入中文提示词(占用约4G显存)。可通过指令修改默认行为,如执行如下语句将使用FP32推理,且不适用翻译模型:
95
+ ```bash
96
+ export CUDA_VISIBLE_DEVICES=0 && python demo.py --use_fp32 --no_translator
97
+ ```
98
+ 如果使用FP16推理,不加载翻译模型或使用CPU推理([方法](https://github.com/tyxsspa/AnyText/issues/33)),每次生成1张512x512的图片,显存占用约7.5G。
99
+ 此外支持用户使用其他字体文件(可能影响效果):
100
+ ```bash
101
+ export CUDA_VISIBLE_DEVICES=0 && python demo.py --font_path your/path/to/font/file.ttf
102
+ ```
103
+ 也可以直接访问如下在线Demo: [创空间](https://modelscope.cn/studios/damo/studio_anytext)(推荐), [HuggingFace](https://huggingface.co/spaces/modelscope/AnyText)
104
+
105
+ ### 画廊
106
+
107
+ ![gallary](description/gallary.png)
108
+
109
+ ### 模型局限性以及可能得偏差
110
+
111
+ * 由于训练数据的原因,当前模型对中文和英文的书写精度要好于其他语种;
112
+ * 有一定概率会产生文本模糊、扭曲、拼写错误等现象;
113
+ * 有一定概率在背景中生成不可读的伪文字或水印;
114
+ * 基于互联网公开数据的训练,生成结果与训练数据分布存在较大关联;
115
+
116
+ ## 训练数据介绍
117
+
118
+ 本模型的训练数据集为AnyWord-3M(即将开源),主要来源于互联网开源数据集,包括LAION-400M, Noah-Wukong以及部分OCR数据集,按照一定规则从中筛选出包含文字的图片,并使用OCR模型和BLIP-2模型进行全自动打标,总计得到300万高质量的图文对,涵盖自然图像、电影海报、书籍封面等各类场景。
119
+
120
+ ## 模型评测指标
121
+
122
+ 我们使用全句准确率(Sen. ACC)和归一化编辑距离(NED)评价生成文字的准确度,使用FID指标评价图像的生成质量。与现有方法相比,AnyText在中英文的文字生成方面均具备显著优势,达到SOTA水平。
123
+
124
+ ![eval](description/eval.jpg)
125
+
126
+ ## 相关论文及引用信息
127
+
128
+ ```BibTex
129
+ @article{tuo2023anytext,
130
+ title={AnyText: Multilingual Visual Text Generation And Editing},
131
+ author={Yuxiang Tuo and Wangmeng Xiang and Jun-Yan He and Yifeng Geng and Xuansong Xie},
132
+ year={2023},
133
+ eprint={2311.03054},
134
+ archivePrefix={arXiv},
135
+ primaryClass={cs.CV}
136
+ }
137
+ ```