Using your own model or built-in models¶
Own model¶
RAITAP allows you to use your own model in any of the following supported formats:
.pt/.pthcontaining astate_dict— recommended. Combine withmodel.archandmodel.num_classesto instantiate a torchvision architecture and load weights into it. Portable across environments and torchvision versions..pt/.pthcontaining a TorchScript archive (saved viatorch.jit.save(scripted, path)). Self-contained — noarch/num_classesneeded..pt/.pthcontaining a full pickledtorch.nn.Module(saved viatorch.save(model, path)). Deprecated and refused by default — this format requires unsafe pickle deserialisation, which executes arbitrary code embedded in the file. Opt in only for fully trusted files; see--allow-unsafe-pickle. The pickle also embeds fully-qualified class paths so it breaks when classes are renamed or when torchvision is bumped. Migrate with one line (in a trusted environment):m = torch.load("model.pth", weights_only=False) torch.save(m.state_dict(), "weights.pth")
.onnx.ubj: XGBoost native binary (fittedXGBClassifiersaved viaestimator.save_model("model.ubj")). Requires thexgboostextra:uv sync --extra xgboost. Use withshap.TreeExplainer(also needs--extra shap) or any model-agnostic SHAP explainer.
Set the source option to the path of your model file (see
Configuration). For state-dict loading also set arch and
num_classes:
model:
source: "weights.pth"
arch: "resnet18"
num_classes: 2
from raitap.models import ModelConfig
model = ModelConfig(
source="weights.pth",
arch="resnet18",
num_classes=2,
)
Not all modules and underlying libraries support all formats. See each module's library compatibility page for more details.
Built-in models¶
Alternatively, you can use any model provided by the torchvision.models library. They will be initialised with weights="DEFAULT". You can find the list here.
You simply need to set the source option to the name of the model (see Configuration).