Error: ImportError: 无法从’torchvision.models.vgg’导入’model_urls’。

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

Error: ImportError: cannot import name 'model_urls' from 'torchvision.models.vgg'

问题

我想使用CRAFT文本检测器模块。我安装了所有必需的包,但当我尝试运行示例代码时,我遇到了一个错误:

ImportError: 无法从'torchvision.models.vgg'中导入名称'model_urls'

我查找了解决方案,但没有一个有效。

有任何建议吗?

英文:

I want to use the CRAFT text detector module. I installed all the required packages, but when I try to run the example code, I get

    ImportError: cannot import name 'model_urls' from 'torchvision.models.vgg'

I looked for solutions, but none of them worked.

Any suggestions ?

答案1

得分: 1

model_urls 显然被移除了,因为它在整个代码库中不一致(参见GitHub 上的此评论)。

作者建议通过删除对不存在模块的导入并获取模型 URL 信息的方式来更新您的代码,例如这样:

import pkgutil
import torchvision
from importlib import import_module

def get_torchvision_models():
    model_urls = dict()
    for _, name, ispkg in pkgutil.walk_packages(torchvision.models.__path__):
        if ispkg:
            continue
        _zoo = import_module(f'torchvision.models.{name}')
        if hasattr(_zoo, 'model_urls'):
            _urls = getattr(_zoo, 'model_urls')
            model_urls.update(_urls)
    return model_urls

(来自 https://github.com/open-mmlab/mmcv/blob/fb486b96fd9932637a16f23a5dc60904c12bea7d/mmcv/runner/checkpoint.py#L108-L117)

英文:

Apparently model_urls was removed because it was inconsistent across the code base
(see this comment on Github).

The authors recommend to update your code by removing the import for a non-existing module and getting model URL information for instance like this:

import pkgutil
import torchvision
from importlib import import_module

def get_torchvision_models():
    model_urls = dict()
    for _, name, ispkg in pkgutil.walk_packages(torchvision.models.__path__):
        if ispkg:
            continue
        _zoo = import_module(f'torchvision.models.{name}')
        if hasattr(_zoo, 'model_urls'):
            _urls = getattr(_zoo, 'model_urls')
            model_urls.update(_urls)
    return model_urls

(from https://github.com/open-mmlab/mmcv/blob/fb486b96fd9932637a16f23a5dc60904c12bea7d/mmcv/runner/checkpoint.py#L108-L117)

huangapple
  • 本文由 发表于 2023年5月17日 22:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273170.html
匿名

发表评论

匿名网友

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

确定