ImportError: cannot import name 'mobilenet_v2' from 'torchvision.models'

I want to run a fastai deep learning model on my pc. Not train, just run the pre-trained model on my PC. I have the .pth file. I tried to import the fastai module that I installed and I recieved the error :

ImportError: cannot import name 'mobilenet_v2' from 'torchvision.models' (C:\file_path\

The Code I tried to execute:

#From the fastai library
from fastai import *
from torchvision.models import *
from import *

I can’t find any solutions as to why I am getting this error.

Im running this code in anaconda, to be specific, the spyder IDE connected to my anaconda environment. I will re-edit this if anyone needs more specifications. Thank you.



I was encountering the same problem after following the instructions from here:

My problem was that I had installed pytorch version 1.0.0 which then uses an earlier version of torchvision which holds the respective model. My issue was resolved after running:

conda install fastai pytorch=1.4.0 -c fastai -c pytorch -c conda-forge

This install a newer pytorch version and therefore uses a newer torchvision version which includes the mobilenet model.

Maybe you are encountering the same problem. Could you share the output of

python -m fastai.utils.show_install

while having your environment activated?


Yes: The output for python -m fastai.ustils.show_install:

Traceback (most recent call last):
File “C:\Users\rjsod\Anaconda3\envs\fastai\lib\”, line 183, in run_module_as_main
mod_name, mod_spec, code = get_module_details(mod_name, Error)
File “C:\Users\rjsod\Anaconda3\envs\fastai\lib\”, line 109, in get_module_details
File "C:\Users\rjsod\fastai\fastai\utils_init
.py", line 1, in
from .collect_env import *
File “C:\Users\rjsod\fastai\fastai\utils\”, line 4, in
from …imports.torch import *
File "C:\Users\rjsod\fastai\fastai\imports_init
.py", line 2, in
from .torch import *
File “C:\Users\rjsod\fastai\fastai\imports\”, line 1, in
import torch, torch.nn.functional as F
File "C:\Users\rjsod\Anaconda3\envs\fastai\lib\site-packages\torch_init
.py", line 81, in
File "C:\Users\rjsod\Anaconda3\envs\fastai\lib\ctypes_init
.py", line 364, in init
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

I guess there is a typo in your command. Remove the ‘s’ from utils:

python -m fastai.utils.show_install

Im sorry. The typo is in the post. The error persists when I copy paste from your reply.Currently Reinstalling Anaconda and contents. Im going to install as shown in the article you posted. Will edit thread if error still persists.

I reinstalled anaconda. Followed instructions to install pytorch and fastai from above the show_install function gives the following output:

=== Software ===
python       : 3.7.7
fastai       : 1.0.61
fastprogress : 0.2.2
torch        : 1.1.0
torch cuda   : None / is **Not available**

=== Hardware ===
No GPUs available

=== Environment ===
platform     : Windows-10-10.0.17763-SP0
conda env    : fastai
python       : C:\Users\rjsod\anaconda\envs\fastai\python.exe
sys.path     : C:\Users\rjsod
no supported gpus found on this system

importing fastai in spyder now gives the following error:

ImportError: cannot import name 'Optional' from 'torch.jit.annotations'

which is a different error than before so i guess thats one step in the positive direction

=== Software ===
python        : 3.6.10
fastai        : 1.0.61
fastprogress  : 0.2.2
torch         : 1.4.0
torch cuda    : 10.1 / is available
torch cudnn   : 7501 / is enabled

=== Hardware ===
torch devices : 1
  - gpu0      : GeForce 940MX

=== Environment ===
platform      : Windows-10-10.0.18362-SP0
conda env     : fastai_v1
python        : C:\Users\DELL\Anaconda3\envs\fastai_v1\python.exe
sys.path      :
no nvidia-smi is found

This is the output to that command. I am still getting the same error even after doing this.

conda install fastai pytorch=1.4.0 -c fastai -c pytorch -c conda-forge

Can you please help me out?

I removed fastai 1.0.61 and installed 1.0.60
At least in my case the import error vanished.

I also found the same error as yours, which has been fixed by following steps for my case:

  1. check the version of torchvision in your environment. The default one is 0.2.0 installed in python3.7/site-packages/

conda list torchvision

  1. uninstall the torchvision0.2.0

pip uninstall torchvision

  1. reinstall torchvison, choose the version that matches with pytorch. since my torch is 1.14.0, 0.5.0 is chosen.

pip install torchvision==0.5.0

Hope this is helpful for you!

Thanx this helped!

If you are running your code in a Jupyter notebook/Lab. It may happen that the notebook is getting triggered from a different (base) environment. In your fastai environment try installing jupyter

conda install -c conda-forge jupyterlab

or if you use Jupyter Notebook then:

conda install -c conda-forge notebook

(base) C:\Users\ksmit>python -m fastai.utils.show_install

=== Software ===
python        : 3.8.5
fastai        : 1.0.61
fastprogress  : 0.2.7
torch         : 1.4.0
nvidia driver : 461.72
torch cuda    : None / is **Not available**

=== Hardware ===
nvidia gpus   : 1
Have 1 GPU(s), but torch can't use them (check nvidia driver)

=== Environment ===
platform      : Windows-10-10.0.19041-SP0
conda env     : base
python        : C:\Users\ksmit\anaconda3\python.exe
sys.path      : C:\Users\ksmit

I am getting the same load error here is the printout

I am encountering the same issue, when I am trying to create the new environment.

Here is my output:


=== Software === 
python        : 3.9.2
fastai        : 1.0.61
fastprogress  : 0.2.7
torch         : 1.8.1
nvidia driver : 455.45
torch cuda    : 10.2 / is available
torch cudnn   : 7605 / is enabled

=== Hardware === 
nvidia gpus   : 1
torch devices : 1
  - gpu0      : 16125MB | Quadro RTX 5000

=== Environment === 
platform      : Linux-5.4.0-62-generic-x86_64-with-glibc2.27
distro        : #70~18.04.1-Ubuntu SMP Tue Jan 12 17:18:00 UTC 2021
conda env     : /home/sandeep/GoogleDrive/beans-home/mount_shared_partition/conda_envs/test1
python        : /home/sandeep/GoogleDrive/beans-home/mount_shared_partition/conda_envs/test1/bin/python
sys.path      : /home/sandeep/GoogleDrive/beans-home/git-repos/beans-ai-publicdata

Anybody has any suggestion?