Models#
Build a classifier. |
|
Freeze a specific module of the model. |
|
Get a specific layer in a model. |
- saliency_metrics.models.build_classifier(cfg, default_args=None)[source]#
Build a classifier.
This function supports building a classifier from timm, torchvision or custom-defined models.
cfgmust contain the field “type”, which should have the format<scope>.<model_name>. Specifically,scopecan be one oftimm,torchvision, orcustom. When building a custom-defined classifier, the model should be already registered undersaliency_metrics.models.CUSTOM_CLASSIFIERSregistry. When building a classifier fromtorchvisionortimm, themodel_nameshould be the name of corresponding builder function, e.g.,resnet18. I.e.,build_classifier(cfg=dict(type="torchvision.resnet18"))is equivalent to calltorchvision.models.resnet18.- Parameters
- Return type
Module- Returns
The classifier.
Examples
Build a torchvision classifier:
from torchvision.models.resnet import ResNet from saliency_metrics.models import build_classifier cfg_1 = dict(type="torchvision.resnet18", num_classes=2, pretrained=False) model = build_classifier(cfg_1) assert isinstance(model, ResNet)
Build a timm classifier:
from timm.models.efficientnet import EfficientNet from saliency_metrics.models import build_classifier cfg_2 = dict(type="timm.efficientnet_b0", num_classes=2) model = build_classifier(cfg_2) assert isinstance(model, EfficientNet)
Build a custom classifier:
import torch import torch.nn as nn from saliency_metrics.models import CUSTOM_CLASSIFIERS, build_classifier # First register the class @CUSTOM_CLASSIFIERS.register_module() class MLP(nn.Module): def __init__(self, hidden_size: int = 10) -> None: super().__init__() self.layers = nn.Sequential( nn.Linear(10, hidden_size), nn.ReLU(), nn.Linear(hidden_size, 2)) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.layers(x) # Now the instance of the class can be built from a config cfg_3 = dict(type="custom.MLP") model = build_classifier(cfg_3, default_args=dict(hidden_size=5)) assert isinstance(model, MLP)
- saliency_metrics.models.freeze_module(model, module=None, eval_mode=True)[source]#
Freeze a specific module of the model.
This function freezes a specific layer of the model by setting the
requires_gradflag of its parameters to False. It also converts the whole model into evaluation mode, ifeval_modeis True.- Parameters
- Return type
- Returns
None
Examples
from saliency_metrics.models import build_classifier, get_module model_1 = build_classifier(dict(type="timm.resnet18", num_classes=2)) freeze_module(model_1, "fc", eval_mode=True) assert not model_1.training assert not model_1.fc.weight.requires_grad model_2 = build_classifier(dict(type="timm.resnet18", num_classes=2)) freeze_module(model_2, None) for p in model_2.parameters(): assert not p.requires_grad
- saliency_metrics.models.get_module(model, module)[source]#
Get a specific layer in a model.
This function is adapted from TorchRay.
moduleis the name of a module (as given by thenamed_modules()function fortorch.nn.Moduleobjects). The function searches for a module with the namemoduleand returns atorch.nn.Moduleif found; otherwise,Noneis returned.- Parameters
model (
Module) – Model in which to search for layer.module (
str) – Name of layer.
- Return type
Optional[Module]- Returns
Specific
nn.Modulelayer (Noneif the layer isn’t found).
Examples
from saliency_metrics.models import build_classifier, get_module cfg = dict(type="torchvision.resnet18", num_classes=2) model = build_classifier(cfg) # get the last block _ = get_module(model, "layer4.1") # get the last BN layer _ = get_module(model, "layer4.1.bn2")