ultralytics/yolov5 · error · RuntimeError

{e}. Cache may be out of date, try `force_reload=True` or se

Error message

{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help.

What it means

hubconf.py wraps every exception raised while downloading or building a torch.hub model into a RuntimeError that suggests force_reload=True. The underlying error {e} can be an HTTP failure, a corrupt cached repo, a KeyError while reading the checkpoint, or an import error inside the hub repo code; the message points at a stale ~/.cache/torch/hub checkout as the most common cause.

Source

Thrown at hubconf.py:103

                        model = AutoShape(model)  # for file/URI/PIL/cv2/np inputs and NMS
            except Exception:
                model = attempt_load(path, device=device, fuse=False)  # arbitrary model
        else:
            cfg = next(iter((Path(__file__).parent / "models").rglob(f"{path.stem}.yaml")))  # model.yaml path
            model = DetectionModel(cfg, channels, classes)  # create model
            if pretrained:
                ckpt = torch_load(attempt_download(path), map_location=device)  # load
                csd = ckpt["model"].float().state_dict()  # checkpoint state_dict as FP32
                csd = intersect_dicts(csd, model.state_dict(), exclude=["anchors"])  # intersect
                model.load_state_dict(csd, strict=False)  # load
                if len(ckpt["model"].names) == classes:
                    model.names = ckpt["model"].names  # set class names attribute
        return model.to(device)

    except Exception as e:
        help_url = "https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading"
        s = f"{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help."
        raise RuntimeError(s) from e

    finally:
        LOGGER.setLevel(prev_level)  # restore on both paths, LOGGER is shared with ultralytics


def custom(path="path/to/model.pt", autoshape=True, _verbose=True, device=None):
    """Loads a custom or local YOLOv5 model from a given path with optional autoshaping and device specification.

    Args:
        path (str): Path to the custom model file (e.g., 'path/to/model.pt').
        autoshape (bool): Apply YOLOv5 .autoshape() wrapper to model if True, enabling compatibility with various input
            types (default is True).
        _verbose (bool): If True, prints all informational messages to the screen; otherwise, operates silently (default
            is True).
        device (str | torch.device | None): Device to load the model on, e.g., 'cpu', 'cuda', torch.device('cuda:0'),
            etc. (default is None, which automatically selects the best available device).

    Returns:

View on GitHub (pinned to 20d1d78a08)

Solutions

  1. Retry with force_reload: torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True).
  2. Clear the hub cache: rm -rf ~/.cache/torch/hub/ultralytics_yolov5* and retry.
  3. Read the original exception in the traceback ({e}) to distinguish network vs. code failure; check connectivity to github.com and the release assets.
  4. For pin-point reproducibility, bypass hub entirely and load the local repo: sys.path.insert(0, repo); import hubconf; hubconf.custom('yolov5s.pt').

Example fix

# before
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# after
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
Defensive patterns

Strategy: retry

Validate before calling

import torch

def hub_model_available(repo: str, name: str) -> bool:
    # cheap network probe before the heavyweight load
    import requests
    return requests.head(f"https://github.com/{repo}", timeout=10, allow_redirects=True).status_code == 200

Try / catch

try:
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
except RuntimeError as e:
    if 'Cache may be out of date' in str(e):
        model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)  # one retry

Prevention

When it happens

Trigger: Calling torch.hub.load('ultralytics/yolov5', 'yolov5s') when the cached repo in ~/.cache/torch/hub is from an older commit whose code no longer matches the downloaded weights; network interruption mid-download; force_reload=False after the upstream repo changed its API.

Common situations: Long-lived Docker images or CI caches holding an old hub checkout; corporate proxies returning HTML error pages instead of weights; running offline after a partial first attempt; switching between yolov5 pip package and hub code paths.

Related errors


AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15). Data as JSON: /api/errors/ce46af300d558324. Report an issue: GitHub.