init
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
name: Publish to Comfy registry
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- "pyproject.toml"
|
||||
|
||||
jobs:
|
||||
publish-node:
|
||||
name: Publish Custom Node to registry
|
||||
runs-on: ubuntu-latest
|
||||
# if this is a forked repository. Skipping the workflow.
|
||||
if: github.event.repository.fork == false
|
||||
steps:
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v4
|
||||
- name: Publish Custom Node
|
||||
uses: Comfy-Org/publish-node-action@main
|
||||
with:
|
||||
## Add your own personal access token to your Github Repository secrets and reference it here.
|
||||
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
||||
.pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,42 @@
|
||||
[中文文档](README.md)
|
||||
|
||||
StableDelight 是一种从纹理表面去除镜面反射的尖端解决方案。
|
||||
|
||||
注:实测下来,物体变化有点大,特别是半透明的物体,如样例中的灯泡、玻璃。期待原项目的后期改进。
|
||||
|
||||
## 预览
|
||||

|
||||
|
||||
## 安装
|
||||
|
||||
- 手动安装
|
||||
```shell
|
||||
cd custom_nodes
|
||||
git clone https://github.com/lldacing/ComfyUI_StableDelight_ll.git
|
||||
cd ComfyUI_StableDelight_ll
|
||||
# 重启comfyUI
|
||||
```
|
||||
|
||||
|
||||
## 模型
|
||||
从[HuggingFace](https://huggingface.co/Stable-X/yoso-delight-v0-4-base/tree/main)下载所有文件放到目录`ComfyUI/models/diffusers/Stable-X--yoso-delight-v0-4-base`
|
||||
|
||||
建议使用huggingface-cli下载
|
||||
```
|
||||
# 设置代理,按需设置,也可开全局代理
|
||||
set https_proxy=http://127.0.0.1:7890
|
||||
# 在ComfyUI/models/diffusers/目录下启动命令行执行下面的命令,如果找不到huggingface-cli,huggingface-cli在${python_home}/Scripts目录下,使用全路径
|
||||
huggingface-cli download Stable-X/yoso-delight-v0-4-base --local-dir Stable-X--yoso-delight-v0-4-base
|
||||
```
|
||||
目录结构如下:
|
||||
```
|
||||
ComfyUI
|
||||
└─models
|
||||
└─diffusers
|
||||
└─Stable-X--yoso-delight-v0-4-base
|
||||
```
|
||||
|
||||
## 感谢
|
||||
|
||||
原项目 [Stable-X/StableDelight](https://github.com/Stable-X/StableDelight)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
[English](README_EN.md)
|
||||
|
||||
StableDelight is a cutting-edge solution for specular reflection removal from textured surfaces.
|
||||
|
||||
Note: According to my tests, the objects will change a lot, especially semi transparent objects such as light bulbs and glass in the sample. Looking forward to future improvements.
|
||||
|
||||
## Preview
|
||||

|
||||
|
||||
## Install
|
||||
|
||||
- Manual
|
||||
```shell
|
||||
cd custom_nodes
|
||||
git clone https://github.com/lldacing/ComfyUI_StableDelight_ll.git
|
||||
cd ComfyUI_StableDelight_ll
|
||||
# restart ComfyUI
|
||||
```
|
||||
|
||||
|
||||
## Model
|
||||
From [HuggingFace](https://huggingface.co/Stable-X/yoso-delight-v0-4-base/tree/main) download all files to `ComfyUI/models/diffusers/Stable-X--yoso-delight-v0-4-base` directory.
|
||||
|
||||
Suggest using huggingface-cli to download
|
||||
```
|
||||
# Start the command line in the ComfyUI/models/diffusers directory and execute the following command
|
||||
huggingface-cli download Stable-X/yoso-delight-v0-4-base --local-dir Stable-X--yoso-delight-v0-4-base
|
||||
```
|
||||
目录结构如下:
|
||||
```
|
||||
ComfyUI
|
||||
└─models
|
||||
└─diffusers
|
||||
└─Stable-X--yoso-delight-v0-4-base
|
||||
```
|
||||
|
||||
|
||||
## Thanks
|
||||
|
||||
Original Project [Stable-X/StableDelight](https://github.com/Stable-X/StableDelight)
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 获取当前目录的父目录
|
||||
parent_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# 添加父目录到系统路径
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from .nodes import BaseNode
|
||||
|
||||
NODE_CLASS_MAPPINGS = {**BaseNode.NODE_CLASS_MAPPINGS}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {**BaseNode.NODE_DISPLAY_NAME_MAPPINGS}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 831 KiB |
@@ -0,0 +1,131 @@
|
||||
import os
|
||||
import torch
|
||||
from comfy import model_management
|
||||
import folder_paths
|
||||
from .libs.YosoDelightPipe import YosoDelightPipeline
|
||||
|
||||
deviceType = model_management.get_torch_device().type
|
||||
|
||||
|
||||
class LoadStableDelightModel:
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
paths = []
|
||||
for search_path in folder_paths.get_folder_paths("diffusers"):
|
||||
if os.path.exists(search_path):
|
||||
for root, subdir, files in os.walk(search_path, followlinks=True):
|
||||
if "model_index.json" in files:
|
||||
paths.append(os.path.relpath(root, start=search_path))
|
||||
return {
|
||||
"required": {
|
||||
"model": (paths,),
|
||||
"device": (["AUTO", "CPU"], )
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("DelightMODEL",)
|
||||
RETURN_NAMES = ("model",)
|
||||
FUNCTION = "load_model"
|
||||
CATEGORY = "stableX/delight"
|
||||
DESCRIPTION = "Load BEN model from folder models/rembg/ben or the path of ben configured in the extra YAML file"
|
||||
|
||||
def load_model(self, model, device):
|
||||
for search_path in folder_paths.get_folder_paths("diffusers"):
|
||||
if os.path.exists(search_path):
|
||||
path = os.path.join(search_path, model)
|
||||
if os.path.exists(path):
|
||||
model = path
|
||||
break
|
||||
|
||||
if device == "AUTO":
|
||||
device_type = deviceType
|
||||
else:
|
||||
device_type = "cpu"
|
||||
# Stable-X/yoso-delight-v0-4-base
|
||||
pipe = YosoDelightPipeline.from_pretrained(model,
|
||||
cache_dir=folder_paths.get_folder_paths("diffusers")[0],
|
||||
safety_checker=None,
|
||||
variant="fp16",
|
||||
torch_dtype=torch.float16 if device_type == "cuda" else None,
|
||||
t_start=0,
|
||||
local_files_only=True).to(device_type)
|
||||
# 启用 xformers
|
||||
if model_management.XFORMERS_IS_AVAILABLE and device_type == "cuda":
|
||||
pipe.enable_xformers_memory_efficient_attention()
|
||||
|
||||
return (pipe, )
|
||||
|
||||
|
||||
class ApplyStableDelight:
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"model": ("DelightMODEL",),
|
||||
"images": ("IMAGE",),
|
||||
"strength": ("FLOAT",
|
||||
{
|
||||
"default": 1,
|
||||
"min": 0.01,
|
||||
"max": 5.0,
|
||||
"step": 0.1
|
||||
}),
|
||||
"resolution": ("INT",
|
||||
{
|
||||
"default": 1024,
|
||||
"step": 8,
|
||||
"min": 0,
|
||||
"max": 4096,
|
||||
"tooltip": "Preprocess the resolution of the image, if eq 0, using image original size"
|
||||
}),
|
||||
"upscale_method": (["nearest-exact", "bilinear", "area", "bicubic", "lanczos"], {"default": "bilinear"}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
RETURN_NAMES = ("image",)
|
||||
FUNCTION = "delight"
|
||||
CATEGORY = "stableX/delight"
|
||||
|
||||
def delight(self, model, images, strength, resolution, upscale_method):
|
||||
_images = []
|
||||
_masks = []
|
||||
|
||||
for image in images:
|
||||
h, w, c = image.shape
|
||||
# h, w, c -> c, h, w
|
||||
im_tensor = image.permute(2, 0, 1)
|
||||
|
||||
with torch.no_grad():
|
||||
result_image = model(im_tensor,
|
||||
controlnet_conditioning_scale=strength,
|
||||
skip_preprocess=resolution == 0,
|
||||
processing_resolution=resolution,
|
||||
resample_methodinput=upscale_method,
|
||||
resample_method_output=upscale_method,
|
||||
output_type="pt"
|
||||
).prediction.cpu()
|
||||
|
||||
# 归一化
|
||||
result_image = (result_image.clamp(-1, 1) + 1) / 2
|
||||
# b, c, h, w -> b, h, w, c
|
||||
result_image = result_image.permute(0, 2, 3, 1)
|
||||
|
||||
_images.append(result_image)
|
||||
|
||||
out_images = torch.cat(_images, dim=0)
|
||||
|
||||
return out_images,
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"LoadStableDelightModel": LoadStableDelightModel,
|
||||
"ApplyStableDelight": ApplyStableDelight,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"LoadStableDelightModel": "LoadStableDelightModel",
|
||||
"ApplyStableDelight": "ApplyStableDelight",
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
# import os
|
||||
#
|
||||
# import folder_paths
|
||||
#
|
||||
# models_dir_key = "ben"
|
||||
# models_dir_default = os.path.join(folder_paths.models_dir, "rembg/ben")
|
||||
#
|
||||
# if not os.path.exists(models_dir_default):
|
||||
# os.makedirs(models_dir_default, exist_ok=True)
|
||||
#
|
||||
# if models_dir_key not in folder_paths.folder_names_and_paths:
|
||||
# folder_paths.folder_names_and_paths[models_dir_key] = (
|
||||
# [models_dir_default], folder_paths.supported_pt_extensions)
|
||||
# else:
|
||||
# folder_paths.add_model_folder_path(models_dir_key, models_dir_default)
|
||||
@@ -0,0 +1,760 @@
|
||||
# Copyright 2024 Marigold authors, PRS ETH Zurich. All rights reserved.
|
||||
# Copyright 2024 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# --------------------------------------------------------------------------
|
||||
# More information and citation instructions are available on the
|
||||
# --------------------------------------------------------------------------
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from PIL import Image
|
||||
from tqdm.auto import tqdm
|
||||
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection
|
||||
|
||||
|
||||
from diffusers.image_processor import PipelineImageInput
|
||||
from diffusers.models import (
|
||||
AutoencoderKL,
|
||||
UNet2DConditionModel,
|
||||
ControlNetModel,
|
||||
)
|
||||
from diffusers.schedulers import (
|
||||
DDIMScheduler
|
||||
)
|
||||
|
||||
from diffusers.utils import (
|
||||
BaseOutput,
|
||||
logging,
|
||||
replace_example_docstring,
|
||||
)
|
||||
|
||||
|
||||
from diffusers.utils.torch_utils import randn_tensor
|
||||
from diffusers.pipelines.controlnet import StableDiffusionControlNetPipeline
|
||||
from diffusers.pipelines.marigold.marigold_image_processing import MarigoldImageProcessor
|
||||
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
|
||||
|
||||
import pdb
|
||||
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
||||
|
||||
|
||||
EXAMPLE_DOC_STRING = """
|
||||
Examples:
|
||||
```py
|
||||
>>> import diffusers
|
||||
>>> import torch
|
||||
|
||||
>>> pipe = diffusers.MarigoldNormalsPipeline.from_pretrained(
|
||||
... "prs-eth/marigold-normals-lcm-v0-1", variant="fp16", torch_dtype=torch.float16
|
||||
... ).to("cuda")
|
||||
|
||||
>>> image = diffusers.utils.load_image("https://marigoldmonodepth.github.io/images/einstein.jpg")
|
||||
>>> normals = pipe(image)
|
||||
|
||||
>>> vis = pipe.image_processor.visualize_normals(normals.prediction)
|
||||
>>> vis[0].save("einstein_normals.png")
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class YosoDelightOutput(BaseOutput):
|
||||
"""
|
||||
Output class for Marigold monocular normals prediction pipeline.
|
||||
|
||||
Args:
|
||||
prediction (`np.ndarray`, `torch.Tensor`):
|
||||
Predicted normals with values in the range [-1, 1]. The shape is always $numimages \times 3 \times height
|
||||
\times width$, regardless of whether the images were passed as a 4D array or a list.
|
||||
uncertainty (`None`, `np.ndarray`, `torch.Tensor`):
|
||||
Uncertainty maps computed from the ensemble, with values in the range [0, 1]. The shape is $numimages
|
||||
\times 1 \times height \times width$.
|
||||
latent (`None`, `torch.Tensor`):
|
||||
Latent features corresponding to the predictions, compatible with the `latents` argument of the pipeline.
|
||||
The shape is $numimages * numensemble \times 4 \times latentheight \times latentwidth$.
|
||||
"""
|
||||
|
||||
prediction: Union[np.ndarray, torch.Tensor]
|
||||
latent: Union[None, torch.Tensor]
|
||||
gaus_noise: Union[None, torch.Tensor]
|
||||
|
||||
|
||||
class YosoDelightPipeline(StableDiffusionControlNetPipeline):
|
||||
""" Pipeline for monocular normals estimation using the Marigold method: https://marigoldmonodepth.github.io.
|
||||
Pipeline for text-to-image generation using Stable Diffusion with ControlNet guidance.
|
||||
|
||||
This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
|
||||
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
|
||||
|
||||
The pipeline also inherits the following loading methods:
|
||||
- [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
|
||||
- [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
|
||||
- [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
|
||||
- [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
|
||||
- [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters
|
||||
|
||||
Args:
|
||||
vae ([`AutoencoderKL`]):
|
||||
Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
|
||||
text_encoder ([`~transformers.CLIPTextModel`]):
|
||||
Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
|
||||
tokenizer ([`~transformers.CLIPTokenizer`]):
|
||||
A `CLIPTokenizer` to tokenize text.
|
||||
unet ([`UNet2DConditionModel`]):
|
||||
A `UNet2DConditionModel` to denoise the encoded image latents.
|
||||
controlnet ([`ControlNetModel`] or `List[ControlNetModel]`):
|
||||
Provides additional conditioning to the `unet` during the denoising process. If you set multiple
|
||||
ControlNets as a list, the outputs from each ControlNet are added together to create one combined
|
||||
additional conditioning.
|
||||
scheduler ([`SchedulerMixin`]):
|
||||
A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
|
||||
[`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
|
||||
safety_checker ([`StableDiffusionSafetyChecker`]):
|
||||
Classification module that estimates whether generated images could be considered offensive or harmful.
|
||||
Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
|
||||
about a model's potential harms.
|
||||
feature_extractor ([`~transformers.CLIPImageProcessor`]):
|
||||
A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
|
||||
"""
|
||||
|
||||
model_cpu_offload_seq = "text_encoder->image_encoder->unet->vae"
|
||||
_optional_components = ["safety_checker", "feature_extractor", "image_encoder"]
|
||||
_exclude_from_cpu_offload = ["safety_checker"]
|
||||
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
|
||||
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vae: AutoencoderKL,
|
||||
text_encoder: CLIPTextModel,
|
||||
tokenizer: CLIPTokenizer,
|
||||
unet: UNet2DConditionModel,
|
||||
controlnet: Union[ControlNetModel, List[ControlNetModel], Tuple[ControlNetModel]],
|
||||
scheduler: Union[DDIMScheduler],
|
||||
safety_checker: StableDiffusionSafetyChecker,
|
||||
feature_extractor: CLIPImageProcessor,
|
||||
image_encoder: CLIPVisionModelWithProjection = None,
|
||||
requires_safety_checker: bool = True,
|
||||
default_denoising_steps: Optional[int] = 1,
|
||||
default_processing_resolution: Optional[int] = 768,
|
||||
prompt="",
|
||||
empty_text_embedding=None,
|
||||
t_start: Optional[int] = 401,
|
||||
):
|
||||
super().__init__(
|
||||
vae,
|
||||
text_encoder,
|
||||
tokenizer,
|
||||
unet,
|
||||
controlnet,
|
||||
scheduler,
|
||||
safety_checker,
|
||||
feature_extractor,
|
||||
image_encoder,
|
||||
requires_safety_checker,
|
||||
)
|
||||
|
||||
# TODO yoso ImageProcessor
|
||||
self.image_processor = MarigoldImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
||||
self.control_image_processor = MarigoldImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
||||
self.default_denoising_steps = default_denoising_steps
|
||||
self.default_processing_resolution = default_processing_resolution
|
||||
self.prompt = prompt
|
||||
self.prompt_embeds = None
|
||||
self.empty_text_embedding = empty_text_embedding
|
||||
self.t_start= t_start # target_out latents
|
||||
|
||||
def check_inputs(
|
||||
self,
|
||||
image: PipelineImageInput,
|
||||
num_inference_steps: int,
|
||||
ensemble_size: int,
|
||||
processing_resolution: int,
|
||||
resample_method_input: str,
|
||||
resample_method_output: str,
|
||||
batch_size: int,
|
||||
ensembling_kwargs: Optional[Dict[str, Any]],
|
||||
latents: Optional[torch.Tensor],
|
||||
generator: Optional[Union[torch.Generator, List[torch.Generator]]],
|
||||
output_type: str,
|
||||
output_uncertainty: bool,
|
||||
) -> int:
|
||||
if num_inference_steps is None:
|
||||
raise ValueError("`num_inference_steps` is not specified and could not be resolved from the model config.")
|
||||
if num_inference_steps < 1:
|
||||
raise ValueError("`num_inference_steps` must be positive.")
|
||||
if ensemble_size < 1:
|
||||
raise ValueError("`ensemble_size` must be positive.")
|
||||
if ensemble_size == 2:
|
||||
logger.warning(
|
||||
"`ensemble_size` == 2 results are similar to no ensembling (1); "
|
||||
"consider increasing the value to at least 3."
|
||||
)
|
||||
if ensemble_size == 1 and output_uncertainty:
|
||||
raise ValueError(
|
||||
"Computing uncertainty by setting `output_uncertainty=True` also requires setting `ensemble_size` "
|
||||
"greater than 1."
|
||||
)
|
||||
if processing_resolution is None:
|
||||
raise ValueError(
|
||||
"`processing_resolution` is not specified and could not be resolved from the model config."
|
||||
)
|
||||
if processing_resolution < 0:
|
||||
raise ValueError(
|
||||
"`processing_resolution` must be non-negative: 0 for native resolution, or any positive value for "
|
||||
"downsampled processing."
|
||||
)
|
||||
if processing_resolution % self.vae_scale_factor != 0:
|
||||
raise ValueError(f"`processing_resolution` must be a multiple of {self.vae_scale_factor}.")
|
||||
if resample_method_input not in ("nearest", "nearest-exact", "bilinear", "bicubic", "area"):
|
||||
raise ValueError(
|
||||
"`resample_method_input` takes string values compatible with PIL library: "
|
||||
"nearest, nearest-exact, bilinear, bicubic, area."
|
||||
)
|
||||
if resample_method_output not in ("nearest", "nearest-exact", "bilinear", "bicubic", "area"):
|
||||
raise ValueError(
|
||||
"`resample_method_output` takes string values compatible with PIL library: "
|
||||
"nearest, nearest-exact, bilinear, bicubic, area."
|
||||
)
|
||||
if batch_size < 1:
|
||||
raise ValueError("`batch_size` must be positive.")
|
||||
if output_type not in ["pt", "np"]:
|
||||
raise ValueError("`output_type` must be one of `pt` or `np`.")
|
||||
if latents is not None and generator is not None:
|
||||
raise ValueError("`latents` and `generator` cannot be used together.")
|
||||
if ensembling_kwargs is not None:
|
||||
if not isinstance(ensembling_kwargs, dict):
|
||||
raise ValueError("`ensembling_kwargs` must be a dictionary.")
|
||||
if "reduction" in ensembling_kwargs and ensembling_kwargs["reduction"] not in ("closest", "mean"):
|
||||
raise ValueError("`ensembling_kwargs['reduction']` can be either `'closest'` or `'mean'`.")
|
||||
|
||||
# image checks
|
||||
num_images = 0
|
||||
W, H = None, None
|
||||
if not isinstance(image, list):
|
||||
image = [image]
|
||||
for i, img in enumerate(image):
|
||||
if isinstance(img, np.ndarray) or torch.is_tensor(img):
|
||||
if img.ndim not in (2, 3, 4):
|
||||
raise ValueError(f"`image[{i}]` has unsupported dimensions or shape: {img.shape}.")
|
||||
H_i, W_i = img.shape[-2:]
|
||||
N_i = 1
|
||||
if img.ndim == 4:
|
||||
N_i = img.shape[0]
|
||||
elif isinstance(img, Image.Image):
|
||||
W_i, H_i = img.size
|
||||
N_i = 1
|
||||
else:
|
||||
raise ValueError(f"Unsupported `image[{i}]` type: {type(img)}.")
|
||||
if W is None:
|
||||
W, H = W_i, H_i
|
||||
elif (W, H) != (W_i, H_i):
|
||||
raise ValueError(
|
||||
f"Input `image[{i}]` has incompatible dimensions {(W_i, H_i)} with the previous images {(W, H)}"
|
||||
)
|
||||
num_images += N_i
|
||||
|
||||
# latents checks
|
||||
if latents is not None:
|
||||
if not torch.is_tensor(latents):
|
||||
raise ValueError("`latents` must be a torch.Tensor.")
|
||||
if latents.dim() != 4:
|
||||
raise ValueError(f"`latents` has unsupported dimensions or shape: {latents.shape}.")
|
||||
|
||||
if processing_resolution > 0:
|
||||
max_orig = max(H, W)
|
||||
new_H = H * processing_resolution // max_orig
|
||||
new_W = W * processing_resolution // max_orig
|
||||
if new_H == 0 or new_W == 0:
|
||||
raise ValueError(f"Extreme aspect ratio of the input image: [{W} x {H}]")
|
||||
W, H = new_W, new_H
|
||||
w = (W + self.vae_scale_factor - 1) // self.vae_scale_factor
|
||||
h = (H + self.vae_scale_factor - 1) // self.vae_scale_factor
|
||||
shape_expected = (num_images * ensemble_size, self.vae.config.latent_channels, h, w)
|
||||
|
||||
if latents.shape != shape_expected:
|
||||
raise ValueError(f"`latents` has unexpected shape={latents.shape} expected={shape_expected}.")
|
||||
|
||||
# generator checks
|
||||
if generator is not None:
|
||||
if isinstance(generator, list):
|
||||
if len(generator) != num_images * ensemble_size:
|
||||
raise ValueError(
|
||||
"The number of generators must match the total number of ensemble members for all input images."
|
||||
)
|
||||
if not all(g.device.type == generator[0].device.type for g in generator):
|
||||
raise ValueError("`generator` device placement is not consistent in the list.")
|
||||
elif not isinstance(generator, torch.Generator):
|
||||
raise ValueError(f"Unsupported generator type: {type(generator)}.")
|
||||
|
||||
return num_images
|
||||
|
||||
def progress_bar(self, iterable=None, total=None, desc=None, leave=True):
|
||||
if not hasattr(self, "_progress_bar_config"):
|
||||
self._progress_bar_config = {}
|
||||
elif not isinstance(self._progress_bar_config, dict):
|
||||
raise ValueError(
|
||||
f"`self._progress_bar_config` should be of type `dict`, but is {type(self._progress_bar_config)}."
|
||||
)
|
||||
|
||||
progress_bar_config = dict(**self._progress_bar_config)
|
||||
progress_bar_config["desc"] = progress_bar_config.get("desc", desc)
|
||||
progress_bar_config["leave"] = progress_bar_config.get("leave", leave)
|
||||
if iterable is not None:
|
||||
return tqdm(iterable, **progress_bar_config)
|
||||
elif total is not None:
|
||||
return tqdm(total=total, **progress_bar_config)
|
||||
else:
|
||||
raise ValueError("Either `total` or `iterable` has to be defined.")
|
||||
|
||||
@torch.no_grad()
|
||||
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
||||
def __call__(
|
||||
self,
|
||||
image: PipelineImageInput,
|
||||
prompt: Union[str, List[str]] = None,
|
||||
negative_prompt: Optional[Union[str, List[str]]] = None,
|
||||
num_inference_steps: Optional[int] = None,
|
||||
ensemble_size: int = 1,
|
||||
processing_resolution: Optional[int] = None,
|
||||
match_input_resolution: bool = True,
|
||||
resample_method_input: str = "bilinear",
|
||||
resample_method_output: str = "bilinear",
|
||||
batch_size: int = 1,
|
||||
ensembling_kwargs: Optional[Dict[str, Any]] = None,
|
||||
latents: Optional[Union[torch.Tensor, List[torch.Tensor]]] = None,
|
||||
prompt_embeds: Optional[torch.Tensor] = None,
|
||||
negative_prompt_embeds: Optional[torch.Tensor] = None,
|
||||
num_images_per_prompt: Optional[int] = 1,
|
||||
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
||||
controlnet_conditioning_scale: Union[float, List[float]] = 1.0,
|
||||
output_type: str = "np",
|
||||
output_uncertainty: bool = False,
|
||||
output_latent: bool = False,
|
||||
skip_preprocess: bool = False,
|
||||
return_dict: bool = True,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
Function invoked when calling the pipeline.
|
||||
|
||||
Args:
|
||||
image (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`),
|
||||
`List[torch.Tensor]`: An input image or images used as an input for the normals estimation task. For
|
||||
arrays and tensors, the expected value range is between `[0, 1]`. Passing a batch of images is possible
|
||||
by providing a four-dimensional array or a tensor. Additionally, a list of images of two- or
|
||||
three-dimensional arrays or tensors can be passed. In the latter case, all list elements must have the
|
||||
same width and height.
|
||||
num_inference_steps (`int`, *optional*, defaults to `None`):
|
||||
Number of denoising diffusion steps during inference. The default value `None` results in automatic
|
||||
selection. The number of steps should be at least 10 with the full Marigold models, and between 1 and 4
|
||||
for Marigold-LCM models.
|
||||
ensemble_size (`int`, defaults to `1`):
|
||||
Number of ensemble predictions. Recommended values are 5 and higher for better precision, or 1 for
|
||||
faster inference.
|
||||
processing_resolution (`int`, *optional*, defaults to `None`):
|
||||
Effective processing resolution. When set to `0`, matches the larger input image dimension. This
|
||||
produces crisper predictions, but may also lead to the overall loss of global context. The default
|
||||
value `None` resolves to the optimal value from the model config.
|
||||
match_input_resolution (`bool`, *optional*, defaults to `True`):
|
||||
When enabled, the output prediction is resized to match the input dimensions. When disabled, the longer
|
||||
side of the output will equal to `processing_resolution`.
|
||||
resample_method_input (`str`, *optional*, defaults to `"bilinear"`):
|
||||
Resampling method used to resize input images to `processing_resolution`. The accepted values are:
|
||||
`"nearest"`, `"nearest-exact"`, `"bilinear"`, `"bicubic"`, or `"area"`.
|
||||
resample_method_output (`str`, *optional*, defaults to `"bilinear"`):
|
||||
Resampling method used to resize output predictions to match the input resolution. The accepted values
|
||||
are `"nearest"`, `"nearest-exact"`, `"bilinear"`, `"bicubic"`, or `"area"`.
|
||||
batch_size (`int`, *optional*, defaults to `1`):
|
||||
Batch size; only matters when setting `ensemble_size` or passing a tensor of images.
|
||||
ensembling_kwargs (`dict`, *optional*, defaults to `None`)
|
||||
Extra dictionary with arguments for precise ensembling control. The following options are available:
|
||||
- reduction (`str`, *optional*, defaults to `"closest"`): Defines the ensembling function applied in
|
||||
every pixel location, can be either `"closest"` or `"mean"`.
|
||||
latents (`torch.Tensor`, *optional*, defaults to `None`):
|
||||
Latent noise tensors to replace the random initialization. These can be taken from the previous
|
||||
function call's output.
|
||||
generator (`torch.Generator`, or `List[torch.Generator]`, *optional*, defaults to `None`):
|
||||
Random number generator object to ensure reproducibility.
|
||||
output_type (`str`, *optional*, defaults to `"np"`):
|
||||
Preferred format of the output's `prediction` and the optional `uncertainty` fields. The accepted
|
||||
values are: `"np"` (numpy array) or `"pt"` (torch tensor).
|
||||
output_uncertainty (`bool`, *optional*, defaults to `False`):
|
||||
When enabled, the output's `uncertainty` field contains the predictive uncertainty map, provided that
|
||||
the `ensemble_size` argument is set to a value above 2.
|
||||
output_latent (`bool`, *optional*, defaults to `False`):
|
||||
When enabled, the output's `latent` field contains the latent codes corresponding to the predictions
|
||||
within the ensemble. These codes can be saved, modified, and used for subsequent calls with the
|
||||
`latents` argument.
|
||||
return_dict (`bool`, *optional*, defaults to `True`):
|
||||
Whether or not to return a [`~pipelines.marigold.MarigoldDepthOutput`] instead of a plain tuple.
|
||||
|
||||
Examples:
|
||||
|
||||
Returns:
|
||||
[`~pipelines.marigold.MarigoldNormalsOutput`] or `tuple`:
|
||||
If `return_dict` is `True`, [`~pipelines.marigold.MarigoldNormalsOutput`] is returned, otherwise a
|
||||
`tuple` is returned where the first element is the prediction, the second element is the uncertainty
|
||||
(or `None`), and the third is the latent (or `None`).
|
||||
"""
|
||||
|
||||
# 0. Resolving variables.
|
||||
device = self._execution_device
|
||||
dtype = self.dtype
|
||||
|
||||
# Model-specific optimal default values leading to fast and reasonable results.
|
||||
if num_inference_steps is None:
|
||||
num_inference_steps = self.default_denoising_steps
|
||||
if processing_resolution is None:
|
||||
processing_resolution = self.default_processing_resolution
|
||||
|
||||
# 1. Check inputs.
|
||||
num_images = self.check_inputs(
|
||||
image,
|
||||
num_inference_steps,
|
||||
ensemble_size,
|
||||
processing_resolution,
|
||||
resample_method_input,
|
||||
resample_method_output,
|
||||
batch_size,
|
||||
ensembling_kwargs,
|
||||
latents,
|
||||
generator,
|
||||
output_type,
|
||||
output_uncertainty,
|
||||
)
|
||||
|
||||
|
||||
# 2. Prepare empty text conditioning.
|
||||
# Model invocation: self.tokenizer, self.text_encoder.
|
||||
if self.empty_text_embedding is None:
|
||||
prompt = ""
|
||||
text_inputs = self.tokenizer(
|
||||
prompt,
|
||||
padding="do_not_pad",
|
||||
max_length=self.tokenizer.model_max_length,
|
||||
truncation=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
text_input_ids = text_inputs.input_ids.to(device)
|
||||
self.empty_text_embedding = self.text_encoder(text_input_ids)[0] # [1,2,1024]
|
||||
|
||||
|
||||
|
||||
# 3. prepare prompt
|
||||
if self.prompt_embeds is None:
|
||||
prompt_embeds, negative_prompt_embeds = self.encode_prompt(
|
||||
self.prompt,
|
||||
device,
|
||||
num_images_per_prompt,
|
||||
False,
|
||||
negative_prompt,
|
||||
prompt_embeds=prompt_embeds,
|
||||
negative_prompt_embeds=None,
|
||||
lora_scale=None,
|
||||
clip_skip=None,
|
||||
)
|
||||
self.prompt_embeds = prompt_embeds
|
||||
self.negative_prompt_embeds = negative_prompt_embeds
|
||||
|
||||
|
||||
|
||||
# 4. Preprocess input images. This function loads input image or images of compatible dimensions `(H, W)`,
|
||||
# optionally downsamples them to the `processing_resolution` `(PH, PW)`, where
|
||||
# `max(PH, PW) == processing_resolution`, and pads the dimensions to `(PPH, PPW)` such that these values are
|
||||
# divisible by the latent space downscaling factor (typically 8 in Stable Diffusion). The default value `None`
|
||||
# of `processing_resolution` resolves to the optimal value from the model config. It is a recommended mode of
|
||||
# operation and leads to the most reasonable results. Using the native image resolution or any other processing
|
||||
# resolution can lead to loss of either fine details or global context in the output predictions.
|
||||
if not skip_preprocess:
|
||||
image, padding, original_resolution = self.image_processor.preprocess(
|
||||
image, processing_resolution, resample_method_input, device, dtype
|
||||
) # [N,3,PPH,PPW]
|
||||
else:
|
||||
padding = (0, 0)
|
||||
original_resolution = image.shape[2:]
|
||||
# 5. Encode input image into latent space. At this step, each of the `N` input images is represented with `E`
|
||||
# ensemble members. Each ensemble member is an independent diffused prediction, just initialized independently.
|
||||
# Latents of each such predictions across all input images and all ensemble members are represented in the
|
||||
# `pred_latent` variable. The variable `image_latent` is of the same shape: it contains each input image encoded
|
||||
# into latent space and replicated `E` times. The latents can be either generated (see `generator` to ensure
|
||||
# reproducibility), or passed explicitly via the `latents` argument. The latter can be set outside the pipeline
|
||||
# code. For example, in the Marigold-LCM video processing demo, the latents initialization of a frame is taken
|
||||
# as a convex combination of the latents output of the pipeline for the previous frame and a newly-sampled
|
||||
# noise. This behavior can be achieved by setting the `output_latent` argument to `True`. The latent space
|
||||
# dimensions are `(h, w)`. Encoding into latent space happens in batches of size `batch_size`.
|
||||
# Model invocation: self.vae.encoder.
|
||||
image_latent, pred_latent = self.prepare_latents(
|
||||
image, latents, generator, ensemble_size, batch_size
|
||||
) # [N*E,4,h,w], [N*E,4,h,w]
|
||||
|
||||
gaus_noise = pred_latent.detach().clone()
|
||||
del image
|
||||
|
||||
|
||||
# 6. obtain control_output
|
||||
|
||||
cond_scale =controlnet_conditioning_scale
|
||||
down_block_res_samples, mid_block_res_sample = self.controlnet(
|
||||
image_latent.detach(),
|
||||
self.t_start,
|
||||
encoder_hidden_states=self.prompt_embeds,
|
||||
conditioning_scale=cond_scale,
|
||||
guess_mode=False,
|
||||
return_dict=False,
|
||||
)
|
||||
|
||||
# 7. YOSO sampling
|
||||
latent_x_t = self.unet(
|
||||
pred_latent,
|
||||
self.t_start,
|
||||
encoder_hidden_states=self.prompt_embeds,
|
||||
down_block_additional_residuals=down_block_res_samples,
|
||||
mid_block_additional_residual=mid_block_res_sample,
|
||||
return_dict=False,
|
||||
)[0]
|
||||
|
||||
|
||||
del (
|
||||
pred_latent,
|
||||
image_latent,
|
||||
)
|
||||
|
||||
# decoder
|
||||
prediction = self.decode_prediction(latent_x_t)
|
||||
prediction = self.image_processor.unpad_image(prediction, padding) # [N*E,3,PH,PW]
|
||||
|
||||
prediction = self.image_processor.resize_antialias(
|
||||
prediction, original_resolution, resample_method_output, is_aa=False
|
||||
) # [N,3,H,W]
|
||||
|
||||
if output_type == "np":
|
||||
prediction = self.image_processor.pt_to_numpy(prediction) # [N,H,W,3]
|
||||
|
||||
# 11. Offload all models
|
||||
self.maybe_free_model_hooks()
|
||||
|
||||
return YosoDelightOutput(
|
||||
prediction=prediction,
|
||||
latent=latent_x_t,
|
||||
gaus_noise=gaus_noise,
|
||||
)
|
||||
|
||||
# Copied from diffusers.pipelines.marigold.pipeline_marigold_depth.MarigoldDepthPipeline.prepare_latents
|
||||
def prepare_latents(
|
||||
self,
|
||||
image: torch.Tensor,
|
||||
latents: Optional[torch.Tensor],
|
||||
generator: Optional[torch.Generator],
|
||||
ensemble_size: int,
|
||||
batch_size: int,
|
||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
准备潜变量。
|
||||
|
||||
该函数旨在对给定的图像进行编码,生成相应的潜变量,并根据需要重复这些潜变量以形成一个集合。
|
||||
同时,它也负责生成对应的预测潜变量。
|
||||
|
||||
参数:
|
||||
- image: 输入图像张量。
|
||||
- latents: 可选的潜变量张量,如果提供,则直接使用,否则将生成一个新的潜变量张量。
|
||||
- generator: 可选的随机数生成器,用于生成潜变量。
|
||||
- ensemble_size: 集合大小,表示潜变量的重复次数。
|
||||
- batch_size: 批处理大小,用于分批处理输入图像。
|
||||
|
||||
返回:
|
||||
- image_latent: 编码后的图像潜变量。
|
||||
- pred_latent: 预测潜变量。
|
||||
"""
|
||||
|
||||
def retrieve_latents(encoder_output):
|
||||
"""
|
||||
提取潜变量。
|
||||
|
||||
该内部函数用于从编码器的输出中提取潜变量。如果输出包含latent_dist属性,则调用其mode方法;
|
||||
如果输出包含latents属性,则直接返回该属性。如果两者都不包含,则抛出异常。
|
||||
|
||||
参数:
|
||||
- encoder_output: 编码器的输出。
|
||||
|
||||
返回:
|
||||
- 提取到的潜变量。
|
||||
"""
|
||||
if hasattr(encoder_output, "latent_dist"):
|
||||
return encoder_output.latent_dist.mode()
|
||||
elif hasattr(encoder_output, "latents"):
|
||||
return encoder_output.latents
|
||||
else:
|
||||
raise AttributeError("Could not access latents of provided encoder_output")
|
||||
|
||||
# 对输入图像进行分批处理并编码,然后将编码后的潜变量进行拼接
|
||||
image_latent = torch.cat(
|
||||
[
|
||||
retrieve_latents(self.vae.encode(image[i : i + batch_size]))
|
||||
for i in range(0, image.shape[0], batch_size)
|
||||
],
|
||||
dim=0,
|
||||
) # [N,4,h,w]
|
||||
|
||||
# 对编码后的潜变量进行缩放
|
||||
image_latent = image_latent * self.vae.config.scaling_factor
|
||||
|
||||
# 根据集合大小重复潜变量
|
||||
image_latent = image_latent.repeat_interleave(ensemble_size, dim=0) # [N*E,4,h,w]
|
||||
|
||||
# 初始化预测潜变量
|
||||
pred_latent = torch.zeros_like(image_latent)
|
||||
|
||||
# 如果预测潜变量未提供,则生成一个新的随机潜变量
|
||||
if pred_latent is None:
|
||||
pred_latent = randn_tensor(
|
||||
image_latent.shape,
|
||||
generator=generator,
|
||||
device=image_latent.device,
|
||||
dtype=image_latent.dtype,
|
||||
) # [N*E,4,h,w]
|
||||
|
||||
# 返回编码后的图像潜变量和预测潜变量
|
||||
return image_latent, pred_latent
|
||||
|
||||
def decode_prediction(self, pred_latent: torch.Tensor) -> torch.Tensor:
|
||||
if pred_latent.dim() != 4 or pred_latent.shape[1] != self.vae.config.latent_channels:
|
||||
raise ValueError(
|
||||
f"Expecting 4D tensor of shape [B,{self.vae.config.latent_channels},H,W]; got {pred_latent.shape}."
|
||||
)
|
||||
|
||||
prediction = self.vae.decode(pred_latent / self.vae.config.scaling_factor, return_dict=False)[0] # [B,3,H,W]
|
||||
|
||||
return prediction # [B,3,H,W]
|
||||
|
||||
@staticmethod
|
||||
def normalize_normals(normals: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
|
||||
if normals.dim() != 4 or normals.shape[1] != 3:
|
||||
raise ValueError(f"Expecting 4D tensor of shape [B,3,H,W]; got {normals.shape}.")
|
||||
|
||||
norm = torch.norm(normals, dim=1, keepdim=True)
|
||||
normals /= norm.clamp(min=eps)
|
||||
|
||||
return normals
|
||||
|
||||
@staticmethod
|
||||
def ensemble_normals(
|
||||
normals: torch.Tensor, output_uncertainty: bool, reduction: str = "closest"
|
||||
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
|
||||
"""
|
||||
Ensembles the normals maps represented by the `normals` tensor with expected shape `(B, 3, H, W)`, where B is
|
||||
the number of ensemble members for a given prediction of size `(H x W)`.
|
||||
|
||||
Args:
|
||||
normals (`torch.Tensor`):
|
||||
Input ensemble normals maps.
|
||||
output_uncertainty (`bool`, *optional*, defaults to `False`):
|
||||
Whether to output uncertainty map.
|
||||
reduction (`str`, *optional*, defaults to `"closest"`):
|
||||
Reduction method used to ensemble aligned predictions. The accepted values are: `"closest"` and
|
||||
`"mean"`.
|
||||
|
||||
Returns:
|
||||
A tensor of aligned and ensembled normals maps with shape `(1, 3, H, W)` and optionally a tensor of
|
||||
uncertainties of shape `(1, 1, H, W)`.
|
||||
"""
|
||||
if normals.dim() != 4 or normals.shape[1] != 3:
|
||||
raise ValueError(f"Expecting 4D tensor of shape [B,3,H,W]; got {normals.shape}.")
|
||||
if reduction not in ("closest", "mean"):
|
||||
raise ValueError(f"Unrecognized reduction method: {reduction}.")
|
||||
|
||||
mean_normals = normals.mean(dim=0, keepdim=True) # [1,3,H,W]
|
||||
mean_normals = MarigoldNormalsPipeline.normalize_normals(mean_normals) # [1,3,H,W]
|
||||
|
||||
sim_cos = (mean_normals * normals).sum(dim=1, keepdim=True) # [E,1,H,W]
|
||||
sim_cos = sim_cos.clamp(-1, 1) # required to avoid NaN in uncertainty with fp16
|
||||
|
||||
uncertainty = None
|
||||
if output_uncertainty:
|
||||
uncertainty = sim_cos.arccos() # [E,1,H,W]
|
||||
uncertainty = uncertainty.mean(dim=0, keepdim=True) / np.pi # [1,1,H,W]
|
||||
|
||||
if reduction == "mean":
|
||||
return mean_normals, uncertainty # [1,3,H,W], [1,1,H,W]
|
||||
|
||||
closest_indices = sim_cos.argmax(dim=0, keepdim=True) # [1,1,H,W]
|
||||
closest_indices = closest_indices.repeat(1, 3, 1, 1) # [1,3,H,W]
|
||||
closest_normals = torch.gather(normals, 0, closest_indices) # [1,3,H,W]
|
||||
|
||||
return closest_normals, uncertainty # [1,3,H,W], [1,1,H,W]
|
||||
|
||||
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
||||
def retrieve_timesteps(
|
||||
scheduler,
|
||||
num_inference_steps: Optional[int] = None,
|
||||
device: Optional[Union[str, torch.device]] = None,
|
||||
timesteps: Optional[List[int]] = None,
|
||||
sigmas: Optional[List[float]] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
||||
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
||||
|
||||
Args:
|
||||
scheduler (`SchedulerMixin`):
|
||||
The scheduler to get timesteps from.
|
||||
num_inference_steps (`int`):
|
||||
The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
|
||||
must be `None`.
|
||||
device (`str` or `torch.device`, *optional*):
|
||||
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
||||
timesteps (`List[int]`, *optional*):
|
||||
Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
|
||||
`num_inference_steps` and `sigmas` must be `None`.
|
||||
sigmas (`List[float]`, *optional*):
|
||||
Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
|
||||
`num_inference_steps` and `timesteps` must be `None`.
|
||||
|
||||
Returns:
|
||||
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
||||
second element is the number of inference steps.
|
||||
"""
|
||||
if timesteps is not None and sigmas is not None:
|
||||
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
||||
if timesteps is not None:
|
||||
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
||||
if not accepts_timesteps:
|
||||
raise ValueError(
|
||||
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
||||
f" timestep schedules. Please check whether you are using the correct scheduler."
|
||||
)
|
||||
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
||||
timesteps = scheduler.timesteps
|
||||
num_inference_steps = len(timesteps)
|
||||
elif sigmas is not None:
|
||||
accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
||||
if not accept_sigmas:
|
||||
raise ValueError(
|
||||
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
||||
f" sigmas schedules. Please check whether you are using the correct scheduler."
|
||||
)
|
||||
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
||||
timesteps = scheduler.timesteps
|
||||
num_inference_steps = len(timesteps)
|
||||
else:
|
||||
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
||||
timesteps = scheduler.timesteps
|
||||
return timesteps, num_inference_steps
|
||||
@@ -0,0 +1,15 @@
|
||||
[project]
|
||||
name = "comfyui_stabledelight_ll"
|
||||
description = "StableDelight: Revealing Hidden Textures by Removing Specular Reflections(移除纹理表面镜面反射)"
|
||||
version = "1.0.0"
|
||||
license = {file = "LICENSE"}
|
||||
dependencies = ["numpy"]
|
||||
|
||||
[project.urls]
|
||||
Repository = "https://github.com/lldacing/ComfyUI_StableDelight_ll"
|
||||
# Used by Comfy Registry https://comfyregistry.org
|
||||
|
||||
[tool.comfy]
|
||||
PublisherId = "lldacing"
|
||||
DisplayName = "ComfyUI_StableDelight_ll"
|
||||
Icon = ""
|
||||
Reference in New Issue
Block a user