I'm trying to load Vision Transformer (ViT)
models from TensorFlow Hub
using the hub.KerasLayer
method. However, when I attempt to load the model, I encounter an OSError
indicating that the URL does not appear to be a valid module. Below is the snippet of code where the error occurs, followed by the error message:
import tensorflow_hub as hubdef load_vit_model(model_name='B16'): if model_name == 'B16': model_url = 'https://tfhub.dev/google/vit_base_patch16_224/2' elif model_name == 'B32': model_url = 'https://tfhub.dev/google/vit_base_patch32_224/2' else: raise ValueError("Unsupported ViT model name") model = hub.KerasLayer(model_url, trainable=False) return model# Error when trying to load the modelmodels = [load_vit_model('B16'), load_vit_model('B32')]
Error Message:
OSError: https://tfhub.dev/google/vit_base_patch16_224/2 does not appear to be a valid module.
Before this error, there are several warnings about deprecation and an indication of a tarfile.ReadError: invalid header, suggesting an issue with downloading or extracting the module.
Environment:
- Python 3.12
- TensorFlow 2.16.1
- TensorFlow Hub
- Windows 10
Attempts:
- Checked for TensorFlow and TensorFlow Hub updates.
- Attempted to clear the TensorFlow Hub cache.
How can I resolve this issue to successfully load the ViT models from TensorFlow Hub?