v1.2.3: 添加BiRefNet动态配置修复功能

- 新增动态配置修复功能,自动解决BiRefNet模型与transformers库的兼容性问题
- 用户无需手动修改models文件夹中的任何文件
- 自动检测并修复所有BiRefNet模型版本的配置问题
- 完全向后兼容,不影响原始模型文件
- 添加详细的修复说明文档
This commit is contained in:
Cyber Dick Lang
2025-09-12 02:25:25 +08:00
parent 474698b6c5
commit e7841deca1
5 changed files with 138 additions and 4 deletions
+74
View File
@@ -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兼容性问题
+8 -1
View File
@@ -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
+1 -1
View File
@@ -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'
__version__ = 'v1.2.3'
+53
View File
@@ -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
+2 -2
View File
@@ -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"]