Can't import torchvision

username: Aminul_Huq
cooked: <p>I wrote the following code snippet and it gave me the following error<br>
torch version 1.4.0<br>
torchvision version 0.5.0<br>
I tried installing using both pip and conda. Both produces the same error</p>
<p>import torchvision</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
----&gt; 1 import torchvision</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/<strong>init</strong>.py in <br>
1 import warnings<br>
2<br>
----&gt; 3 from torchvision import models<br>
4 from torchvision import datasets<br>
5 from torchvision import ops</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/models/<strong>init</strong>.py in <br>
10 from .shufflenetv2 import *<br>
11 from . import segmentation<br>
—&gt; 12 from . import detection<br>
13 from . import video<br>
14 from . import quantization</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/<strong>init</strong>.py in <br>
----&gt; 1 from .faster_rcnn import *<br>
2 from .mask_rcnn import *<br>
3 from .keypoint_rcnn import *</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/faster_rcnn.py in <br>
11<br>
12 from .generalized_rcnn import GeneralizedRCNN<br>
—&gt; 13 from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork<br>
14 from .roi_heads import RoIHeads<br>
15 from .transform import GeneralizedRCNNTransform</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/rpn.py in <br>
9 from torchvision.ops import boxes as box_ops<br>
10<br>
—&gt; 11 from . import _utils as det_utils<br>
12 from .image_list import ImageList<br>
13</p>
<p>~/anaconda3/lib/python3.7/site-packages/torchvision/models/detection/_utils.py in <br>
17<br>
18 <span class="mention">@torch.jit.script</span><br>
—&gt; 19 class BalancedPositiveNegativeSampler(object):<br>
20     “”"<br>
21     This class samples batches, ensuring that they contain a fixed proportion of positives</p>
<p>~/anaconda3/lib/python3.7/site-packages/torch/jit/<strong>init</strong>.py in script(obj, optimize, _frames_up, _rcb)<br>
1217         use :func:<code>@torch.jit.ignore &lt;torch.jit.ignore&gt;</code>.<br>
1218<br>
-&gt; 1219         Example (an exported and ignored method in a module)::<br>
1220<br>
1221             import torch</p>
<p>~/anaconda3/lib/python3.7/site-packages/torch/jit/<strong>init</strong>.py in _compile_and_register_class(obj, rcb, qualified_name)<br>
1074<br>
1075 def _try_get_overloaded_fn(mod, field):<br>
-&gt; 1076     return mod._overloads.get(field, None) if isinstance(mod, ScriptModule) else None<br>
1077<br>
1078</p>
<p>RuntimeError: class ‘<strong>torch</strong>.torchvision.models.detection._utils.BalancedPositiveNegativeSampler’ already defined. (register_type at /pytorch/torch/csrc/jit/script/compilation_unit.h:166)<br>
frame <span class="hashtag">#0:</span> c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x33 (0x7f57f44dd813 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libc10.so)<br>
frame <span class="hashtag">#1:</span>  + 0x5a47a9 (0x7f57f4eef7a9 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)<br>
frame <span class="hashtag">#2:</span>  + 0x58c0be (0x7f57f4ed70be in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)<br>
frame <span class="hashtag">#3:</span>  + 0x58d393 (0x7f57f4ed8393 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)<br>
frame <span class="hashtag">#4:</span>  + 0x210ba4 (0x7f57f4b5bba4 in /home/aminul/anaconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)<br>
frame <span class="hashtag">#5:</span> _PyMethodDef_RawFastCallKeywords + 0x254 (0x7f5816ff4744 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#6:</span> _PyCFunction_FastCallKeywords + 0x21 (0x7f5816ff4861 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#7:</span> _PyEval_EvalFrameDefault + 0x47a4 (0x7f581705fb94 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#8:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#9:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#10:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#11:</span> _PyFunction_FastCallKeywords + 0x325 (0x7f5816ff3ef5 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#12:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#13:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#14:</span> PyEval_EvalCodeEx + 0x44 (0x7f5816fa5424 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#15:</span> PyEval_EvalCode + 0x1c (0x7f5816fa544c in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#16:</span>  + 0x1dae81 (0x7f581706ae81 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#17:</span> _PyMethodDef_RawFastCallDict + 0x2b2 (0x7f5816fc56a2 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#18:</span> _PyCFunction_FastCallDict + 0x21 (0x7f5816fc57c1 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#19:</span> _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#20:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#21:</span> _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#22:</span> _PyEval_EvalFrameDefault + 0x4b39 (0x7f581705ff29 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#23:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#24:</span> _PyEval_EvalFrameDefault + 0x6a3 (0x7f581705ba93 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#25:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#26:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#27:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#28:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#29:</span> _PyFunction_FastCallDict + 0x10b (0x7f5816fa556b in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#30:</span>  + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#31:</span> _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#32:</span> PyImport_ImportModuleLevelObject + 0x29c (0x7f5816fadecc in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#33:</span>  + 0x1a2fba (0x7f5817032fba in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#34:</span> PyCFunction_Call + 0x65 (0x7f5816fc5845 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#35:</span> _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#36:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#37:</span> _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#38:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#39:</span> _PyEval_EvalCodeWithName + 0xac9 (0x7f5816fa4d09 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#40:</span> _PyFunction_FastCallDict + 0x1d5 (0x7f5816fa5635 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#41:</span>  + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#42:</span> _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#43:</span> PyImport_ImportModuleLevelObject + 0x21a (0x7f5816fade4a in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#44:</span> _PyEval_EvalFrameDefault + 0x2b49 (0x7f581705df39 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#45:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#46:</span> PyEval_EvalCodeEx + 0x44 (0x7f5816fa5424 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#47:</span> PyEval_EvalCode + 0x1c (0x7f5816fa544c in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#48:</span>  + 0x1dae81 (0x7f581706ae81 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#49:</span> _PyMethodDef_RawFastCallDict + 0x2b2 (0x7f5816fc56a2 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#50:</span> _PyCFunction_FastCallDict + 0x21 (0x7f5816fc57c1 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#51:</span> _PyEval_EvalFrameDefault + 0x5c17 (0x7f5817061007 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#52:</span> _PyEval_EvalCodeWithName + 0x2f9 (0x7f5816fa4539 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#53:</span> _PyFunction_FastCallKeywords + 0x387 (0x7f5816ff3f57 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#54:</span> _PyEval_EvalFrameDefault + 0x4b39 (0x7f581705ff29 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#55:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#56:</span> _PyEval_EvalFrameDefault + 0x6a3 (0x7f581705ba93 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#57:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#58:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#59:</span> _PyFunction_FastCallKeywords + 0xfb (0x7f5816ff3ccb in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#60:</span> _PyEval_EvalFrameDefault + 0x416 (0x7f581705b806 in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#61:</span> _PyFunction_FastCallDict + 0x10b (0x7f5816fa556b in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#62:</span>  + 0x13414e (0x7f5816fc414e in /home/aminul/anaconda3/bin/python)<br>
frame <span class="hashtag">#63:</span> _PyObject_CallMethodIdObjArgs + 0xbd (0x7f581701f68d in /home/aminul/anaconda3/bin/python)</p>

username: Pelk
cooked: <p>Hi, I am facing the same issue</p>

username: ptrblck
cooked: <p>Could you delete all <code>torchvision</code> installations and re-install it again, please?</p>

username: Pelk
cooked: <p>I did delete all torchvisions installations, but it did not help. But I changed to pytorch nightly and it works fine now. (:</p>

username: Aminul_Huq
cooked: <p>Fixed the issue. I needed to change the torchvision version.</p>

username: farah_jabeen
cooked: <p>Hi, which torchvision version did you migrate to?That solved your issue</p>