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

12.8k views Asked by At

I have imported easyocr before. Yesterday I created a virtual environment to run a program on Jupyter terminal. Since today I am facing this error everytime I try to import easyocr. Any help would be appreciated.

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)
2

There are 2 answers

3
Nicholas Jela On

Can't believe this question no one come to answer.

First of all, for all torchvision > 0.13 users, the model_urls are gone, you shouldn't use it.

TL, DR:

solution is simple:

# 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)

If you want using other resnet arch, just replace with ResNet101_Weights etc.

Mine torchvision version tested:

'0.13.0.dev20220419+cu115'

0
bdz On

Else you can see there the content https://github.com/pytorch/vision/blob/v0.1.9/torchvision/models/vgg.py And replace the values of model_urls['xx'] by its corresponding value