有可能加载没有config.json文件的huggingface模型吗?

huangapple go评论66阅读模式
英文:

Is it possible to load huggingface model which does not have config.json file?

问题

我试图使用以下代码从HF加载[这个](https://huggingface.co/Carve/u2net-universal)语义分割模型:

from transformers import pipeline

model = pipeline("image-segmentation", model="Carve/u2net-universal", device="cpu")

但是我收到了以下错误:

OSError: tamnvcc/isnet-general-use 似乎没有名为config.json的文件。请查看'https://huggingface.co/tamnvcc/isnet-general-use/main'获取可用文件。

在没有提供config.json文件的情况下,是否可以从HuggingFace加载模型呢?

我还尝试通过以下方式加载模型:

id2label = {0: "background", 1: "target"}
label2id = {"background": 0, "target": 1}
image_processor = AutoImageProcessor.from_pretrained("Carve/u2net-universal")
model = AutoModelForSemanticSegmentation("Carve/u2net-universal", id2label=id2label, label2id=label2id)

但是得到了相同的错误。
英文:

I am trying to load this semantic segmentation model from HF using the following code:

from transformers import pipeline

model = pipeline("image-segmentation", model="Carve/u2net-universal", device="cpu")

But I get the following error:

OSError: tamnvcc/isnet-general-use does not appear to have a file named config.json. Checkout 'https://huggingface.co/tamnvcc/isnet-general-use/main' for available files.

Is it even possible to load models from HuggingFace without config.json file provided?

I also tried loading the model via:

id2label = {0: "background", 1: "target"}
label2id = {"background": 0, "target": 1}
image_processor = AutoImageProcessor.from_pretrained("Carve/u2net-universal")
model = AutoModelForSemanticSegmentation("Carve/u2net-universal", id2label=id2label, label2id=label2id)

But got the same error.

答案1

得分: 1

TL;DR

如果没有 config.json,并且模型卡片没有任何文档,您将需要做很多假设

在经过一些猜测之后,可能是这样的:

from u2net import U2NET

model = U2NET()

model.load_state_dict(torch.load('full_weights.pth', map_location=torch.device('cpu')))

In Long

查看模型卡片中提供的文件,我们看到这些文件:

  • .gitattributes
  • README.md
  • full_weights.pth

一个很好的猜测是 .pth 文件是一个PyTorch模型二进制文件。鉴于这一点,我们可以尝试:

import shutil
import requests

import torch


# 从远程下载 .pth 文件到本地
url = "https://huggingface.co/Carve/u2net-universal/resolve/main/full_weights.pth"
response = requests.get(url, stream=True)
with open('full_weights.pth', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

model = torch.load('full_weights.pth', map_location=torch.device('cpu'))

但最终得到的不是一个可用的模型,只是模型参数/权重(即检查点文件),即

type(model)

[out]:

collections.OrderedDict

查看层的名称,它看起来像一个 rebnconvin 模型,指向 https://github.com/xuebinqin/U-2-Net 代码:

model.keys()

[out]:

odict_keys(['stage1.rebnconvin.conv_s1.weight', 'stage1.rebnconvin.conv_s1.bias', 'stage1.rebnconvin.bn_s1.weight', 'stage1.rebnconvin.bn_s1.bias', 'stage1.rebnconvin.bn_s1.running_mean', 'stage1.rebnconvin.bn_s1.running_var', 'stage1.rebnconv1.conv_s1.weight', 'stage1.rebnconv1.conv_s1.bias', 'stage1.rebnconv1.bn_s1.weight', 'stage1.rebnconv1.bn_s1.bias', 'stage1.rebnconv1.bn_s1.running_mean', 'stage1.rebnconv1.bn_s1.running_var', ...])

假设您可以信任来自 GitHub 的代码,您可以尝试安装它:

! wget https://raw.githubusercontent.com/xuebinqin/U-2-Net/master/model/u2net.py

并根据层的名称和模型名称的猜测,它看起来像是来自 https://arxiv.org/abs/2005.09007v3 的 U2Net 模型。

因此,您可以尝试:

from u2net import U2NET

model = U2NET()

model.load_state_dict(torch.load('full_weights.pth', map_location=torch.device('cpu')))
英文:

TL;DR

You will need to make a lot of assumption if you don't have the config.json and the model card doesn't have any documentation

After some guessing, possibly it's this:

from u2net import U2NET

model = U2NET()

model.load_state_dict(torch.load('full_weights.pth', map_location=torch.device('cpu')))

In Long

Looking at the files available in the model card, we see these files:

  • .gitattributes
  • README.md
  • full_weights.pth

A good guess would be that the .pth file is a PyTorch model binary. Given that, we can try:

import shutil
import requests

import torch


# Download the .pth file locally
url = "https://huggingface.co/Carve/u2net-universal/resolve/main/full_weights.pth"
response = requests.get(url, stream=True)
with open('full_weights.pth', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

model = torch.load('full_weights.pth', map_location=torch.device('cpu'))

But what you end up with is NOT a usable model, it's just the model parameters/weights (aka checkpoint file), i.e.

type(model)

[out]:

collections.OrderedDict

Looking at the layer names, it looks like a rebnconvin model that points to the https://github.com/xuebinqin/U-2-Net code:

model.keys()

[out]:

odict_keys(['stage1.rebnconvin.conv_s1.weight', 'stage1.rebnconvin.conv_s1.bias', 'stage1.rebnconvin.bn_s1.weight', 'stage1.rebnconvin.bn_s1.bias', 'stage1.rebnconvin.bn_s1.running_mean', 'stage1.rebnconvin.bn_s1.running_var', 'stage1.rebnconv1.conv_s1.weight', 'stage1.rebnconv1.conv_s1.bias', 'stage1.rebnconv1.bn_s1.weight', 'stage1.rebnconv1.bn_s1.bias', 'stage1.rebnconv1.bn_s1.running_mean', 'stage1.rebnconv1.bn_s1.running_var', ...])

ASSUMING THAT YOU CAN TRUST THE CODE from the github, you can try installing it with:

! wget https://raw.githubusercontent.com/xuebinqin/U-2-Net/master/model/u2net.py

And guessing from the layer names and model name, it looks like a U2Net from https://arxiv.org/abs/2005.09007v3

So you can try:

from u2net import U2NET

model = U2NET()

model.load_state_dict(torch.load('full_weights.pth', map_location=torch.device('cpu')))

答案2

得分: 0

在我的经验中,当model=下的模型属性路径错误时,会导致提到的错误:
OSError[...]似乎没有配置文件config.json的文件名

确保指向模型文件夹(因此config.json)的指针是正确的。

实际上,OSError本身已经表明可能与路径相关的某些问题。

英文:

Im my experience, when the model attribute under model= has erroneous path, it yields the mentioned error:
OSError [...] does not appear to have the file names config.json

Make sure the pointer to the model folder (hence config.json) is correct.

Actually, the OSError itself already communicates there is probably something off path related.

huangapple
  • 本文由 发表于 2023年3月3日 20:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定