|
1 | 1 | # AnyTextPipeline Pipeline |
2 | 2 |
|
3 | | -From the project [page](https://zhendong-wang.github.io/prompt-diffusion.github.io/) |
| 3 | +From the repo [page](https://github.com/tyxsspa/AnyText) |
4 | 4 |
|
5 | | -"With a prompt consisting of a task-specific example pair of images and text guidance, and a new query image, Prompt Diffusion can comprehend the desired task and generate the corresponding output image on both seen (trained) and unseen (new) task types." |
| 5 | +"AnyText comprises a diffusion pipeline with two primary elements: an auxiliary latent module and a text embedding module. The former uses inputs like text glyph, position, and masked image to generate latent features for text generation or editing. The latter employs an OCR model for encoding stroke data as embeddings, which blend with image caption embeddings from the tokenizer to generate texts that seamlessly integrate with the background. We employed text-control diffusion loss and text perceptual loss for training to further enhance writing accuracy." |
6 | 6 |
|
7 | | -For any usage questions, please refer to the [paper](https://arxiv.org/abs/2305.01115). |
8 | | - |
9 | | -Prepare models by converting them from the [checkpoint](https://huggingface.co/zhendongw/prompt-diffusion) |
10 | | - |
11 | | -To convert the controlnet, use cldm_v15.yaml from the [repository](https://github.com/Zhendong-Wang/Prompt-Diffusion/tree/main/models/): |
12 | | - |
13 | | -```sh |
14 | | -python convert_original_anytext_to_diffusers.py --checkpoint_path path-to-network-step04999.ckpt --original_config_file path-to-cldm_v15.yaml --dump_path path-to-output-directory |
15 | | -``` |
16 | | - |
17 | | -To learn about how to convert the fine-tuned stable diffusion model, see the [Load different Stable Diffusion formats guide](https://huggingface.co/docs/diffusers/main/en/using-diffusers/other-formats). |
| 7 | +For any usage questions, please refer to the [paper](https://arxiv.org/abs/2311.03054). |
18 | 8 |
|
19 | 9 |
|
20 | 10 | ```py |
21 | 11 | import torch |
22 | | -from pipeline_anytext import AnyTextPipeline |
23 | | -from text_controlnet import AnyTextControlNetModel |
| 12 | +from diffusers import DiffusionPipeline |
| 13 | +from anytext_controlnet import AnyTextControlNetModel |
24 | 14 | from diffusers import DDIMScheduler |
25 | 15 | from diffusers.utils import load_image |
26 | 16 |
|
27 | 17 |
|
28 | | -controlnet = AnyTextControlNetModel.from_pretrained("tolgacangoz/anytext-controlnet", torch_dtype=torch.float16, |
29 | | - variant="fp16") |
30 | | -pipe = AnyTextPipeline.from_pretrained("tolgacangoz/anytext", controlnet=controlnet, |
31 | | - torch_dtype=torch.float16, variant="fp16") |
| 18 | +# I chose a font file shared by an HF staff: |
| 19 | +!wget https://huggingface.co/spaces/ysharma/TranslateQuotesInImageForwards/resolve/main/arial-unicode-ms.ttf |
| 20 | + |
| 21 | +# load control net and stable diffusion v1-5 |
| 22 | +anytext_controlnet = AnyTextControlNetModel.from_pretrained("tolgacangoz/anytext-controlnet", torch_dtype=torch.float16, |
| 23 | + variant="fp16",) |
| 24 | +pipe = DiffusionPipeline.from_pretrained("tolgacangoz/anytext", font_path="arial-unicode-ms.ttf", |
| 25 | + controlnet=anytext_controlnet, torch_dtype=torch.float16, |
| 26 | + trust_remote_code=True, |
| 27 | + ).to("cuda") |
32 | 28 |
|
33 | | -# speed up diffusion process with faster scheduler and memory optimization |
34 | 29 | pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config) |
35 | | -# uncomment following line if torch<2.0 |
| 30 | +# uncomment following line if PyTorch>=2.0 is not installed for memory optimization |
36 | 31 | #pipe.enable_xformers_memory_efficient_attention() |
37 | | -pipe.enable_model_cpu_offload() |
| 32 | + |
| 33 | +# uncomment following line if you want to offload the model to CPU for memory optimization |
| 34 | +# also remove the `.to("cuda")` part |
| 35 | +#pipe.enable_model_cpu_offload() |
| 36 | + |
38 | 37 | # generate image |
39 | | -generator = torch.Generator("cpu").manual_seed(66273235) |
40 | 38 | prompt = 'photo of caramel macchiato coffee on the table, top-down perspective, with "Any" "Text" written on it using cream' |
41 | | -draw_pos = load_image("www.huggingface.co/a/AnyText/tree/main/examples/gen9.png") |
42 | | -image = pipe(prompt, num_inference_steps=20, generator=generator, mode="generate", draw_pos=draw_pos, |
43 | | - ).images[0] |
| 39 | +draw_pos = load_image("https://raw.githubusercontent.com/tyxsspa/AnyText/refs/heads/main/example_images/gen9.png") |
| 40 | +image = pipe(prompt, num_inference_steps=20, mode="generate", draw_pos=draw_pos, |
| 41 | + ).images[0] |
44 | 42 | image |
45 | 43 | ``` |
0 commit comments