Skip to content

Incompatible with PyTorch 2.6+: torch.load() default weights_only=True breaks model loading #280

@Xyc2016

Description

@Xyc2016

Description

PyTorch 2.6+ changed the default value of weights_only in torch.load() from False to True. This breaks loading the FastSAM checkpoint because the .pt file contains custom classes (e.g. ultralytics.nn.tasks.SegmentationModel) that are not in the default allowlist.

Error

_pickle.UnpicklingError: Weights only load failed.
WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.SegmentationModel was not an allowed global by default.

Location

ultralytics/nn/tasks.py lines 518 and 533 (two occurrences in torch_safe_load()):

return torch.load(file, map_location='cpu'), file  # load

Suggested Fix

Add weights_only=False to the torch.load() calls:

return torch.load(file, map_location='cpu', weights_only=False), file  # load

Since the checkpoint comes from the official model release (a trusted source), this is safe.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions