From 802b727ed84a43b3296f3ae82e2c4ba5fb0ce61a Mon Sep 17 00:00:00 2001 From: Kingpython123 Date: Sat, 31 Oct 2026 11:10:53 -0700 Subject: [PATCH] Pass weights_only=False when loading predictor checkpoints torch>=2.6 defaults torch.load to weights_only=True, which cannot unpickle the EasyDict config stored under ckpt['opt'], so every predictor failed to load the released checkpoints. Match training/train.py and training/evaluate.py, and add a regression test. Fixes #79 --- lighthouse/models.py | 5 ++++- tests/test_models.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lighthouse/models.py b/lighthouse/models.py index e57fb1a..89c9ca5 100644 --- a/lighthouse/models.py +++ b/lighthouse/models.py @@ -64,7 +64,10 @@ def __init__( slowfast_path: Optional[str] = None, pann_path: Optional[str] = None) -> None: - ckpt = torch.load(ckpt_path, map_location='cpu') + # Checkpoints store their config as an EasyDict under 'opt', which the + # weights-only unpickler (default since torch 2.6) rejects. Only load + # checkpoints from trusted sources such as the released weights. + ckpt = torch.load(ckpt_path, map_location='cpu', weights_only=False) args = ckpt['opt'] self._clip_len: float = args.clip_length self._device: str = device diff --git a/tests/test_models.py b/tests/test_models.py index af7e271..283a579 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -3,6 +3,10 @@ import pytest import subprocess import random +import torch +import yaml +from easydict import EasyDict +from lighthouse.common.moment_detr import build_model as build_model_moment_detr from lighthouse.models import (MomentDETRPredictor, QDDETRPredictor, EaTRPredictor, CGDETRPredictor, UVCOMPredictor) @@ -141,3 +145,36 @@ def test_audio_model_prediction(): f'The number of moments from {feature}_{model_name}_{dataset} is expected {MOMENT_NUM}, but got {len(prediction["pred_relevant_windows"])}.' assert len(prediction['pred_saliency_scores']) == math.ceil(second / model._clip_len), \ f'The number of saliency scores from {feature}_{model_name}_{dataset} is expected {math.ceil(second / model._clip_len)}, but got {len(prediction["pred_saliency_scores"])}.' + + +def test_load_checkpoint_with_easydict_opt(tmp_path, monkeypatch): + """ + Checkpoints store their config as an EasyDict under 'opt'. torch>=2.6 defaults to + torch.load(weights_only=True), which cannot unpickle it, so the predictor must pass + weights_only=False. The torch.load spy keeps this test meaningful on torch<2.6. + """ + opt = {} + for cfg in ['configs/base.yml', 'configs/feature/clip.yml', + 'configs/model/moment_detr.yml', 'configs/dataset/qvhighlight.yml']: + with open(cfg, 'r') as f: + opt.update(yaml.load(f, Loader=yaml.FullLoader)) + opt = EasyDict(opt) + opt.device = 'cpu' + opt.enc_layers, opt.dec_layers, opt.hidden_dim, opt.dim_feedforward = 1, 1, 32, 64 + + model, _ = build_model_moment_detr(opt) + ckpt_path = tmp_path / 'best.ckpt' + torch.save({'model': model.state_dict(), 'opt': opt}, ckpt_path) + + load_kwargs = [] + original_load = torch.load + + def spy_load(*args, **kwargs): + load_kwargs.append(kwargs) + return original_load(*args, **kwargs) + + monkeypatch.setattr(torch, 'load', spy_load) + predictor = MomentDETRPredictor(str(ckpt_path), device='cpu', feature_name='clip') + + assert load_kwargs[0].get('weights_only') is False + assert predictor._clip_len == opt.clip_length