导入错误:无法从“torchvision.models.vgg”导入名称“model_urls”

问题描述 投票:0回答:2

我之前导入过easyocr。昨天我创建了一个虚拟环境来在 Jupyter 终端上运行程序。从今天开始,每次我尝试导入 easyocr 时都会遇到此错误。任何帮助将不胜感激。

ImportError                               Traceback (most recent call last)
<ipython-input-12-4f7ed86c5a6a> in <module>
      2 import os
      3 from IPython.display import Image
----> 4 import easyocr

~\AppData\Local\Continuum\anaconda3\lib\site-packages\easyocr\__init__.py in <module>
----> 1 from .easyocr import Reader
      2 
      3 __version__ = '1.3.1'

~\AppData\Local\Continuum\anaconda3\lib\site-packages\easyocr\easyocr.py in <module>
      1 # -*- coding: utf-8 -*-
      2 
----> 3 from .detection import get_detector, get_textbox
      4 from .recognition import get_recognizer, get_text
      5 from .utils import group_text_box, get_image_list, calculate_md5, get_paragraph,\

~\AppData\Local\Continuum\anaconda3\lib\site-packages\easyocr\detection.py in <module>
      9 from .craft_utils import getDetBoxes, adjustResultCoordinates
     10 from .imgproc import resize_aspect_ratio, normalizeMeanVariance
---> 11 from .craft import CRAFT
     12 
     13 def copyStateDict(state_dict):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\easyocr\craft.py in <module>
      9 import torch.nn.functional as F
     10 
---> 11 from .model.modules import vgg16_bn, init_weights
     12 
     13 class double_conv(nn.Module):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\easyocr\model\modules.py in <module>
      4 import torch.nn.init as init
      5 from torchvision import models
----> 6 from torchvision.models.vgg import model_urls
      7 from collections import namedtuple
      8 

ImportError: cannot import name 'model_urls' from 'torchvision.models.vgg' (C:\Users\name\AppData\Local\Continuum\anaconda3\lib\site-packages\torchvision\models\vgg.py)
python ocr torchvision easyocr
2个回答
2
投票

不敢相信这个问题没有人来回答。

首先,对于所有torchvision > 0.13的用户,

model_urls
已经消失了,你不应该使用它。

TL,博士

解决方案很简单:

# change from your model_urls to this
from torchvision.models.resnet import ResNet50_Weights

org_resnet = torch.utils.model_zoo.load_url(ResNet50_Weights.IMAGENET1K_V2.url)

如果你想使用其他resnet arch,只需替换为

ResNet101_Weights

我的 torchvision 版本已测试:

'0.13.0.dev20220419+cu115'


0
投票

否则你可以看到那里的内容 https://github.com/pytorch/vision/blob/v0.1.9/torchvision/models/vgg.py 并将 model_urls['xx'] 的值替换为其对应的值

© www.soinside.com 2019 - 2024. All rights reserved.