Can't import torchvision

I wrote the following code snippet and it gave me the following error
torch version 1.4.0
torchvision version 0.5.0
I tried installing using both pip and conda. Both produces the same error

import torchvision


RuntimeError Traceback (most recent call last)
in
----> 1 import torchvision

~/anaconda3/lib/python3.7/site-packages/torchvision/init.py in
1 import warnings
2
----> 3 from torchvision import models
4 from torchvision import datasets
5 from torchvision import ops

~/anaconda3/lib/python3.7/site-packages/torchvision/models/init.py in
10 from .shufflenetv2 import *
11 from . import segmentation
—> 12 from . import detection
13 from . import video
14 from . import quantization

~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/init.py in
----> 1 from .faster_rcnn import *
2 from .mask_rcnn import *
3 from .keypoint_rcnn import *

~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/faster_rcnn.py in
11
12 from .generalized_rcnn import GeneralizedRCNN
—> 13 from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork
14 from .roi_heads import RoIHeads
15 from .transform import GeneralizedRCNNTransform

~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/rpn.py in
9 from torchvision.ops import boxes as box_ops
10
—> 11 from . import _utils as det_utils
12 from .image_list import ImageList
13

~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/_utils.py in
17
18 @torch.jit.script
—> 19 class BalancedPositiveNegativeSampler(object):
20 “”"
21 This class samples batches, ensuring that they contain a fixed proportion of positives

~/anaconda3/lib/python3.7/site-packages/torch/jit/init.py in script(obj, optimize, _frames_up, _rcb)
1217 use :func:@torch.jit.ignore <torch.jit.ignore>.
1218
-> 1219 Example (an exported and ignored method in a module)::
1220
1221 import torch

~/anaconda3/lib/python3.7/site-packages/torch/jit/init.py in _compile_and_register_class(obj, rcb, qualified_name)
1074
1075 def _try_get_overloaded_fn(mod, field):
-> 1076 return mod._overloads.get(field, None) if isinstance(mod, ScriptModule) else None
1077
1078

RuntimeError: class ‘torch.torchvision.models.detection._utils.BalancedPositiveNegativeSampler’ already defined. (register_type at /pytorch/torch/csrc/jit/script/compilation_unit.h:166)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x33 (0x7f57f44dd813 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #1: + 0x5a47a9 (0x7f57f4eef7a9 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #2: + 0x58c0be (0x7f57f4ed70be in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #3: + 0x58d393 (0x7f57f4ed8393 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #4: + 0x210ba4 (0x7f57f4b5bba4 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #5: _PyMethodDef_RawFastCallKeywords + 0x254 (0x7f5816ff4744 in /home/aminul/anaconda3/bin/python)
frame #6: _PyCFunction_FastCallKeywords + 0x21 (0x7f5816ff4861 in /home/aminul/anaconda3/bin/python)
frame #7: _PyEval_EvalFrameDefault + 0x47a4 (0x7f581705fb94 in /home/aminul/anaconda3/bin/python)
frame #8: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #9: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #10: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #11: _PyFunction_FastCallKeywords + 0x325 (0x7f5816ff3ef5 in /home/aminul/anaconda3/bin/python)
frame #12: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #13: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #14: PyEval_EvalCodeEx + 0x44 (0x7f5816fa5424 in /home/aminul/anaconda3/bin/python)
frame #15: PyEval_EvalCode + 0x1c (0x7f5816fa544c in /home/aminul/anaconda3/bin/python)
frame #16: + 0x1dae81 (0x7f581706ae81 in /home/aminul/anaconda3/bin/python)
frame #17: _PyMethodDef_RawFastCallDict + 0x2b2 (0x7f5816fc56a2 in /home/aminul/anaconda3/bin/python)
frame #18: _PyCFunction_FastCallDict + 0x21 (0x7f5816fc57c1 in /home/aminul/anaconda3/bin/python)
frame #19: _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)
frame #20: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #21: _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)
frame #22: _PyEval_EvalFrameDefault + 0x4b39 (0x7f581705ff29 in /home/aminul/anaconda3/bin/python)
frame #23: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #24: _PyEval_EvalFrameDefault + 0x6a3 (0x7f581705ba93 in /home/aminul/anaconda3/bin/python)
frame #25: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #26: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #27: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #28: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #29: _PyFunction_FastCallDict + 0x10b (0x7f5816fa556b in /home/aminul/anaconda3/bin/python)
frame #30: + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)
frame #31: _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)
frame #32: PyImport_ImportModuleLevelObject + 0x29c (0x7f5816fadecc in /home/aminul/anaconda3/bin/python)
frame #33: + 0x1a2fba (0x7f5817032fba in /home/aminul/anaconda3/bin/python)
frame #34: PyCFunction_Call + 0x65 (0x7f5816fc5845 in /home/aminul/anaconda3/bin/python)
frame #35: _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)
frame #36: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #37: _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)
frame #38: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #39: _PyEval_EvalCodeWithName + 0xac9 (0x7f5816fa4d09 in /home/aminul/anaconda3/bin/python)
frame #40: _PyFunction_FastCallDict + 0x1d5 (0x7f5816fa5635 in /home/aminul/anaconda3/bin/python)
frame #41: + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)
frame #42: _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)
frame #43: PyImport_ImportModuleLevelObject + 0x21a (0x7f5816fade4a in /home/aminul/anaconda3/bin/python)
frame #44: _PyEval_EvalFrameDefault + 0x2b49 (0x7f581705df39 in /home/aminul/anaconda3/bin/python)
frame #45: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #46: PyEval_EvalCodeEx + 0x44 (0x7f5816fa5424 in /home/aminul/anaconda3/bin/python)
frame #47: PyEval_EvalCode + 0x1c (0x7f5816fa544c in /home/aminul/anaconda3/bin/python)
frame #48: + 0x1dae81 (0x7f581706ae81 in /home/aminul/anaconda3/bin/python)
frame #49: _PyMethodDef_RawFastCallDict + 0x2b2 (0x7f5816fc56a2 in /home/aminul/anaconda3/bin/python)
frame #50: _PyCFunction_FastCallDict + 0x21 (0x7f5816fc57c1 in /home/aminul/anaconda3/bin/python)
frame #51: _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)
frame #52: _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)
frame #53: _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)
frame #54: _PyEval_EvalFrameDefault + 0x4b39 (0x7f581705ff29 in /home/aminul/anaconda3/bin/python)
frame #55: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #56: _PyEval_EvalFrameDefault + 0x6a3 (0x7f581705ba93 in /home/aminul/anaconda3/bin/python)
frame #57: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #58: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #59: _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)
frame #60: _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)
frame #61: _PyFunction_FastCallDict + 0x10b (0x7f5816fa556b in /home/aminul/anaconda3/bin/python)
frame #62: + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)
frame #63: _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)

Hi, I am facing the same issue

Could you delete all torchvision installations and re-install it again, please?

I did delete all torchvisions installations, but it did not help. But I changed to pytorch nightly and it works fine now. (:

Fixed the issue. I needed to change the torchvision version.

Hi, which torchvision version did you migrate to?That solved your issue