From e7841deca11a3e95d35bf82c8cf2cee015b6e5ac Mon Sep 17 00:00:00 2001 From: Cyber Dick Lang <286878701@qq.com> Date: Fri, 12 Sep 2025 02:25:25 +0800 Subject: [PATCH] =?UTF-8?q?v1.2.3:=20=E6=B7=BB=E5=8A=A0BiRefNet=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E9=85=8D=E7=BD=AE=E4=BF=AE=E5=A4=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增动态配置修复功能,自动解决BiRefNet模型与transformers库的兼容性问题 - 用户无需手动修改models文件夹中的任何文件 - 自动检测并修复所有BiRefNet模型版本的配置问题 - 完全向后兼容,不影响原始模型文件 - 添加详细的修复说明文档 --- BIREFNET_FIX_README.md | 74 ++++++++++++++++++++++++++++++++++++++++++ README.md | 9 ++++- __init__.py | 2 +- nodes.py | 53 ++++++++++++++++++++++++++++++ pyproject.toml | 4 +-- 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 BIREFNET_FIX_README.md diff --git a/BIREFNET_FIX_README.md b/BIREFNET_FIX_README.md new file mode 100644 index 0000000..36b726e --- /dev/null +++ b/BIREFNET_FIX_README.md @@ -0,0 +1,74 @@ +# BiRefNet 配置修复说明 + +## 问题描述 + +在使用BiRefNet模型时,可能会遇到以下错误: +``` +'Config' object has no attribute 'is_encoder_decoder' +``` + +这是因为transformers库期望配置对象具有`is_encoder_decoder`属性,但BiRefNet的配置类中缺少这个属性。 + +## 解决方案 + +本插件已经内置了动态修复方案,无需手动修改models文件夹中的文件。 + +### 自动修复机制 + +插件会在加载BiRefNet模型时自动应用以下修复: + +1. **动态检测配置类**:自动检测BiRefNet模型目录中的配置类 +2. **运行时补丁**:在模型加载前动态添加缺失的`is_encoder_decoder`属性 +3. **兼容性保证**:确保与transformers库的兼容性 + +### 修复的配置类 + +- `BiRefNetConfig` (在BiRefNet_config.py中) +- `Config` (在birefnet.py中) + +### 支持的模型版本 + +- BiRefNet-General +- BiRefNet_HR +- BiRefNet_HR-matting +- BiRefNet_dynamic +- RMBG-2.0 + +## 使用方法 + +无需任何额外操作,插件会自动处理配置修复。当您使用BiRefNet节点时: + +1. 插件会自动检测模型配置 +2. 应用必要的修复补丁 +3. 正常加载模型 + +## 技术细节 + +修复通过以下方式实现: + +```python +def apply_birefnet_config_patch(self, model_path): + """动态修复BiRefNet配置类缺少is_encoder_decoder属性的问题""" + # 动态导入配置模块 + # 检查并修复配置类 + # 添加缺失的属性 +``` + +这种方法的优势: +- ✅ 无需修改models文件夹 +- ✅ 自动适配不同版本的BiRefNet模型 +- ✅ 不影响原始模型文件 +- ✅ 向后兼容 + +## 故障排除 + +如果仍然遇到问题,请检查: + +1. 模型是否正确下载到models/BiRefNet/目录 +2. 模型文件是否完整 +3. 是否有足够的磁盘空间和内存 + +## 更新日志 + +- v1.0: 添加动态配置修复功能 +- 自动处理BiRefNet模型的transformers兼容性问题 diff --git a/README.md b/README.md index 8bcf57d..d5b386d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,17 @@ # ComfyUI-RemoveBackgroundSuite -**版本**: v1.2.1 +**版本**: v1.2.3 **ComfyUI 插件**: 抠图工具包 A matting toolkit based on ComfyUI, supporting multiple matting models and detail processing methods. +## 🆕 最新更新 (v1.2.3) + +- **自动修复BiRefNet兼容性问题**: 新增动态配置修复功能,自动解决BiRefNet模型与transformers库的兼容性问题 +- **无需手动修改**: 用户安装插件后无需修改models文件夹中的任何文件 +- **智能检测**: 自动检测并修复所有BiRefNet模型版本的配置问题 +- **向后兼容**: 完全兼容现有工作流程,不影响原始模型文件 + ## Features - **Multiple Models**: Support for various matting models including BiRefNet, RMBG, and more diff --git a/__init__.py b/__init__.py index 509916d..de6025a 100644 --- a/__init__.py +++ b/__init__.py @@ -2,4 +2,4 @@ from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS __all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] -__version__ = 'v1.2.1' \ No newline at end of file +__version__ = 'v1.2.3' \ No newline at end of file diff --git a/nodes.py b/nodes.py index 93ec52a..7740141 100644 --- a/nodes.py +++ b/nodes.py @@ -191,6 +191,56 @@ class BiRefNetUltra_RBS: FUNCTION = "birefnet_ultra" CATEGORY = 'RemoveBackgroundSuite' + def apply_birefnet_config_patch(self, model_path): + """动态修复BiRefNet配置类缺少is_encoder_decoder属性的问题""" + try: + # 动态导入并修复BiRefNetConfig类 + import sys + import importlib.util + + # 查找BiRefNet_config.py文件 + config_file = os.path.join(model_path, "BiRefNet_config.py") + if os.path.exists(config_file): + spec = importlib.util.spec_from_file_location("BiRefNet_config", config_file) + config_module = importlib.util.module_from_spec(spec) + sys.modules["BiRefNet_config"] = config_module + spec.loader.exec_module(config_module) + + # 检查并修复BiRefNetConfig类 + if hasattr(config_module, 'BiRefNetConfig'): + original_init = config_module.BiRefNetConfig.__init__ + + def patched_init(self, *args, **kwargs): + original_init(self, *args, **kwargs) + if not hasattr(self, 'is_encoder_decoder'): + self.is_encoder_decoder = False + + config_module.BiRefNetConfig.__init__ = patched_init + log(f"Applied BiRefNetConfig patch for {model_path}", message_type='info') + + # 查找birefnet.py文件中的Config类 + birefnet_file = os.path.join(model_path, "birefnet.py") + if os.path.exists(birefnet_file): + spec = importlib.util.spec_from_file_location("birefnet", birefnet_file) + birefnet_module = importlib.util.module_from_spec(spec) + sys.modules["birefnet"] = birefnet_module + spec.loader.exec_module(birefnet_module) + + # 检查并修复Config类 + if hasattr(birefnet_module, 'Config'): + original_init = birefnet_module.Config.__init__ + + def patched_init(self, *args, **kwargs): + original_init(self, *args, **kwargs) + if not hasattr(self, 'is_encoder_decoder'): + self.is_encoder_decoder = False + + birefnet_module.Config.__init__ = patched_init + log(f"Applied Config patch for {model_path}", message_type='info') + + except Exception as e: + log(f"Warning: Could not apply config patch for {model_path}: {str(e)}", message_type='warning') + def load_birefnet_model(self, version): model_path = os.path.join(folder_paths.models_dir, "BiRefNet", version) os.makedirs(model_path, exist_ok=True) @@ -216,6 +266,9 @@ class BiRefNetUltra_RBS: # 使用本地路径加载模型 if os.path.exists(model_path): try: + # 应用配置补丁 + self.apply_birefnet_config_patch(model_path) + from transformers import AutoModelForImageSegmentation model = AutoModelForImageSegmentation.from_pretrained(model_path, local_files_only=True, trust_remote_code=True) return model diff --git a/pyproject.toml b/pyproject.toml index 42d1cde..bb5b132 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "removebackgroundsuite" description = "A matting toolkit based on ComfyUI, supporting multiple matting models and detail processing methods." -version = "1.2.2" +version = "1.2.3" license = {file = "LICENSE"} dependencies = ["torch", "numpy", "Pillow", "torchvision", "opencv-python", "scipy", "transformers", "tqdm", "pymatting", "guided-filter", "vitmatte", "timm"] @@ -13,7 +13,7 @@ Repository = "https://github.com/whmc76/ComfyUI-RemoveBackgroundSuite" PublisherId = "whmc76" DisplayName = "ComfyUI-RemoveBackgroundSuite" Description = "A comprehensive matting toolkit for ComfyUI with support for multiple models including BiRefNet, RMBG, and advanced mask processing capabilities." -Version = "1.2.2" +Version = "1.2.3" Icon = "" includes = [] tags = ["matting", "background-removal", "image-processing", "mask-processing"]