ZhengPeng7 / BiRefNet

[CAAI AIR'24] Bilateral Reference for High-Resolution Dichotomous Image Segmentation
https://www.birefnet.top
MIT License
1.27k stars 99 forks source link

AttributeError: type object 'BiRefNet' has no attribute 'from_pretrained'. After updating the repository, the inference script on colab cannot be used. #38

Closed YUANMU227 closed 3 months ago

YUANMU227 commented 3 months ago

I saw that you updated the repository code three days ago, and it seems that the nn.Module and PyTorchModelHubMixin modules were deleted. However, when using the single-graph inference script of colab, the corresponding function cannot be called. The error is as follows: https://colab.research.google.com/drive/14Dqg7oeBkFEtchaHLNpig2BcdkZEogba?usp=drive_link image

报错: Traceback (most recent call last): model = BiRefNet.from_pretrained('zhengpeng7/birefnet') AttributeError: type object 'BiRefNet' has no attribute 'from_pretrained'

YUANMU227 commented 3 months ago

For the new version of the code, can you provide an official single-graph inference script? If so, I would be very grateful.

ZhengPeng7 commented 3 months ago

Hi, @YUANMU227 , you can try it again, which surely works well now!

I was trying to do some upgrades on the model repo on HF -- to embed codes there so that people can use model = AutoModelForImageSegmentation.from_pretrained("zhengpeng7/birefnet",trust_remote_code=True) to load all elements needed for inference without downloading codes from Github. But things are not easy there. So, I rolled the codes there back to the correct version with some updated docs. This feature may come in the future.

YUANMU227 commented 3 months ago

Hello, thank you for your reply, but this problem is still not solved. I cloned BiRefNet.git from colab, and the code is correct (from_pretrained can be used normally). But when I run the script locally (the code cloned to the local has PyTorchModelHubMixin), an error is reported: Traceback (most recent call last): File "/home/BiRefNet/inference_single_image.py", line 7, in from models.birefnet import BiRefNet File "/home/BiRefNet/models/birefnet.py", line 18, in class BiRefNet( TypeError: BiRefNet.__init_subclass__() takes no keyword arguments

ZhengPeng7 commented 3 months ago

You mean that you want to load and run BiRefNet locally, but the from_pretrained method can not be recognized?

ZhengPeng7 commented 3 months ago

截屏2024-07-16 17 32 37 I tried it again, and everything was okay as above. Can you show me your codes?

YUANMU227 commented 3 months ago

Hello, thank you for your reply. There are still some problems when running. Can you add WeChat and explain it in detail?

YUANMU227 commented 3 months ago

报错如下: In [1]: from models.birefnet import BiRefNet

TypeError Traceback (most recent call last) Cell In[1], line 1 ----> 1 from models.birefnet import BiRefNet

File ~/BiRefNet/models/birefnet.py:18 14 from models.refinement.refiner import Refiner, RefinerPVTInChannels4, RefUNet 15 from models.refinement.stem_layer import StemLayer ---> 18 class BiRefNet( 19 nn.Module, 20 PyTorchModelHubMixin, 21 library_name="birefnet", 22 repo_url="https://github.com/ZhengPeng7/BiRefNet", 23 tags=['Image Segmentation', 'Background Removal', 'Mask Generation', 'Dichotomous Image Segmentation', 'Camouflaged Object Detection', 'Salient Object Detection'] 24 ): 25 def init(self, bb_pretrained=True): 26 super(BiRefNet, self).init()

TypeError: BiRefNet.__init_subclass__() takes no keyword arguments

ZhengPeng7 commented 3 months ago

没事, 在这里就行. 当然可以用中文交流, 请随意哈.

你的环境是按照我给的安装的吗, 有可能是环境问题: ref-1.

再其次, 如果急用, 可以先把huggingface相关的那几行删掉 然后原生python本地load权重使用, 直接去掉是不影响使用的.

YUANMU227 commented 3 months ago

感谢,我做了两处修改,之后可以成功运行。 image image 载入模型这几行我是根据inference.py进行修改的,麻烦帮我看看有没有问题。 image bb_pretrained=False请问该设置的作用是什么

ZhengPeng7 commented 3 months ago

代码 我看上去是没问题的, 当然得看你运行结果, 毕竟有其他因素影响. bb_pretrained=False是因为, 这里是inference, 直接读取了整体weights, 那自然不需要先去加载backbone的权重, 而且如果加载的话你需要把对应的backbone权重文件放在本地, 那就非常麻烦. 所以为了inference时的简洁, 我添加了bb_pretrained=False这个参数.

YUANMU227 commented 3 months ago

感谢大佬,明白啦

aiot-tech commented 2 months ago

Update the huggingface-hub version is all you need

ZhengPeng7 commented 2 months ago

Update the huggingface-hub version is all you need

Yeah, I also met that problem by myself weeks ago. It's caused by the missing of 'huggingface-hub'.