How do I load a local model with torch.hub.load?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
torch.hub.load can load from a local directory as well as from GitHub. To make that work, the directory must behave like a Torch Hub repository: it needs a hubconf.py file that exposes callable entry points.
The Minimum Local Repository Shape
A small local hub repository usually looks like this:
The important file is hubconf.py. That is where Torch Hub looks for the named functions you want to load.
Define an Entry Point in hubconf.py
A simple local entry point can construct the model and optionally load weights.
Each public function defined there becomes a load target.
Load the Model From a Local Path
Use the local repository path and set source="local".
That tells Torch Hub not to resolve a GitHub repo. It should treat the first argument as a local directory.
Use torch.hub.list to Check the Entry Points
If loading fails because the function name is wrong or not exported the way you expect, inspect the available hub entry points.
If your expected function is not listed, the problem is usually in hubconf.py, not in the checkpoint.
Know When torch.hub.load Is the Right Tool
Use Torch Hub when you want a stable constructor-style API for models. It is especially useful when several scripts or teams should load models through the same public entry points.
If you already control the codebase and only need to restore weights inside one project, plain torch.load plus normal model construction may be simpler.
Torch Hub is an interface layer, not just a serialization shortcut.
Keep Code and Weights Compatible
Most local-loading failures are compatibility problems:
- the checkpoint was saved from an older architecture version
- the repository path is wrong
- '
hubconf.pyimports the wrong module names' - the checkpoint expects GPU tensors but the current environment loads on CPU
That is why a small smoke test after loading is worth keeping near the model package.
Keep the Entry Point Small and Stable
A good hubconf.py entry point should stay thin. It should mostly construct the model, load weights if requested, and return a ready-to-use object. If you hide too much environment-specific logic inside the entry point, local hub loading becomes harder to reuse and harder to debug.
Treat the entry point as a public loading API for the repository, not as a place for training-time assumptions.
Smoke-Test the Loaded Model Immediately
After loading, run one tiny forward pass or at least inspect the model summary. This catches shape mismatches, missing keys, and bad checkpoint paths at load time instead of much later in an inference job.
Common Pitfalls
The biggest mistake is forgetting source="local", which makes Torch Hub assume a remote GitHub source.
Another issue is pointing at a directory without a valid hubconf.py file.
A third problem is loading weights into a model definition that has changed since the checkpoint was created.
Summary
- '
torch.hub.loadsupports local directories whensource="local"is used.' - The directory must contain a
hubconf.pyfile with loadable entry points. - Pass checkpoint paths explicitly when the entry point expects them.
- Use
torch.hub.list(..., source="local")to inspect available local functions. - Keep the repository code and checkpoint files aligned so loading stays reproducible.
Related reading
- How do I load custom image based datasets into Pytorch for use with a CNN?
- How do I make the initial state of an LSTM trainable in Keras?
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I print the model summary in PyTorch?
- How do I print the model summary in PyTorch?
- How do I make a ragged batch in Tensorflow 2.0?
- How do I make a U-matrix?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.