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?

1 Like

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?