Hash key error while downloading pretrain model called efficientnet

RuntimeError Traceback (most recent call last)
Cell In[31], line 3
1 #object of model selection class
----> 3 img2vec = Img2Vec(cuda=False, model=‘efficientnet_b0’)
4 #img2vec = Img2Vec(cuda=False, model=‘vit_base_patch16_224’, layer=‘head’)

Cell In[28], line 35, in Img2Vec.init(self, cuda, model, layer, layer_output_size, gpu)
32 self.layer_output_size = layer_output_size
33 self.model_name = model
—> 35 self.model, self.extraction_layer = self._get_model_and_layer(model, layer)
37 self.model = self.model.to(self.device)
39 self.model.eval()

Cell In[28], line 176, in Img2Vec._get_model_and_layer(self, model_name, layer)
173 elif “efficientnet” in model_name:
174 # efficientnet-b0 ~ efficientnet-b7
175 if model_name == “efficientnet_b0”:
→ 176 model = models.efficientnet_b0(pretrained=True)
177 elif model_name == “efficientnet_b1”:
178 model = models.efficientnet_b1(pretrained=True)

File C:\Anaconda\lib\site-packages\torchvision\models_utils.py:142, in kwonly_to_pos_or_kw..wrapper(*args, **kwargs)
135 warnings.warn(
136 f"Using {sequence_to_str(tuple(keyword_only_kwargs.keys()), separate_last='and ')} as positional "
137 f"parameter(s) is deprecated since 0.13 and may be removed in the future. Please use keyword parameter(s) "
138 f"instead."
139 )
140 kwargs.update(keyword_only_kwargs)
→ 142 return fn(*args, **kwargs)

File C:\Anaconda\lib\site-packages\torchvision\models_utils.py:228, in handle_legacy_interface..outer_wrapper..inner_wrapper(*args, **kwargs)
225 del kwargs[pretrained_param]
226 kwargs[weights_param] = default_weights_arg
→ 228 return builder(*args, **kwargs)

File C:\Anaconda\lib\site-packages\torchvision\models\efficientnet.py:770, in efficientnet_b0(weights, progress, **kwargs)
767 weights = EfficientNet_B0_Weights.verify(weights)
769 inverted_residual_setting, last_channel = _efficientnet_conf(“efficientnet_b0”, width_mult=1.0, depth_mult=1.0)
→ 770 return _efficientnet(
771 inverted_residual_setting, kwargs.pop(“dropout”, 0.2), last_channel, weights, progress, **kwargs
772 )

File C:\Anaconda\lib\site-packages\torchvision\models\efficientnet.py:360, in _efficientnet(inverted_residual_setting, dropout, last_channel, weights, progress, **kwargs)
357 model = EfficientNet(inverted_residual_setting, dropout, last_channel=last_channel, **kwargs)
359 if weights is not None:
→ 360 model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))
362 return model

File C:\Anaconda\lib\site-packages\torchvision\models_api.py:90, in WeightsEnum.get_state_dict(self, *args, **kwargs)
89 def get_state_dict(self, *args: Any, **kwargs: Any) → Mapping[str, Any]:
—> 90 return load_state_dict_from_url(self.url, *args, **kwargs)

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)
755 r = HASH_REGEX.search(filename) # r is Optional[Match[str]]
756 hash_prefix = r.group(1) if r else None
→ 757 download_url_to_file(url, cached_file, hash_prefix, progress=progress)
759 if _is_legacy_zip_format(cached_file):
760 return _legacy_zip_load(cached_file, model_dir, map_location, weights_only)

File C:\Anaconda\lib\site-packages\torch\hub.py:653, in download_url_to_file(url, dst, hash_prefix, progress)
651 digest = sha256.hexdigest()
652 if digest[:len(hash_prefix)] != hash_prefix:
→ 653 raise RuntimeError(‘invalid hash value (expected “{}”, got “{}”)’
654 .format(hash_prefix, digest))
655 shutil.move(f.name, dst)
656 finally:

RuntimeError: invalid hash value (expected “3dd342df”, got “7f5810bc96def8f7552d5b7e68d53c4786f81167d28291b21c0d90e1fca14934”)

Thanks for reporting the issue!
I’ve created a new issue on GitHub so that we can track and fix it as I’m able to reproduce it with a nightly binary.

Greetings: I am having problems with this same error and it is (1/8/2024).

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.

I am trying to download these weights to a notebook on Colab.

I would appreciate some help with this problem.
Note: I am review some course materials and I noted in the Q&A section of the course, I am not the only person having this problem.

I cannot reproduce any issues downloading torchvision.models.EfficientNet_B0_Weights.IMAGENET1K_V1 in the current nightly and stable releases:

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%

>>> print(torchvision.__version__)
0.16.2+cu121

Thank you for getting back to me quickly.

The code I was having problems with is below:

weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT # “.DEFAULT” = best available weights

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

model

This is different then what you you sent me. I will try the new statements and share them with the other students.

Cheers!

Frank

This also works for me:

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<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)
    )
...

For anyone who might still be stuck with the same error. The major issue is from torchvision==0.16.0. An easy workaround is to install higher versions or nightly. Tested and worked on torchvision v0.16.1, v0.16.2, v0.17.0.

pip install "torchvision>0.16.0" 
1 Like