Upload 35 files
Browse files- .pre-commit-config.yaml +55 -0
- LICENSE +21 -0
- LICENSE.ControlNet +201 -0
- README.md +6 -3
- app.py +77 -0
- app_depth.py +140 -0
- app_openpose.py +95 -0
- checkpoints/depth/controlnet/config.json +52 -0
- checkpoints/depth/controlnet/diffusion_pytorch_model.safetensors +3 -0
- cv_utils.py +17 -0
- depth_estimator.py +25 -0
- image_segmentor.py +33 -0
- images/canny/canny_demo.jpg +0 -0
- images/canny/canny_demo2.jpg +0 -0
- images/canny/canny_demo3.jpg +0 -0
- images/depth/depth_demo.png +0 -0
- images/depth/depth_demo2.png +0 -0
- images/depth/depth_demo3.jpg +0 -0
- images/hed/hed_demo.jpeg +0 -0
- images/hed/hed_demo2.jpg +0 -0
- images/hed/hed_demo3.jpg +0 -0
- images/lineart/Ryan Reynolds_1.png +0 -0
- images/lineart/Ryan Reynolds_2.png +0 -0
- images/lineart/Ryan Reynolds_3.png +0 -0
- images/lineart/tube.jpg +0 -0
- images/seg/143.png +0 -0
- images/seg/33.png +0 -0
- images/seg/seg_demo.png +0 -0
- model.py +436 -0
- notebooks/notebook.ipynb +51 -0
- preprocessor.py +112 -0
- requirements.txt +12 -0
- settings.py +15 -0
- style.css +10 -0
- utils.py +9 -0
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.4.0
|
4 |
+
hooks:
|
5 |
+
- id: check-executables-have-shebangs
|
6 |
+
- id: check-json
|
7 |
+
- id: check-merge-conflict
|
8 |
+
- id: check-shebang-scripts-are-executable
|
9 |
+
- id: check-toml
|
10 |
+
- id: check-yaml
|
11 |
+
- id: end-of-file-fixer
|
12 |
+
- id: mixed-line-ending
|
13 |
+
args: ["--fix=lf"]
|
14 |
+
- id: requirements-txt-fixer
|
15 |
+
- id: trailing-whitespace
|
16 |
+
- repo: https://github.com/myint/docformatter
|
17 |
+
rev: v1.7.5
|
18 |
+
hooks:
|
19 |
+
- id: docformatter
|
20 |
+
args: ["--in-place"]
|
21 |
+
- repo: https://github.com/pycqa/isort
|
22 |
+
rev: 5.12.0
|
23 |
+
hooks:
|
24 |
+
- id: isort
|
25 |
+
args: ["--profile", "black"]
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v1.5.1
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ["--ignore-missing-imports"]
|
31 |
+
additional_dependencies:
|
32 |
+
["types-python-slugify", "types-requests", "types-PyYAML"]
|
33 |
+
- repo: https://github.com/psf/black
|
34 |
+
rev: 23.9.1
|
35 |
+
hooks:
|
36 |
+
- id: black
|
37 |
+
language_version: python3.10
|
38 |
+
args: ["--line-length", "119"]
|
39 |
+
- repo: https://github.com/kynan/nbstripout
|
40 |
+
rev: 0.6.1
|
41 |
+
hooks:
|
42 |
+
- id: nbstripout
|
43 |
+
args:
|
44 |
+
[
|
45 |
+
"--extra-keys",
|
46 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
47 |
+
]
|
48 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
49 |
+
rev: 1.7.0
|
50 |
+
hooks:
|
51 |
+
- id: nbqa-black
|
52 |
+
- id: nbqa-pyupgrade
|
53 |
+
args: ["--py37-plus"]
|
54 |
+
- id: nbqa-isort
|
55 |
+
args: ["--float-to-top"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 hysts
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
LICENSE.ControlNet
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: ControlNet++
|
3 |
+
emoji: 📉
|
4 |
colorFrom: yellow
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.0.0
|
8 |
+
python_version: 3.10.13
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
license: mit
|
12 |
+
suggested_hardware: t4-medium
|
13 |
---
|
14 |
|
15 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import torch
|
7 |
+
|
8 |
+
torch.jit.script = lambda f: f
|
9 |
+
import spaces
|
10 |
+
|
11 |
+
from app_depth import create_demo as create_demo_depth
|
12 |
+
from model import Model
|
13 |
+
from settings import ALLOW_CHANGING_BASE_MODEL, DEFAULT_MODEL_ID, SHOW_DUPLICATE_BUTTON
|
14 |
+
from transformers.utils.hub import move_cache
|
15 |
+
|
16 |
+
move_cache()
|
17 |
+
|
18 |
+
DESCRIPTION = "# ControlNet"
|
19 |
+
|
20 |
+
if not torch.cuda.is_available():
|
21 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
22 |
+
|
23 |
+
model = Model(base_model_id=DEFAULT_MODEL_ID, task_name="depth")
|
24 |
+
|
25 |
+
|
26 |
+
with gr.Blocks(css="style.css") as demo:
|
27 |
+
gr.Markdown(DESCRIPTION)
|
28 |
+
gr.DuplicateButton(
|
29 |
+
value="Duplicate Space for private use",
|
30 |
+
elem_id="duplicate-button",
|
31 |
+
visible=SHOW_DUPLICATE_BUTTON,
|
32 |
+
)
|
33 |
+
|
34 |
+
with gr.Tabs():
|
35 |
+
with gr.TabItem("Depth"):
|
36 |
+
create_demo_depth(model.process_depth)
|
37 |
+
|
38 |
+
with gr.Accordion(label="Base model", open=False):
|
39 |
+
with gr.Row():
|
40 |
+
with gr.Column(scale=5):
|
41 |
+
current_base_model = gr.Text(label="Current base model")
|
42 |
+
with gr.Column(scale=1):
|
43 |
+
check_base_model_button = gr.Button("Check current base model")
|
44 |
+
with gr.Row():
|
45 |
+
with gr.Column(scale=5):
|
46 |
+
new_base_model_id = gr.Text(
|
47 |
+
label="New base model",
|
48 |
+
max_lines=1,
|
49 |
+
placeholder="runwayml/stable-diffusion-v1-5",
|
50 |
+
info="The base model must be compatible with Stable Diffusion v1.5.",
|
51 |
+
interactive=ALLOW_CHANGING_BASE_MODEL,
|
52 |
+
)
|
53 |
+
with gr.Column(scale=1):
|
54 |
+
change_base_model_button = gr.Button(
|
55 |
+
"Change base model", interactive=ALLOW_CHANGING_BASE_MODEL
|
56 |
+
)
|
57 |
+
if not ALLOW_CHANGING_BASE_MODEL:
|
58 |
+
gr.Markdown(
|
59 |
+
"""The base model is not allowed to be changed in this Space so as not to slow down the demo, but it can be changed if you duplicate the Space."""
|
60 |
+
)
|
61 |
+
|
62 |
+
check_base_model_button.click(
|
63 |
+
fn=lambda: model.base_model_id,
|
64 |
+
outputs=current_base_model,
|
65 |
+
queue=False,
|
66 |
+
api_name="check_base_model",
|
67 |
+
)
|
68 |
+
gr.on(
|
69 |
+
triggers=[new_base_model_id.submit, change_base_model_button.click],
|
70 |
+
fn=model.set_base_model,
|
71 |
+
inputs=new_base_model_id,
|
72 |
+
outputs=current_base_model,
|
73 |
+
api_name=False,
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.queue(max_size=20).launch()
|
app_depth.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from settings import (
|
6 |
+
DEFAULT_IMAGE_RESOLUTION,
|
7 |
+
DEFAULT_NUM_IMAGES,
|
8 |
+
MAX_IMAGE_RESOLUTION,
|
9 |
+
MAX_NUM_IMAGES,
|
10 |
+
MAX_SEED,
|
11 |
+
)
|
12 |
+
from utils import randomize_seed_fn
|
13 |
+
|
14 |
+
examples = []
|
15 |
+
|
16 |
+
|
17 |
+
def create_demo(process):
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
with gr.Row():
|
20 |
+
with gr.Column():
|
21 |
+
image = gr.Image()
|
22 |
+
prompt = gr.Textbox(label="Prompt")
|
23 |
+
run_button = gr.Button("Run")
|
24 |
+
with gr.Accordion("Advanced options", open=False):
|
25 |
+
preprocessor_name = gr.Radio(
|
26 |
+
label="Preprocessor",
|
27 |
+
choices=["Midas", "DPT", "None"],
|
28 |
+
type="value",
|
29 |
+
value="DPT",
|
30 |
+
)
|
31 |
+
num_samples = gr.Slider(
|
32 |
+
label="Number of images",
|
33 |
+
minimum=1,
|
34 |
+
maximum=MAX_NUM_IMAGES,
|
35 |
+
value=DEFAULT_NUM_IMAGES,
|
36 |
+
step=1,
|
37 |
+
)
|
38 |
+
image_resolution = gr.Slider(
|
39 |
+
label="Image resolution",
|
40 |
+
minimum=256,
|
41 |
+
maximum=MAX_IMAGE_RESOLUTION,
|
42 |
+
value=DEFAULT_IMAGE_RESOLUTION,
|
43 |
+
step=256,
|
44 |
+
)
|
45 |
+
preprocess_resolution = gr.Slider(
|
46 |
+
label="Preprocess resolution",
|
47 |
+
minimum=128,
|
48 |
+
maximum=512,
|
49 |
+
value=384,
|
50 |
+
step=1,
|
51 |
+
)
|
52 |
+
num_steps = gr.Slider(
|
53 |
+
label="Number of steps",
|
54 |
+
minimum=1,
|
55 |
+
maximum=100,
|
56 |
+
value=20,
|
57 |
+
step=1,
|
58 |
+
)
|
59 |
+
guidance_scale = gr.Slider(
|
60 |
+
label="Guidance scale",
|
61 |
+
minimum=0.1,
|
62 |
+
maximum=30.0,
|
63 |
+
value=7.5,
|
64 |
+
step=0.1,
|
65 |
+
)
|
66 |
+
seed = gr.Slider(
|
67 |
+
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0
|
68 |
+
)
|
69 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
70 |
+
a_prompt = gr.Textbox(
|
71 |
+
label="Additional prompt",
|
72 |
+
value="high-quality, extremely detailed, 4K",
|
73 |
+
)
|
74 |
+
n_prompt = gr.Textbox(
|
75 |
+
label="Negative prompt",
|
76 |
+
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
77 |
+
)
|
78 |
+
with gr.Column():
|
79 |
+
result = gr.Gallery(
|
80 |
+
label="Output", show_label=False, columns=2, object_fit="scale-down"
|
81 |
+
)
|
82 |
+
|
83 |
+
gr.Examples(
|
84 |
+
examples=examples,
|
85 |
+
inputs=[
|
86 |
+
image,
|
87 |
+
prompt,
|
88 |
+
guidance_scale,
|
89 |
+
seed,
|
90 |
+
],
|
91 |
+
outputs=result,
|
92 |
+
fn=process,
|
93 |
+
)
|
94 |
+
|
95 |
+
inputs = [
|
96 |
+
image,
|
97 |
+
prompt,
|
98 |
+
a_prompt,
|
99 |
+
n_prompt,
|
100 |
+
num_samples,
|
101 |
+
image_resolution,
|
102 |
+
preprocess_resolution,
|
103 |
+
num_steps,
|
104 |
+
guidance_scale,
|
105 |
+
seed,
|
106 |
+
preprocessor_name,
|
107 |
+
]
|
108 |
+
prompt.submit(
|
109 |
+
fn=randomize_seed_fn,
|
110 |
+
inputs=[seed, randomize_seed],
|
111 |
+
outputs=seed,
|
112 |
+
queue=False,
|
113 |
+
api_name=False,
|
114 |
+
).then(
|
115 |
+
fn=process,
|
116 |
+
inputs=inputs,
|
117 |
+
outputs=result,
|
118 |
+
api_name=False,
|
119 |
+
)
|
120 |
+
run_button.click(
|
121 |
+
fn=randomize_seed_fn,
|
122 |
+
inputs=[seed, randomize_seed],
|
123 |
+
outputs=seed,
|
124 |
+
queue=False,
|
125 |
+
api_name=False,
|
126 |
+
).then(
|
127 |
+
fn=process,
|
128 |
+
inputs=inputs,
|
129 |
+
outputs=result,
|
130 |
+
api_name="depth",
|
131 |
+
)
|
132 |
+
return demo
|
133 |
+
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
from model import Model
|
137 |
+
|
138 |
+
model = Model(task_name="depth")
|
139 |
+
demo = create_demo(model.process_depth)
|
140 |
+
demo.queue().launch()
|
app_openpose.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from settings import (
|
6 |
+
DEFAULT_IMAGE_RESOLUTION,
|
7 |
+
DEFAULT_NUM_IMAGES,
|
8 |
+
MAX_IMAGE_RESOLUTION,
|
9 |
+
MAX_NUM_IMAGES,
|
10 |
+
MAX_SEED,
|
11 |
+
)
|
12 |
+
from utils import randomize_seed_fn
|
13 |
+
|
14 |
+
|
15 |
+
def create_demo(process):
|
16 |
+
with gr.Blocks() as demo:
|
17 |
+
with gr.Row():
|
18 |
+
with gr.Column():
|
19 |
+
image = gr.Image()
|
20 |
+
prompt = gr.Textbox(label="Prompt")
|
21 |
+
run_button = gr.Button(label="Run")
|
22 |
+
with gr.Accordion("Advanced options", open=False):
|
23 |
+
preprocessor_name = gr.Radio(
|
24 |
+
label="Preprocessor", choices=["Openpose", "None"], type="value", value="Openpose"
|
25 |
+
)
|
26 |
+
num_samples = gr.Slider(
|
27 |
+
label="Number of images", minimum=1, maximum=MAX_NUM_IMAGES, value=DEFAULT_NUM_IMAGES, step=1
|
28 |
+
)
|
29 |
+
image_resolution = gr.Slider(
|
30 |
+
label="Image resolution",
|
31 |
+
minimum=256,
|
32 |
+
maximum=MAX_IMAGE_RESOLUTION,
|
33 |
+
value=DEFAULT_IMAGE_RESOLUTION,
|
34 |
+
step=256,
|
35 |
+
)
|
36 |
+
preprocess_resolution = gr.Slider(
|
37 |
+
label="Preprocess resolution", minimum=128, maximum=512, value=512, step=1
|
38 |
+
)
|
39 |
+
num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=20, step=1)
|
40 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=7.5, step=0.1)
|
41 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
42 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
43 |
+
a_prompt = gr.Textbox(label="Additional prompt", value="high-quality, extremely detailed, 4K")
|
44 |
+
n_prompt = gr.Textbox(
|
45 |
+
label="Negative prompt",
|
46 |
+
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
47 |
+
)
|
48 |
+
with gr.Column():
|
49 |
+
result = gr.Gallery(label="Output", show_label=False, columns=2, object_fit="scale-down")
|
50 |
+
inputs = [
|
51 |
+
image,
|
52 |
+
prompt,
|
53 |
+
a_prompt,
|
54 |
+
n_prompt,
|
55 |
+
num_samples,
|
56 |
+
image_resolution,
|
57 |
+
preprocess_resolution,
|
58 |
+
num_steps,
|
59 |
+
guidance_scale,
|
60 |
+
seed,
|
61 |
+
preprocessor_name,
|
62 |
+
]
|
63 |
+
prompt.submit(
|
64 |
+
fn=randomize_seed_fn,
|
65 |
+
inputs=[seed, randomize_seed],
|
66 |
+
outputs=seed,
|
67 |
+
queue=False,
|
68 |
+
api_name=False,
|
69 |
+
).then(
|
70 |
+
fn=process,
|
71 |
+
inputs=inputs,
|
72 |
+
outputs=result,
|
73 |
+
api_name=False,
|
74 |
+
)
|
75 |
+
run_button.click(
|
76 |
+
fn=randomize_seed_fn,
|
77 |
+
inputs=[seed, randomize_seed],
|
78 |
+
outputs=seed,
|
79 |
+
queue=False,
|
80 |
+
api_name=False,
|
81 |
+
).then(
|
82 |
+
fn=process,
|
83 |
+
inputs=inputs,
|
84 |
+
outputs=result,
|
85 |
+
api_name="openpose",
|
86 |
+
)
|
87 |
+
return demo
|
88 |
+
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
from model import Model
|
92 |
+
|
93 |
+
model = Model(task_name="Openpose")
|
94 |
+
demo = create_demo(model.process_openpose)
|
95 |
+
demo.queue().launch()
|
checkpoints/depth/controlnet/config.json
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_class_name": "ControlNetModel",
|
3 |
+
"_diffusers_version": "0.26.3",
|
4 |
+
"_name_or_path": "work_dirs/finetune/MultiGen20M_depth/ft_controlnet_sd15_depth_res512_bs256_lr1e-5_warmup100_iter5k_fp16ft0-200/checkpoint-5000",
|
5 |
+
"act_fn": "silu",
|
6 |
+
"addition_embed_type": null,
|
7 |
+
"addition_embed_type_num_heads": 64,
|
8 |
+
"addition_time_embed_dim": null,
|
9 |
+
"attention_head_dim": 8,
|
10 |
+
"block_out_channels": [
|
11 |
+
320,
|
12 |
+
640,
|
13 |
+
1280,
|
14 |
+
1280
|
15 |
+
],
|
16 |
+
"class_embed_type": null,
|
17 |
+
"conditioning_channels": 3,
|
18 |
+
"conditioning_embedding_out_channels": [
|
19 |
+
16,
|
20 |
+
32,
|
21 |
+
96,
|
22 |
+
256
|
23 |
+
],
|
24 |
+
"controlnet_conditioning_channel_order": "rgb",
|
25 |
+
"cross_attention_dim": 768,
|
26 |
+
"down_block_types": [
|
27 |
+
"CrossAttnDownBlock2D",
|
28 |
+
"CrossAttnDownBlock2D",
|
29 |
+
"CrossAttnDownBlock2D",
|
30 |
+
"DownBlock2D"
|
31 |
+
],
|
32 |
+
"downsample_padding": 1,
|
33 |
+
"encoder_hid_dim": null,
|
34 |
+
"encoder_hid_dim_type": null,
|
35 |
+
"flip_sin_to_cos": true,
|
36 |
+
"freq_shift": 0,
|
37 |
+
"global_pool_conditions": false,
|
38 |
+
"in_channels": 4,
|
39 |
+
"layers_per_block": 2,
|
40 |
+
"mid_block_scale_factor": 1,
|
41 |
+
"mid_block_type": "UNetMidBlock2DCrossAttn",
|
42 |
+
"norm_eps": 1e-05,
|
43 |
+
"norm_num_groups": 32,
|
44 |
+
"num_attention_heads": null,
|
45 |
+
"num_class_embeds": null,
|
46 |
+
"only_cross_attention": false,
|
47 |
+
"projection_class_embeddings_input_dim": null,
|
48 |
+
"resnet_time_scale_shift": "default",
|
49 |
+
"transformer_layers_per_block": 1,
|
50 |
+
"upcast_attention": false,
|
51 |
+
"use_linear_projection": false
|
52 |
+
}
|
checkpoints/depth/controlnet/diffusion_pytorch_model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d7450404d13ef888c9701433a3c17b2a86c021a6d042f9f5d2519602abd7f2f3
|
3 |
+
size 1445157120
|
cv_utils.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
|
5 |
+
def resize_image(input_image, resolution, interpolation=None):
|
6 |
+
H, W, C = input_image.shape
|
7 |
+
H = float(H)
|
8 |
+
W = float(W)
|
9 |
+
k = float(resolution) / max(H, W)
|
10 |
+
H *= k
|
11 |
+
W *= k
|
12 |
+
H = int(np.round(H / 64.0)) * 64
|
13 |
+
W = int(np.round(W / 64.0)) * 64
|
14 |
+
if interpolation is None:
|
15 |
+
interpolation = cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA
|
16 |
+
img = cv2.resize(input_image, (W, H), interpolation=interpolation)
|
17 |
+
return img
|
depth_estimator.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import PIL.Image
|
3 |
+
from controlnet_aux.util import HWC3
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
from cv_utils import resize_image
|
7 |
+
|
8 |
+
|
9 |
+
class DepthEstimator:
|
10 |
+
def __init__(self):
|
11 |
+
self.model = pipeline("depth-estimation")
|
12 |
+
|
13 |
+
def __call__(self, image: np.ndarray, **kwargs) -> PIL.Image.Image:
|
14 |
+
detect_resolution = kwargs.pop("detect_resolution", 512)
|
15 |
+
image_resolution = kwargs.pop("image_resolution", 512)
|
16 |
+
image = np.array(image)
|
17 |
+
image = HWC3(image)
|
18 |
+
image = resize_image(image, resolution=detect_resolution)
|
19 |
+
image = PIL.Image.fromarray(image)
|
20 |
+
image = self.model(image)
|
21 |
+
image = image["depth"]
|
22 |
+
image = np.array(image)
|
23 |
+
image = HWC3(image)
|
24 |
+
image = resize_image(image, resolution=image_resolution)
|
25 |
+
return PIL.Image.fromarray(image)
|
image_segmentor.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import PIL.Image
|
4 |
+
import torch
|
5 |
+
from controlnet_aux.util import HWC3, ade_palette
|
6 |
+
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
|
7 |
+
|
8 |
+
from cv_utils import resize_image
|
9 |
+
|
10 |
+
|
11 |
+
class ImageSegmentor:
|
12 |
+
def __init__(self):
|
13 |
+
self.image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")
|
14 |
+
self.image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")
|
15 |
+
|
16 |
+
@torch.no_grad()
|
17 |
+
def __call__(self, image: np.ndarray, **kwargs) -> PIL.Image.Image:
|
18 |
+
detect_resolution = kwargs.pop("detect_resolution", 512)
|
19 |
+
image_resolution = kwargs.pop("image_resolution", 512)
|
20 |
+
image = HWC3(image)
|
21 |
+
image = resize_image(image, resolution=detect_resolution)
|
22 |
+
image = PIL.Image.fromarray(image)
|
23 |
+
|
24 |
+
pixel_values = self.image_processor(image, return_tensors="pt").pixel_values
|
25 |
+
outputs = self.image_segmentor(pixel_values)
|
26 |
+
seg = self.image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
27 |
+
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
|
28 |
+
for label, color in enumerate(ade_palette()):
|
29 |
+
color_seg[seg == label, :] = color
|
30 |
+
color_seg = color_seg.astype(np.uint8)
|
31 |
+
|
32 |
+
color_seg = resize_image(color_seg, resolution=image_resolution, interpolation=cv2.INTER_NEAREST)
|
33 |
+
return PIL.Image.fromarray(color_seg)
|
images/canny/canny_demo.jpg
ADDED
images/canny/canny_demo2.jpg
ADDED
images/canny/canny_demo3.jpg
ADDED
images/depth/depth_demo.png
ADDED
images/depth/depth_demo2.png
ADDED
images/depth/depth_demo3.jpg
ADDED
images/hed/hed_demo.jpeg
ADDED
images/hed/hed_demo2.jpg
ADDED
images/hed/hed_demo3.jpg
ADDED
images/lineart/Ryan Reynolds_1.png
ADDED
images/lineart/Ryan Reynolds_2.png
ADDED
images/lineart/Ryan Reynolds_3.png
ADDED
images/lineart/tube.jpg
ADDED
images/seg/143.png
ADDED
images/seg/33.png
ADDED
images/seg/seg_demo.png
ADDED
model.py
ADDED
@@ -0,0 +1,436 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import gc
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import PIL.Image
|
7 |
+
import spaces
|
8 |
+
import torch
|
9 |
+
from controlnet_aux.util import HWC3
|
10 |
+
from diffusers import (
|
11 |
+
ControlNetModel,
|
12 |
+
DiffusionPipeline,
|
13 |
+
StableDiffusionControlNetPipeline,
|
14 |
+
UniPCMultistepScheduler,
|
15 |
+
)
|
16 |
+
|
17 |
+
from cv_utils import resize_image
|
18 |
+
from preprocessor import Preprocessor
|
19 |
+
from settings import MAX_IMAGE_RESOLUTION, MAX_NUM_IMAGES
|
20 |
+
|
21 |
+
CONTROLNET_MODEL_IDS = {
|
22 |
+
"depth": "checkpoints/depth/controlnet",
|
23 |
+
}
|
24 |
+
|
25 |
+
|
26 |
+
def download_all_controlnet_weights() -> None:
|
27 |
+
for model_id in CONTROLNET_MODEL_IDS.values():
|
28 |
+
ControlNetModel.from_pretrained(model_id)
|
29 |
+
|
30 |
+
|
31 |
+
class Model:
|
32 |
+
def __init__(
|
33 |
+
self,
|
34 |
+
base_model_id: str = "runwayml/stable-diffusion-v1-5",
|
35 |
+
task_name: str = "depth",
|
36 |
+
):
|
37 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
38 |
+
self.base_model_id = ""
|
39 |
+
self.task_name = ""
|
40 |
+
self.pipe = self.load_pipe(base_model_id, task_name)
|
41 |
+
self.preprocessor = Preprocessor()
|
42 |
+
|
43 |
+
def load_pipe(self, base_model_id: str, task_name) -> DiffusionPipeline:
|
44 |
+
if (
|
45 |
+
base_model_id == self.base_model_id
|
46 |
+
and task_name == self.task_name
|
47 |
+
and hasattr(self, "pipe")
|
48 |
+
and self.pipe is not None
|
49 |
+
):
|
50 |
+
return self.pipe
|
51 |
+
model_id = CONTROLNET_MODEL_IDS[task_name]
|
52 |
+
controlnet = ControlNetModel.from_pretrained(
|
53 |
+
model_id, torch_dtype=torch.float32
|
54 |
+
)
|
55 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
56 |
+
base_model_id,
|
57 |
+
safety_checker=None,
|
58 |
+
controlnet=controlnet,
|
59 |
+
torch_dtype=torch.float32,
|
60 |
+
)
|
61 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
62 |
+
# if self.device.type == "cuda":
|
63 |
+
# pipe.disable_xformers_memory_efficient_attention()
|
64 |
+
pipe.to(self.device)
|
65 |
+
|
66 |
+
torch.cuda.empty_cache()
|
67 |
+
gc.collect()
|
68 |
+
self.base_model_id = base_model_id
|
69 |
+
self.task_name = task_name
|
70 |
+
return pipe
|
71 |
+
|
72 |
+
def set_base_model(self, base_model_id: str) -> str:
|
73 |
+
if not base_model_id or base_model_id == self.base_model_id:
|
74 |
+
return self.base_model_id
|
75 |
+
del self.pipe
|
76 |
+
torch.cuda.empty_cache()
|
77 |
+
gc.collect()
|
78 |
+
try:
|
79 |
+
self.pipe = self.load_pipe(base_model_id, self.task_name)
|
80 |
+
except Exception:
|
81 |
+
self.pipe = self.load_pipe(self.base_model_id, self.task_name)
|
82 |
+
return self.base_model_id
|
83 |
+
|
84 |
+
def load_controlnet_weight(self, task_name: str) -> None:
|
85 |
+
if task_name == self.task_name:
|
86 |
+
return
|
87 |
+
if self.pipe is not None and hasattr(self.pipe, "controlnet"):
|
88 |
+
del self.pipe.controlnet
|
89 |
+
torch.cuda.empty_cache()
|
90 |
+
gc.collect()
|
91 |
+
model_id = CONTROLNET_MODEL_IDS[task_name]
|
92 |
+
controlnet = ControlNetModel.from_pretrained(
|
93 |
+
model_id, torch_dtype=torch.float32
|
94 |
+
)
|
95 |
+
controlnet.to(self.device)
|
96 |
+
torch.cuda.empty_cache()
|
97 |
+
gc.collect()
|
98 |
+
self.pipe.controlnet = controlnet
|
99 |
+
self.task_name = task_name
|
100 |
+
|
101 |
+
def get_prompt(self, prompt: str, additional_prompt: str) -> str:
|
102 |
+
if not prompt:
|
103 |
+
prompt = additional_prompt
|
104 |
+
else:
|
105 |
+
prompt = f"{prompt}, {additional_prompt}"
|
106 |
+
return prompt
|
107 |
+
|
108 |
+
@torch.autocast("cuda")
|
109 |
+
def run_pipe(
|
110 |
+
self,
|
111 |
+
prompt: str,
|
112 |
+
negative_prompt: str,
|
113 |
+
control_image: PIL.Image.Image,
|
114 |
+
num_images: int,
|
115 |
+
num_steps: int,
|
116 |
+
guidance_scale: float,
|
117 |
+
seed: int,
|
118 |
+
) -> list[PIL.Image.Image]:
|
119 |
+
self.pipe.to(self.device)
|
120 |
+
self.pipe.controlnet.to(self.device)
|
121 |
+
generator = torch.Generator().manual_seed(seed)
|
122 |
+
return self.pipe(
|
123 |
+
prompt=prompt,
|
124 |
+
negative_prompt=negative_prompt,
|
125 |
+
guidance_scale=guidance_scale,
|
126 |
+
num_images_per_prompt=num_images,
|
127 |
+
num_inference_steps=num_steps,
|
128 |
+
generator=generator,
|
129 |
+
image=control_image,
|
130 |
+
).images
|
131 |
+
|
132 |
+
@torch.no_grad()
|
133 |
+
@spaces.GPU(enable_queue=True)
|
134 |
+
def process_canny(
|
135 |
+
self,
|
136 |
+
image: np.ndarray,
|
137 |
+
prompt: str,
|
138 |
+
additional_prompt: str,
|
139 |
+
negative_prompt: str,
|
140 |
+
num_images: int,
|
141 |
+
image_resolution: int,
|
142 |
+
num_steps: int,
|
143 |
+
guidance_scale: float,
|
144 |
+
seed: int,
|
145 |
+
low_threshold: int,
|
146 |
+
high_threshold: int,
|
147 |
+
) -> list[PIL.Image.Image]:
|
148 |
+
if image is None:
|
149 |
+
raise ValueError
|
150 |
+
if image_resolution > MAX_IMAGE_RESOLUTION:
|
151 |
+
raise ValueError
|
152 |
+
if num_images > MAX_NUM_IMAGES:
|
153 |
+
raise ValueError
|
154 |
+
|
155 |
+
self.preprocessor.load("Canny")
|
156 |
+
control_image = self.preprocessor(
|
157 |
+
image=image,
|
158 |
+
low_threshold=low_threshold,
|
159 |
+
high_threshold=high_threshold,
|
160 |
+
detect_resolution=image_resolution,
|
161 |
+
)
|
162 |
+
|
163 |
+
self.load_controlnet_weight("Canny")
|
164 |
+
results = self.run_pipe(
|
165 |
+
prompt=self.get_prompt(prompt, additional_prompt),
|
166 |
+
negative_prompt=negative_prompt,
|
167 |
+
control_image=control_image,
|
168 |
+
num_images=num_images,
|
169 |
+
num_steps=num_steps,
|
170 |
+
guidance_scale=guidance_scale,
|
171 |
+
seed=seed,
|
172 |
+
)
|
173 |
+
conditions_of_generated_imgs = [
|
174 |
+
self.preprocessor(
|
175 |
+
image=x,
|
176 |
+
low_threshold=low_threshold,
|
177 |
+
high_threshold=high_threshold,
|
178 |
+
detect_resolution=image_resolution,
|
179 |
+
)
|
180 |
+
for x in results
|
181 |
+
]
|
182 |
+
return [control_image] * num_images + results + conditions_of_generated_imgs
|
183 |
+
|
184 |
+
@torch.no_grad()
|
185 |
+
@spaces.GPU(enable_queue=True)
|
186 |
+
def process_softedge(
|
187 |
+
self,
|
188 |
+
image: np.ndarray,
|
189 |
+
prompt: str,
|
190 |
+
additional_prompt: str,
|
191 |
+
negative_prompt: str,
|
192 |
+
num_images: int,
|
193 |
+
image_resolution: int,
|
194 |
+
preprocess_resolution: int,
|
195 |
+
num_steps: int,
|
196 |
+
guidance_scale: float,
|
197 |
+
seed: int,
|
198 |
+
preprocessor_name: str,
|
199 |
+
) -> list[PIL.Image.Image]:
|
200 |
+
if image is None:
|
201 |
+
raise ValueError
|
202 |
+
if image_resolution > MAX_IMAGE_RESOLUTION:
|
203 |
+
raise ValueError
|
204 |
+
if num_images > MAX_NUM_IMAGES:
|
205 |
+
raise ValueError
|
206 |
+
|
207 |
+
if preprocessor_name == "None":
|
208 |
+
image = HWC3(image)
|
209 |
+
image = resize_image(image, resolution=image_resolution)
|
210 |
+
control_image = PIL.Image.fromarray(image)
|
211 |
+
elif preprocessor_name in ["HED", "HED safe"]:
|
212 |
+
safe = "safe" in preprocessor_name
|
213 |
+
self.preprocessor.load("HED")
|
214 |
+
control_image = self.preprocessor(
|
215 |
+
image=image,
|
216 |
+
image_resolution=image_resolution,
|
217 |
+
detect_resolution=preprocess_resolution,
|
218 |
+
scribble=safe,
|
219 |
+
)
|
220 |
+
elif preprocessor_name in ["PidiNet", "PidiNet safe"]:
|
221 |
+
safe = "safe" in preprocessor_name
|
222 |
+
self.preprocessor.load("PidiNet")
|
223 |
+
control_image = self.preprocessor(
|
224 |
+
image=image,
|
225 |
+
image_resolution=image_resolution,
|
226 |
+
detect_resolution=preprocess_resolution,
|
227 |
+
safe=safe,
|
228 |
+
)
|
229 |
+
else:
|
230 |
+
raise ValueError
|
231 |
+
self.load_controlnet_weight("softedge")
|
232 |
+
results = self.run_pipe(
|
233 |
+
prompt=self.get_prompt(prompt, additional_prompt),
|
234 |
+
negative_prompt=negative_prompt,
|
235 |
+
control_image=control_image,
|
236 |
+
num_images=num_images,
|
237 |
+
num_steps=num_steps,
|
238 |
+
guidance_scale=guidance_scale,
|
239 |
+
seed=seed,
|
240 |
+
)
|
241 |
+
conditions_of_generated_imgs = [
|
242 |
+
self.preprocessor(
|
243 |
+
image=x,
|
244 |
+
image_resolution=image_resolution,
|
245 |
+
detect_resolution=preprocess_resolution,
|
246 |
+
scribble=safe,
|
247 |
+
)
|
248 |
+
for x in results
|
249 |
+
]
|
250 |
+
return [control_image] * num_images + results + conditions_of_generated_imgs
|
251 |
+
|
252 |
+
@torch.no_grad()
|
253 |
+
@spaces.GPU(enable_queue=True)
|
254 |
+
def process_segmentation(
|
255 |
+
self,
|
256 |
+
image: np.ndarray,
|
257 |
+
prompt: str,
|
258 |
+
additional_prompt: str,
|
259 |
+
negative_prompt: str,
|
260 |
+
num_images: int,
|
261 |
+
image_resolution: int,
|
262 |
+
preprocess_resolution: int,
|
263 |
+
num_steps: int,
|
264 |
+
guidance_scale: float,
|
265 |
+
seed: int,
|
266 |
+
preprocessor_name: str,
|
267 |
+
) -> list[PIL.Image.Image]:
|
268 |
+
if image is None:
|
269 |
+
raise ValueError
|
270 |
+
if image_resolution > MAX_IMAGE_RESOLUTION:
|
271 |
+
raise ValueError
|
272 |
+
if num_images > MAX_NUM_IMAGES:
|
273 |
+
raise ValueError
|
274 |
+
|
275 |
+
if preprocessor_name == "None":
|
276 |
+
image = HWC3(image)
|
277 |
+
image = resize_image(image, resolution=image_resolution)
|
278 |
+
control_image = PIL.Image.fromarray(image)
|
279 |
+
else:
|
280 |
+
self.preprocessor.load(preprocessor_name)
|
281 |
+
control_image = self.preprocessor(
|
282 |
+
image=image,
|
283 |
+
image_resolution=image_resolution,
|
284 |
+
detect_resolution=preprocess_resolution,
|
285 |
+
)
|
286 |
+
self.load_controlnet_weight("segmentation")
|
287 |
+
results = self.run_pipe(
|
288 |
+
prompt=self.get_prompt(prompt, additional_prompt),
|
289 |
+
negative_prompt=negative_prompt,
|
290 |
+
control_image=control_image,
|
291 |
+
num_images=num_images,
|
292 |
+
num_steps=num_steps,
|
293 |
+
guidance_scale=guidance_scale,
|
294 |
+
seed=seed,
|
295 |
+
)
|
296 |
+
self.preprocessor.load("UPerNet")
|
297 |
+
conditions_of_generated_imgs = [
|
298 |
+
self.preprocessor(
|
299 |
+
image=np.array(x),
|
300 |
+
image_resolution=image_resolution,
|
301 |
+
detect_resolution=preprocess_resolution,
|
302 |
+
)
|
303 |
+
for x in results
|
304 |
+
]
|
305 |
+
return [control_image] * num_images + results + conditions_of_generated_imgs
|
306 |
+
|
307 |
+
@torch.no_grad()
|
308 |
+
@spaces.GPU(enable_queue=True)
|
309 |
+
def process_depth(
|
310 |
+
self,
|
311 |
+
image: np.ndarray,
|
312 |
+
prompt: str,
|
313 |
+
additional_prompt: str,
|
314 |
+
negative_prompt: str,
|
315 |
+
num_images: int,
|
316 |
+
image_resolution: int,
|
317 |
+
preprocess_resolution: int,
|
318 |
+
num_steps: int,
|
319 |
+
guidance_scale: float,
|
320 |
+
seed: int,
|
321 |
+
preprocessor_name: str,
|
322 |
+
) -> list[PIL.Image.Image]:
|
323 |
+
if image is None:
|
324 |
+
raise ValueError
|
325 |
+
if image_resolution > MAX_IMAGE_RESOLUTION:
|
326 |
+
raise ValueError
|
327 |
+
if num_images > MAX_NUM_IMAGES:
|
328 |
+
raise ValueError
|
329 |
+
|
330 |
+
if preprocessor_name == "None":
|
331 |
+
image = HWC3(image)
|
332 |
+
image = resize_image(image, resolution=image_resolution)
|
333 |
+
control_image = PIL.Image.fromarray(image)
|
334 |
+
else:
|
335 |
+
self.preprocessor.load(preprocessor_name)
|
336 |
+
control_image = self.preprocessor(
|
337 |
+
image=image,
|
338 |
+
image_resolution=image_resolution,
|
339 |
+
detect_resolution=preprocess_resolution,
|
340 |
+
)
|
341 |
+
self.load_controlnet_weight("depth")
|
342 |
+
results = self.run_pipe(
|
343 |
+
prompt=self.get_prompt(prompt, additional_prompt),
|
344 |
+
negative_prompt=negative_prompt,
|
345 |
+
control_image=control_image,
|
346 |
+
num_images=num_images,
|
347 |
+
num_steps=num_steps,
|
348 |
+
guidance_scale=guidance_scale,
|
349 |
+
seed=seed,
|
350 |
+
)
|
351 |
+
conditions_of_generated_imgs = [
|
352 |
+
self.preprocessor(
|
353 |
+
image=x,
|
354 |
+
image_resolution=image_resolution,
|
355 |
+
detect_resolution=preprocess_resolution,
|
356 |
+
)
|
357 |
+
for x in results
|
358 |
+
]
|
359 |
+
return [control_image] * num_images + results + conditions_of_generated_imgs
|
360 |
+
|
361 |
+
@torch.no_grad()
|
362 |
+
@spaces.GPU(enable_queue=True)
|
363 |
+
def process_lineart(
|
364 |
+
self,
|
365 |
+
image: np.ndarray,
|
366 |
+
prompt: str,
|
367 |
+
additional_prompt: str,
|
368 |
+
negative_prompt: str,
|
369 |
+
num_images: int,
|
370 |
+
image_resolution: int,
|
371 |
+
preprocess_resolution: int,
|
372 |
+
num_steps: int,
|
373 |
+
guidance_scale: float,
|
374 |
+
seed: int,
|
375 |
+
preprocessor_name: str,
|
376 |
+
) -> list[PIL.Image.Image]:
|
377 |
+
if image is None:
|
378 |
+
raise ValueError
|
379 |
+
if image_resolution > MAX_IMAGE_RESOLUTION:
|
380 |
+
raise ValueError
|
381 |
+
if num_images > MAX_NUM_IMAGES:
|
382 |
+
raise ValueError
|
383 |
+
|
384 |
+
if preprocessor_name in ["None", "None (anime)"]:
|
385 |
+
image = 255 - HWC3(image)
|
386 |
+
image = resize_image(image, resolution=image_resolution)
|
387 |
+
control_image = PIL.Image.fromarray(image)
|
388 |
+
elif preprocessor_name in ["Lineart", "Lineart coarse"]:
|
389 |
+
coarse = "coarse" in preprocessor_name
|
390 |
+
self.preprocessor.load("Lineart")
|
391 |
+
control_image = self.preprocessor(
|
392 |
+
image=image,
|
393 |
+
image_resolution=image_resolution,
|
394 |
+
detect_resolution=preprocess_resolution,
|
395 |
+
coarse=coarse,
|
396 |
+
)
|
397 |
+
elif preprocessor_name == "Lineart (anime)":
|
398 |
+
self.preprocessor.load("LineartAnime")
|
399 |
+
control_image = self.preprocessor(
|
400 |
+
image=image,
|
401 |
+
image_resolution=image_resolution,
|
402 |
+
detect_resolution=preprocess_resolution,
|
403 |
+
)
|
404 |
+
# NOTE: We still use the general lineart model
|
405 |
+
if "anime" in preprocessor_name:
|
406 |
+
self.load_controlnet_weight("lineart_anime")
|
407 |
+
else:
|
408 |
+
self.load_controlnet_weight("lineart")
|
409 |
+
results = self.run_pipe(
|
410 |
+
prompt=self.get_prompt(prompt, additional_prompt),
|
411 |
+
negative_prompt=negative_prompt,
|
412 |
+
control_image=control_image,
|
413 |
+
num_images=num_images,
|
414 |
+
num_steps=num_steps,
|
415 |
+
guidance_scale=guidance_scale,
|
416 |
+
seed=seed,
|
417 |
+
)
|
418 |
+
self.preprocessor.load("Lineart")
|
419 |
+
conditions_of_generated_imgs = [
|
420 |
+
self.preprocessor(
|
421 |
+
image=x,
|
422 |
+
image_resolution=image_resolution,
|
423 |
+
detect_resolution=preprocess_resolution,
|
424 |
+
)
|
425 |
+
for x in results
|
426 |
+
]
|
427 |
+
|
428 |
+
control_image = PIL.Image.fromarray(
|
429 |
+
(255 - np.array(control_image)).astype(np.uint8)
|
430 |
+
)
|
431 |
+
conditions_of_generated_imgs = [
|
432 |
+
PIL.Image.fromarray((255 - np.array(x)).astype(np.uint8))
|
433 |
+
for x in conditions_of_generated_imgs
|
434 |
+
]
|
435 |
+
|
436 |
+
return [control_image] * num_images + results + conditions_of_generated_imgs
|
notebooks/notebook.ipynb
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {
|
7 |
+
"id": "8CnkIPtjn8Dc"
|
8 |
+
},
|
9 |
+
"outputs": [],
|
10 |
+
"source": [
|
11 |
+
"!git clone --recursive https://huggingface.co/spaces/hysts/ControlNet-v1-1\n",
|
12 |
+
"%cd ControlNet-v1-1\n",
|
13 |
+
"%pip install -q -r requirements.txt"
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"cell_type": "code",
|
18 |
+
"execution_count": null,
|
19 |
+
"metadata": {
|
20 |
+
"id": "GOfGng5Woktd"
|
21 |
+
},
|
22 |
+
"outputs": [],
|
23 |
+
"source": [
|
24 |
+
"from app import demo\n",
|
25 |
+
"\n",
|
26 |
+
"demo.queue().launch()"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": null,
|
32 |
+
"metadata": {
|
33 |
+
"id": "7Cued230ol7T"
|
34 |
+
},
|
35 |
+
"outputs": [],
|
36 |
+
"source": []
|
37 |
+
}
|
38 |
+
],
|
39 |
+
"metadata": {
|
40 |
+
"accelerator": "GPU",
|
41 |
+
"colab": {
|
42 |
+
"provenance": []
|
43 |
+
},
|
44 |
+
"gpuClass": "standard",
|
45 |
+
"language_info": {
|
46 |
+
"name": "python"
|
47 |
+
}
|
48 |
+
},
|
49 |
+
"nbformat": 4,
|
50 |
+
"nbformat_minor": 0
|
51 |
+
}
|
preprocessor.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gc
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import PIL.Image
|
5 |
+
import torch
|
6 |
+
import torchvision
|
7 |
+
from controlnet_aux import (
|
8 |
+
CannyDetector,
|
9 |
+
ContentShuffleDetector,
|
10 |
+
HEDdetector,
|
11 |
+
LineartAnimeDetector,
|
12 |
+
LineartDetector,
|
13 |
+
MidasDetector,
|
14 |
+
MLSDdetector,
|
15 |
+
NormalBaeDetector,
|
16 |
+
OpenposeDetector,
|
17 |
+
PidiNetDetector,
|
18 |
+
)
|
19 |
+
from controlnet_aux.util import HWC3
|
20 |
+
|
21 |
+
from cv_utils import resize_image
|
22 |
+
from depth_estimator import DepthEstimator
|
23 |
+
from image_segmentor import ImageSegmentor
|
24 |
+
|
25 |
+
from kornia.core import Tensor
|
26 |
+
from kornia.filters import canny
|
27 |
+
|
28 |
+
|
29 |
+
class Canny:
|
30 |
+
|
31 |
+
def __call__(
|
32 |
+
self,
|
33 |
+
images: np.array,
|
34 |
+
low_threshold: float = 0.1,
|
35 |
+
high_threshold: float = 0.2,
|
36 |
+
kernel_size: tuple[int, int] | int = (5, 5),
|
37 |
+
sigma: tuple[float, float] | Tensor = (1, 1),
|
38 |
+
hysteresis: bool = True,
|
39 |
+
eps: float = 1e-6
|
40 |
+
) -> torch.Tensor:
|
41 |
+
|
42 |
+
assert low_threshold is not None, "low_threshold must be provided"
|
43 |
+
assert high_threshold is not None, "high_threshold must be provided"
|
44 |
+
|
45 |
+
images = torch.from_numpy(images).permute(2, 0, 1).unsqueeze(0) / 255.0
|
46 |
+
|
47 |
+
images_tensor = canny(images, low_threshold, high_threshold, kernel_size, sigma, hysteresis, eps)[1]
|
48 |
+
images_tensor = (images_tensor[0][0].numpy() * 255).astype(np.uint8)
|
49 |
+
return images_tensor
|
50 |
+
|
51 |
+
|
52 |
+
class Preprocessor:
|
53 |
+
MODEL_ID = "lllyasviel/Annotators"
|
54 |
+
|
55 |
+
def __init__(self):
|
56 |
+
self.model = None
|
57 |
+
self.name = ""
|
58 |
+
|
59 |
+
def load(self, name: str) -> None:
|
60 |
+
if name == self.name:
|
61 |
+
return
|
62 |
+
if name == "HED":
|
63 |
+
self.model = HEDdetector.from_pretrained(self.MODEL_ID)
|
64 |
+
elif name == "Midas":
|
65 |
+
self.model = MidasDetector.from_pretrained(self.MODEL_ID)
|
66 |
+
elif name == "MLSD":
|
67 |
+
self.model = MLSDdetector.from_pretrained(self.MODEL_ID)
|
68 |
+
elif name == "Openpose":
|
69 |
+
self.model = OpenposeDetector.from_pretrained(self.MODEL_ID)
|
70 |
+
elif name == "PidiNet":
|
71 |
+
self.model = PidiNetDetector.from_pretrained(self.MODEL_ID)
|
72 |
+
elif name == "NormalBae":
|
73 |
+
self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID)
|
74 |
+
elif name == "Lineart":
|
75 |
+
self.model = LineartDetector.from_pretrained(self.MODEL_ID)
|
76 |
+
elif name == "LineartAnime":
|
77 |
+
self.model = LineartAnimeDetector.from_pretrained(self.MODEL_ID)
|
78 |
+
elif name == "Canny":
|
79 |
+
self.model = Canny()
|
80 |
+
elif name == "ContentShuffle":
|
81 |
+
self.model = ContentShuffleDetector()
|
82 |
+
elif name == "DPT":
|
83 |
+
self.model = DepthEstimator()
|
84 |
+
elif name == "UPerNet":
|
85 |
+
self.model = ImageSegmentor()
|
86 |
+
else:
|
87 |
+
raise ValueError
|
88 |
+
torch.cuda.empty_cache()
|
89 |
+
gc.collect()
|
90 |
+
self.name = name
|
91 |
+
|
92 |
+
def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
|
93 |
+
if self.name == "Canny":
|
94 |
+
if "detect_resolution" in kwargs:
|
95 |
+
detect_resolution = kwargs.pop("detect_resolution")
|
96 |
+
image = np.array(image)
|
97 |
+
image = HWC3(image)
|
98 |
+
image = resize_image(image, resolution=detect_resolution)
|
99 |
+
image = self.model(image, **kwargs)
|
100 |
+
return PIL.Image.fromarray(image).convert('RGB')
|
101 |
+
elif self.name == "Midas":
|
102 |
+
detect_resolution = kwargs.pop("detect_resolution", 512)
|
103 |
+
image_resolution = kwargs.pop("image_resolution", 512)
|
104 |
+
image = np.array(image)
|
105 |
+
image = HWC3(image)
|
106 |
+
image = resize_image(image, resolution=detect_resolution)
|
107 |
+
image = self.model(image, **kwargs)
|
108 |
+
image = HWC3(image)
|
109 |
+
image = resize_image(image, resolution=image_resolution)
|
110 |
+
return PIL.Image.fromarray(image)
|
111 |
+
else:
|
112 |
+
return self.model(image, **kwargs)
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
controlnet-aux
|
2 |
+
torch==2.0.1
|
3 |
+
torchvision==0.15.2
|
4 |
+
diffusers
|
5 |
+
transformers
|
6 |
+
safetensors
|
7 |
+
accelerate
|
8 |
+
kornia
|
9 |
+
huggingface_hub
|
10 |
+
gradio==4.0.0
|
11 |
+
mediapipe
|
12 |
+
spaces
|
settings.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
DEFAULT_MODEL_ID = os.getenv("DEFAULT_MODEL_ID", "runwayml/stable-diffusion-v1-5")
|
6 |
+
|
7 |
+
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "4"))
|
8 |
+
DEFAULT_NUM_IMAGES = min(MAX_NUM_IMAGES, int(os.getenv("DEFAULT_NUM_IMAGES", "2")))
|
9 |
+
MAX_IMAGE_RESOLUTION = int(os.getenv("MAX_IMAGE_RESOLUTION", "768"))
|
10 |
+
DEFAULT_IMAGE_RESOLUTION = min(MAX_IMAGE_RESOLUTION, int(os.getenv("DEFAULT_IMAGE_RESOLUTION", "512")))
|
11 |
+
|
12 |
+
ALLOW_CHANGING_BASE_MODEL = os.getenv("SPACE_ID") != "hysts/ControlNet-v1-1"
|
13 |
+
SHOW_DUPLICATE_BUTTON = os.getenv("SHOW_DUPLICATE_BUTTON") == "1"
|
14 |
+
|
15 |
+
MAX_SEED = np.iinfo(np.int32).max
|
style.css
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
#duplicate-button {
|
6 |
+
margin: auto;
|
7 |
+
color: #fff;
|
8 |
+
background: #1565c0;
|
9 |
+
border-radius: 100vh;
|
10 |
+
}
|
utils.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
from settings import MAX_SEED
|
4 |
+
|
5 |
+
|
6 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
7 |
+
if randomize_seed:
|
8 |
+
seed = random.randint(0, MAX_SEED)
|
9 |
+
return seed
|