Spaces:
Build error
Build error
Duplicate from nupurkmr9/custom-diffusion
Browse filesCo-authored-by: Nupur Kumari <[email protected]>
- .gitattributes +35 -0
- .gitignore +164 -0
- .gitmodules +3 -0
- .pre-commit-config.yaml +35 -0
- .style.yapf +5 -0
- LICENSE +37 -0
- README.md +13 -0
- app.py +389 -0
- custom-diffusion-models/barn.bin +3 -0
- custom-diffusion-models/cat.bin +3 -0
- custom-diffusion-models/chair.bin +3 -0
- custom-diffusion-models/dog.bin +3 -0
- custom-diffusion-models/flower.bin +3 -0
- custom-diffusion-models/moongate.bin +3 -0
- custom-diffusion-models/table.bin +3 -0
- custom-diffusion-models/teddybear.bin +3 -0
- custom-diffusion-models/tortoise_plushy.bin +3 -0
- custom-diffusion-models/wooden_pot.bin +3 -0
- inference.py +81 -0
- method.jpg +3 -0
- requirements.txt +11 -0
- style.css +3 -0
- trainer.py +164 -0
- uploader.py +20 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
method.jpg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
training_data/
|
2 |
+
results/
|
3 |
+
|
4 |
+
|
5 |
+
# Byte-compiled / optimized / DLL files
|
6 |
+
__pycache__/
|
7 |
+
*.py[cod]
|
8 |
+
*$py.class
|
9 |
+
|
10 |
+
# C extensions
|
11 |
+
*.so
|
12 |
+
|
13 |
+
# Distribution / packaging
|
14 |
+
.Python
|
15 |
+
build/
|
16 |
+
develop-eggs/
|
17 |
+
dist/
|
18 |
+
downloads/
|
19 |
+
eggs/
|
20 |
+
.eggs/
|
21 |
+
lib/
|
22 |
+
lib64/
|
23 |
+
parts/
|
24 |
+
sdist/
|
25 |
+
var/
|
26 |
+
wheels/
|
27 |
+
share/python-wheels/
|
28 |
+
*.egg-info/
|
29 |
+
.installed.cfg
|
30 |
+
*.egg
|
31 |
+
MANIFEST
|
32 |
+
|
33 |
+
# PyInstaller
|
34 |
+
# Usually these files are written by a python script from a template
|
35 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
36 |
+
*.manifest
|
37 |
+
*.spec
|
38 |
+
|
39 |
+
# Installer logs
|
40 |
+
pip-log.txt
|
41 |
+
pip-delete-this-directory.txt
|
42 |
+
|
43 |
+
# Unit test / coverage reports
|
44 |
+
htmlcov/
|
45 |
+
.tox/
|
46 |
+
.nox/
|
47 |
+
.coverage
|
48 |
+
.coverage.*
|
49 |
+
.cache
|
50 |
+
nosetests.xml
|
51 |
+
coverage.xml
|
52 |
+
*.cover
|
53 |
+
*.py,cover
|
54 |
+
.hypothesis/
|
55 |
+
.pytest_cache/
|
56 |
+
cover/
|
57 |
+
|
58 |
+
# Translations
|
59 |
+
*.mo
|
60 |
+
*.pot
|
61 |
+
|
62 |
+
# Django stuff:
|
63 |
+
*.log
|
64 |
+
local_settings.py
|
65 |
+
db.sqlite3
|
66 |
+
db.sqlite3-journal
|
67 |
+
|
68 |
+
# Flask stuff:
|
69 |
+
instance/
|
70 |
+
.webassets-cache
|
71 |
+
|
72 |
+
# Scrapy stuff:
|
73 |
+
.scrapy
|
74 |
+
|
75 |
+
# Sphinx documentation
|
76 |
+
docs/_build/
|
77 |
+
|
78 |
+
# PyBuilder
|
79 |
+
.pybuilder/
|
80 |
+
target/
|
81 |
+
|
82 |
+
# Jupyter Notebook
|
83 |
+
.ipynb_checkpoints
|
84 |
+
|
85 |
+
# IPython
|
86 |
+
profile_default/
|
87 |
+
ipython_config.py
|
88 |
+
|
89 |
+
# pyenv
|
90 |
+
# For a library or package, you might want to ignore these files since the code is
|
91 |
+
# intended to run in multiple environments; otherwise, check them in:
|
92 |
+
# .python-version
|
93 |
+
|
94 |
+
# pipenv
|
95 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
96 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
97 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
98 |
+
# install all needed dependencies.
|
99 |
+
#Pipfile.lock
|
100 |
+
|
101 |
+
# poetry
|
102 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
103 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
104 |
+
# commonly ignored for libraries.
|
105 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
106 |
+
#poetry.lock
|
107 |
+
|
108 |
+
# pdm
|
109 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
110 |
+
#pdm.lock
|
111 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
112 |
+
# in version control.
|
113 |
+
# https://pdm.fming.dev/#use-with-ide
|
114 |
+
.pdm.toml
|
115 |
+
|
116 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
117 |
+
__pypackages__/
|
118 |
+
|
119 |
+
# Celery stuff
|
120 |
+
celerybeat-schedule
|
121 |
+
celerybeat.pid
|
122 |
+
|
123 |
+
# SageMath parsed files
|
124 |
+
*.sage.py
|
125 |
+
|
126 |
+
# Environments
|
127 |
+
.env
|
128 |
+
.venv
|
129 |
+
env/
|
130 |
+
venv/
|
131 |
+
ENV/
|
132 |
+
env.bak/
|
133 |
+
venv.bak/
|
134 |
+
|
135 |
+
# Spyder project settings
|
136 |
+
.spyderproject
|
137 |
+
.spyproject
|
138 |
+
|
139 |
+
# Rope project settings
|
140 |
+
.ropeproject
|
141 |
+
|
142 |
+
# mkdocs documentation
|
143 |
+
/site
|
144 |
+
|
145 |
+
# mypy
|
146 |
+
.mypy_cache/
|
147 |
+
.dmypy.json
|
148 |
+
dmypy.json
|
149 |
+
|
150 |
+
# Pyre type checker
|
151 |
+
.pyre/
|
152 |
+
|
153 |
+
# pytype static type analyzer
|
154 |
+
.pytype/
|
155 |
+
|
156 |
+
# Cython debug symbols
|
157 |
+
cython_debug/
|
158 |
+
|
159 |
+
# PyCharm
|
160 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
161 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
162 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
163 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
164 |
+
#.idea/
|
.gitmodules
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[submodule "custom-diffusion"]
|
2 |
+
path = custom-diffusion
|
3 |
+
url = https://github.com/adobe-research/custom-diffusion
|
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.2.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: double-quote-string-fixer
|
12 |
+
- id: end-of-file-fixer
|
13 |
+
- id: mixed-line-ending
|
14 |
+
args: ['--fix=lf']
|
15 |
+
- id: requirements-txt-fixer
|
16 |
+
- id: trailing-whitespace
|
17 |
+
- repo: https://github.com/myint/docformatter
|
18 |
+
rev: v1.4
|
19 |
+
hooks:
|
20 |
+
- id: docformatter
|
21 |
+
args: ['--in-place']
|
22 |
+
- repo: https://github.com/pycqa/isort
|
23 |
+
rev: 5.10.1
|
24 |
+
hooks:
|
25 |
+
- id: isort
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v0.991
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ['--ignore-missing-imports']
|
31 |
+
- repo: https://github.com/google/yapf
|
32 |
+
rev: v0.32.0
|
33 |
+
hooks:
|
34 |
+
- id: yapf
|
35 |
+
args: ['--parallel', '--in-place']
|
.style.yapf
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[style]
|
2 |
+
based_on_style = pep8
|
3 |
+
blank_line_before_nested_class_or_def = false
|
4 |
+
spaces_before_comment = 2
|
5 |
+
split_before_logical_operator = true
|
LICENSE
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Copyright 2022, Adobe Inc. and its licensors. All rights reserved.
|
2 |
+
|
3 |
+
ADOBE RESEARCH LICENSE
|
4 |
+
|
5 |
+
Adobe grants any person or entity ("you" or "your") obtaining a copy of these certain research materials that are owned by Adobe ("Licensed Materials") a nonexclusive, worldwide, royalty-free, revocable, fully paid license to (A) reproduce, use, modify, and publicly display the Licensed Materials; and (B) redistribute the Licensed Materials, and modifications or derivative works thereof, provided the following conditions are met:
|
6 |
+
|
7 |
+
- The rights granted herein may be exercised for noncommercial research purposes (i.e., academic research and teaching) only. Noncommercial research purposes do not include commercial licensing or distribution, development of commercial products, or any other activity that results in commercial gain.
|
8 |
+
- You may add your own copyright statement to your modifications and/or provide additional or different license terms for use, reproduction, modification, public display, and redistribution of your modifications and derivative works, provided that such license terms limit the use, reproduction, modification, public display, and redistribution of such modifications and derivative works to noncommercial research purposes only.
|
9 |
+
- You acknowledge that Adobe and its licensors own all right, title, and interest in the Licensed Materials.
|
10 |
+
- All copies of the Licensed Materials must include the above copyright notice, this list of conditions, and the disclaimer below.
|
11 |
+
|
12 |
+
Failure to meet any of the above conditions will automatically terminate the rights granted herein.
|
13 |
+
|
14 |
+
THE LICENSED MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK AS TO THE USE, RESULTS, AND PERFORMANCE OF THE LICENSED MATERIALS IS ASSUMED BY YOU. ADOBE DISCLAIMS ALL WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, WITH REGARD TO YOUR USE OF THE LICENSED MATERIALS, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT OF THIRD-PARTY RIGHTS. IN NO EVENT WILL ADOBE BE LIABLE FOR ANY ACTUAL, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES, INCLUDING WITHOUT LIMITATION, LOSS OF PROFITS OR OTHER COMMERCIAL LOSS, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE LICENSED MATERIALS, EVEN IF ADOBE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
15 |
+
|
16 |
+
|
17 |
+
MIT License
|
18 |
+
|
19 |
+
Copyright (c) 2022 hysts
|
20 |
+
|
21 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
22 |
+
of this software and associated documentation files (the "Software"), to deal
|
23 |
+
in the Software without restriction, including without limitation the rights
|
24 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
25 |
+
copies of the Software, and to permit persons to whom the Software is
|
26 |
+
furnished to do so, subject to the following conditions:
|
27 |
+
|
28 |
+
The above copyright notice and this permission notice shall be included in all
|
29 |
+
copies or substantial portions of the Software.
|
30 |
+
|
31 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
32 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
33 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
34 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
35 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
36 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
37 |
+
SOFTWARE.
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Custom-Diffusion + SD Training
|
3 |
+
emoji: 🏢
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.12.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: nupurkmr9/custom-diffusion
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,389 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
"""Demo app for https://github.com/adobe-research/custom-diffusion.
|
3 |
+
|
4 |
+
The code in this repo is partly adapted from the following repository:
|
5 |
+
https://huggingface.co/spaces/hysts/LoRA-SD-training
|
6 |
+
MIT License
|
7 |
+
Copyright (c) 2022 hysts
|
8 |
+
|
9 |
+
==========================================================================================
|
10 |
+
|
11 |
+
Adobe’s modifications are Copyright 2022 Adobe Research. All rights reserved.
|
12 |
+
Adobe’s modifications are licensed under the Adobe Research License. To view a copy of the license, visit
|
13 |
+
LICENSE.
|
14 |
+
|
15 |
+
==========================================================================================
|
16 |
+
"""
|
17 |
+
|
18 |
+
from __future__ import annotations
|
19 |
+
import sys
|
20 |
+
import os
|
21 |
+
import pathlib
|
22 |
+
|
23 |
+
import gradio as gr
|
24 |
+
import torch
|
25 |
+
|
26 |
+
from inference import InferencePipeline
|
27 |
+
from trainer import Trainer
|
28 |
+
from uploader import upload
|
29 |
+
|
30 |
+
TITLE = '# Custom Diffusion + StableDiffusion Training UI'
|
31 |
+
DESCRIPTION = '''This is a demo for [https://github.com/adobe-research/custom-diffusion](https://github.com/adobe-research/custom-diffusion).
|
32 |
+
It is recommended to upgrade to GPU in Settings after duplicating this space to use it.
|
33 |
+
<a href="https://huggingface.co/spaces/nupurkmr9/custom-diffusion?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
34 |
+
'''
|
35 |
+
DETAILDESCRIPTION='''
|
36 |
+
Custom Diffusion allows you to fine-tune text-to-image diffusion models, such as Stable Diffusion, given a few images of a new concept (~4-20).
|
37 |
+
We fine-tune only a subset of model parameters, namely key and value projection matrices, in the cross-attention layers and the modifier token used to represent the object.
|
38 |
+
This also reduces the extra storage for each additional concept to 75MB. Our method also allows you to use a combination of concepts. There's still limitations on which compositions work. For more analysis please refer to our [website](https://www.cs.cmu.edu/~custom-diffusion/).
|
39 |
+
<center>
|
40 |
+
<img src="https://huggingface.co/spaces/nupurkmr9/custom-diffusion/resolve/main/method.jpg" width="600" align="center" >
|
41 |
+
</center>
|
42 |
+
'''
|
43 |
+
|
44 |
+
ORIGINAL_SPACE_ID = 'nupurkmr9/custom-diffusion'
|
45 |
+
SPACE_ID = os.getenv('SPACE_ID', ORIGINAL_SPACE_ID)
|
46 |
+
SHARED_UI_WARNING = f'''# Attention - This Space doesn't work in this shared UI. You can duplicate and use it with a paid private T4 GPU.
|
47 |
+
|
48 |
+
<center><a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></center>
|
49 |
+
'''
|
50 |
+
if os.getenv('SYSTEM') == 'spaces' and SPACE_ID != ORIGINAL_SPACE_ID:
|
51 |
+
SETTINGS = f'<a href="https://huggingface.co/spaces/{SPACE_ID}/settings">Settings</a>'
|
52 |
+
|
53 |
+
else:
|
54 |
+
SETTINGS = 'Settings'
|
55 |
+
CUDA_NOT_AVAILABLE_WARNING = f'''# Attention - Running on CPU.
|
56 |
+
<center>
|
57 |
+
You can assign a GPU in the {SETTINGS} tab if you are running this on HF Spaces.
|
58 |
+
"T4 small" is sufficient to run this demo.
|
59 |
+
</center>
|
60 |
+
'''
|
61 |
+
|
62 |
+
os.system("git clone https://github.com/adobe-research/custom-diffusion")
|
63 |
+
sys.path.append("custom-diffusion")
|
64 |
+
|
65 |
+
def show_warning(warning_text: str) -> gr.Blocks:
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
with gr.Box():
|
68 |
+
gr.Markdown(warning_text)
|
69 |
+
return demo
|
70 |
+
|
71 |
+
|
72 |
+
def update_output_files() -> dict:
|
73 |
+
paths = sorted(pathlib.Path('results').glob('*.bin'))
|
74 |
+
paths = [path.as_posix() for path in paths] # type: ignore
|
75 |
+
return gr.update(value=paths or None)
|
76 |
+
|
77 |
+
|
78 |
+
def create_training_demo(trainer: Trainer,
|
79 |
+
pipe: InferencePipeline) -> gr.Blocks:
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
base_model = gr.Dropdown(
|
82 |
+
choices=['stabilityai/stable-diffusion-2-1-base', 'CompVis/stable-diffusion-v1-4'],
|
83 |
+
value='CompVis/stable-diffusion-v1-4',
|
84 |
+
label='Base Model',
|
85 |
+
visible=True)
|
86 |
+
resolution = gr.Dropdown(choices=['512', '768'],
|
87 |
+
value='512',
|
88 |
+
label='Resolution',
|
89 |
+
visible=True)
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
with gr.Box():
|
93 |
+
concept_images_collection = []
|
94 |
+
concept_prompt_collection = []
|
95 |
+
class_prompt_collection = []
|
96 |
+
buttons_collection = []
|
97 |
+
delete_collection = []
|
98 |
+
is_visible = []
|
99 |
+
maximum_concepts = 3
|
100 |
+
row = [None] * maximum_concepts
|
101 |
+
for x in range(maximum_concepts):
|
102 |
+
ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[(n // 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
|
103 |
+
ordinal_concept = ["<new1> cat", "<new2> wooden pot", "<new3> chair"]
|
104 |
+
if(x == 0):
|
105 |
+
visible = True
|
106 |
+
is_visible.append(gr.State(value=True))
|
107 |
+
else:
|
108 |
+
visible = False
|
109 |
+
is_visible.append(gr.State(value=False))
|
110 |
+
|
111 |
+
concept_images_collection.append(gr.Files(label=f'''Upload the images for your {ordinal(x+1) if (x>0) else ""} concept''', visible=visible))
|
112 |
+
with gr.Column(visible=visible) as row[x]:
|
113 |
+
concept_prompt_collection.append(
|
114 |
+
gr.Textbox(label=f'''{ordinal(x+1) if (x>0) else ""} concept prompt ''', max_lines=1,
|
115 |
+
placeholder=f'''Example: "photo of a {ordinal_concept[x]}"''' )
|
116 |
+
)
|
117 |
+
class_prompt_collection.append(
|
118 |
+
gr.Textbox(label=f'''{ordinal(x+1) if (x>0) else ""} class prompt ''',
|
119 |
+
max_lines=1, placeholder=f'''Example: "{ordinal_concept[x][7:]}"''')
|
120 |
+
)
|
121 |
+
with gr.Row():
|
122 |
+
if(x < maximum_concepts-1):
|
123 |
+
buttons_collection.append(gr.Button(value=f"Add {ordinal(x+2)} concept", visible=visible))
|
124 |
+
if(x > 0):
|
125 |
+
delete_collection.append(gr.Button(value=f"Delete {ordinal(x+1)} concept"))
|
126 |
+
|
127 |
+
counter_add = 1
|
128 |
+
for button in buttons_collection:
|
129 |
+
if(counter_add < len(buttons_collection)):
|
130 |
+
button.click(lambda:
|
131 |
+
[gr.update(visible=True),gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), True, None],
|
132 |
+
None,
|
133 |
+
[row[counter_add], concept_images_collection[counter_add], buttons_collection[counter_add-1], buttons_collection[counter_add], is_visible[counter_add], concept_images_collection[counter_add]], queue=False)
|
134 |
+
else:
|
135 |
+
button.click(lambda:
|
136 |
+
[gr.update(visible=True),gr.update(visible=True), gr.update(visible=False), True],
|
137 |
+
None,
|
138 |
+
[row[counter_add], concept_images_collection[counter_add], buttons_collection[counter_add-1], is_visible[counter_add]], queue=False)
|
139 |
+
counter_add += 1
|
140 |
+
|
141 |
+
counter_delete = 1
|
142 |
+
for delete_button in delete_collection:
|
143 |
+
if(counter_delete < len(delete_collection)+1):
|
144 |
+
if counter_delete == 1:
|
145 |
+
delete_button.click(lambda:
|
146 |
+
[gr.update(visible=False, value=None),gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),False],
|
147 |
+
None,
|
148 |
+
[concept_images_collection[counter_delete], row[counter_delete], buttons_collection[counter_delete-1], buttons_collection[counter_delete], is_visible[counter_delete]], queue=False)
|
149 |
+
else:
|
150 |
+
delete_button.click(lambda:
|
151 |
+
[gr.update(visible=False, value=None),gr.update(visible=False), gr.update(visible=True), False],
|
152 |
+
None,
|
153 |
+
[concept_images_collection[counter_delete], row[counter_delete], buttons_collection[counter_delete-1], is_visible[counter_delete]], queue=False)
|
154 |
+
counter_delete += 1
|
155 |
+
gr.Markdown('''
|
156 |
+
- We use "\<new1\>" modifier_token in front of the concept, e.g., "\<new1\> cat". For multiple concepts use "\<new2\>", "\<new3\>" etc. Increase the number of steps with more concepts.
|
157 |
+
- For a new concept an e.g. concept prompt is "photo of a \<new1\> cat" and "cat" for class prompt.
|
158 |
+
- For a style concept, use "painting in the style of \<new1\> art" for concept prompt and "art" for class prompt.
|
159 |
+
- Class prompt should be the object category.
|
160 |
+
- If "Train Text Encoder", disable "modifier token" and use any unique text to describe the concept e.g. "ktn cat".
|
161 |
+
''')
|
162 |
+
with gr.Box():
|
163 |
+
gr.Markdown('Training Parameters')
|
164 |
+
with gr.Row():
|
165 |
+
modifier_token = gr.Checkbox(label='modifier token',
|
166 |
+
value=True)
|
167 |
+
train_text_encoder = gr.Checkbox(label='Train Text Encoder',
|
168 |
+
value=False)
|
169 |
+
num_training_steps = gr.Number(
|
170 |
+
label='Number of Training Steps', value=1000, precision=0)
|
171 |
+
learning_rate = gr.Number(label='Learning Rate', value=0.00001)
|
172 |
+
batch_size = gr.Number(
|
173 |
+
label='batch_size', value=1, precision=0)
|
174 |
+
with gr.Row():
|
175 |
+
use_8bit_adam = gr.Checkbox(label='Use 8bit Adam', value=True)
|
176 |
+
gradient_checkpointing = gr.Checkbox(label='Enable gradient checkpointing', value=False)
|
177 |
+
with gr.Accordion('Other Parameters', open=False):
|
178 |
+
gradient_accumulation = gr.Number(
|
179 |
+
label='Number of Gradient Accumulation',
|
180 |
+
value=1,
|
181 |
+
precision=0)
|
182 |
+
num_reg_images = gr.Number(
|
183 |
+
label='Number of Class Concept images',
|
184 |
+
value=200,
|
185 |
+
precision=0)
|
186 |
+
gen_images = gr.Checkbox(label='Generated images as regularization',
|
187 |
+
value=False)
|
188 |
+
gr.Markdown('''
|
189 |
+
- It will take about ~10 minutes to train for 1000 steps and ~21GB on a 3090 GPU.
|
190 |
+
- Our results in the paper are trained with batch-size 4 (8 including class regularization samples).
|
191 |
+
- Enable gradient checkpointing for lower memory requirements (~14GB) at the expense of slower backward pass.
|
192 |
+
- Note that your trained models will be deleted when the second training is started. You can upload your trained model in the "Upload" tab.
|
193 |
+
- We retrieve real images for class concept using clip_retireval library which can take some time.
|
194 |
+
''')
|
195 |
+
|
196 |
+
run_button = gr.Button('Start Training')
|
197 |
+
with gr.Box():
|
198 |
+
with gr.Row():
|
199 |
+
check_status_button = gr.Button('Check Training Status')
|
200 |
+
with gr.Column():
|
201 |
+
with gr.Box():
|
202 |
+
gr.Markdown('Message')
|
203 |
+
training_status = gr.Markdown()
|
204 |
+
output_files = gr.Files(label='Trained Weight Files')
|
205 |
+
|
206 |
+
run_button.click(fn=pipe.clear,
|
207 |
+
inputs=None,
|
208 |
+
outputs=None,)
|
209 |
+
run_button.click(fn=trainer.run,
|
210 |
+
inputs=[
|
211 |
+
base_model,
|
212 |
+
resolution,
|
213 |
+
num_training_steps,
|
214 |
+
learning_rate,
|
215 |
+
train_text_encoder,
|
216 |
+
modifier_token,
|
217 |
+
gradient_accumulation,
|
218 |
+
batch_size,
|
219 |
+
use_8bit_adam,
|
220 |
+
gradient_checkpointing,
|
221 |
+
gen_images,
|
222 |
+
num_reg_images,
|
223 |
+
] +
|
224 |
+
concept_images_collection +
|
225 |
+
concept_prompt_collection +
|
226 |
+
class_prompt_collection
|
227 |
+
,
|
228 |
+
outputs=[
|
229 |
+
training_status,
|
230 |
+
output_files,
|
231 |
+
],
|
232 |
+
queue=False)
|
233 |
+
check_status_button.click(fn=trainer.check_if_running,
|
234 |
+
inputs=None,
|
235 |
+
outputs=training_status,
|
236 |
+
queue=False)
|
237 |
+
check_status_button.click(fn=update_output_files,
|
238 |
+
inputs=None,
|
239 |
+
outputs=output_files,
|
240 |
+
queue=False)
|
241 |
+
return demo
|
242 |
+
|
243 |
+
|
244 |
+
def find_weight_files() -> list[str]:
|
245 |
+
curr_dir = pathlib.Path(__file__).parent
|
246 |
+
paths = sorted(curr_dir.rglob('*.bin'))
|
247 |
+
paths = [path for path in paths if '.lfs' not in str(path)]
|
248 |
+
return [path.relative_to(curr_dir).as_posix() for path in paths]
|
249 |
+
|
250 |
+
|
251 |
+
def reload_custom_diffusion_weight_list() -> dict:
|
252 |
+
return gr.update(choices=find_weight_files())
|
253 |
+
|
254 |
+
|
255 |
+
def create_inference_demo(pipe: InferencePipeline) -> gr.Blocks:
|
256 |
+
with gr.Blocks() as demo:
|
257 |
+
with gr.Row():
|
258 |
+
with gr.Column():
|
259 |
+
base_model = gr.Dropdown(
|
260 |
+
choices=['stabilityai/stable-diffusion-2-1-base', 'CompVis/stable-diffusion-v1-4'],
|
261 |
+
value='CompVis/stable-diffusion-v1-4',
|
262 |
+
label='Base Model',
|
263 |
+
visible=True)
|
264 |
+
resolution = gr.Dropdown(choices=[512, 768],
|
265 |
+
value=512,
|
266 |
+
label='Resolution',
|
267 |
+
visible=True)
|
268 |
+
reload_button = gr.Button('Reload Weight List')
|
269 |
+
weight_name = gr.Dropdown(choices=find_weight_files(),
|
270 |
+
value='custom-diffusion-models/cat.bin',
|
271 |
+
label='Custom Diffusion Weight File')
|
272 |
+
prompt = gr.Textbox(
|
273 |
+
label='Prompt',
|
274 |
+
max_lines=1,
|
275 |
+
placeholder='Example: "\<new1\> cat in outer space"')
|
276 |
+
seed = gr.Slider(label='Seed',
|
277 |
+
minimum=0,
|
278 |
+
maximum=100000,
|
279 |
+
step=1,
|
280 |
+
value=42)
|
281 |
+
with gr.Accordion('Other Parameters', open=False):
|
282 |
+
num_steps = gr.Slider(label='Number of Steps',
|
283 |
+
minimum=0,
|
284 |
+
maximum=500,
|
285 |
+
step=1,
|
286 |
+
value=100)
|
287 |
+
guidance_scale = gr.Slider(label='CFG Scale',
|
288 |
+
minimum=0,
|
289 |
+
maximum=50,
|
290 |
+
step=0.1,
|
291 |
+
value=6)
|
292 |
+
eta = gr.Slider(label='DDIM eta',
|
293 |
+
minimum=0,
|
294 |
+
maximum=1.,
|
295 |
+
step=0.1,
|
296 |
+
value=1.)
|
297 |
+
batch_size = gr.Slider(label='Batch Size',
|
298 |
+
minimum=0,
|
299 |
+
maximum=10.,
|
300 |
+
step=1,
|
301 |
+
value=1)
|
302 |
+
|
303 |
+
run_button = gr.Button('Generate')
|
304 |
+
|
305 |
+
gr.Markdown('''
|
306 |
+
- Models with names starting with "custom-diffusion-models/" are the pretrained models provided in the [original repo](https://github.com/adobe-research/custom-diffusion), and the ones with names starting with "results/delta.bin" are your trained models.
|
307 |
+
- After training, you can press "Reload Weight List" button to load your trained model names.
|
308 |
+
- Increase number of steps in Other parameters for better samples qualitatively.
|
309 |
+
''')
|
310 |
+
with gr.Column():
|
311 |
+
result = gr.Image(label='Result')
|
312 |
+
|
313 |
+
reload_button.click(fn=reload_custom_diffusion_weight_list,
|
314 |
+
inputs=None,
|
315 |
+
outputs=weight_name)
|
316 |
+
prompt.submit(fn=pipe.run,
|
317 |
+
inputs=[
|
318 |
+
base_model,
|
319 |
+
weight_name,
|
320 |
+
prompt,
|
321 |
+
seed,
|
322 |
+
num_steps,
|
323 |
+
guidance_scale,
|
324 |
+
eta,
|
325 |
+
batch_size,
|
326 |
+
resolution
|
327 |
+
],
|
328 |
+
outputs=result,
|
329 |
+
queue=False)
|
330 |
+
run_button.click(fn=pipe.run,
|
331 |
+
inputs=[
|
332 |
+
base_model,
|
333 |
+
weight_name,
|
334 |
+
prompt,
|
335 |
+
seed,
|
336 |
+
num_steps,
|
337 |
+
guidance_scale,
|
338 |
+
eta,
|
339 |
+
batch_size,
|
340 |
+
resolution
|
341 |
+
],
|
342 |
+
outputs=result,
|
343 |
+
queue=False)
|
344 |
+
return demo
|
345 |
+
|
346 |
+
|
347 |
+
def create_upload_demo() -> gr.Blocks:
|
348 |
+
with gr.Blocks() as demo:
|
349 |
+
model_name = gr.Textbox(label='Model Name')
|
350 |
+
hf_token = gr.Textbox(
|
351 |
+
label='Hugging Face Token (with write permission)')
|
352 |
+
upload_button = gr.Button('Upload')
|
353 |
+
with gr.Box():
|
354 |
+
gr.Markdown('Message')
|
355 |
+
result = gr.Markdown()
|
356 |
+
gr.Markdown('''
|
357 |
+
- You can upload your trained model to your private Model repo (i.e. https://huggingface.co/{your_username}/{model_name}).
|
358 |
+
- You can find your Hugging Face token [here](https://huggingface.co/settings/tokens).
|
359 |
+
''')
|
360 |
+
|
361 |
+
upload_button.click(fn=upload,
|
362 |
+
inputs=[model_name, hf_token],
|
363 |
+
outputs=result)
|
364 |
+
|
365 |
+
return demo
|
366 |
+
|
367 |
+
|
368 |
+
pipe = InferencePipeline()
|
369 |
+
trainer = Trainer()
|
370 |
+
|
371 |
+
with gr.Blocks(css='style.css') as demo:
|
372 |
+
if os.getenv('IS_SHARED_UI'):
|
373 |
+
show_warning(SHARED_UI_WARNING)
|
374 |
+
if not torch.cuda.is_available():
|
375 |
+
show_warning(CUDA_NOT_AVAILABLE_WARNING)
|
376 |
+
|
377 |
+
gr.Markdown(TITLE)
|
378 |
+
gr.Markdown(DESCRIPTION)
|
379 |
+
gr.Markdown(DETAILDESCRIPTION)
|
380 |
+
|
381 |
+
with gr.Tabs():
|
382 |
+
with gr.TabItem('Train'):
|
383 |
+
create_training_demo(trainer, pipe)
|
384 |
+
with gr.TabItem('Test'):
|
385 |
+
create_inference_demo(pipe)
|
386 |
+
with gr.TabItem('Upload'):
|
387 |
+
create_upload_demo()
|
388 |
+
|
389 |
+
demo.queue(default_enabled=False).launch(share=False)
|
custom-diffusion-models/barn.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b200774e6929f9f51d0c0f3f1e13455853a703e5552d563b7a7aa98ab6ff942c
|
3 |
+
size 76690690
|
custom-diffusion-models/cat.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:93899d5900e9af2a7cff824146981194859d5c62c637c47ae6c487e16668aa6e
|
3 |
+
size 76690690
|
custom-diffusion-models/chair.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a17c8e6b5b3d5f76c3b6792536113987a1de4958e107cfded31a6dd3f6d236b5
|
3 |
+
size 76690690
|
custom-diffusion-models/dog.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bfacae58002ad0ac1a4e48bd98de9719a91038c4b55cd4caa721e3329460d6d4
|
3 |
+
size 76690690
|
custom-diffusion-models/flower.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c10d2361abfe98728f647cdbd3fa0506e872ad65e9591d70a3cf2b0eb94f5cac
|
3 |
+
size 76690690
|
custom-diffusion-models/moongate.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:865ccdd950d4384af1b4cf45d955db4b26ec3736eb03bccab70fee4f51abb441
|
3 |
+
size 76687301
|
custom-diffusion-models/table.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ebea89e9d968f8bce8f06d9b881dacc5002edeca989a23ddf1473684219f87cb
|
3 |
+
size 76690690
|
custom-diffusion-models/teddybear.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c521048539a1b73a9d909f78f91fd776aa1441a12ae2358477660ad3eee1fcf0
|
3 |
+
size 76690690
|
custom-diffusion-models/tortoise_plushy.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1f457414ca52d6ae71d96c432266c1ab4b53fe5c163055162a1f62318bc5f146
|
3 |
+
size 76690690
|
custom-diffusion-models/wooden_pot.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2affc64108eb7ba9c867fa3acbde15f8352b95bb24b0bb95dfeab913916d1db3
|
3 |
+
size 76690690
|
inference.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import gc
|
4 |
+
import pathlib
|
5 |
+
import sys
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import PIL.Image
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
import torch
|
12 |
+
from diffusers import StableDiffusionPipeline
|
13 |
+
sys.path.insert(0, './custom-diffusion')
|
14 |
+
|
15 |
+
|
16 |
+
class InferencePipeline:
|
17 |
+
def __init__(self):
|
18 |
+
self.pipe = None
|
19 |
+
self.device = torch.device(
|
20 |
+
'cuda:0' if torch.cuda.is_available() else 'cpu')
|
21 |
+
self.weight_path = None
|
22 |
+
|
23 |
+
def clear(self) -> None:
|
24 |
+
self.weight_path = None
|
25 |
+
del self.pipe
|
26 |
+
self.pipe = None
|
27 |
+
torch.cuda.empty_cache()
|
28 |
+
gc.collect()
|
29 |
+
|
30 |
+
@staticmethod
|
31 |
+
def get_weight_path(name: str) -> pathlib.Path:
|
32 |
+
curr_dir = pathlib.Path(__file__).parent
|
33 |
+
return curr_dir / name
|
34 |
+
|
35 |
+
def load_pipe(self, model_id: str, filename: str) -> None:
|
36 |
+
weight_path = self.get_weight_path(filename)
|
37 |
+
if weight_path == self.weight_path:
|
38 |
+
return
|
39 |
+
self.weight_path = weight_path
|
40 |
+
weight = torch.load(self.weight_path, map_location=self.device)
|
41 |
+
|
42 |
+
if self.device.type == 'cpu':
|
43 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
44 |
+
else:
|
45 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
46 |
+
model_id, torch_dtype=torch.float16)
|
47 |
+
pipe = pipe.to(self.device)
|
48 |
+
|
49 |
+
from src import diffuser_training
|
50 |
+
diffuser_training.load_model(pipe.text_encoder, pipe.tokenizer, pipe.unet, weight_path, compress=False)
|
51 |
+
|
52 |
+
self.pipe = pipe
|
53 |
+
|
54 |
+
def run(
|
55 |
+
self,
|
56 |
+
base_model: str,
|
57 |
+
weight_name: str,
|
58 |
+
prompt: str,
|
59 |
+
seed: int,
|
60 |
+
n_steps: int,
|
61 |
+
guidance_scale: float,
|
62 |
+
eta: float,
|
63 |
+
batch_size: int,
|
64 |
+
resolution: int,
|
65 |
+
) -> PIL.Image.Image:
|
66 |
+
if not torch.cuda.is_available():
|
67 |
+
raise gr.Error('CUDA is not available.')
|
68 |
+
|
69 |
+
self.load_pipe(base_model, weight_name)
|
70 |
+
|
71 |
+
generator = torch.Generator(device=self.device).manual_seed(seed)
|
72 |
+
out = self.pipe([prompt]*batch_size,
|
73 |
+
num_inference_steps=n_steps,
|
74 |
+
guidance_scale=guidance_scale,
|
75 |
+
height=resolution, width=resolution,
|
76 |
+
eta = eta,
|
77 |
+
generator=generator) # type: ignore
|
78 |
+
torch.cuda.empty_cache()
|
79 |
+
out = out.images
|
80 |
+
out = PIL.Image.fromarray(np.hstack([np.array(x) for x in out]))
|
81 |
+
return out
|
method.jpg
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.15.0
|
2 |
+
bitsandbytes==0.35.4
|
3 |
+
diffusers==0.10.2
|
4 |
+
ftfy==6.1.1
|
5 |
+
Pillow==9.3.0
|
6 |
+
torch==1.13.0
|
7 |
+
torchvision==0.14.0
|
8 |
+
transformers==4.25.1
|
9 |
+
triton==2.0.0.dev20220701
|
10 |
+
xformers==0.0.13
|
11 |
+
clip_retrieval
|
style.css
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
trainer.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import os
|
4 |
+
import pathlib
|
5 |
+
import shlex
|
6 |
+
import shutil
|
7 |
+
import subprocess
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
import PIL.Image
|
11 |
+
import torch
|
12 |
+
import json
|
13 |
+
|
14 |
+
os.environ['PYTHONPATH'] = f'custom-diffusion:{os.getenv("PYTHONPATH", "")}'
|
15 |
+
|
16 |
+
|
17 |
+
def pad_image(image: PIL.Image.Image) -> PIL.Image.Image:
|
18 |
+
w, h = image.size
|
19 |
+
if w == h:
|
20 |
+
return image
|
21 |
+
elif w > h:
|
22 |
+
new_image = PIL.Image.new(image.mode, (w, w), (0, 0, 0))
|
23 |
+
new_image.paste(image, (0, (w - h) // 2))
|
24 |
+
return new_image
|
25 |
+
else:
|
26 |
+
new_image = PIL.Image.new(image.mode, (h, h), (0, 0, 0))
|
27 |
+
new_image.paste(image, ((h - w) // 2, 0))
|
28 |
+
return new_image
|
29 |
+
|
30 |
+
|
31 |
+
class Trainer:
|
32 |
+
def __init__(self):
|
33 |
+
self.is_running = False
|
34 |
+
self.is_running_message = 'Another training is in progress.'
|
35 |
+
|
36 |
+
self.output_dir = pathlib.Path('results')
|
37 |
+
self.instance_data_dir = self.output_dir / 'training_data'
|
38 |
+
self.class_data_dir = self.output_dir / 'regularization_data'
|
39 |
+
|
40 |
+
def check_if_running(self) -> dict:
|
41 |
+
if self.is_running:
|
42 |
+
return gr.update(value=self.is_running_message)
|
43 |
+
else:
|
44 |
+
return gr.update(value='No training is running.')
|
45 |
+
|
46 |
+
def cleanup_dirs(self) -> None:
|
47 |
+
shutil.rmtree(self.output_dir, ignore_errors=True)
|
48 |
+
|
49 |
+
def prepare_dataset(self, concept_images_collection: list, concept_prompt_collection: list, class_prompt_collection: list, resolution: int) -> None:
|
50 |
+
self.instance_data_dir.mkdir(parents=True)
|
51 |
+
concepts_list = []
|
52 |
+
|
53 |
+
for i in range(len(concept_images_collection)):
|
54 |
+
concept_dir = self.instance_data_dir / f'{i}'
|
55 |
+
class_dir = self.class_data_dir / f'{i}'
|
56 |
+
concept_dir.mkdir(parents=True)
|
57 |
+
concept_images = concept_images_collection[i]
|
58 |
+
|
59 |
+
concepts_list.append(
|
60 |
+
{
|
61 |
+
"instance_prompt": concept_prompt_collection[i],
|
62 |
+
"class_prompt": class_prompt_collection[i],
|
63 |
+
"instance_data_dir": f'{concept_dir}',
|
64 |
+
"class_data_dir": f'{class_dir}'
|
65 |
+
}
|
66 |
+
)
|
67 |
+
|
68 |
+
for i, temp_path in enumerate(concept_images):
|
69 |
+
image = PIL.Image.open(temp_path.name)
|
70 |
+
image = pad_image(image)
|
71 |
+
# image = image.resize((resolution, resolution))
|
72 |
+
image = image.convert('RGB')
|
73 |
+
out_path = concept_dir / f'{i:03d}.jpg'
|
74 |
+
image.save(out_path, format='JPEG', quality=100)
|
75 |
+
|
76 |
+
print(concepts_list)
|
77 |
+
json.dump(concepts_list, open( f'{self.output_dir}/temp.json' , 'w') )
|
78 |
+
|
79 |
+
|
80 |
+
def run(
|
81 |
+
self,
|
82 |
+
base_model: str,
|
83 |
+
resolution_s: str,
|
84 |
+
n_steps: int,
|
85 |
+
learning_rate: float,
|
86 |
+
train_text_encoder: bool,
|
87 |
+
modifier_token: bool,
|
88 |
+
gradient_accumulation: int,
|
89 |
+
batch_size: int,
|
90 |
+
use_8bit_adam: bool,
|
91 |
+
gradient_checkpointing: bool,
|
92 |
+
gen_images: bool,
|
93 |
+
num_reg_images: int,
|
94 |
+
*inputs,
|
95 |
+
) -> tuple[dict, list[pathlib.Path]]:
|
96 |
+
if not torch.cuda.is_available():
|
97 |
+
raise gr.Error('CUDA is not available.')
|
98 |
+
|
99 |
+
num_concept = 0
|
100 |
+
for i in range(len(inputs) // 3):
|
101 |
+
if inputs[i] != None:
|
102 |
+
num_concept +=1
|
103 |
+
|
104 |
+
print(num_concept, inputs)
|
105 |
+
concept_images_collection = inputs[: num_concept]
|
106 |
+
concept_prompt_collection = inputs[3: 3 + num_concept]
|
107 |
+
class_prompt_collection = inputs[6: 6+num_concept]
|
108 |
+
if self.is_running:
|
109 |
+
return gr.update(value=self.is_running_message), []
|
110 |
+
|
111 |
+
if concept_images_collection is None:
|
112 |
+
raise gr.Error('You need to upload images.')
|
113 |
+
if not concept_prompt_collection:
|
114 |
+
raise gr.Error('The concept prompt is missing.')
|
115 |
+
|
116 |
+
resolution = int(resolution_s)
|
117 |
+
|
118 |
+
self.cleanup_dirs()
|
119 |
+
self.prepare_dataset(concept_images_collection, concept_prompt_collection, class_prompt_collection, resolution)
|
120 |
+
torch.cuda.empty_cache()
|
121 |
+
command = f'''
|
122 |
+
accelerate launch custom-diffusion/src/diffuser_training.py \
|
123 |
+
--pretrained_model_name_or_path={base_model} \
|
124 |
+
--output_dir={self.output_dir} \
|
125 |
+
--concepts_list={f'{self.output_dir}/temp.json'} \
|
126 |
+
--with_prior_preservation --prior_loss_weight=1.0 \
|
127 |
+
--resolution={resolution} \
|
128 |
+
--train_batch_size={batch_size} \
|
129 |
+
--gradient_accumulation_steps={gradient_accumulation} \
|
130 |
+
--learning_rate={learning_rate} \
|
131 |
+
--lr_scheduler="constant" \
|
132 |
+
--lr_warmup_steps=0 \
|
133 |
+
--max_train_steps={n_steps} \
|
134 |
+
--num_class_images={num_reg_images} \
|
135 |
+
--initializer_token="ktn+pll+ucd" \
|
136 |
+
--scale_lr --hflip
|
137 |
+
'''
|
138 |
+
if modifier_token:
|
139 |
+
tokens = '+'.join([f'<new{i+1}>' for i in range(num_concept)])
|
140 |
+
command += f' --modifier_token {tokens}'
|
141 |
+
|
142 |
+
if not gen_images:
|
143 |
+
command += ' --real_prior'
|
144 |
+
if use_8bit_adam:
|
145 |
+
command += ' --use_8bit_adam'
|
146 |
+
if train_text_encoder:
|
147 |
+
command += f' --train_text_encoder'
|
148 |
+
if gradient_checkpointing:
|
149 |
+
command += f' --gradient_checkpointing'
|
150 |
+
|
151 |
+
with open(self.output_dir / 'train.sh', 'w') as f:
|
152 |
+
command_s = ' '.join(command.split())
|
153 |
+
f.write(command_s)
|
154 |
+
|
155 |
+
self.is_running = True
|
156 |
+
res = subprocess.run(shlex.split(command))
|
157 |
+
self.is_running = False
|
158 |
+
|
159 |
+
if res.returncode == 0:
|
160 |
+
result_message = 'Training Completed!'
|
161 |
+
else:
|
162 |
+
result_message = 'Training Failed!'
|
163 |
+
weight_paths = sorted(self.output_dir.glob('*.bin'))
|
164 |
+
return gr.update(value=result_message), weight_paths
|
uploader.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import HfApi
|
3 |
+
|
4 |
+
|
5 |
+
def upload(model_name: str, hf_token: str) -> None:
|
6 |
+
api = HfApi(token=hf_token)
|
7 |
+
user_name = api.whoami()['name']
|
8 |
+
model_id = f'{user_name}/{model_name}'
|
9 |
+
try:
|
10 |
+
api.create_repo(model_id, repo_type='model', private=True)
|
11 |
+
api.upload_folder(repo_id=model_id,
|
12 |
+
folder_path='results',
|
13 |
+
path_in_repo='results',
|
14 |
+
repo_type='model')
|
15 |
+
url = f'https://huggingface.co/{model_id}'
|
16 |
+
message = f'Your model was successfully uploaded to [{url}]({url}).'
|
17 |
+
except Exception as e:
|
18 |
+
message = str(e)
|
19 |
+
|
20 |
+
return gr.update(value=message, visible=True)
|