Torchvision will not import into Python after jetson-inference build of PyTorch

Hi All,

I have been following Paul McWhorter’s “AI on the Jetson Nano LESSON 49: Installing NVIDIA Object Detection and Inference tools”

I am running Jetpack 4.3 on a Jetson Nano and using Python3.6.9.

After building, installing, and linking the modules in the jetson-inference download, I am able to import Torch (1.1.0) into Python3, however when I attempt to import Torchvision (0.3.0) into Python3 I receive this message:

import torch
import torchvision
Traceback (most recent call last):
File “”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/init.py”, line 11, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/detection/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/detection/faster_rcnn.py”, line 7, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/ops/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/ops/boxes.py”, line 2, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/_C.py”, line 8, in
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/_C.py”, line 7, in bootstrap
TypeError: exec_module() missing 1 required positional argument: ‘module’

I have researched this issue on the IoT and have not been able to find a satisfactory solution to correct this error.

Could it be that the configuration linker (ldconfig) is not correctly linking the torchvision module?

In the jetson-inference build directory the Torchvision directory is shown as torchvision36.

Suggestions please as to how to correct his issue and get torchvision to import into Python3.

Regards,
TCIII

Can you try re-installing torchvision by hand and see if there are any errors?

$ sudo apt-get install libjpeg-dev zlib1g-dev
$ git clone --branch v0.3.0 https://github.com/pytorch/vision torchvision
$ cd torchvision
$ sudo python3 setup.py install
$ cd ../  # attempting to load torchvision from build dir will result in import error
$ pip3 install 'pillow<7'

Hi Dusty,

I am new to ubuntu linux and the jetpack and am trying to learn on my own. Your quick response is much appreciated.

According to the McWorter’s tutorial, the code came from your github: git clone --recursive https://github.com/dusty-nv/jetson-inference.

I took a look at your command snippet during my IoT investigation of the issue I am having, but was not sure about where to install torchvision in relation to the build sub-directory in the jetson-inference directory.

In what directory do I run these commands, the root or the build directory created in the jetson-inference directory when cloning the jetson-inference code from the github?

Should these commands be run after installing Pytorch using the jetson-inference code as in McWhorter’s tutorial?

Regards,
TCIII

I would just try this re-installation of torchvision in your user’s home directory (or wherever you want). It doesn’t actually matter where torchvision gets downloaded to during the git clone operation, because it will be installed to your system’s Python modules during the sudo python3 setup.py install step.

And actually first before you do that, uninstall the existing torchvision that isn’t working:

sudo pip3 uninstall torchvision

Then try re-installing it from the steps above.

Hi Dusty,

Thanks for the response, much appreciated.

I will give it a shot.

Regards,
TCIII

@dusty_nv,

Per your instructions I uninstalled torchvision successfully and then proceeded to reinstall torchvision using your command instructions.

Unfortunately I am still getting the same error message when I try to import torchvision into python3:

import torchvision
Traceback (most recent call last):
File “”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/init.py”, line 11, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/detection/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/models/detection/faster_rcnn.py”, line 7, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/ops/init.py”, line 1, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/ops/boxes.py”, line 2, in
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 656, in _load_unlocked
File “”, line 626, in _load_backward_compatible
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/_C.py”, line 8, in
File “/usr/local/lib/python3.6/dist-packages/torchvision-0.3.0-py3.6-linux-aarch64.egg/torchvision/_C.py”, line 7, in bootstrap
TypeError: exec_module() missing 1 required positional argument: ‘module’

Any suggestions on how I should proceed?

Do you think that updating and upgrading the Jetpack4.2 Ubuntu 18.0.4 and associated packages might be the cause of the problem?

How about if I skip the PyTorch installation during the jetson-inference build and install it by itself?

Regards,
TCIII

@dusty_nv,

I decided to do a complete reinstall of Jetpack4.3 complete with updates and upgrades. I then tried to install torchvision by doing the following:

I successfully installed Pytorch 1.1.0 individually (using the instructions here PyTorch for Jetson Nano - version 1.5.0 now available) instead of letting “The Model Downloader” in jetson-inference install PyTorch 1.1.0 for Python3.6.
I then proceeded to successfully install Torchvision 0.3.0 per your instructions above and I still get the same error when I try to import torchvision into Python3 after a successful import of torch.

There is a definite disconnect between Python3 and Torchvision and would appreciate your help determining the cause of the disconnect and how to correct it.

Regards,
Tom Coyle

Hmm sorry about that, I wonder if there is some update required to torchvision. Unfortunately my systems are all on JetPack 4.4 now - can you try installing PyTorch 1.4 and torchvision 0.5.0 instead?

@dusty_nv,

Thanks for the quick response, much appreciated.

Ok, I will give that a try and get back to you.

Regards,
Tom C

@dusty_nv,

Ok, uninstalling both torch 1.1.0 and torchvision 0.3.0 and successfully installing PyTorch 1.4.0 and torchvision 0.5.0 allowed me to import both torch and torchvision into python3. One thing to note is uninstalling torch 0.3.0 using pip3 does not clear out all of the torchvision sub-directories which must be cleared out for a new version of torchvision to be installed.

Now my question is: Will I have to change anything in “The Model Downloader” in jetson-inference to get it to use PyTorch 1.4.0 in place of 1.1.0 when I execute make -j$(nproc) after cmake …/ completes? If so, where do I need to look in the jetson-inference directory to make any corrections.

I really appreciate your help as I have learned a lot here about Ubuntu and PyTorch.

Regards,
Tom C

Glad you were able to get it working with PyTorch 1.4 and torchvision 0.5 - you don’t have to change anything in the ‘Model Downloader’, because that actually doesn’t download PyTorch models (only ONNX, caffe, and UFF). And even if it did download PyTorch models, those aren’t typically dependent on the PyTorch version if saved correctly.

However as far as the ‘PyTorch Installer’ tool goes, you should skip that, because it will just want to install PyTorch 1.1 and torchvision 0.3 again. It sounds like I need to change that tool to only install PyTorch 1.4 and torchvision 0.5 when on JetPack 4.3. Thanks for letting me know about it.

@dusty_nv,

Thanks for all of your help, much appreciated.

I have forwarded your last response to Paul McWhorter (TopTechBoy.com) who is using your jetson-inference in his tutorial for installing “NVIDIA Object Detection and Inference Tools”.

Here is McWhorter’s response to shifting from Jetpack 4.3 to Jetpack 4.4:
“And the problem with jetpack 4.4 is that is breaks the face recognizer library, and brings it down to a few frames per second, where it runs at over 10 fps on jetpack 4.3. We need some stable build to work on. It is a really big investment for me to make a tutorial series, and if it becomes obsolete because the core os behave differently a month or so later, that leads to a platform you can not educate on or build on.”

Also I found that if I don’t install PyTorch 1.1.0 during “The Model Download” my install of the models ends differently than McWhorter’s install. Mine:
[ 93%] Built target jetson-inference-python-27
[ 94%] Built target homography-camera
[ 95%] Built target trt-bench
[100%] Built target jetson-inference-python-3
McWhorter’s:
[100%] Link CXX executable …/…/aarch64/bin/camera-capture
[100%] Built target camer-capture
And does not include “Built target homography-camera”
and “Built target trt-bench”

Will this be an issue?

Regards, Tom C

The only different is you have libpython3-dev installed, so it built the jetson-inference bindings for Python3 for you - so no issue.

FYI I just updated the install-pytorch.sh script to use PyTorch 1.4 / torchvision 0.5 on JetPack 4.2 and JetPack 4.3. See commit 8a7acf to jetson-inference master branch. Thanks again for letting me know about the problem.

@dusty_nv,

Thanks for the update, much appreciated.

I will pass this along to Paul McWhorter.

Regards,
Tom C