layerdiffusion
commited on
Commit
•
95b168f
1
Parent(s):
8b2b47a
This view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +5 -0
- .gitignore +162 -0
- .idea/.gitignore +0 -8
- .idea/IC-Light.iml +0 -8
- .idea/deployment.xml +0 -70
- .idea/inspectionProfiles/Project_Default.xml +0 -84
- .idea/inspectionProfiles/profiles_settings.xml +0 -6
- .idea/misc.xml +0 -4
- .idea/modules.xml +0 -8
- .idea/vcs.xml +0 -6
- app.py +425 -9
- briarmbg.py +462 -0
- db_examples.py +217 -0
- imgs/alter/i1.jpeg +3 -0
- imgs/alter/i2.png +3 -0
- imgs/alter/i3.png +3 -0
- imgs/alter/i4.png +3 -0
- imgs/alter/i5.png +3 -0
- imgs/alter/i6.webp +3 -0
- imgs/alter/o1.png +3 -0
- imgs/alter/o2.png +3 -0
- imgs/alter/o3.png +3 -0
- imgs/alter/o4.png +3 -0
- imgs/alter/o5.png +3 -0
- imgs/bgs/1.webp +3 -0
- imgs/bgs/10.webp +3 -0
- imgs/bgs/11.png +3 -0
- imgs/bgs/12.png +3 -0
- imgs/bgs/13.png +3 -0
- imgs/bgs/14.png +3 -0
- imgs/bgs/15.png +3 -0
- imgs/bgs/2.webp +3 -0
- imgs/bgs/3.webp +3 -0
- imgs/bgs/4.webp +3 -0
- imgs/bgs/5.webp +3 -0
- imgs/bgs/6.webp +3 -0
- imgs/bgs/7.webp +3 -0
- imgs/bgs/8.webp +3 -0
- imgs/bgs/9.webp +3 -0
- imgs/i1.webp +3 -0
- imgs/i10.png +3 -0
- imgs/i11.png +3 -0
- imgs/i13.png +3 -0
- imgs/i14.png +3 -0
- imgs/i15.png +3 -0
- imgs/i16.png +3 -0
- imgs/i3.png +3 -0
- imgs/i5.png +3 -0
- imgs/i6.jpg +3 -0
- imgs/i7.jpg +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.ong filter=lfs diff=lfs merge=lfs -text
|
39 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
40 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.safetensors
|
2 |
+
|
3 |
+
# Byte-compiled / optimized / DLL files
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
7 |
+
|
8 |
+
# C extensions
|
9 |
+
*.so
|
10 |
+
|
11 |
+
# Distribution / packaging
|
12 |
+
.Python
|
13 |
+
build/
|
14 |
+
develop-eggs/
|
15 |
+
dist/
|
16 |
+
downloads/
|
17 |
+
eggs/
|
18 |
+
.eggs/
|
19 |
+
lib/
|
20 |
+
lib64/
|
21 |
+
parts/
|
22 |
+
sdist/
|
23 |
+
var/
|
24 |
+
wheels/
|
25 |
+
share/python-wheels/
|
26 |
+
*.egg-info/
|
27 |
+
.installed.cfg
|
28 |
+
*.egg
|
29 |
+
MANIFEST
|
30 |
+
|
31 |
+
# PyInstaller
|
32 |
+
# Usually these files are written by a python script from a template
|
33 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
34 |
+
*.manifest
|
35 |
+
*.spec
|
36 |
+
|
37 |
+
# Installer logs
|
38 |
+
pip-log.txt
|
39 |
+
pip-delete-this-directory.txt
|
40 |
+
|
41 |
+
# Unit test / coverage reports
|
42 |
+
htmlcov/
|
43 |
+
.tox/
|
44 |
+
.nox/
|
45 |
+
.coverage
|
46 |
+
.coverage.*
|
47 |
+
.cache
|
48 |
+
nosetests.xml
|
49 |
+
coverage.xml
|
50 |
+
*.cover
|
51 |
+
*.py,cover
|
52 |
+
.hypothesis/
|
53 |
+
.pytest_cache/
|
54 |
+
cover/
|
55 |
+
|
56 |
+
# Translations
|
57 |
+
*.mo
|
58 |
+
*.pot
|
59 |
+
|
60 |
+
# Django stuff:
|
61 |
+
*.log
|
62 |
+
local_settings.py
|
63 |
+
db.sqlite3
|
64 |
+
db.sqlite3-journal
|
65 |
+
|
66 |
+
# Flask stuff:
|
67 |
+
instance/
|
68 |
+
.webassets-cache
|
69 |
+
|
70 |
+
# Scrapy stuff:
|
71 |
+
.scrapy
|
72 |
+
|
73 |
+
# Sphinx documentation
|
74 |
+
docs/_build/
|
75 |
+
|
76 |
+
# PyBuilder
|
77 |
+
.pybuilder/
|
78 |
+
target/
|
79 |
+
|
80 |
+
# Jupyter Notebook
|
81 |
+
.ipynb_checkpoints
|
82 |
+
|
83 |
+
# IPython
|
84 |
+
profile_default/
|
85 |
+
ipython_config.py
|
86 |
+
|
87 |
+
# pyenv
|
88 |
+
# For a library or package, you might want to ignore these files since the code is
|
89 |
+
# intended to run in multiple environments; otherwise, check them in:
|
90 |
+
# .python-version
|
91 |
+
|
92 |
+
# pipenv
|
93 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
94 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
95 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
96 |
+
# install all needed dependencies.
|
97 |
+
#Pipfile.lock
|
98 |
+
|
99 |
+
# poetry
|
100 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
101 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
102 |
+
# commonly ignored for libraries.
|
103 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
104 |
+
#poetry.lock
|
105 |
+
|
106 |
+
# pdm
|
107 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
108 |
+
#pdm.lock
|
109 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
110 |
+
# in version control.
|
111 |
+
# https://pdm.fming.dev/#use-with-ide
|
112 |
+
.pdm.toml
|
113 |
+
|
114 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
115 |
+
__pypackages__/
|
116 |
+
|
117 |
+
# Celery stuff
|
118 |
+
celerybeat-schedule
|
119 |
+
celerybeat.pid
|
120 |
+
|
121 |
+
# SageMath parsed files
|
122 |
+
*.sage.py
|
123 |
+
|
124 |
+
# Environments
|
125 |
+
.env
|
126 |
+
.venv
|
127 |
+
env/
|
128 |
+
venv/
|
129 |
+
ENV/
|
130 |
+
env.bak/
|
131 |
+
venv.bak/
|
132 |
+
|
133 |
+
# Spyder project settings
|
134 |
+
.spyderproject
|
135 |
+
.spyproject
|
136 |
+
|
137 |
+
# Rope project settings
|
138 |
+
.ropeproject
|
139 |
+
|
140 |
+
# mkdocs documentation
|
141 |
+
/site
|
142 |
+
|
143 |
+
# mypy
|
144 |
+
.mypy_cache/
|
145 |
+
.dmypy.json
|
146 |
+
dmypy.json
|
147 |
+
|
148 |
+
# Pyre type checker
|
149 |
+
.pyre/
|
150 |
+
|
151 |
+
# pytype static type analyzer
|
152 |
+
.pytype/
|
153 |
+
|
154 |
+
# Cython debug symbols
|
155 |
+
cython_debug/
|
156 |
+
|
157 |
+
# PyCharm
|
158 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
159 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
160 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
161 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
162 |
+
.idea/
|
.idea/.gitignore
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
# Default ignored files
|
2 |
-
/shelf/
|
3 |
-
/workspace.xml
|
4 |
-
# Editor-based HTTP Client requests
|
5 |
-
/httpRequests/
|
6 |
-
# Datasource local storage ignored files
|
7 |
-
/dataSources/
|
8 |
-
/dataSources.local.xml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/IC-Light.iml
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
-
<module type="PYTHON_MODULE" version="4">
|
3 |
-
<component name="NewModuleRootManager">
|
4 |
-
<content url="file://$MODULE_DIR$" />
|
5 |
-
<orderEntry type="jdk" jdkName="iclight" jdkType="Python SDK" />
|
6 |
-
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
-
</component>
|
8 |
-
</module>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/deployment.xml
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
-
<project version="4">
|
3 |
-
<component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
|
4 |
-
<serverData>
|
5 |
-
<paths name="172dnet">
|
6 |
-
<serverdata>
|
7 |
-
<mappings>
|
8 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
9 |
-
</mappings>
|
10 |
-
</serverdata>
|
11 |
-
</paths>
|
12 |
-
<paths name="dnet1215">
|
13 |
-
<serverdata>
|
14 |
-
<mappings>
|
15 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
16 |
-
</mappings>
|
17 |
-
</serverdata>
|
18 |
-
</paths>
|
19 |
-
<paths name="gcpa100">
|
20 |
-
<serverdata>
|
21 |
-
<mappings>
|
22 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
23 |
-
</mappings>
|
24 |
-
</serverdata>
|
25 |
-
</paths>
|
26 |
-
<paths name="[email protected]:22 password">
|
27 |
-
<serverdata>
|
28 |
-
<mappings>
|
29 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
30 |
-
</mappings>
|
31 |
-
</serverdata>
|
32 |
-
</paths>
|
33 |
-
<paths name="[email protected]:22 password (2)">
|
34 |
-
<serverdata>
|
35 |
-
<mappings>
|
36 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
37 |
-
</mappings>
|
38 |
-
</serverdata>
|
39 |
-
</paths>
|
40 |
-
<paths name="[email protected]:22 password (3)">
|
41 |
-
<serverdata>
|
42 |
-
<mappings>
|
43 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
44 |
-
</mappings>
|
45 |
-
</serverdata>
|
46 |
-
</paths>
|
47 |
-
<paths name="[email protected]:22 password">
|
48 |
-
<serverdata>
|
49 |
-
<mappings>
|
50 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
51 |
-
</mappings>
|
52 |
-
</serverdata>
|
53 |
-
</paths>
|
54 |
-
<paths name="[email protected]:22 password">
|
55 |
-
<serverdata>
|
56 |
-
<mappings>
|
57 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
58 |
-
</mappings>
|
59 |
-
</serverdata>
|
60 |
-
</paths>
|
61 |
-
<paths name="[email protected]:22 password">
|
62 |
-
<serverdata>
|
63 |
-
<mappings>
|
64 |
-
<mapping local="$PROJECT_DIR$" web="/" />
|
65 |
-
</mappings>
|
66 |
-
</serverdata>
|
67 |
-
</paths>
|
68 |
-
</serverData>
|
69 |
-
</component>
|
70 |
-
</project>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/inspectionProfiles/Project_Default.xml
DELETED
@@ -1,84 +0,0 @@
|
|
1 |
-
<component name="InspectionProjectProfileManager">
|
2 |
-
<profile version="1.0">
|
3 |
-
<option name="myName" value="Project Default" />
|
4 |
-
<inspection_tool class="DuplicatedCode" enabled="false" level="WEAK WARNING" enabled_by_default="false">
|
5 |
-
<Languages>
|
6 |
-
<language minSize="77" name="Python" />
|
7 |
-
</Languages>
|
8 |
-
</inspection_tool>
|
9 |
-
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
10 |
-
<inspection_tool class="PyDocstringTypesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
11 |
-
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
12 |
-
<option name="ignoredPackages">
|
13 |
-
<value>
|
14 |
-
<list size="39">
|
15 |
-
<item index="0" class="java.lang.String" itemvalue="gradio" />
|
16 |
-
<item index="1" class="java.lang.String" itemvalue="transformers" />
|
17 |
-
<item index="2" class="java.lang.String" itemvalue="timm" />
|
18 |
-
<item index="3" class="java.lang.String" itemvalue="pytorch_lightning" />
|
19 |
-
<item index="4" class="java.lang.String" itemvalue="scipy" />
|
20 |
-
<item index="5" class="java.lang.String" itemvalue="opencv_contrib_python" />
|
21 |
-
<item index="6" class="java.lang.String" itemvalue="numpy" />
|
22 |
-
<item index="7" class="java.lang.String" itemvalue="jinja2" />
|
23 |
-
<item index="8" class="java.lang.String" itemvalue="pymatting" />
|
24 |
-
<item index="9" class="java.lang.String" itemvalue="scikit-image" />
|
25 |
-
<item index="10" class="java.lang.String" itemvalue="paddlepaddle-gpu" />
|
26 |
-
<item index="11" class="java.lang.String" itemvalue="scikit-learn" />
|
27 |
-
<item index="12" class="java.lang.String" itemvalue="paddleseg" />
|
28 |
-
<item index="13" class="java.lang.String" itemvalue="Pillow" />
|
29 |
-
<item index="14" class="java.lang.String" itemvalue="torch" />
|
30 |
-
<item index="15" class="java.lang.String" itemvalue="opencv-python" />
|
31 |
-
<item index="16" class="java.lang.String" itemvalue="xformers" />
|
32 |
-
<item index="17" class="java.lang.String" itemvalue="facexlib" />
|
33 |
-
<item index="18" class="java.lang.String" itemvalue="GitPython" />
|
34 |
-
<item index="19" class="java.lang.String" itemvalue="open-clip-torch" />
|
35 |
-
<item index="20" class="java.lang.String" itemvalue="jsonmerge" />
|
36 |
-
<item index="21" class="java.lang.String" itemvalue="tomesd" />
|
37 |
-
<item index="22" class="java.lang.String" itemvalue="torchdiffeq" />
|
38 |
-
<item index="23" class="java.lang.String" itemvalue="blendmodes" />
|
39 |
-
<item index="24" class="java.lang.String" itemvalue="clean-fid" />
|
40 |
-
<item index="25" class="java.lang.String" itemvalue="omegaconf" />
|
41 |
-
<item index="26" class="java.lang.String" itemvalue="psutil" />
|
42 |
-
<item index="27" class="java.lang.String" itemvalue="resize-right" />
|
43 |
-
<item index="28" class="java.lang.String" itemvalue="kornia" />
|
44 |
-
<item index="29" class="java.lang.String" itemvalue="torchsde" />
|
45 |
-
<item index="30" class="java.lang.String" itemvalue="fastapi" />
|
46 |
-
<item index="31" class="java.lang.String" itemvalue="safetensors" />
|
47 |
-
<item index="32" class="java.lang.String" itemvalue="accelerate" />
|
48 |
-
<item index="33" class="java.lang.String" itemvalue="einops" />
|
49 |
-
<item index="34" class="java.lang.String" itemvalue="lark" />
|
50 |
-
<item index="35" class="java.lang.String" itemvalue="inflection" />
|
51 |
-
<item index="36" class="java.lang.String" itemvalue="piexif" />
|
52 |
-
<item index="37" class="java.lang.String" itemvalue="diffusers" />
|
53 |
-
<item index="38" class="java.lang.String" itemvalue="pillow" />
|
54 |
-
</list>
|
55 |
-
</value>
|
56 |
-
</option>
|
57 |
-
</inspection_tool>
|
58 |
-
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
59 |
-
<option name="ignoredErrors">
|
60 |
-
<list>
|
61 |
-
<option value="E722" />
|
62 |
-
<option value="E731" />
|
63 |
-
</list>
|
64 |
-
</option>
|
65 |
-
</inspection_tool>
|
66 |
-
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
67 |
-
<option name="ignoredErrors">
|
68 |
-
<list>
|
69 |
-
<option value="N802" />
|
70 |
-
<option value="N803" />
|
71 |
-
</list>
|
72 |
-
</option>
|
73 |
-
</inspection_tool>
|
74 |
-
<inspection_tool class="PyTypeCheckerInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
75 |
-
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
76 |
-
<option name="ignoredIdentifiers">
|
77 |
-
<list>
|
78 |
-
<option value="int.long" />
|
79 |
-
<option value="float.detach" />
|
80 |
-
</list>
|
81 |
-
</option>
|
82 |
-
</inspection_tool>
|
83 |
-
</profile>
|
84 |
-
</component>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/inspectionProfiles/profiles_settings.xml
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
<component name="InspectionProjectProfileManager">
|
2 |
-
<settings>
|
3 |
-
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
-
<version value="1.0" />
|
5 |
-
</settings>
|
6 |
-
</component>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/misc.xml
DELETED
@@ -1,4 +0,0 @@
|
|
1 |
-
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
-
<project version="4">
|
3 |
-
<component name="ProjectRootManager" version="2" project-jdk-name="iclight" project-jdk-type="Python SDK" />
|
4 |
-
</project>
|
|
|
|
|
|
|
|
|
|
.idea/modules.xml
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
-
<project version="4">
|
3 |
-
<component name="ProjectModuleManager">
|
4 |
-
<modules>
|
5 |
-
<module fileurl="file://$PROJECT_DIR$/.idea/IC-Light.iml" filepath="$PROJECT_DIR$/.idea/IC-Light.iml" />
|
6 |
-
</modules>
|
7 |
-
</component>
|
8 |
-
</project>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.idea/vcs.xml
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
-
<project version="4">
|
3 |
-
<component name="VcsDirectoryMappings">
|
4 |
-
<mapping directory="" vcs="Git" />
|
5 |
-
</component>
|
6 |
-
</project>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -1,13 +1,429 @@
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
-
|
3 |
-
|
4 |
-
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
5 |
-
# Tesla T4
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
def greet(name):
|
10 |
-
return "Hello " + name + "!!"
|
11 |
|
12 |
-
|
13 |
-
demo.launch()
|
|
|
1 |
+
import math
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
import torch
|
5 |
+
import safetensors.torch as sf
|
6 |
+
import db_examples
|
|
|
|
|
7 |
|
8 |
+
from PIL import Image
|
9 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
10 |
+
from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler, EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler
|
11 |
+
from diffusers.models.attention_processor import AttnProcessor2_0
|
12 |
+
from transformers import CLIPTextModel, CLIPTokenizer
|
13 |
+
from briarmbg import BriaRMBG
|
14 |
+
from enum import Enum
|
15 |
+
from torch.hub import download_url_to_file
|
16 |
+
|
17 |
+
|
18 |
+
# 'stablediffusionapi/realistic-vision-v51'
|
19 |
+
# 'runwayml/stable-diffusion-v1-5'
|
20 |
+
sd15_name = 'stablediffusionapi/realistic-vision-v51'
|
21 |
+
tokenizer = CLIPTokenizer.from_pretrained(sd15_name, subfolder="tokenizer")
|
22 |
+
text_encoder = CLIPTextModel.from_pretrained(sd15_name, subfolder="text_encoder")
|
23 |
+
vae = AutoencoderKL.from_pretrained(sd15_name, subfolder="vae")
|
24 |
+
unet = UNet2DConditionModel.from_pretrained(sd15_name, subfolder="unet")
|
25 |
+
rmbg = BriaRMBG.from_pretrained("briaai/RMBG-1.4")
|
26 |
+
|
27 |
+
# Change UNet
|
28 |
+
|
29 |
+
with torch.no_grad():
|
30 |
+
new_conv_in = torch.nn.Conv2d(8, unet.conv_in.out_channels, unet.conv_in.kernel_size, unet.conv_in.stride, unet.conv_in.padding)
|
31 |
+
new_conv_in.weight.zero_()
|
32 |
+
new_conv_in.weight[:, :4, :, :].copy_(unet.conv_in.weight)
|
33 |
+
new_conv_in.bias = unet.conv_in.bias
|
34 |
+
unet.conv_in = new_conv_in
|
35 |
+
|
36 |
+
unet_original_forward = unet.forward
|
37 |
+
|
38 |
+
|
39 |
+
def hooked_unet_forward(sample, timestep, encoder_hidden_states, **kwargs):
|
40 |
+
c_concat = kwargs['cross_attention_kwargs']['concat_conds'].to(sample)
|
41 |
+
c_concat = torch.cat([c_concat] * (sample.shape[0] // c_concat.shape[0]), dim=0)
|
42 |
+
new_sample = torch.cat([sample, c_concat], dim=1)
|
43 |
+
kwargs['cross_attention_kwargs'] = {}
|
44 |
+
return unet_original_forward(new_sample, timestep, encoder_hidden_states, **kwargs)
|
45 |
+
|
46 |
+
|
47 |
+
unet.forward = hooked_unet_forward
|
48 |
+
|
49 |
+
# Load
|
50 |
+
|
51 |
+
model_path = './models/iclight_sd15_fc.safetensors'
|
52 |
+
download_url_to_file(url='https://huggingface.co/lllyasviel/ic-light/resolve/main/iclight_sd15_fc.safetensors', dst=model_path)
|
53 |
+
sd_offset = sf.load_file(model_path)
|
54 |
+
sd_origin = unet.state_dict()
|
55 |
+
keys = sd_origin.keys()
|
56 |
+
sd_merged = {k: sd_origin[k] + sd_offset[k] for k in sd_origin.keys()}
|
57 |
+
unet.load_state_dict(sd_merged, strict=True)
|
58 |
+
del sd_offset, sd_origin, sd_merged, keys
|
59 |
+
|
60 |
+
# Device
|
61 |
+
|
62 |
+
device = torch.device('cuda')
|
63 |
+
text_encoder = text_encoder.to(device=device, dtype=torch.float16)
|
64 |
+
vae = vae.to(device=device, dtype=torch.bfloat16)
|
65 |
+
unet = unet.to(device=device, dtype=torch.float16)
|
66 |
+
rmbg = rmbg.to(device=device, dtype=torch.float32)
|
67 |
+
|
68 |
+
# SDP
|
69 |
+
|
70 |
+
unet.set_attn_processor(AttnProcessor2_0())
|
71 |
+
vae.set_attn_processor(AttnProcessor2_0())
|
72 |
+
|
73 |
+
# Samplers
|
74 |
+
|
75 |
+
ddim_scheduler = DDIMScheduler(
|
76 |
+
num_train_timesteps=1000,
|
77 |
+
beta_start=0.00085,
|
78 |
+
beta_end=0.012,
|
79 |
+
beta_schedule="scaled_linear",
|
80 |
+
clip_sample=False,
|
81 |
+
set_alpha_to_one=False,
|
82 |
+
steps_offset=1,
|
83 |
+
)
|
84 |
+
|
85 |
+
euler_a_scheduler = EulerAncestralDiscreteScheduler(
|
86 |
+
num_train_timesteps=1000,
|
87 |
+
beta_start=0.00085,
|
88 |
+
beta_end=0.012,
|
89 |
+
steps_offset=1
|
90 |
+
)
|
91 |
+
|
92 |
+
dpmpp_2m_sde_karras_scheduler = DPMSolverMultistepScheduler(
|
93 |
+
num_train_timesteps=1000,
|
94 |
+
beta_start=0.00085,
|
95 |
+
beta_end=0.012,
|
96 |
+
algorithm_type="sde-dpmsolver++",
|
97 |
+
use_karras_sigmas=True,
|
98 |
+
steps_offset=1
|
99 |
+
)
|
100 |
+
|
101 |
+
# Pipelines
|
102 |
+
|
103 |
+
t2i_pipe = StableDiffusionPipeline(
|
104 |
+
vae=vae,
|
105 |
+
text_encoder=text_encoder,
|
106 |
+
tokenizer=tokenizer,
|
107 |
+
unet=unet,
|
108 |
+
scheduler=dpmpp_2m_sde_karras_scheduler,
|
109 |
+
safety_checker=None,
|
110 |
+
requires_safety_checker=False,
|
111 |
+
feature_extractor=None,
|
112 |
+
image_encoder=None
|
113 |
+
)
|
114 |
+
|
115 |
+
i2i_pipe = StableDiffusionImg2ImgPipeline(
|
116 |
+
vae=vae,
|
117 |
+
text_encoder=text_encoder,
|
118 |
+
tokenizer=tokenizer,
|
119 |
+
unet=unet,
|
120 |
+
scheduler=dpmpp_2m_sde_karras_scheduler,
|
121 |
+
safety_checker=None,
|
122 |
+
requires_safety_checker=False,
|
123 |
+
feature_extractor=None,
|
124 |
+
image_encoder=None
|
125 |
+
)
|
126 |
+
|
127 |
+
|
128 |
+
@torch.inference_mode()
|
129 |
+
def encode_prompt_inner(txt: str):
|
130 |
+
max_length = tokenizer.model_max_length
|
131 |
+
chunk_length = tokenizer.model_max_length - 2
|
132 |
+
id_start = tokenizer.bos_token_id
|
133 |
+
id_end = tokenizer.eos_token_id
|
134 |
+
id_pad = id_end
|
135 |
+
|
136 |
+
def pad(x, p, i):
|
137 |
+
return x[:i] if len(x) >= i else x + [p] * (i - len(x))
|
138 |
+
|
139 |
+
tokens = tokenizer(txt, truncation=False, add_special_tokens=False)["input_ids"]
|
140 |
+
chunks = [[id_start] + tokens[i: i + chunk_length] + [id_end] for i in range(0, len(tokens), chunk_length)]
|
141 |
+
chunks = [pad(ck, id_pad, max_length) for ck in chunks]
|
142 |
+
|
143 |
+
token_ids = torch.tensor(chunks).to(device=device, dtype=torch.int64)
|
144 |
+
conds = text_encoder(token_ids).last_hidden_state
|
145 |
+
|
146 |
+
return conds
|
147 |
+
|
148 |
+
|
149 |
+
@torch.inference_mode()
|
150 |
+
def encode_prompt_pair(positive_prompt, negative_prompt):
|
151 |
+
c = encode_prompt_inner(positive_prompt)
|
152 |
+
uc = encode_prompt_inner(negative_prompt)
|
153 |
+
|
154 |
+
c_len = float(len(c))
|
155 |
+
uc_len = float(len(uc))
|
156 |
+
max_count = max(c_len, uc_len)
|
157 |
+
c_repeat = int(math.ceil(max_count / c_len))
|
158 |
+
uc_repeat = int(math.ceil(max_count / uc_len))
|
159 |
+
max_chunk = max(len(c), len(uc))
|
160 |
+
|
161 |
+
c = torch.cat([c] * c_repeat, dim=0)[:max_chunk]
|
162 |
+
uc = torch.cat([uc] * uc_repeat, dim=0)[:max_chunk]
|
163 |
+
|
164 |
+
c = torch.cat([p[None, ...] for p in c], dim=1)
|
165 |
+
uc = torch.cat([p[None, ...] for p in uc], dim=1)
|
166 |
+
|
167 |
+
return c, uc
|
168 |
+
|
169 |
+
|
170 |
+
@torch.inference_mode()
|
171 |
+
def pytorch2numpy(imgs, quant=True):
|
172 |
+
results = []
|
173 |
+
for x in imgs:
|
174 |
+
y = x.movedim(0, -1)
|
175 |
+
|
176 |
+
if quant:
|
177 |
+
y = y * 127.5 + 127.5
|
178 |
+
y = y.detach().float().cpu().numpy().clip(0, 255).astype(np.uint8)
|
179 |
+
else:
|
180 |
+
y = y * 0.5 + 0.5
|
181 |
+
y = y.detach().float().cpu().numpy().clip(0, 1).astype(np.float32)
|
182 |
+
|
183 |
+
results.append(y)
|
184 |
+
return results
|
185 |
+
|
186 |
+
|
187 |
+
@torch.inference_mode()
|
188 |
+
def numpy2pytorch(imgs):
|
189 |
+
h = torch.from_numpy(np.stack(imgs, axis=0)).float() / 127.0 - 1.0 # so that 127 must be strictly 0.0
|
190 |
+
h = h.movedim(-1, 1)
|
191 |
+
return h
|
192 |
+
|
193 |
+
|
194 |
+
def resize_and_center_crop(image, target_width, target_height):
|
195 |
+
pil_image = Image.fromarray(image)
|
196 |
+
original_width, original_height = pil_image.size
|
197 |
+
scale_factor = max(target_width / original_width, target_height / original_height)
|
198 |
+
resized_width = int(round(original_width * scale_factor))
|
199 |
+
resized_height = int(round(original_height * scale_factor))
|
200 |
+
resized_image = pil_image.resize((resized_width, resized_height), Image.LANCZOS)
|
201 |
+
left = (resized_width - target_width) / 2
|
202 |
+
top = (resized_height - target_height) / 2
|
203 |
+
right = (resized_width + target_width) / 2
|
204 |
+
bottom = (resized_height + target_height) / 2
|
205 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
206 |
+
return np.array(cropped_image)
|
207 |
+
|
208 |
+
|
209 |
+
def resize_without_crop(image, target_width, target_height):
|
210 |
+
pil_image = Image.fromarray(image)
|
211 |
+
resized_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
|
212 |
+
return np.array(resized_image)
|
213 |
+
|
214 |
+
|
215 |
+
@torch.inference_mode()
|
216 |
+
def run_rmbg(img, sigma=0.0):
|
217 |
+
H, W, C = img.shape
|
218 |
+
assert C == 3
|
219 |
+
k = (256.0 / float(H * W)) ** 0.5
|
220 |
+
feed = resize_without_crop(img, int(64 * round(W * k)), int(64 * round(H * k)))
|
221 |
+
feed = numpy2pytorch([feed]).to(device=device, dtype=torch.float32)
|
222 |
+
alpha = rmbg(feed)[0][0]
|
223 |
+
alpha = torch.nn.functional.interpolate(alpha, size=(H, W), mode="bilinear")
|
224 |
+
alpha = alpha.movedim(1, -1)[0]
|
225 |
+
alpha = alpha.detach().float().cpu().numpy().clip(0, 1)
|
226 |
+
result = 127 + (img.astype(np.float32) - 127 + sigma) * alpha
|
227 |
+
return result.clip(0, 255).astype(np.uint8), alpha
|
228 |
+
|
229 |
+
|
230 |
+
@torch.inference_mode()
|
231 |
+
def process(input_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source):
|
232 |
+
bg_source = BGSource(bg_source)
|
233 |
+
input_bg = None
|
234 |
+
|
235 |
+
if bg_source == BGSource.NONE:
|
236 |
+
pass
|
237 |
+
elif bg_source == BGSource.LEFT:
|
238 |
+
gradient = np.linspace(255, 0, image_width)
|
239 |
+
image = np.tile(gradient, (image_height, 1))
|
240 |
+
input_bg = np.stack((image,) * 3, axis=-1).astype(np.uint8)
|
241 |
+
elif bg_source == BGSource.RIGHT:
|
242 |
+
gradient = np.linspace(0, 255, image_width)
|
243 |
+
image = np.tile(gradient, (image_height, 1))
|
244 |
+
input_bg = np.stack((image,) * 3, axis=-1).astype(np.uint8)
|
245 |
+
elif bg_source == BGSource.TOP:
|
246 |
+
gradient = np.linspace(255, 0, image_height)[:, None]
|
247 |
+
image = np.tile(gradient, (1, image_width))
|
248 |
+
input_bg = np.stack((image,) * 3, axis=-1).astype(np.uint8)
|
249 |
+
elif bg_source == BGSource.BOTTOM:
|
250 |
+
gradient = np.linspace(0, 255, image_height)[:, None]
|
251 |
+
image = np.tile(gradient, (1, image_width))
|
252 |
+
input_bg = np.stack((image,) * 3, axis=-1).astype(np.uint8)
|
253 |
+
else:
|
254 |
+
raise 'Wrong initial latent!'
|
255 |
+
|
256 |
+
rng = torch.Generator(device=device).manual_seed(int(seed))
|
257 |
+
|
258 |
+
fg = resize_and_center_crop(input_fg, image_width, image_height)
|
259 |
+
|
260 |
+
concat_conds = numpy2pytorch([fg]).to(device=vae.device, dtype=vae.dtype)
|
261 |
+
concat_conds = vae.encode(concat_conds).latent_dist.mode() * vae.config.scaling_factor
|
262 |
+
|
263 |
+
conds, unconds = encode_prompt_pair(positive_prompt=prompt + ', ' + a_prompt, negative_prompt=n_prompt)
|
264 |
+
|
265 |
+
if input_bg is None:
|
266 |
+
latents = t2i_pipe(
|
267 |
+
prompt_embeds=conds,
|
268 |
+
negative_prompt_embeds=unconds,
|
269 |
+
width=image_width,
|
270 |
+
height=image_height,
|
271 |
+
num_inference_steps=steps,
|
272 |
+
num_images_per_prompt=num_samples,
|
273 |
+
generator=rng,
|
274 |
+
output_type='latent',
|
275 |
+
guidance_scale=cfg,
|
276 |
+
cross_attention_kwargs={'concat_conds': concat_conds},
|
277 |
+
).images.to(vae.dtype) / vae.config.scaling_factor
|
278 |
+
else:
|
279 |
+
bg = resize_and_center_crop(input_bg, image_width, image_height)
|
280 |
+
bg_latent = numpy2pytorch([bg]).to(device=vae.device, dtype=vae.dtype)
|
281 |
+
bg_latent = vae.encode(bg_latent).latent_dist.mode() * vae.config.scaling_factor
|
282 |
+
latents = i2i_pipe(
|
283 |
+
image=bg_latent,
|
284 |
+
strength=lowres_denoise,
|
285 |
+
prompt_embeds=conds,
|
286 |
+
negative_prompt_embeds=unconds,
|
287 |
+
width=image_width,
|
288 |
+
height=image_height,
|
289 |
+
num_inference_steps=int(round(steps / lowres_denoise)),
|
290 |
+
num_images_per_prompt=num_samples,
|
291 |
+
generator=rng,
|
292 |
+
output_type='latent',
|
293 |
+
guidance_scale=cfg,
|
294 |
+
cross_attention_kwargs={'concat_conds': concat_conds},
|
295 |
+
).images.to(vae.dtype) / vae.config.scaling_factor
|
296 |
+
|
297 |
+
pixels = vae.decode(latents).sample
|
298 |
+
pixels = pytorch2numpy(pixels)
|
299 |
+
pixels = [resize_without_crop(
|
300 |
+
image=p,
|
301 |
+
target_width=int(round(image_width * highres_scale / 64.0) * 64),
|
302 |
+
target_height=int(round(image_height * highres_scale / 64.0) * 64))
|
303 |
+
for p in pixels]
|
304 |
+
|
305 |
+
pixels = numpy2pytorch(pixels).to(device=vae.device, dtype=vae.dtype)
|
306 |
+
latents = vae.encode(pixels).latent_dist.mode() * vae.config.scaling_factor
|
307 |
+
latents = latents.to(device=unet.device, dtype=unet.dtype)
|
308 |
+
|
309 |
+
image_height, image_width = latents.shape[2] * 8, latents.shape[3] * 8
|
310 |
+
|
311 |
+
fg = resize_and_center_crop(input_fg, image_width, image_height)
|
312 |
+
concat_conds = numpy2pytorch([fg]).to(device=vae.device, dtype=vae.dtype)
|
313 |
+
concat_conds = vae.encode(concat_conds).latent_dist.mode() * vae.config.scaling_factor
|
314 |
+
|
315 |
+
latents = i2i_pipe(
|
316 |
+
image=latents,
|
317 |
+
strength=highres_denoise,
|
318 |
+
prompt_embeds=conds,
|
319 |
+
negative_prompt_embeds=unconds,
|
320 |
+
width=image_width,
|
321 |
+
height=image_height,
|
322 |
+
num_inference_steps=int(round(steps / highres_denoise)),
|
323 |
+
num_images_per_prompt=num_samples,
|
324 |
+
generator=rng,
|
325 |
+
output_type='latent',
|
326 |
+
guidance_scale=cfg,
|
327 |
+
cross_attention_kwargs={'concat_conds': concat_conds},
|
328 |
+
).images.to(vae.dtype) / vae.config.scaling_factor
|
329 |
+
|
330 |
+
pixels = vae.decode(latents).sample
|
331 |
+
|
332 |
+
return pytorch2numpy(pixels)
|
333 |
+
|
334 |
+
|
335 |
+
@torch.inference_mode()
|
336 |
+
def process_relight(input_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source):
|
337 |
+
input_fg, matting = run_rmbg(input_fg)
|
338 |
+
results = process(input_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source)
|
339 |
+
return input_fg, results
|
340 |
+
|
341 |
+
|
342 |
+
quick_prompts = [
|
343 |
+
'sunshine from window',
|
344 |
+
'neon light, city',
|
345 |
+
'sunset over sea',
|
346 |
+
'golden time',
|
347 |
+
'sci-fi RGB glowing, cyberpunk',
|
348 |
+
'natural lighting',
|
349 |
+
'warm atmosphere, at home, bedroom',
|
350 |
+
'magic lit',
|
351 |
+
'evil, gothic, Yharnam',
|
352 |
+
'light and shadow',
|
353 |
+
'shadow from window',
|
354 |
+
'soft studio lighting',
|
355 |
+
'home atmosphere, cozy bedroom illumination',
|
356 |
+
'neon, Wong Kar-wai, warm'
|
357 |
+
]
|
358 |
+
quick_prompts = [[x] for x in quick_prompts]
|
359 |
+
|
360 |
+
|
361 |
+
quick_subjects = [
|
362 |
+
'beautiful woman, detailed face',
|
363 |
+
'handsome man, detailed face',
|
364 |
+
]
|
365 |
+
quick_subjects = [[x] for x in quick_subjects]
|
366 |
+
|
367 |
+
|
368 |
+
class BGSource(Enum):
|
369 |
+
NONE = "None"
|
370 |
+
LEFT = "Left Light"
|
371 |
+
RIGHT = "Right Light"
|
372 |
+
TOP = "Top Light"
|
373 |
+
BOTTOM = "Bottom Light"
|
374 |
+
|
375 |
+
|
376 |
+
block = gr.Blocks().queue()
|
377 |
+
with block:
|
378 |
+
with gr.Row():
|
379 |
+
gr.Markdown("## IC-Light (Relighting with Foreground Condition)")
|
380 |
+
with gr.Row():
|
381 |
+
with gr.Column():
|
382 |
+
with gr.Row():
|
383 |
+
input_fg = gr.Image(source='upload', type="numpy", label="Image", height=480)
|
384 |
+
output_bg = gr.Image(type="numpy", label="Preprocessed Foreground", height=480)
|
385 |
+
prompt = gr.Textbox(label="Prompt")
|
386 |
+
bg_source = gr.Radio(choices=[e.value for e in BGSource],
|
387 |
+
value=BGSource.NONE.value,
|
388 |
+
label="Lighting Preference (Initial Latent)", type='value')
|
389 |
+
example_quick_subjects = gr.Dataset(samples=quick_subjects, label='Subject Quick List', samples_per_page=1000, components=[prompt])
|
390 |
+
example_quick_prompts = gr.Dataset(samples=quick_prompts, label='Lighting Quick List', samples_per_page=1000, components=[prompt])
|
391 |
+
relight_button = gr.Button(value="Relight")
|
392 |
+
|
393 |
+
with gr.Group():
|
394 |
+
with gr.Row():
|
395 |
+
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
|
396 |
+
seed = gr.Number(label="Seed", value=12345, precision=0)
|
397 |
+
|
398 |
+
with gr.Row():
|
399 |
+
image_width = gr.Slider(label="Image Width", minimum=256, maximum=1024, value=512, step=64)
|
400 |
+
image_height = gr.Slider(label="Image Height", minimum=256, maximum=1024, value=640, step=64)
|
401 |
+
|
402 |
+
with gr.Accordion("Advanced options", open=False):
|
403 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=25, step=1)
|
404 |
+
cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=2, step=0.01)
|
405 |
+
lowres_denoise = gr.Slider(label="Lowres Denoise (for initial latent)", minimum=0.1, maximum=1.0, value=0.9, step=0.01)
|
406 |
+
highres_scale = gr.Slider(label="Highres Scale", minimum=1.0, maximum=3.0, value=1.5, step=0.01)
|
407 |
+
highres_denoise = gr.Slider(label="Highres Denoise", minimum=0.1, maximum=1.0, value=0.5, step=0.01)
|
408 |
+
a_prompt = gr.Textbox(label="Added Prompt", value='best quality')
|
409 |
+
n_prompt = gr.Textbox(label="Negative Prompt", value='lowres, bad anatomy, bad hands, cropped, worst quality')
|
410 |
+
with gr.Column():
|
411 |
+
result_gallery = gr.Gallery(height=832, object_fit='contain', label='Outputs')
|
412 |
+
with gr.Row():
|
413 |
+
dummy_image_for_outputs = gr.Image(visible=False, label='Result')
|
414 |
+
gr.Examples(
|
415 |
+
fn=lambda *args: ([args[-1]], None),
|
416 |
+
examples=db_examples.foreground_conditioned_examples,
|
417 |
+
inputs=[
|
418 |
+
input_fg, prompt, bg_source, image_width, image_height, seed, dummy_image_for_outputs
|
419 |
+
],
|
420 |
+
outputs=[result_gallery, output_bg],
|
421 |
+
run_on_click=True, examples_per_page=1024
|
422 |
+
)
|
423 |
+
ips = [input_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source]
|
424 |
+
relight_button.click(fn=process_relight, inputs=ips, outputs=[output_bg, result_gallery])
|
425 |
+
example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
|
426 |
+
example_quick_subjects.click(lambda x: x[0], inputs=example_quick_subjects, outputs=prompt, show_progress=False, queue=False)
|
427 |
|
|
|
|
|
428 |
|
429 |
+
block.launch(server_name='0.0.0.0')
|
|
briarmbg.py
ADDED
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# RMBG1.4 (diffusers implementation)
|
2 |
+
# Found on huggingface space of several projects
|
3 |
+
# Not sure which project is the source of this file
|
4 |
+
|
5 |
+
import torch
|
6 |
+
import torch.nn as nn
|
7 |
+
import torch.nn.functional as F
|
8 |
+
from huggingface_hub import PyTorchModelHubMixin
|
9 |
+
|
10 |
+
|
11 |
+
class REBNCONV(nn.Module):
|
12 |
+
def __init__(self, in_ch=3, out_ch=3, dirate=1, stride=1):
|
13 |
+
super(REBNCONV, self).__init__()
|
14 |
+
|
15 |
+
self.conv_s1 = nn.Conv2d(
|
16 |
+
in_ch, out_ch, 3, padding=1 * dirate, dilation=1 * dirate, stride=stride
|
17 |
+
)
|
18 |
+
self.bn_s1 = nn.BatchNorm2d(out_ch)
|
19 |
+
self.relu_s1 = nn.ReLU(inplace=True)
|
20 |
+
|
21 |
+
def forward(self, x):
|
22 |
+
hx = x
|
23 |
+
xout = self.relu_s1(self.bn_s1(self.conv_s1(hx)))
|
24 |
+
|
25 |
+
return xout
|
26 |
+
|
27 |
+
|
28 |
+
def _upsample_like(src, tar):
|
29 |
+
src = F.interpolate(src, size=tar.shape[2:], mode="bilinear")
|
30 |
+
return src
|
31 |
+
|
32 |
+
|
33 |
+
### RSU-7 ###
|
34 |
+
class RSU7(nn.Module):
|
35 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3, img_size=512):
|
36 |
+
super(RSU7, self).__init__()
|
37 |
+
|
38 |
+
self.in_ch = in_ch
|
39 |
+
self.mid_ch = mid_ch
|
40 |
+
self.out_ch = out_ch
|
41 |
+
|
42 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) ## 1 -> 1/2
|
43 |
+
|
44 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
45 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
46 |
+
|
47 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
48 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
49 |
+
|
50 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
51 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
52 |
+
|
53 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
54 |
+
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
55 |
+
|
56 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
57 |
+
self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
58 |
+
|
59 |
+
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
60 |
+
|
61 |
+
self.rebnconv7 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
62 |
+
|
63 |
+
self.rebnconv6d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
64 |
+
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
65 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
66 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
67 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
68 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
69 |
+
|
70 |
+
def forward(self, x):
|
71 |
+
b, c, h, w = x.shape
|
72 |
+
|
73 |
+
hx = x
|
74 |
+
hxin = self.rebnconvin(hx)
|
75 |
+
|
76 |
+
hx1 = self.rebnconv1(hxin)
|
77 |
+
hx = self.pool1(hx1)
|
78 |
+
|
79 |
+
hx2 = self.rebnconv2(hx)
|
80 |
+
hx = self.pool2(hx2)
|
81 |
+
|
82 |
+
hx3 = self.rebnconv3(hx)
|
83 |
+
hx = self.pool3(hx3)
|
84 |
+
|
85 |
+
hx4 = self.rebnconv4(hx)
|
86 |
+
hx = self.pool4(hx4)
|
87 |
+
|
88 |
+
hx5 = self.rebnconv5(hx)
|
89 |
+
hx = self.pool5(hx5)
|
90 |
+
|
91 |
+
hx6 = self.rebnconv6(hx)
|
92 |
+
|
93 |
+
hx7 = self.rebnconv7(hx6)
|
94 |
+
|
95 |
+
hx6d = self.rebnconv6d(torch.cat((hx7, hx6), 1))
|
96 |
+
hx6dup = _upsample_like(hx6d, hx5)
|
97 |
+
|
98 |
+
hx5d = self.rebnconv5d(torch.cat((hx6dup, hx5), 1))
|
99 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
100 |
+
|
101 |
+
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1))
|
102 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
103 |
+
|
104 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
105 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
106 |
+
|
107 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
108 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
109 |
+
|
110 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
111 |
+
|
112 |
+
return hx1d + hxin
|
113 |
+
|
114 |
+
|
115 |
+
### RSU-6 ###
|
116 |
+
class RSU6(nn.Module):
|
117 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
118 |
+
super(RSU6, self).__init__()
|
119 |
+
|
120 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
121 |
+
|
122 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
123 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
124 |
+
|
125 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
126 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
127 |
+
|
128 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
129 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
130 |
+
|
131 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
132 |
+
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
133 |
+
|
134 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
135 |
+
|
136 |
+
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
137 |
+
|
138 |
+
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
139 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
140 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
141 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
142 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
143 |
+
|
144 |
+
def forward(self, x):
|
145 |
+
hx = x
|
146 |
+
|
147 |
+
hxin = self.rebnconvin(hx)
|
148 |
+
|
149 |
+
hx1 = self.rebnconv1(hxin)
|
150 |
+
hx = self.pool1(hx1)
|
151 |
+
|
152 |
+
hx2 = self.rebnconv2(hx)
|
153 |
+
hx = self.pool2(hx2)
|
154 |
+
|
155 |
+
hx3 = self.rebnconv3(hx)
|
156 |
+
hx = self.pool3(hx3)
|
157 |
+
|
158 |
+
hx4 = self.rebnconv4(hx)
|
159 |
+
hx = self.pool4(hx4)
|
160 |
+
|
161 |
+
hx5 = self.rebnconv5(hx)
|
162 |
+
|
163 |
+
hx6 = self.rebnconv6(hx5)
|
164 |
+
|
165 |
+
hx5d = self.rebnconv5d(torch.cat((hx6, hx5), 1))
|
166 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
167 |
+
|
168 |
+
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1))
|
169 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
170 |
+
|
171 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
172 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
173 |
+
|
174 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
175 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
176 |
+
|
177 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
178 |
+
|
179 |
+
return hx1d + hxin
|
180 |
+
|
181 |
+
|
182 |
+
### RSU-5 ###
|
183 |
+
class RSU5(nn.Module):
|
184 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
185 |
+
super(RSU5, self).__init__()
|
186 |
+
|
187 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
188 |
+
|
189 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
190 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
191 |
+
|
192 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
193 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
194 |
+
|
195 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
196 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
197 |
+
|
198 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
199 |
+
|
200 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
201 |
+
|
202 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
203 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
204 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
205 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
206 |
+
|
207 |
+
def forward(self, x):
|
208 |
+
hx = x
|
209 |
+
|
210 |
+
hxin = self.rebnconvin(hx)
|
211 |
+
|
212 |
+
hx1 = self.rebnconv1(hxin)
|
213 |
+
hx = self.pool1(hx1)
|
214 |
+
|
215 |
+
hx2 = self.rebnconv2(hx)
|
216 |
+
hx = self.pool2(hx2)
|
217 |
+
|
218 |
+
hx3 = self.rebnconv3(hx)
|
219 |
+
hx = self.pool3(hx3)
|
220 |
+
|
221 |
+
hx4 = self.rebnconv4(hx)
|
222 |
+
|
223 |
+
hx5 = self.rebnconv5(hx4)
|
224 |
+
|
225 |
+
hx4d = self.rebnconv4d(torch.cat((hx5, hx4), 1))
|
226 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
227 |
+
|
228 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
229 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
230 |
+
|
231 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
232 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
233 |
+
|
234 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
235 |
+
|
236 |
+
return hx1d + hxin
|
237 |
+
|
238 |
+
|
239 |
+
### RSU-4 ###
|
240 |
+
class RSU4(nn.Module):
|
241 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
242 |
+
super(RSU4, self).__init__()
|
243 |
+
|
244 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
245 |
+
|
246 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
247 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
248 |
+
|
249 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
250 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
251 |
+
|
252 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
253 |
+
|
254 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
255 |
+
|
256 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
257 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
258 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
259 |
+
|
260 |
+
def forward(self, x):
|
261 |
+
hx = x
|
262 |
+
|
263 |
+
hxin = self.rebnconvin(hx)
|
264 |
+
|
265 |
+
hx1 = self.rebnconv1(hxin)
|
266 |
+
hx = self.pool1(hx1)
|
267 |
+
|
268 |
+
hx2 = self.rebnconv2(hx)
|
269 |
+
hx = self.pool2(hx2)
|
270 |
+
|
271 |
+
hx3 = self.rebnconv3(hx)
|
272 |
+
|
273 |
+
hx4 = self.rebnconv4(hx3)
|
274 |
+
|
275 |
+
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1))
|
276 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
277 |
+
|
278 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
279 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
280 |
+
|
281 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
282 |
+
|
283 |
+
return hx1d + hxin
|
284 |
+
|
285 |
+
|
286 |
+
### RSU-4F ###
|
287 |
+
class RSU4F(nn.Module):
|
288 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
289 |
+
super(RSU4F, self).__init__()
|
290 |
+
|
291 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
292 |
+
|
293 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
294 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
295 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=4)
|
296 |
+
|
297 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=8)
|
298 |
+
|
299 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=4)
|
300 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=2)
|
301 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
302 |
+
|
303 |
+
def forward(self, x):
|
304 |
+
hx = x
|
305 |
+
|
306 |
+
hxin = self.rebnconvin(hx)
|
307 |
+
|
308 |
+
hx1 = self.rebnconv1(hxin)
|
309 |
+
hx2 = self.rebnconv2(hx1)
|
310 |
+
hx3 = self.rebnconv3(hx2)
|
311 |
+
|
312 |
+
hx4 = self.rebnconv4(hx3)
|
313 |
+
|
314 |
+
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1))
|
315 |
+
hx2d = self.rebnconv2d(torch.cat((hx3d, hx2), 1))
|
316 |
+
hx1d = self.rebnconv1d(torch.cat((hx2d, hx1), 1))
|
317 |
+
|
318 |
+
return hx1d + hxin
|
319 |
+
|
320 |
+
|
321 |
+
class myrebnconv(nn.Module):
|
322 |
+
def __init__(
|
323 |
+
self,
|
324 |
+
in_ch=3,
|
325 |
+
out_ch=1,
|
326 |
+
kernel_size=3,
|
327 |
+
stride=1,
|
328 |
+
padding=1,
|
329 |
+
dilation=1,
|
330 |
+
groups=1,
|
331 |
+
):
|
332 |
+
super(myrebnconv, self).__init__()
|
333 |
+
|
334 |
+
self.conv = nn.Conv2d(
|
335 |
+
in_ch,
|
336 |
+
out_ch,
|
337 |
+
kernel_size=kernel_size,
|
338 |
+
stride=stride,
|
339 |
+
padding=padding,
|
340 |
+
dilation=dilation,
|
341 |
+
groups=groups,
|
342 |
+
)
|
343 |
+
self.bn = nn.BatchNorm2d(out_ch)
|
344 |
+
self.rl = nn.ReLU(inplace=True)
|
345 |
+
|
346 |
+
def forward(self, x):
|
347 |
+
return self.rl(self.bn(self.conv(x)))
|
348 |
+
|
349 |
+
|
350 |
+
class BriaRMBG(nn.Module, PyTorchModelHubMixin):
|
351 |
+
def __init__(self, config: dict = {"in_ch": 3, "out_ch": 1}):
|
352 |
+
super(BriaRMBG, self).__init__()
|
353 |
+
in_ch = config["in_ch"]
|
354 |
+
out_ch = config["out_ch"]
|
355 |
+
self.conv_in = nn.Conv2d(in_ch, 64, 3, stride=2, padding=1)
|
356 |
+
self.pool_in = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
357 |
+
|
358 |
+
self.stage1 = RSU7(64, 32, 64)
|
359 |
+
self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
360 |
+
|
361 |
+
self.stage2 = RSU6(64, 32, 128)
|
362 |
+
self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
363 |
+
|
364 |
+
self.stage3 = RSU5(128, 64, 256)
|
365 |
+
self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
366 |
+
|
367 |
+
self.stage4 = RSU4(256, 128, 512)
|
368 |
+
self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
369 |
+
|
370 |
+
self.stage5 = RSU4F(512, 256, 512)
|
371 |
+
self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
372 |
+
|
373 |
+
self.stage6 = RSU4F(512, 256, 512)
|
374 |
+
|
375 |
+
# decoder
|
376 |
+
self.stage5d = RSU4F(1024, 256, 512)
|
377 |
+
self.stage4d = RSU4(1024, 128, 256)
|
378 |
+
self.stage3d = RSU5(512, 64, 128)
|
379 |
+
self.stage2d = RSU6(256, 32, 64)
|
380 |
+
self.stage1d = RSU7(128, 16, 64)
|
381 |
+
|
382 |
+
self.side1 = nn.Conv2d(64, out_ch, 3, padding=1)
|
383 |
+
self.side2 = nn.Conv2d(64, out_ch, 3, padding=1)
|
384 |
+
self.side3 = nn.Conv2d(128, out_ch, 3, padding=1)
|
385 |
+
self.side4 = nn.Conv2d(256, out_ch, 3, padding=1)
|
386 |
+
self.side5 = nn.Conv2d(512, out_ch, 3, padding=1)
|
387 |
+
self.side6 = nn.Conv2d(512, out_ch, 3, padding=1)
|
388 |
+
|
389 |
+
# self.outconv = nn.Conv2d(6*out_ch,out_ch,1)
|
390 |
+
|
391 |
+
def forward(self, x):
|
392 |
+
hx = x
|
393 |
+
|
394 |
+
hxin = self.conv_in(hx)
|
395 |
+
# hx = self.pool_in(hxin)
|
396 |
+
|
397 |
+
# stage 1
|
398 |
+
hx1 = self.stage1(hxin)
|
399 |
+
hx = self.pool12(hx1)
|
400 |
+
|
401 |
+
# stage 2
|
402 |
+
hx2 = self.stage2(hx)
|
403 |
+
hx = self.pool23(hx2)
|
404 |
+
|
405 |
+
# stage 3
|
406 |
+
hx3 = self.stage3(hx)
|
407 |
+
hx = self.pool34(hx3)
|
408 |
+
|
409 |
+
# stage 4
|
410 |
+
hx4 = self.stage4(hx)
|
411 |
+
hx = self.pool45(hx4)
|
412 |
+
|
413 |
+
# stage 5
|
414 |
+
hx5 = self.stage5(hx)
|
415 |
+
hx = self.pool56(hx5)
|
416 |
+
|
417 |
+
# stage 6
|
418 |
+
hx6 = self.stage6(hx)
|
419 |
+
hx6up = _upsample_like(hx6, hx5)
|
420 |
+
|
421 |
+
# -------------------- decoder --------------------
|
422 |
+
hx5d = self.stage5d(torch.cat((hx6up, hx5), 1))
|
423 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
424 |
+
|
425 |
+
hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1))
|
426 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
427 |
+
|
428 |
+
hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1))
|
429 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
430 |
+
|
431 |
+
hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1))
|
432 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
433 |
+
|
434 |
+
hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1))
|
435 |
+
|
436 |
+
# side output
|
437 |
+
d1 = self.side1(hx1d)
|
438 |
+
d1 = _upsample_like(d1, x)
|
439 |
+
|
440 |
+
d2 = self.side2(hx2d)
|
441 |
+
d2 = _upsample_like(d2, x)
|
442 |
+
|
443 |
+
d3 = self.side3(hx3d)
|
444 |
+
d3 = _upsample_like(d3, x)
|
445 |
+
|
446 |
+
d4 = self.side4(hx4d)
|
447 |
+
d4 = _upsample_like(d4, x)
|
448 |
+
|
449 |
+
d5 = self.side5(hx5d)
|
450 |
+
d5 = _upsample_like(d5, x)
|
451 |
+
|
452 |
+
d6 = self.side6(hx6)
|
453 |
+
d6 = _upsample_like(d6, x)
|
454 |
+
|
455 |
+
return [
|
456 |
+
F.sigmoid(d1),
|
457 |
+
F.sigmoid(d2),
|
458 |
+
F.sigmoid(d3),
|
459 |
+
F.sigmoid(d4),
|
460 |
+
F.sigmoid(d5),
|
461 |
+
F.sigmoid(d6),
|
462 |
+
], [hx1d, hx2d, hx3d, hx4d, hx5d, hx6]
|
db_examples.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
foreground_conditioned_examples = [
|
2 |
+
[
|
3 |
+
"imgs/i1.webp",
|
4 |
+
"beautiful woman, detailed face, sunshine, outdoor, warm atmosphere",
|
5 |
+
"Right Light",
|
6 |
+
512,
|
7 |
+
960,
|
8 |
+
12345,
|
9 |
+
"imgs/o1.png",
|
10 |
+
],
|
11 |
+
[
|
12 |
+
"imgs/i1.webp",
|
13 |
+
"beautiful woman, detailed face, sunshine, outdoor, warm atmosphere",
|
14 |
+
"Left Light",
|
15 |
+
512,
|
16 |
+
960,
|
17 |
+
50,
|
18 |
+
"imgs/o2.png",
|
19 |
+
],
|
20 |
+
[
|
21 |
+
"imgs/i3.png",
|
22 |
+
"beautiful woman, detailed face, neon, Wong Kar-wai, warm",
|
23 |
+
"Left Light",
|
24 |
+
512,
|
25 |
+
768,
|
26 |
+
12345,
|
27 |
+
"imgs/o3.png",
|
28 |
+
],
|
29 |
+
[
|
30 |
+
"imgs/i3.png",
|
31 |
+
"beautiful woman, detailed face, sunshine from window",
|
32 |
+
"Left Light",
|
33 |
+
512,
|
34 |
+
768,
|
35 |
+
12345,
|
36 |
+
"imgs/o4.png",
|
37 |
+
],
|
38 |
+
[
|
39 |
+
"imgs/i5.png",
|
40 |
+
"beautiful woman, detailed face, warm atmosphere, at home, bedroom",
|
41 |
+
"Left Light",
|
42 |
+
512,
|
43 |
+
768,
|
44 |
+
123,
|
45 |
+
"imgs/o5.png",
|
46 |
+
],
|
47 |
+
[
|
48 |
+
"imgs/i6.jpg",
|
49 |
+
"beautiful woman, detailed face, sunshine from window",
|
50 |
+
"Right Light",
|
51 |
+
512,
|
52 |
+
768,
|
53 |
+
42,
|
54 |
+
"imgs/o6.png",
|
55 |
+
],
|
56 |
+
[
|
57 |
+
"imgs/i7.jpg",
|
58 |
+
"beautiful woman, detailed face, shadow from window",
|
59 |
+
"Left Light",
|
60 |
+
512,
|
61 |
+
768,
|
62 |
+
8888,
|
63 |
+
"imgs/o7.png",
|
64 |
+
],
|
65 |
+
[
|
66 |
+
"imgs/i8.webp",
|
67 |
+
"beautiful woman, detailed face, sunset over sea",
|
68 |
+
"Right Light",
|
69 |
+
512,
|
70 |
+
640,
|
71 |
+
42,
|
72 |
+
"imgs/o8.png",
|
73 |
+
],
|
74 |
+
[
|
75 |
+
"imgs/i9.png",
|
76 |
+
"handsome boy, detailed face, neon light, city",
|
77 |
+
"Left Light",
|
78 |
+
512,
|
79 |
+
640,
|
80 |
+
12345,
|
81 |
+
"imgs/o9.png",
|
82 |
+
],
|
83 |
+
[
|
84 |
+
"imgs/i10.png",
|
85 |
+
"beautiful woman, detailed face, light and shadow",
|
86 |
+
"Left Light",
|
87 |
+
512,
|
88 |
+
960,
|
89 |
+
8888,
|
90 |
+
"imgs/o10.png",
|
91 |
+
],
|
92 |
+
[
|
93 |
+
"imgs/i11.png",
|
94 |
+
"Buddha, detailed face, sci-fi RGB glowing, cyberpunk",
|
95 |
+
"Left Light",
|
96 |
+
512,
|
97 |
+
768,
|
98 |
+
8888,
|
99 |
+
"imgs/o11.png",
|
100 |
+
],
|
101 |
+
[
|
102 |
+
"imgs/i11.png",
|
103 |
+
"Buddha, detailed face, natural lighting",
|
104 |
+
"Left Light",
|
105 |
+
512,
|
106 |
+
768,
|
107 |
+
12345,
|
108 |
+
"imgs/o12.png",
|
109 |
+
],
|
110 |
+
[
|
111 |
+
"imgs/i13.png",
|
112 |
+
"toy, detailed face, shadow from window",
|
113 |
+
"Bottom Light",
|
114 |
+
512,
|
115 |
+
704,
|
116 |
+
12345,
|
117 |
+
"imgs/o13.png",
|
118 |
+
],
|
119 |
+
[
|
120 |
+
"imgs/i14.png",
|
121 |
+
"toy, detailed face, sunset over sea",
|
122 |
+
"Right Light",
|
123 |
+
512,
|
124 |
+
704,
|
125 |
+
100,
|
126 |
+
"imgs/o14.png",
|
127 |
+
],
|
128 |
+
[
|
129 |
+
"imgs/i15.png",
|
130 |
+
"dog, magic lit, sci-fi RGB glowing, studio lighting",
|
131 |
+
"Bottom Light",
|
132 |
+
512,
|
133 |
+
768,
|
134 |
+
12345,
|
135 |
+
"imgs/o15.png",
|
136 |
+
],
|
137 |
+
[
|
138 |
+
"imgs/i16.png",
|
139 |
+
"mysteriou human, warm atmosphere, warm atmosphere, at home, bedroom",
|
140 |
+
"Right Light",
|
141 |
+
512,
|
142 |
+
768,
|
143 |
+
100,
|
144 |
+
"imgs/o16.png",
|
145 |
+
],
|
146 |
+
]
|
147 |
+
|
148 |
+
bg_samples = [
|
149 |
+
'imgs/bgs/1.webp',
|
150 |
+
'imgs/bgs/2.webp',
|
151 |
+
'imgs/bgs/3.webp',
|
152 |
+
'imgs/bgs/4.webp',
|
153 |
+
'imgs/bgs/5.webp',
|
154 |
+
'imgs/bgs/6.webp',
|
155 |
+
'imgs/bgs/7.webp',
|
156 |
+
'imgs/bgs/8.webp',
|
157 |
+
'imgs/bgs/9.webp',
|
158 |
+
'imgs/bgs/10.webp',
|
159 |
+
'imgs/bgs/11.png',
|
160 |
+
'imgs/bgs/12.png',
|
161 |
+
'imgs/bgs/13.png',
|
162 |
+
'imgs/bgs/14.png',
|
163 |
+
'imgs/bgs/15.png',
|
164 |
+
]
|
165 |
+
|
166 |
+
background_conditioned_examples = [
|
167 |
+
[
|
168 |
+
"imgs/alter/i3.png",
|
169 |
+
"imgs/bgs/7.webp",
|
170 |
+
"beautiful woman, cinematic lighting",
|
171 |
+
"Use Background Image",
|
172 |
+
512,
|
173 |
+
768,
|
174 |
+
12345,
|
175 |
+
"imgs/alter/o1.png",
|
176 |
+
],
|
177 |
+
[
|
178 |
+
"imgs/alter/i2.png",
|
179 |
+
"imgs/bgs/11.png",
|
180 |
+
"statue of an angel, natural lighting",
|
181 |
+
"Use Flipped Background Image",
|
182 |
+
512,
|
183 |
+
768,
|
184 |
+
12345,
|
185 |
+
"imgs/alter/o2.png",
|
186 |
+
],
|
187 |
+
[
|
188 |
+
"imgs/alter/i1.jpeg",
|
189 |
+
"imgs/bgs/2.webp",
|
190 |
+
"beautiful woman, cinematic lighting",
|
191 |
+
"Use Background Image",
|
192 |
+
512,
|
193 |
+
768,
|
194 |
+
12345,
|
195 |
+
"imgs/alter/o3.png",
|
196 |
+
],
|
197 |
+
[
|
198 |
+
"imgs/alter/i1.jpeg",
|
199 |
+
"imgs/bgs/3.webp",
|
200 |
+
"beautiful woman, cinematic lighting",
|
201 |
+
"Use Background Image",
|
202 |
+
512,
|
203 |
+
768,
|
204 |
+
12345,
|
205 |
+
"imgs/alter/o4.png",
|
206 |
+
],
|
207 |
+
[
|
208 |
+
"imgs/alter/i6.webp",
|
209 |
+
"imgs/bgs/15.png",
|
210 |
+
"handsome man, cinematic lighting",
|
211 |
+
"Use Background Image",
|
212 |
+
512,
|
213 |
+
768,
|
214 |
+
12345,
|
215 |
+
"imgs/alter/o5.png",
|
216 |
+
],
|
217 |
+
]
|
imgs/alter/i1.jpeg
ADDED
Git LFS Details
|
imgs/alter/i2.png
ADDED
Git LFS Details
|
imgs/alter/i3.png
ADDED
Git LFS Details
|
imgs/alter/i4.png
ADDED
Git LFS Details
|
imgs/alter/i5.png
ADDED
Git LFS Details
|
imgs/alter/i6.webp
ADDED
Git LFS Details
|
imgs/alter/o1.png
ADDED
Git LFS Details
|
imgs/alter/o2.png
ADDED
Git LFS Details
|
imgs/alter/o3.png
ADDED
Git LFS Details
|
imgs/alter/o4.png
ADDED
Git LFS Details
|
imgs/alter/o5.png
ADDED
Git LFS Details
|
imgs/bgs/1.webp
ADDED
Git LFS Details
|
imgs/bgs/10.webp
ADDED
Git LFS Details
|
imgs/bgs/11.png
ADDED
Git LFS Details
|
imgs/bgs/12.png
ADDED
Git LFS Details
|
imgs/bgs/13.png
ADDED
Git LFS Details
|
imgs/bgs/14.png
ADDED
Git LFS Details
|
imgs/bgs/15.png
ADDED
Git LFS Details
|
imgs/bgs/2.webp
ADDED
Git LFS Details
|
imgs/bgs/3.webp
ADDED
Git LFS Details
|
imgs/bgs/4.webp
ADDED
Git LFS Details
|
imgs/bgs/5.webp
ADDED
Git LFS Details
|
imgs/bgs/6.webp
ADDED
Git LFS Details
|
imgs/bgs/7.webp
ADDED
Git LFS Details
|
imgs/bgs/8.webp
ADDED
Git LFS Details
|
imgs/bgs/9.webp
ADDED
Git LFS Details
|
imgs/i1.webp
ADDED
Git LFS Details
|
imgs/i10.png
ADDED
Git LFS Details
|
imgs/i11.png
ADDED
Git LFS Details
|
imgs/i13.png
ADDED
Git LFS Details
|
imgs/i14.png
ADDED
Git LFS Details
|
imgs/i15.png
ADDED
Git LFS Details
|
imgs/i16.png
ADDED
Git LFS Details
|
imgs/i3.png
ADDED
Git LFS Details
|
imgs/i5.png
ADDED
Git LFS Details
|
imgs/i6.jpg
ADDED
Git LFS Details
|
imgs/i7.jpg
ADDED
Git LFS Details
|