question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Torchvision import breaks OpenCV code

See original GitHub issue

šŸ› Bug

When I import torchvisions in an opencv python code, the code crashes with ā€œSegmentation Faultā€ (in console) or ā€œRestarting kernelā€ in Spyder / iPython.

To Reproduce

Steps to reproduce the behavior:

  1. Execute the code, the execution ends
  2. Execute the code and uncomment the torchvision import, the execution gives ā€œSegmentation faultā€
  3. Changing the order of import (torchvision then cv2) removes the issue for this short code sample but not for my complete code
import cv2
#import torchvision

pth = 'video.mp4'
cap = cv2.VideoCapture(pth)

ret,frame = cap.read()
cv2.imshow('frame',frame)

I also have an environment using Pytorch 1.5, the same code sample works fine under this environment. Thanks for checking if this is a real issue

Environment

PyTorch version: 1.8.0 Is debug build: False CUDA used to build PyTorch: 11.1 ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64) GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Clang version: Could not collect CMake version: version 3.16.3 Libc version: glibc-2.10

Python version: 3.7 (64-bit runtime) Python platform: Linux-5.4.0-65-generic-x86_64-with-debian-bullseye-sid Is CUDA available: True CUDA runtime version: Could not collect

cuDNN version: Could not collect HIP runtime version: N/A MIOpen runtime version: N/A

Versions of relevant libraries: [pip3] numpy==1.18.1 [pip3] numpydoc==1.1.0 [pip3] torch==1.8.0 [pip3] torchaudio==0.8.0a0+a751e1d [pip3] torchfile==0.1.0 [pip3] torchlars==0.1.2 [pip3] torchsummary==1.5.1 [pip3] torchvision==0.9.0 [conda] blas 1.0 mkl
[conda] cudatoolkit 11.1.1 h6406543_8 conda-forge [conda] ffmpeg 4.3 hf484d3e_0 pytorch [conda] mkl 2020.2 256
[conda] mkl-service 2.3.0 py37he8ac12f_0
[conda] mkl_fft 1.2.0 py37h23d657b_0
[conda] mkl_random 1.1.1 py37h0573a6f_0
[conda] numpy 1.19.2 py37h54aff64_0
[conda] numpy-base 1.19.2 py37hfa32c7d_0
[conda] numpydoc 1.1.0 pyhd3eb1b0_1
[conda] pytorch 1.8.0 py3.7_cuda11.1_cudnn8.0.5_0 pytorch [conda] torch 1.7.1 pypi_0 pypi [conda] torchaudio 0.7.2 pypi_0 pypi [conda] torchfile 0.1.0 pypi_0 pypi [conda] torchlars 0.1.2 dev_0 <develop> [conda] torchvision 0.9.0 py37_cu111 pytorch

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
andfoycommented, May 19, 2021

Have you tried creating a clean environment that uses just conda-forge/defaults and not pip packages? That combination is always guaranteed to generate segfaults

0reactions
Whiaxcommented, May 19, 2021

Yeah I saw the multiple versions, not sure if that could be the issue. I’m recreating a new clean environment to check that.

conda install torchvision -c pytorch conda install -c conda-forge opencv

If I do that, pytorch goes from 1.8.1 to 1.8.0 and torchvision also from 0.9.1 to 0.9.0. It installs opencv 4.5.2. And yeah ok, doing that solves the issue, so I guess it was on my side. Sorry for the trouble, it’s hard to get from where the bugs come from with these weird behaviors. _

For those with the same issue:

  • Create a clean conda environment

Thanks for the help!

edit: I’ll also look on how to use spyder better, thanks for the tip

Read more comments on GitHub >

github_iconTop Results From Across the Web

Torchvision import leads to OpenCV imshow freeze #5940
Showing an image using OpenCV leads to the program getting stuck (100% CPU) without showing the image if torchvision is imported.
Read more >
OpenCV 2 imshow not showing the video using python 3.6
I have code for pose estimated. I am trying to run in real-time but OpenCV not showing the video. How to solve this...
Read more >
How to load Pytorch models with OpenCV • Jean Vitor
I'll separate the code in two (the complete implementation is at the end). ... import torchvision.models as models. import sys.
Read more >
OpenCV build in Windows with PyTorch - Python
I'm using OpenCV exclusively from Python 3, and I cannot import PyTorch after OpenCV. E.g., import cv2 import t…
Read more >
Values after softmax in torchscript in c++ is not same as ...
Python Code: import torch import torchvision.models as models import urllib.request from PIL import Image from torchvision import transformsĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found