Hash key error while downloading pretrain model called efficientnet

Amit_Parmar: <p>RuntimeError                              Traceback (most recent call last)<br>
Cell In[31], line 3<br>
1 <span class="hashtag-raw">#object</span> of model selection class<br>
----&gt; 3 img2vec = Img2Vec(cuda=False, model=‘efficientnet_b0’)<br>
4 <span class="hashtag-raw">#img2vec</span> = Img2Vec(cuda=False, model=‘vit_base_patch16_224’, layer=‘head’)</p>
<p>Cell In[28], line 35, in Img2Vec.<strong>init</strong>(self, cuda, model, layer, layer_output_size, gpu)<br>
32 self.layer_output_size = layer_output_size<br>
33 self.model_name = model<br>
—&gt; 35 self.model, self.extraction_layer = self._get_model_and_layer(model, layer)<br>
37 self.model = self.model.to(self.device)<br>
39 self.model.eval()</p>
<p>Cell In[28], line 176, in Img2Vec._get_model_and_layer(self, model_name, layer)<br>
173 elif “efficientnet” in model_name:<br>
174     # efficientnet-b0 ~ efficientnet-b7<br>
175     if model_name == “efficientnet_b0”:<br>
 → 176         model = models.efficientnet_b0(pretrained=True)<br>
177     elif model_name == “efficientnet_b1”:<br>
178         model = models.efficientnet_b1(pretrained=True)</p>
<p>File C:\Anaconda\lib\site-packages\torchvision\models_utils.py:142, in kwonly_to_pos_or_kw..wrapper(*args, **kwargs)<br>
135     warnings.warn(<br>
136         f"Using {sequence_to_str(tuple(keyword_only_kwargs.keys()), separate_last='and ')} as positional "<br>
137         f"parameter(s) is deprecated since 0.13 and may be removed in the future. Please use keyword parameter(s) "<br>
138         f"instead."<br>
139     )<br>
140     kwargs.update(keyword_only_kwargs)<br>
 → 142 return fn(*args, **kwargs)</p>
<p>File C:\Anaconda\lib\site-packages\torchvision\models_utils.py:228, in handle_legacy_interface..outer_wrapper..inner_wrapper(*args, **kwargs)<br>
225     del kwargs[pretrained_param]<br>
226     kwargs[weights_param] = default_weights_arg<br>
 → 228 return builder(*args, **kwargs)</p>
<p>File C:\Anaconda\lib\site-packages\torchvision\models\efficientnet.py:770, in efficientnet_b0(weights, progress, **kwargs)<br>
767 weights = EfficientNet_B0_Weights.verify(weights)<br>
769 inverted_residual_setting, last_channel = _efficientnet_conf(“efficientnet_b0”, width_mult=1.0, depth_mult=1.0)<br>
 → 770 return _efficientnet(<br>
771     inverted_residual_setting, kwargs.pop(“dropout”, 0.2), last_channel, weights, progress, **kwargs<br>
772 )</p>
<p>File C:\Anaconda\lib\site-packages\torchvision\models\efficientnet.py:360, in _efficientnet(inverted_residual_setting, dropout, last_channel, weights, progress, **kwargs)<br>
357 model = EfficientNet(inverted_residual_setting, dropout, last_channel=last_channel, **kwargs)<br>
359 if weights is not None:<br>
 → 360     model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))<br>
362 return model</p>
<p>File C:\Anaconda\lib\site-packages\torchvision\models_api.py:90, in WeightsEnum.get_state_dict(self, *args, **kwargs)<br>
89 def get_state_dict(self, *args: Any, **kwargs: Any) → Mapping[str, Any]:<br>
—&gt; 90     return load_state_dict_from_url(self.url, *args, **kwargs)</p>
<p>File C:\Anaconda\lib\site-packages\torch\hub.py:757, in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name, weights_only)<br>
755         r = HASH_REGEX.search(filename)  # r is Optional[Match[str]]<br>
756         hash_prefix = r.group(1) if r else None<br>
 → 757     download_url_to_file(url, cached_file, hash_prefix, progress=progress)<br>
759 if _is_legacy_zip_format(cached_file):<br>
760     return _legacy_zip_load(cached_file, model_dir, map_location, weights_only)</p>
<p>File C:\Anaconda\lib\site-packages\torch\hub.py:653, in download_url_to_file(url, dst, hash_prefix, progress)<br>
651         digest = sha256.hexdigest()<br>
652         if digest[:len(hash_prefix)] != hash_prefix:<br>
 → 653             raise RuntimeError(‘invalid hash value (expected “{}”, got “{}”)’<br>
654                                .format(hash_prefix, digest))<br>
655     shutil.move(f.name, dst)<br>
656 finally:</p>
<p>RuntimeError: invalid hash value (expected “3dd342df”, got “7f5810bc96def8f7552d5b7e68d53c4786f81167d28291b21c0d90e1fca14934”)</p>

ptrblck: <p>Thanks for reporting the issue!<br>
I’ve created a new issue on <a href="https://github.com/pytorch/vision/issues/7744">GitHub</a> so that we can track and fix it as I’m able to reproduce it with a nightly binary.</p>

funderdown: <p>Greetings:  I am having problems with this same error and it is (1/8/2024).</p>
<p>I am trying to download the weights for EfficientNet_B0_Weights. Note: I downloaded these weights in the past without issue.  However, today I am having trouble.</p>
<p>I am trying to download these weights to a notebook on Colab.</p>
<p>I would appreciate some help with this problem.<br>
Note: I am review some course materials and I noted in the Q&amp;A section of the course, I am not the only person having this problem.</p>

ptrblck: <p>I cannot reproduce any issues downloading <code>torchvision.models.EfficientNet_B0_Weights.IMAGENET1K_V1</code> in the current nightly and stable releases:</p>
<pre data-code-wrap="python"><code class="lang-python">model = torchvision.models.efficientnet_b0(weights=torchvision.models.EfficientNet_B0_Weights.IMAGENET1K_V1)
Downloading: "https://download.pytorch.org/models/efficientnet_b0_rwightman-7f5810bc.pth" to /home/pbialecki/.cache/torch/hub/checkpoints/efficientnet_b0_rwightman-7f5810bc.pth
100.0%

&gt;&gt;&gt; print(torchvision.__version__)
0.16.2+cu121
</code></pre>

funderdown: <p>Thank you for getting back to me quickly.</p>
<p>The code I was having problems with is below:</p>
<p>weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT # “.DEFAULT” = best available weights</p>
<p>model = torchvision.models.efficientnet_b0(weights=weights).to(device)</p>
<p>model</p>
<p>This is different then what you you sent me.  I will try the new statements and share them with the other students.</p>
<p>Cheers!</p>
<p>Frank</p>

ptrblck: <p>This also works for me:</p>
<pre data-code-wrap="python"><code class="lang-python">weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT # “.DEFAULT” = best available weights

model = torchvision.models.efficientnet_b0(weights=weights)

model
Downloading: "https://download.pytorch.org/models/efficientnet_b0_rwightman-7f5810bc.pth" to /home/pbialecki/.cache/torch/hub/checkpoints/efficientnet_b0_rwightman-7f5810bc.pth
100%|██████████| 20.5M/20.5M [00:01&lt;00:00, 13.9MB/s]
Out[26]: 
EfficientNet(
  (features): Sequential(
    (0): Conv2dNormActivation(
      (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): SiLU(inplace=True)
    )
...
</code></pre>

mustious: <p>For anyone who might still be stuck with the same error. The major issue is from <code>torchvision==0.16.0</code>. An easy workaround is to install higher versions or nightly. Tested and worked on <code>torchvision</code> <code>v0.16.1</code>, <code>v0.16.2</code>, <code>v0.17.0</code>.</p>
<pre><code class="lang-auto">pip install "torchvision&gt;0.16.0" 
</code></pre>