Diffusion模型遇到问题:缺少某个库,不能自动下载。

OSError: Can't load tokenizer for 'xxxxxxxxx'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'xxxxxxxxx' is the correct path to a directory containing all relevant files for a CLIPTokenizer tokenizer.

例如

  File "/home/spai/code/SD/stable-diffusion/ldm/util.py", line 85, in instantiate_from_config
    return get_obj_from_str(config["target"])(**config.get("params", dict()))
  File "/home/spai/code/SD/stable-diffusion/ldm/modules/encoders/modules.py", line 141, in __init__
    self.tokenizer = CLIPTokenizer.from_pretrained(version)
  File "/home/spai/anaconda3/envs/metap/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 1838, in from_pretrained
    raise EnvironmentError(
OSError: Can't load tokenizer for 'openai/clip-vit-large-patch14'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'openai/clip-vit-large-patch14' is the correct path to a directory containing all relevant files for a CLIPTokenizer tokenizer.

原因

提示显示无法加载该模型的分词器(tokenizer),可能出现了以下情况之一:

  1. 本地目录与 ‘openai/clip-vit-large-patch14’ 同名:如果你的本地目录中有一个名为 ‘openai/clip-vit-large-patch14’ 的文件夹,可能会导致加载错误。请检查你的工作目录或者其他相关目录中是否有同名文件夹,并确保没有命名冲突。

  2. 模型路径错误:确保 ‘openai/clip-vit-large-patch14’ 是正确的模型路径,可以尝试重新确认模型路径是否正确。

  3. 自动下载没找到,需要手动下载,放进去。可能是防火墙、urlib、ssl、服务器等问题

解决

手动下载模型

以上面的clip缺失为例:
在huggingface.co中搜索,找到相关库
从https://huggingface.co/openai/clip-vit-large-patch14/tree/main下载所有文件
在这里插入图片描述

 mkdir -p stable-diffusion-webui/openai/clip-vit-large-patch14

并将其移动到 本地目录/openai//clip-vit-large-patch14/

下载失败的话,看我的其他文章,用到 LFS 或者迅雷单独下载

修正网络

    import ssl
    ssl._create_default_https_context=ssl._create_unverified_context

为什么可以解决?

    这段代码的功能和作用是临时禁用 Python 中的 SSL 证书验证,通常被用来解决在使用 urllib 或其他网络请求库时,可能因为证书验证问题而导致的连接问题。这段代码可以暂时绕过 SSL 证书验证,但同时也可能引入一些安全风险。

    在代码的上下文中,出现错误的代码段涉及到 "openai/clip-vit-large-patch14" 模型的加载问题,而上面提供的这段代码被用来禁用了 SSL

证书验证。这可能是因为在加载模型的过程中,可能涉及到从远程服务器下载模型权重文件,而默认情况下,Python 的 SSL
证书验证可能会阻止未验证的连接。禁用 SSL 证书验证会允许下载,但也可能存在安全风险,因为没有验证服务器的真实性。

    虽然在某些情况下禁用 SSL 证书验证可能会解决连接问题,但这并不是一个推荐的做法,因为这会绕过了安全层。更好的做法是在保证安全的前提下,使用受信任的证书,或者找到其他合适的解决方法来处理连接问题,而不是直接禁用证书验证。

    如果你确实需要继续禁用 SSL 证书验证,请确保你了解这样做可能带来的安全风险,并且只在临时和受控的环境中使用。在生产环境中,绕过 SSL 证书验证是不推荐的做法。

参考 https://blog.csdn.net/BetrayFree/article/details/132482843

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐