AttributeError: module 'torch' has no attribute '_utils'

suvployrada: <p>I installed</p>
<pre><code class="lang-auto">pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
</code></pre>
<p>also my nvcc --version is 12.1<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/7/f72b4a84dc295f0055722dd311bb5b4f48cac6fc.png" data-download-href="https://discuss.pytorch.org/uploads/default/f72b4a84dc295f0055722dd311bb5b4f48cac6fc" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72b4a84dc295f0055722dd311bb5b4f48cac6fc_2_690x200.png" alt="image" data-base62-sha1="zgyGwJ2GCLngbZxgY80s1bVqQiM" width="690" height="200" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72b4a84dc295f0055722dd311bb5b4f48cac6fc_2_690x200.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72b4a84dc295f0055722dd311bb5b4f48cac6fc_2_1035x300.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72b4a84dc295f0055722dd311bb5b4f48cac6fc_2_1380x400.png 2x" data-dominant-color="232424"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1511×439 15.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
and this is my system details.</p>
<p>However, when I run the code it shown</p>
<blockquote>
<p>AttributeError: module ‘torch’ has no attribute ‘_utils’</p>
</blockquote>
<p>So I tried to run <code>conda install pytorch torchvision torchaudio cudatoolkit=11.1</code> as I asked chatGPT but it still show same issue.</p>
<p>The question is what should I import or install to fix this issue.</p>
<p>Please help. Thank you in advanced.</p>

ptrblck: <p>Could you describe when this error is raised?<br>
I can directly access this module in <code>2.0.0</code>:</p>
<pre><code class="lang-python">import torch
print(torch.__version__)
# 2.0.0+cu118
torch._utils
# &lt;module 'torch._utils' from ...
</code></pre>
<p>but also note that it’s an internal module.</p>

ptrblck: <p>Could you post the full stacktrace here?</p>

ptrblck: <p>Based on the stacktrace it seems the error is raised from <a href="https://github.com/pytorch/vision/blob/185be3a9811a9cc6dc6206a397d035be3c20649b/torchvision/models/segmentation/segmentation.py#L176">here</a>. However, I cannot reproduce the error by initializing the model via:</p>
<pre><code class="lang-python">model = models.segmentation.deeplabv3_resnet50(pretrained=False, num_classes=19, output_stride=8)
</code></pre>
<p>so would need to get an executable code snippet to debug it further.</p>

sammlapp: <p>As of today, I am getting a similar error where I did not previously get any errors:</p>
<p>Torch version is 1.13.1, torchvision 0.14.1</p>
<pre><code class="lang-auto">from torchvision import models
models.resnet34(weights='DEFAULT')
</code></pre>
<p>raises:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/var/folders/d8/265wdp1n0bn_r85dh3pp95fh0000gq/T/ipykernel_64242/3473222446.py in &lt;cell line: 2&gt;()
      1 from torchvision import models
----&gt; 2 models.resnet34(weights='DEFAULT')

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/_utils.py in wrapper(*args, **kwargs)
    140             kwargs.update(keyword_only_kwargs)
    141 
--&gt; 142         return fn(*args, **kwargs)
    143 
    144     return wrapper

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/_utils.py in inner_wrapper(*args, **kwargs)
    226                 kwargs[weights_param] = default_weights_arg
    227 
--&gt; 228             return builder(*args, **kwargs)
    229 
    230         return inner_wrapper

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/resnet.py in resnet34(weights, progress, **kwargs)
    695     weights = ResNet34_Weights.verify(weights)
    696 
--&gt; 697     return _resnet(BasicBlock, [3, 4, 6, 3], weights, progress, **kwargs)
    698 
    699 

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/resnet.py in _resnet(block, layers, weights, progress, **kwargs)
    299 
    300     if weights is not None:
--&gt; 301         model.load_state_dict(weights.get_state_dict(progress=progress))
    302 
    303     return model

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torchvision/models/_api.py in get_state_dict(self, progress)
     64 
     65     def get_state_dict(self, progress: bool) -&gt; Mapping[str, Any]:
---&gt; 66         return load_state_dict_from_url(self.url, progress=progress)
     67 
     68     def __repr__(self) -&gt; str:

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
    733     if _is_legacy_zip_format(cached_file):
    734         return _legacy_zip_load(cached_file, model_dir, map_location)
--&gt; 735     return torch.load(cached_file, map_location=map_location)

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/serialization.py in load(f, map_location, pickle_module, weights_only, **pickle_load_args)
    787                     except RuntimeError as e:
    788                         raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
--&gt; 789                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    790         if weights_only:
    791             try:

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/serialization.py in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
   1129     unpickler = UnpicklerWrapper(data_file, **pickle_load_args)
   1130     unpickler.persistent_load = persistent_load
-&gt; 1131     result = unpickler.load()
   1132 
   1133     torch._utils._validate_loaded_sparse_tensors()

~/miniconda3/envs/opso_dev/lib/python3.9/site-packages/torch/serialization.py in persistent_load(saved_id)
   1098 
   1099         if key not in loaded_storages:
-&gt; 1100             nbytes = numel * torch._utils._element_size(dtype)
   1101             load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
   1102 

AttributeError: module 'torch' has no attribute '_utils'
</code></pre>

sammlapp: <p>Downgrading to torchvision 0.14.0 or upgrading torchvision to 0.15.1 both resolve this error</p>