Skip to content

torch.load(weights_only=True) gives cryptic error for .pth files containing numpy metadata #482

@oeway

Description

@oeway

When loading PyTorch state dict weights, bioimageio.core uses torch.load(weights_only=True). If the .pth file was saved with extra metadata (e.g. numpy arrays for normalization stats, as done by deepflash2), this raises a cryptic error that does not explain the fix.

Suggested fix: Add a try-except that catches the error and prints a helpful message like: "Your weights file appears to contain non-tensor objects (e.g. numpy arrays). Save only the state dict: torch.save(checkpoint["model"], "weights.pt")"

Steps to reproduce:

  1. Download a deepflash2 model (e.g. matjesg/cFOS_in_HC on HuggingFace)
  2. Load with bioimageio.core — fails with cryptic torch.load error
  3. Fix: extract state dict with weights_only=False then save clean

cc @FynnBe

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