Paper99 commited on
Commit
9374e61
1 Parent(s): b94ca76

Initial commit: Update app.py and requirements

Browse files
LICENSE ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Tencent is pleased to support the open source community by making PhotoMaker available.
2
+
3
+ Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4
+
5
+ PhotoMaker is licensed under the Apache License Version 2.0 except for the third-party components listed below.
6
+
7
+
8
+ Terms of the Apache License Version 2.0:
9
+ ---------------------------------------------
10
+ Apache License
11
+
12
+ Version 2.0, January 2004
13
+
14
+ http://www.apache.org/licenses/
15
+
16
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
17
+ 1. Definitions.
18
+
19
+ “License” shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
20
+
21
+ “Licensor” shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
22
+
23
+ “Legal Entity” shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, “control” means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ “You” (or “Your”) shall mean an individual or Legal Entity exercising permissions granted by this License.
26
+
27
+ “Source” form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
28
+
29
+ “Object” form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
30
+
31
+ “Work” shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
32
+
33
+ “Derivative Works” shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
34
+
35
+ “Contribution” shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, “submitted” means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as “Not a Contribution.”
36
+
37
+ “Contributor” shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
38
+
39
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
40
+
41
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
42
+
43
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
44
+
45
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
46
+
47
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
48
+
49
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
50
+
51
+ If the Work includes a “NOTICE” text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
52
+
53
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
54
+
55
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
56
+
57
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
58
+
59
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
60
+
61
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
62
+
63
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
64
+
65
+ END OF TERMS AND CONDITIONS
app.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import random
4
+ import os
5
+
6
+ from diffusers.utils import load_image
7
+ from diffusers import DDIMScheduler
8
+
9
+ from huggingface_hub import hf_hub_download
10
+ import spaces
11
+ import gradio as gr
12
+
13
+ from pipeline import PhotoMakerStableDiffusionXLPipeline
14
+ from style_template import styles
15
+
16
+ # global variable
17
+ base_model_path = 'SG161222/RealVisXL_V3.0'
18
+ device = "cuda"
19
+ MAX_SEED = np.iinfo(np.int32).max
20
+ STYLE_NAMES = list(styles.keys())
21
+ DEFAULT_STYLE_NAME = "Photographic (Default)"
22
+
23
+ # download PhotoMaker checkpoint to cache
24
+ photomaker_ckpt = hf_hub_download(repo_id="TencentARC/PhotoMaker", filename="photomaker-v1.bin", repo_type="model")
25
+
26
+ pipe = PhotoMakerStableDiffusionXLPipeline.from_pretrained(
27
+ base_model_path,
28
+ torch_dtype=torch.bfloat16,
29
+ use_safetensors=True,
30
+ variant="fp16",
31
+ ).to(device)
32
+
33
+ pipe.load_photomaker_adapter(
34
+ os.path.dirname(photomaker_ckpt),
35
+ subfolder="",
36
+ weight_name=os.path.basename(photomaker_ckpt),
37
+ trigger_word="img"
38
+ )
39
+
40
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
41
+ # pipe.set_adapters(["photomaker"], adapter_weights=[1.0])
42
+ pipe.fuse_lora()
43
+
44
+ @spaces.GPU(enable_queue=True)
45
+ def generate_image(upload_images, prompt, negative_prompt, style_name, num_steps, style_strength_ratio, num_outputs, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
46
+ # check the trigger word
47
+ image_token_id = pipe.tokenizer.convert_tokens_to_ids(pipe.trigger_word)
48
+ input_ids = pipe.tokenizer.encode(prompt)
49
+ if image_token_id not in input_ids:
50
+ raise gr.Error(f"Cannot find the trigger word '{pipe.trigger_word}' in text prompt! Please refer to step 2️⃣")
51
+
52
+ if input_ids.count(image_token_id) > 1:
53
+ raise gr.Error(f"Cannot use multiple trigger words '{pipe.trigger_word}' in text prompt!")
54
+
55
+ # apply the style template
56
+ prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
57
+
58
+ if upload_images is None:
59
+ raise gr.Error(f"Cannot find any input face image! Please refer to step 1️⃣")
60
+
61
+ input_id_images = []
62
+ for img in upload_images:
63
+ input_id_images.append(load_image(img))
64
+
65
+ generator = torch.Generator(device=device).manual_seed(seed)
66
+
67
+ print("Start inference...")
68
+ print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
69
+ start_merge_step = int(float(style_strength_ratio) / 100 * num_steps)
70
+ if start_merge_step > 30:
71
+ start_merge_step = 30
72
+ print(start_merge_step)
73
+ images = pipe(
74
+ prompt=prompt,
75
+ input_id_images=input_id_images,
76
+ negative_prompt=negative_prompt,
77
+ num_images_per_prompt=num_outputs,
78
+ num_inference_steps=num_steps,
79
+ start_merge_step=start_merge_step,
80
+ generator=generator,
81
+ guidance_scale=guidance_scale,
82
+ ).images
83
+ return images, gr.update(visible=True)
84
+
85
+ def swap_to_gallery(images):
86
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
87
+
88
+ def upload_example_to_gallery(images, prompt, style, negative_prompt):
89
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
90
+
91
+ def remove_back_to_files():
92
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
93
+
94
+ def remove_tips():
95
+ return gr.update(visible=False)
96
+
97
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
98
+ if randomize_seed:
99
+ seed = random.randint(0, MAX_SEED)
100
+ return seed
101
+
102
+ def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
103
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
104
+ return p.replace("{prompt}", positive), n + ' ' + negative
105
+
106
+ def get_image_path_list(folder_name):
107
+ image_basename_list = os.listdir(folder_name)
108
+ image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
109
+ return image_path_list
110
+
111
+ def get_example():
112
+ case = [
113
+ [
114
+ get_image_path_list('./examples/scarletthead_woman'),
115
+ "instagram photo, portrait photo of a woman img, colorful, perfect face, natural skin, hard shadows, film grain",
116
+ "(No style)",
117
+ "(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
118
+ ],
119
+ [
120
+ get_image_path_list('./examples/newton_man'),
121
+ "sci-fi, closeup portrait photo of a man img wearing the sunglasses in Iron man suit, face, slim body, high quality, film grain",
122
+ "(No style)",
123
+ "(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
124
+ ],
125
+ ]
126
+ return case
127
+
128
+ ### Description and style
129
+ logo = r"""
130
+ <center><img src='https://photo-maker.github.io/assets/logo.png' alt='PhotoMaker logo' style="width:80px; margin-bottom:10px"></center>
131
+ """
132
+ title = r"""<h1 align="center">PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</h1>"""
133
+
134
+ description = r"""
135
+ <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'><b>PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</b></a>.<br>
136
+ <br>
137
+ ❗️❗️❗️[<b>Important</b>] Personalization steps:<br>
138
+ 1️⃣ Upload images of someone you want to customize. One image is ok, but more is better. Although we do not perform face detection, the face in the uploaded image should <b>occupy the majority of the image</b>.<br>
139
+ 2️⃣ Enter a text prompt, making sure to <b>follow the class word</b> you want to customize with the <b>trigger word</b>: `img`, such as: `man img` or `woman img` or `girl img`.<br>
140
+ 3️⃣ Choose your preferred style template.<br>
141
+ 4️⃣ Click the <b>Submit</b> button to start customizing.
142
+ """
143
+
144
+ article = r"""
145
+
146
+ If PhotoMaker is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'>Github Repo</a>. Thanks!
147
+ [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/PhotoMaker?style=social)](https://github.com/TencentARC/PhotoMaker)
148
+ ---
149
+ 📝 **Citation**
150
+ <br>
151
+ If our work is useful for your research, please consider citing:
152
+
153
+ ```bibtex
154
+ @article{li2023photomaker,
155
+ title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
156
+ author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
157
+ booktitle={arXiv preprint arxiv:2312.04461},
158
+ year={2023}
159
+ }
160
+ ```
161
+ 📋 **License**
162
+
163
+ 📧 **Contact**
164
+ <br>
165
+ If you have any questions, please feel free to reach me out at <b>zhenli1031@gmail.com</b>.
166
+ """
167
+
168
+ tips = r"""
169
+ ### Usage tips of PhotoMaker
170
+ 1. Upload more photos of the person to be customized to **improve ID fidelty**. If the input is Asian face(s), maybe consider adding 'asian' before the class word, e.g., `asian woman img`
171
+ 2. When stylizing, does the generated face look too realistic? Try switching to our **other gradio demo** [PhotoMaker-Style](). Adjust the **Style strength** to 30-50, the larger the number, the less ID fidelty, but the stylization ability will be better.
172
+ 3. For **faster** speed, reduce the number of generated images and sampling steps. However, please note that reducing the sampling steps may compromise the ID fidelity.
173
+ """
174
+ # We have provided some generate examples and comparisons at: [this website]().
175
+ # 3. Don't make the prompt too long, as we will trim it if it exceeds 77 tokens.
176
+ # 4. When generating realistic photos, if it's not real enough, try switching to our other gradio application [PhotoMaker-Realistic]().
177
+
178
+ css = '''
179
+ .gradio-container {width: 85% !important}
180
+ '''
181
+ with gr.Blocks(css=css) as demo:
182
+ gr.Markdown(logo)
183
+ gr.Markdown(title)
184
+ gr.Markdown(description)
185
+ # gr.DuplicateButton(
186
+ # value="Duplicate Space for private use ",
187
+ # elem_id="duplicate-button",
188
+ # visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
189
+ # )
190
+ with gr.Row():
191
+ with gr.Column():
192
+ files = gr.Files(
193
+ label="Drag (Select) 1 or more photos of your face",
194
+ file_types=["image"]
195
+ )
196
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
197
+ with gr.Column(visible=False) as clear_button:
198
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
199
+ prompt = gr.Textbox(label="Prompt",
200
+ info="Try something like 'a photo of a man/woman img' instead of 'A photo of a man/woman'",
201
+ placeholder="A photo of a [man/woman img]...")
202
+ style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
203
+ submit = gr.Button("Submit")
204
+
205
+ with gr.Accordion(open=False, label="Advanced Options"):
206
+ negative_prompt = gr.Textbox(
207
+ label="Negative Prompt",
208
+ placeholder="low quality",
209
+ value="nsfw, 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",
210
+ )
211
+ num_steps = gr.Slider(
212
+ label="Number of sample steps",
213
+ minimum=20,
214
+ maximum=100,
215
+ step=1,
216
+ value=50,
217
+ )
218
+ style_strength_ratio = gr.Slider(
219
+ label="Style strength (%)",
220
+ minimum=15,
221
+ maximum=50,
222
+ step=1,
223
+ value=20,
224
+ )
225
+ num_outputs = gr.Slider(
226
+ label="Number of output images",
227
+ minimum=1,
228
+ maximum=4,
229
+ step=1,
230
+ value=4,
231
+ )
232
+ guidance_scale = gr.Slider(
233
+ label="Guidance scale",
234
+ minimum=0.1,
235
+ maximum=10.0,
236
+ step=0.1,
237
+ value=5,
238
+ )
239
+ seed = gr.Slider(
240
+ label="Seed",
241
+ minimum=0,
242
+ maximum=MAX_SEED,
243
+ step=1,
244
+ value=0,
245
+ )
246
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
247
+ with gr.Column():
248
+ gallery = gr.Gallery(label="Generated Images")
249
+ usage_tips = gr.Markdown(label="Usage tips of PhotoMaker", value=tips ,visible=False)
250
+
251
+ files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
252
+ remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
253
+
254
+ submit.click(
255
+ fn=remove_tips,
256
+ outputs=usage_tips,
257
+ ).then(
258
+ fn=randomize_seed_fn,
259
+ inputs=[seed, randomize_seed],
260
+ outputs=seed,
261
+ queue=False,
262
+ api_name=False,
263
+ ).then(
264
+ fn=generate_image,
265
+ inputs=[files, prompt, negative_prompt, style, num_steps, style_strength_ratio, num_outputs, guidance_scale, seed],
266
+ outputs=[gallery, usage_tips]
267
+ )
268
+
269
+ gr.Examples(
270
+ examples=get_example(),
271
+ inputs=[files, prompt, style, negative_prompt],
272
+ run_on_click=True,
273
+ fn=upload_example_to_gallery,
274
+ outputs=[uploaded_files, clear_button, files],
275
+ )
276
+
277
+ gr.Markdown(article)
278
+
279
+ demo.launch()
examples/newton_man/newton_0.jpg ADDED
examples/newton_man/newton_1.jpg ADDED
examples/newton_man/newton_3.jpg ADDED
examples/scarletthead_woman/scarlett_0.jpg ADDED
examples/scarletthead_woman/scarlett_1.jpg ADDED
examples/scarletthead_woman/scarlett_2.jpg ADDED
examples/scarletthead_woman/scarlett_3.jpg ADDED
model.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Merge image encoder and fuse module to create a ID Encoder
2
+ # send multiple ID images, we can directly obtain the updated text encoder containing a stacked ID embedding
3
+
4
+ import torch
5
+ import torch.nn as nn
6
+ from transformers.models.clip.modeling_clip import CLIPVisionModelWithProjection
7
+ from transformers.models.clip.configuration_clip import CLIPVisionConfig
8
+ from transformers import PretrainedConfig
9
+
10
+ VISION_CONFIG_DICT = {
11
+ "hidden_size": 1024,
12
+ "intermediate_size": 4096,
13
+ "num_attention_heads": 16,
14
+ "num_hidden_layers": 24,
15
+ "patch_size": 14,
16
+ "projection_dim": 768
17
+ }
18
+
19
+ class MLP(nn.Module):
20
+ def __init__(self, in_dim, out_dim, hidden_dim, use_residual=True):
21
+ super().__init__()
22
+ if use_residual:
23
+ assert in_dim == out_dim
24
+ self.layernorm = nn.LayerNorm(in_dim)
25
+ self.fc1 = nn.Linear(in_dim, hidden_dim)
26
+ self.fc2 = nn.Linear(hidden_dim, out_dim)
27
+ self.use_residual = use_residual
28
+ self.act_fn = nn.GELU()
29
+
30
+ def forward(self, x):
31
+ residual = x
32
+ x = self.layernorm(x)
33
+ x = self.fc1(x)
34
+ x = self.act_fn(x)
35
+ x = self.fc2(x)
36
+ if self.use_residual:
37
+ x = x + residual
38
+ return x
39
+
40
+
41
+ class FuseModule(nn.Module):
42
+ def __init__(self, embed_dim):
43
+ super().__init__()
44
+ self.mlp1 = MLP(embed_dim * 2, embed_dim, embed_dim, use_residual=False)
45
+ self.mlp2 = MLP(embed_dim, embed_dim, embed_dim, use_residual=True)
46
+ self.layer_norm = nn.LayerNorm(embed_dim)
47
+
48
+ def fuse_fn(self, prompt_embeds, id_embeds):
49
+ print(prompt_embeds.shape, id_embeds.shape)
50
+ stacked_id_embeds = torch.cat([prompt_embeds, id_embeds], dim=-1)
51
+ stacked_id_embeds = self.mlp1(stacked_id_embeds) + prompt_embeds
52
+ stacked_id_embeds = self.mlp2(stacked_id_embeds)
53
+ stacked_id_embeds = self.layer_norm(stacked_id_embeds)
54
+ return stacked_id_embeds
55
+
56
+ def forward(
57
+ self,
58
+ prompt_embeds,
59
+ id_embeds,
60
+ class_tokens_mask,
61
+ ) -> torch.Tensor:
62
+ # id_embeds shape: [b, max_num_inputs, 1, 2048]
63
+ id_embeds = id_embeds.to(prompt_embeds.dtype)
64
+ num_inputs = class_tokens_mask.sum().unsqueeze(0) # TODO: check for training case
65
+ batch_size, max_num_inputs = id_embeds.shape[:2]
66
+ # seq_length: 77
67
+ seq_length = prompt_embeds.shape[1]
68
+ # flat_id_embeds shape: [b*max_num_inputs, 1, 2048]
69
+ flat_id_embeds = id_embeds.view(
70
+ -1, id_embeds.shape[-2], id_embeds.shape[-1]
71
+ )
72
+ # valid_id_mask [b*max_num_inputs]
73
+ valid_id_mask = (
74
+ torch.arange(max_num_inputs, device=flat_id_embeds.device)[None, :]
75
+ < num_inputs[:, None]
76
+ )
77
+ valid_id_embeds = flat_id_embeds[valid_id_mask.flatten()]
78
+
79
+ prompt_embeds = prompt_embeds.view(-1, prompt_embeds.shape[-1])
80
+ class_tokens_mask = class_tokens_mask.view(-1)
81
+ valid_id_embeds = valid_id_embeds.view(-1, valid_id_embeds.shape[-1])
82
+ # slice out the image token embeddings
83
+ image_token_embeds = prompt_embeds[class_tokens_mask]
84
+ stacked_id_embeds = self.fuse_fn(image_token_embeds, valid_id_embeds)
85
+ assert class_tokens_mask.sum() == stacked_id_embeds.shape[0], f"{class_tokens_mask.sum()} != {stacked_id_embeds.shape[0]}"
86
+ prompt_embeds.masked_scatter_(class_tokens_mask[:, None], stacked_id_embeds.to(prompt_embeds.dtype))
87
+ updated_prompt_embeds = prompt_embeds.view(batch_size, seq_length, -1)
88
+ return updated_prompt_embeds
89
+
90
+ class PhotoMakerIDEncoder(CLIPVisionModelWithProjection):
91
+ def __init__(self):
92
+ super().__init__(CLIPVisionConfig(**VISION_CONFIG_DICT))
93
+ self.visual_projection_2 = nn.Linear(1024, 1280, bias=False)
94
+ self.fuse_module = FuseModule(2048)
95
+
96
+ def forward(self, id_pixel_values, prompt_embeds, class_tokens_mask):
97
+ b, num_inputs, c, h, w = id_pixel_values.shape
98
+ id_pixel_values = id_pixel_values.view(b * num_inputs, c, h, w)
99
+
100
+ shared_id_embeds = self.vision_model(id_pixel_values)[1]
101
+ id_embeds = self.visual_projection(shared_id_embeds)
102
+ id_embeds_2 = self.visual_projection_2(shared_id_embeds)
103
+
104
+ id_embeds = id_embeds.view(b, num_inputs, 1, -1)
105
+ id_embeds_2 = id_embeds_2.view(b, num_inputs, 1, -1)
106
+
107
+ id_embeds = torch.cat((id_embeds, id_embeds_2), dim=-1)
108
+ updated_prompt_embeds = self.fuse_module(prompt_embeds, id_embeds, class_tokens_mask)
109
+
110
+ return updated_prompt_embeds
111
+
112
+
113
+ if __name__ == "__main__":
114
+ PhotoMakerIDEncoder()
pipeline.py ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Callable, Dict, List, Optional, Union, Tuple
2
+ from collections import OrderedDict
3
+ import os
4
+ import PIL
5
+ import numpy as np
6
+
7
+ import torch
8
+ from torchvision import transforms as T
9
+
10
+ from safetensors import safe_open
11
+ from huggingface_hub.utils import validate_hf_hub_args
12
+ from transformers import CLIPImageProcessor, CLIPTokenizer
13
+ from diffusers import StableDiffusionXLPipeline
14
+ from diffusers.pipelines.stable_diffusion_xl import StableDiffusionXLPipelineOutput
15
+ from diffusers.utils import (
16
+ _get_model_file,
17
+ is_transformers_available,
18
+ logging,
19
+ )
20
+
21
+ from model import PhotoMakerIDEncoder
22
+
23
+ PipelineImageInput = Union[
24
+ PIL.Image.Image,
25
+ torch.FloatTensor,
26
+ List[PIL.Image.Image],
27
+ List[torch.FloatTensor],
28
+ ]
29
+
30
+
31
+ class PhotoMakerStableDiffusionXLPipeline(StableDiffusionXLPipeline):
32
+ @validate_hf_hub_args
33
+ def load_photomaker_adapter(
34
+ self,
35
+ pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
36
+ weight_name: str,
37
+ subfolder: str = '',
38
+ trigger_word: str = 'img',
39
+ **kwargs,
40
+ ):
41
+ """
42
+ #TODO
43
+ Parameters:
44
+ pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
45
+ Can be either:
46
+
47
+ - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
48
+ the Hub.
49
+ - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
50
+ with [`ModelMixin.save_pretrained`].
51
+ - A [torch state
52
+ dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
53
+
54
+ weight_name (`str`):
55
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
56
+
57
+ subfolder (`str`, defaults to `""`):
58
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
59
+
60
+ trigger_word (`str`, *optional*, defaults to `"img"`):
61
+ The subfolder location of a model file within a larger model repository on the Hub or locally.
62
+ """
63
+
64
+ # Load the main state dict first.
65
+ cache_dir = kwargs.pop("cache_dir", None)
66
+ force_download = kwargs.pop("force_download", False)
67
+ resume_download = kwargs.pop("resume_download", False)
68
+ proxies = kwargs.pop("proxies", None)
69
+ local_files_only = kwargs.pop("local_files_only", None)
70
+ token = kwargs.pop("token", None)
71
+ revision = kwargs.pop("revision", None)
72
+
73
+ user_agent = {
74
+ "file_type": "attn_procs_weights",
75
+ "framework": "pytorch",
76
+ }
77
+
78
+ if not isinstance(pretrained_model_name_or_path_or_dict, dict):
79
+ model_file = _get_model_file(
80
+ pretrained_model_name_or_path_or_dict,
81
+ weights_name=weight_name,
82
+ cache_dir=cache_dir,
83
+ force_download=force_download,
84
+ resume_download=resume_download,
85
+ proxies=proxies,
86
+ local_files_only=local_files_only,
87
+ token=token,
88
+ revision=revision,
89
+ subfolder=subfolder,
90
+ user_agent=user_agent,
91
+ )
92
+ if weight_name.endswith(".safetensors"):
93
+ state_dict = {"id_encoder": {}, "lora_weights": {}}
94
+ with safe_open(model_file, framework="pt", device="cpu") as f:
95
+ for key in f.keys():
96
+ if key.startswith("id_encoder."):
97
+ state_dict["id_encoder"][key.replace("id_encoder.", "")] = f.get_tensor(key)
98
+ elif key.startswith("lora_weights."):
99
+ state_dict["lora_weights"][key.replace("lora_weights.", "")] = f.get_tensor(key)
100
+ else:
101
+ state_dict = torch.load(model_file, map_location="cpu")
102
+ else:
103
+ state_dict = pretrained_model_name_or_path_or_dict
104
+
105
+ keys = list(state_dict.keys())
106
+ if keys != ["id_encoder", "lora_weights"]:
107
+ raise ValueError("Required keys are (`id_encoder` and `lora_weights`) missing from the state dict.")
108
+
109
+ self.trigger_word = trigger_word
110
+ # load finetuned CLIP image encoder and fuse module here if it has not been registered to the pipeline yet
111
+ print(f"Loading PhotoMaker components [1] id_encoder from [{pretrained_model_name_or_path_or_dict}]...")
112
+ id_encoder = PhotoMakerIDEncoder()
113
+ id_encoder.load_state_dict(state_dict["id_encoder"], strict=True)
114
+ id_encoder = id_encoder.to(self.device, dtype=self.unet.dtype)
115
+ self.id_encoder = id_encoder
116
+ self.id_image_processor = CLIPImageProcessor()
117
+
118
+ # load lora into models
119
+ print(f"Loading PhotoMaker components [2] lora_weights from [{pretrained_model_name_or_path_or_dict}]")
120
+ self.load_lora_weights(state_dict["lora_weights"], adapter_name="photomaker")
121
+
122
+ # Add trigger word token
123
+ if self.tokenizer is not None:
124
+ self.tokenizer.add_tokens([self.trigger_word], special_tokens=True)
125
+
126
+ self.tokenizer_2.add_tokens([self.trigger_word], special_tokens=True)
127
+
128
+
129
+ def encode_prompt_with_trigger_word(
130
+ self,
131
+ prompt: str,
132
+ prompt_2: Optional[str] = None,
133
+ num_id_images: int = 1,
134
+ device: Optional[torch.device] = None,
135
+ prompt_embeds: Optional[torch.FloatTensor] = None,
136
+ pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
137
+ class_tokens_mask: Optional[torch.LongTensor] = None,
138
+ ):
139
+ device = device or self._execution_device
140
+
141
+ if prompt is not None and isinstance(prompt, str):
142
+ batch_size = 1
143
+ elif prompt is not None and isinstance(prompt, list):
144
+ batch_size = len(prompt)
145
+ else:
146
+ batch_size = prompt_embeds.shape[0]
147
+
148
+ # Find the token id of the trigger word
149
+ image_token_id = self.tokenizer_2.convert_tokens_to_ids(self.trigger_word)
150
+
151
+ # Define tokenizers and text encoders
152
+ tokenizers = [self.tokenizer, self.tokenizer_2] if self.tokenizer is not None else [self.tokenizer_2]
153
+ text_encoders = (
154
+ [self.text_encoder, self.text_encoder_2] if self.text_encoder is not None else [self.text_encoder_2]
155
+ )
156
+
157
+ if prompt_embeds is None:
158
+ prompt_2 = prompt_2 or prompt
159
+ prompt_embeds_list = []
160
+ prompts = [prompt, prompt_2]
161
+ for prompt, tokenizer, text_encoder in zip(prompts, tokenizers, text_encoders):
162
+ input_ids = tokenizer.encode(prompt) # TODO: batch encode
163
+ clean_index = 0
164
+ clean_input_ids = []
165
+ class_token_index = []
166
+ # Find out the corrresponding class word token based on the newly added trigger word token
167
+ for i, token_id in enumerate(input_ids):
168
+ if token_id == image_token_id:
169
+ class_token_index.append(clean_index - 1)
170
+ else:
171
+ clean_input_ids.append(token_id)
172
+ clean_index += 1
173
+
174
+ if len(class_token_index) != 1:
175
+ raise ValueError(
176
+ f"PhotoMaker currently does not support multiple trigger words in a single prompt.\
177
+ Trigger word: {self.trigger_word}, Prompt: {prompt}."
178
+ )
179
+ class_token_index = class_token_index[0]
180
+
181
+ # Expand the class word token and corresponding mask
182
+ class_token = clean_input_ids[class_token_index]
183
+ clean_input_ids = clean_input_ids[:class_token_index] + [class_token] * num_id_images + \
184
+ clean_input_ids[class_token_index+1:]
185
+
186
+ # Truncation or padding
187
+ max_len = tokenizer.model_max_length
188
+ if len(clean_input_ids) > max_len:
189
+ clean_input_ids = clean_input_ids[:max_len]
190
+ else:
191
+ clean_input_ids = clean_input_ids + [tokenizer.pad_token_id] * (
192
+ max_len - len(clean_input_ids)
193
+ )
194
+
195
+ class_tokens_mask = [True if class_token_index <= i < class_token_index+num_id_images else False \
196
+ for i in range(len(clean_input_ids))]
197
+
198
+ clean_input_ids = torch.tensor(clean_input_ids, dtype=torch.long).unsqueeze(0)
199
+ class_tokens_mask = torch.tensor(class_tokens_mask, dtype=torch.bool).unsqueeze(0)
200
+
201
+ prompt_embeds = text_encoder(
202
+ clean_input_ids.to(device),
203
+ output_hidden_states=True,
204
+ )
205
+
206
+ # We are only ALWAYS interested in the pooled output of the final text encoder
207
+ pooled_prompt_embeds = prompt_embeds[0]
208
+ prompt_embeds = prompt_embeds.hidden_states[-2]
209
+ prompt_embeds_list.append(prompt_embeds)
210
+
211
+ prompt_embeds = torch.concat(prompt_embeds_list, dim=-1)
212
+
213
+ prompt_embeds = prompt_embeds.to(dtype=self.text_encoder_2.dtype, device=device)
214
+ class_tokens_mask = class_tokens_mask.to(device=device) # TODO: ignoring two-prompt case
215
+
216
+ return prompt_embeds, pooled_prompt_embeds, class_tokens_mask
217
+
218
+
219
+ @torch.no_grad()
220
+ def __call__(
221
+ self,
222
+ prompt: Union[str, List[str]] = None,
223
+ prompt_2: Optional[Union[str, List[str]]] = None,
224
+ height: Optional[int] = None,
225
+ width: Optional[int] = None,
226
+ num_inference_steps: int = 50,
227
+ denoising_end: Optional[float] = None,
228
+ guidance_scale: float = 5.0,
229
+ negative_prompt: Optional[Union[str, List[str]]] = None,
230
+ negative_prompt_2: Optional[Union[str, List[str]]] = None,
231
+ num_images_per_prompt: Optional[int] = 1,
232
+ eta: float = 0.0,
233
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
234
+ latents: Optional[torch.FloatTensor] = None,
235
+ prompt_embeds: Optional[torch.FloatTensor] = None,
236
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
237
+ pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
238
+ negative_pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
239
+ output_type: Optional[str] = "pil",
240
+ return_dict: bool = True,
241
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
242
+ guidance_rescale: float = 0.0,
243
+ original_size: Optional[Tuple[int, int]] = None,
244
+ crops_coords_top_left: Tuple[int, int] = (0, 0),
245
+ target_size: Optional[Tuple[int, int]] = None,
246
+ callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
247
+ callback_steps: int = 1,
248
+ # Added parameters (for PhotoMaker)
249
+ input_id_images: PipelineImageInput = None,
250
+ class_tokens_mask: Optional[torch.LongTensor] = None,
251
+ prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
252
+ pooled_prompt_embeds_text_only: Optional[torch.FloatTensor] = None,
253
+ start_merge_step: int = 0,
254
+ ):
255
+ # TODO: doc
256
+ # 0. Default height and width to unet
257
+ height = height or self.unet.config.sample_size * self.vae_scale_factor
258
+ width = width or self.unet.config.sample_size * self.vae_scale_factor
259
+
260
+ original_size = original_size or (height, width)
261
+ target_size = target_size or (height, width)
262
+
263
+ # 1. Check inputs. Raise error if not correct
264
+ self.check_inputs(
265
+ prompt,
266
+ prompt_2,
267
+ height,
268
+ width,
269
+ callback_steps,
270
+ negative_prompt,
271
+ negative_prompt_2,
272
+ prompt_embeds,
273
+ negative_prompt_embeds,
274
+ pooled_prompt_embeds,
275
+ negative_pooled_prompt_embeds,
276
+ )
277
+ #
278
+ if prompt_embeds is not None and class_tokens_mask is None:
279
+ raise ValueError(
280
+ "If `prompt_embeds` are provided, `class_tokens_mask` also have to be passed. Make sure to generate `class_tokens_mask` from the same tokenizer that was used to generate `prompt_embeds`."
281
+ )
282
+ # check the input id images
283
+ if input_id_images is None:
284
+ raise ValueError(
285
+ "Provide `input_id_images`. Cannot leave `input_id_images` undefined for PhotoMaker pipeline."
286
+ )
287
+ if not isinstance(input_id_images, list):
288
+ input_id_images = [input_id_images]
289
+
290
+ # 2. Define call parameters
291
+ if prompt is not None and isinstance(prompt, str):
292
+ batch_size = 1
293
+ elif prompt is not None and isinstance(prompt, list):
294
+ batch_size = len(prompt)
295
+ else:
296
+ batch_size = prompt_embeds.shape[0]
297
+
298
+ device = self._execution_device
299
+
300
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
301
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
302
+ # corresponds to doing no classifier free guidance.
303
+ do_classifier_free_guidance = guidance_scale > 1.0
304
+
305
+ assert do_classifier_free_guidance
306
+
307
+ # 3. Encode input prompt
308
+ num_id_images = len(input_id_images)
309
+
310
+ (
311
+ prompt_embeds,
312
+ pooled_prompt_embeds,
313
+ class_tokens_mask,
314
+ ) = self.encode_prompt_with_trigger_word(
315
+ prompt=prompt,
316
+ prompt_2=prompt_2,
317
+ device=device,
318
+ num_id_images=num_id_images,
319
+ prompt_embeds=prompt_embeds,
320
+ pooled_prompt_embeds=pooled_prompt_embeds,
321
+ class_tokens_mask=class_tokens_mask,
322
+ )
323
+
324
+ # 4. Encode input prompt without the trigger word for delayed conditioning
325
+ prompt_text_only = prompt.replace(" "+self.trigger_word, "") # sensitive to white space
326
+ (
327
+ prompt_embeds_text_only,
328
+ negative_prompt_embeds,
329
+ pooled_prompt_embeds_text_only, # TODO: replace the pooled_prompt_embeds with text only prompt
330
+ negative_pooled_prompt_embeds,
331
+ ) = self.encode_prompt(
332
+ prompt=prompt_text_only,
333
+ prompt_2=prompt_2,
334
+ device=device,
335
+ num_images_per_prompt=num_images_per_prompt,
336
+ do_classifier_free_guidance=do_classifier_free_guidance,
337
+ negative_prompt=negative_prompt,
338
+ negative_prompt_2=negative_prompt_2,
339
+ prompt_embeds=prompt_embeds_text_only,
340
+ negative_prompt_embeds=negative_prompt_embeds,
341
+ pooled_prompt_embeds=pooled_prompt_embeds_text_only,
342
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
343
+ )
344
+
345
+ # 5. Prepare the input ID images
346
+ dtype = next(self.id_encoder.parameters()).dtype
347
+ if not isinstance(input_id_images[0], torch.Tensor):
348
+ id_pixel_values = self.id_image_processor(input_id_images, return_tensors="pt").pixel_values
349
+
350
+ id_pixel_values = id_pixel_values.unsqueeze(0).to(device=device, dtype=dtype) # TODO: multiple prompts
351
+
352
+ # 6. Get the update text embedding with the stacked ID embedding
353
+ prompt_embeds = self.id_encoder(id_pixel_values, prompt_embeds, class_tokens_mask)
354
+
355
+ bs_embed, seq_len, _ = prompt_embeds.shape
356
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
357
+ prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
358
+ prompt_embeds = prompt_embeds.view(bs_embed * num_images_per_prompt, seq_len, -1)
359
+ pooled_prompt_embeds = pooled_prompt_embeds.repeat(1, num_images_per_prompt).view(
360
+ bs_embed * num_images_per_prompt, -1
361
+ )
362
+
363
+ # 7. Prepare timesteps
364
+ self.scheduler.set_timesteps(num_inference_steps, device=device)
365
+ timesteps = self.scheduler.timesteps
366
+
367
+ # 8. Prepare latent variables
368
+ num_channels_latents = self.unet.config.in_channels
369
+ latents = self.prepare_latents(
370
+ batch_size * num_images_per_prompt,
371
+ num_channels_latents,
372
+ height,
373
+ width,
374
+ prompt_embeds.dtype,
375
+ device,
376
+ generator,
377
+ latents,
378
+ )
379
+
380
+ # 9. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
381
+ extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
382
+
383
+ # 10. Prepare added time ids & embeddings
384
+ if self.text_encoder_2 is None:
385
+ text_encoder_projection_dim = int(pooled_prompt_embeds.shape[-1])
386
+ else:
387
+ text_encoder_projection_dim = self.text_encoder_2.config.projection_dim
388
+
389
+ add_time_ids = self._get_add_time_ids(
390
+ original_size,
391
+ crops_coords_top_left,
392
+ target_size,
393
+ dtype=prompt_embeds.dtype,
394
+ text_encoder_projection_dim=text_encoder_projection_dim,
395
+ )
396
+ add_time_ids = torch.cat([add_time_ids, add_time_ids], dim=0)
397
+ add_time_ids = add_time_ids.to(device).repeat(batch_size * num_images_per_prompt, 1)
398
+
399
+ # 11. Denoising loop
400
+ num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
401
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
402
+ for i, t in enumerate(timesteps):
403
+ latent_model_input = (
404
+ torch.cat([latents] * 2) if do_classifier_free_guidance else latents
405
+ )
406
+ latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
407
+
408
+ if i <= start_merge_step:
409
+ current_prompt_embeds = torch.cat(
410
+ [negative_prompt_embeds, prompt_embeds_text_only], dim=0
411
+ )
412
+ add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds_text_only], dim=0)
413
+ else:
414
+ current_prompt_embeds = torch.cat(
415
+ [negative_prompt_embeds, prompt_embeds], dim=0
416
+ )
417
+ add_text_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds], dim=0)
418
+ # predict the noise residual
419
+ added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
420
+ noise_pred = self.unet(
421
+ latent_model_input,
422
+ t,
423
+ encoder_hidden_states=current_prompt_embeds,
424
+ cross_attention_kwargs=cross_attention_kwargs,
425
+ added_cond_kwargs=added_cond_kwargs,
426
+ return_dict=False,
427
+ )[0]
428
+
429
+ # perform guidance
430
+ if do_classifier_free_guidance:
431
+ noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
432
+ noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
433
+
434
+ if do_classifier_free_guidance and guidance_rescale > 0.0:
435
+ # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
436
+ noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=guidance_rescale)
437
+
438
+ # compute the previous noisy sample x_t -> x_t-1
439
+ latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
440
+
441
+ # call the callback, if provided
442
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
443
+ progress_bar.update()
444
+ if callback is not None and i % callback_steps == 0:
445
+ callback(i, t, latents)
446
+
447
+ # make sure the VAE is in float32 mode, as it overflows in float16
448
+ if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
449
+ self.upcast_vae()
450
+ latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
451
+
452
+ if not output_type == "latent":
453
+ image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
454
+ else:
455
+ image = latents
456
+ return StableDiffusionXLPipelineOutput(images=image)
457
+
458
+ # apply watermark if available
459
+ # if self.watermark is not None:
460
+ # image = self.watermark.apply_watermark(image)
461
+
462
+ image = self.image_processor.postprocess(image, output_type=output_type)
463
+
464
+ # Offload last model to CPU
465
+ if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
466
+ self.final_offload_hook.offload()
467
+
468
+ if not return_dict:
469
+ return (image,)
470
+
471
+ return StableDiffusionXLPipelineOutput(images=image)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.25.0
2
+ transformers==4.34.1
3
+ accelerate
4
+ safetensors
5
+ einops
6
+ onnxruntime-gpu
7
+ spaces==0.19.4
8
+ omegaconf
9
+ peft
10
+ huggingface-hub
style_template.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ style_list = [
2
+ {
3
+ "name": "(No style)",
4
+ "prompt": "{prompt}",
5
+ "negative_prompt": "",
6
+ },
7
+ {
8
+ "name": "Cinematic",
9
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
10
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
11
+ },
12
+ {
13
+ "name": "Disney Charactor",
14
+ "prompt": "A Pixar animation character of {prompt} . pixar-style, studio anime, Disney, high-quality",
15
+ "negative_prompt": "lowres, bad anatomy, bad hands, text, bad eyes, bad arms, bad legs, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, blurry, grayscale, noisy, sloppy, messy, grainy, highly detailed, ultra textured, photo",
16
+ },
17
+ {
18
+ "name": "Digital Art",
19
+ "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
20
+ "negative_prompt": "photo, photorealistic, realism, ugly",
21
+ },
22
+ {
23
+ "name": "Photographic (Default)",
24
+ "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
25
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
26
+ },
27
+ {
28
+ "name": "Fantasy art",
29
+ "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
30
+ "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
31
+ },
32
+ {
33
+ "name": "Neonpunk",
34
+ "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
35
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
36
+ },
37
+ {
38
+ "name": "Enhance",
39
+ "prompt": "breathtaking {prompt} . award-winning, professional, highly detailed",
40
+ "negative_prompt": "ugly, deformed, noisy, blurry, distorted, grainy",
41
+ },
42
+ {
43
+ "name": "Comic book",
44
+ "prompt": "comic {prompt} . graphic illustration, comic art, graphic novel art, vibrant, highly detailed",
45
+ "negative_prompt": "photograph, deformed, glitch, noisy, realistic, stock photo",
46
+ },
47
+ {
48
+ "name": "Lowpoly",
49
+ "prompt": "low-poly style {prompt} . low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition",
50
+ "negative_prompt": "noisy, sloppy, messy, grainy, highly detailed, ultra textured, photo",
51
+ },
52
+ {
53
+ "name": "Line art",
54
+ "prompt": "line art drawing {prompt} . professional, sleek, modern, minimalist, graphic, line art, vector graphics",
55
+ "negative_prompt": "anime, photorealistic, 35mm film, deformed, glitch, blurry, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, mutated, realism, realistic, impressionism, expressionism, oil, acrylic",
56
+ }
57
+ ]
58
+
59
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}