File size: 3,111 Bytes
d8aacd8
 
 
 
 
 
 
 
 
 
 
 
7c0384a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
language:
- en
tags:
- text-to-image
- stable-diffusion
- safetensors
- stable-diffusion-xl
- animagine-xl
base_model: cagliostrolab/animagine-xl-3.0
---

# AnimagineXL-v3-openvino

This is an *unofficial* [OpenVINO](https://github.com/openvinotoolkit/openvino) variant of [cagliostrolab/animagine-xl-3.0](https://huggingface.co/cagliostrolab/animagine-xl-3.0). 

The repo is provided for convenience of running the Animagine XL v3 model on Intel CPU/GPU, as loading & converting a SDXL model to openvino can be pretty slow (dozens of minutes).

Table of contents:
- [Usage](#usage)
- [How the conversion was done](#how-the-conversion-was-done)
- [Appendix](#appendix)


## Usage

Take CPU for example:

```python
from optimum.intel.openvino import OVStableDiffusionXLPipeline
from diffusers import (
    EulerAncestralDiscreteScheduler,
    DPMSolverMultistepScheduler
)

model_id = "CodeChris/AnimagineXL-v3-openvino"
pipe = OVStableDiffusionXLPipeline.from_pretrained(model_model)
# Fix output image size & batch_size for faster speed
img_w, img_h = 832, 1216  # Example
pipe.reshape(width=img_w, height=img_h,
             batch_size=1, num_images_per_prompt=1)

## Change scheduler
# AnimagineXL recommand Euler A:
# pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
    pipe.scheduler.config,
    use_karras_sigmas=True,
    algorithm_type="dpmsolver++"
)  # I prefer DPM++ 2M Karras
# Turn off the filter
pipe.safety_checker = None

# If run on a GPU, you need:
# pipe.to('cuda')
```

After the pipe is prepared, a txt2img task can be executed as below:
```python
prompt = "1girl, dress, day, masterpiece, best quality"
negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name"

images = pipe(
    prompt,
    negative_prompt,
    # If reshaped, image size must equal the reshaped size
    width=img_w, height=img_h,
    guidance_scale=7,
    num_inference_steps=20
)
img = images[0]
img.save('sample.png')
```

For convenience, here is the recommended image sizes from the official AnimagineXL doc:

```
# Or their transpose
896 x 1152
832 x 1216
768 x 1344
640 x 1536
1024 x 1024
```

## How the conversion was done

First, install optimum:

```powershell
pip install --upgrade-strategy eager optimum[openvino,nncf]
```

Then, the repo is converted using the following command:

```powershell
optimum-cli export openvino --model 'cagliostrolab/animagine-xl-3.0' 'models/openvino/AnimagineXL-v3' --task 'stable-diffusion-xl'
```

## Appendix

Push large files **without** git commit the latest changes:

```
git lfs install
huggingface-cli lfs-enable-largefiles .
huggingface-cli upload --commit-message 'Upload model files' 'CodeChris/AnimagineXL-v3-openvino' .
```

Other notes:

* The conversion was done using `optimum==1.16.1` and `openvino==2023.2.0`.
* You may query `optimum-cli export openvino --help` for more usage details.