File size: 577 Bytes
82e11f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from pathlib import Path
from typing import Any, cast

import torch
from torch import nn


def load_checkpoint_state(path: Path) -> dict[str, torch.Tensor]:
    checkpoint = cast(
        dict[str, Any],
        torch.load(path, map_location="cpu", weights_only=True),
    )
    return cast(dict[str, torch.Tensor], checkpoint["state_dict"])


def load_model_checkpoint(model: nn.Module, path: Path) -> None:
    state_dict = {name.removeprefix("model."): parameter for name, parameter in load_checkpoint_state(path).items()}
    model.load_state_dict(state_dict, strict=True)