padim - OpenAOI/anodet GitHub Wiki
Provides classes and functions for working with PaDiM.
class Padim()
A padim model with functions to train and perform inference.
| __init__(backbone: str = 'resnet18', device: torch.device = torch.device('cpu'), mean: Optional[torch.Tensor] = None, cov_inv: Optional[torch.Tensor] = None, channel_indices: Optional[torch.Tensor] = None, layer_indices: Optional[List[int]] = None, layer_hook: Optional[Callable[[torch.Tensor], torch.Tensor]] = None) -> None
Construct the model and initialize the attributes
Arguments:
-
backbone
- The name of the desired backbone. Must be one of: [resnet18, wide_resnet50]. -
device
- The device where to run the model. -
mean
- A tensor with the mean vectors of each patch with size (D, H, W),
where D is the number of channel_indices. -
cov_inv
- A tensor with the inverse of the covariance matrices of each patch
with size (D, D, H, W), where D is the number of channel_indices. -
channel_indices
- A tensor with the desired channel indices to extract
from the backbone, with size (D). -
layer_indices
- A list with the desired layers to extract from the backbone,
allowed indices are 1, 2, 3 and 4. -
layer_hook
- A function that can modify the layers during extraction.
| to_device(device: torch.device) -> None
Perform device conversion on backone, mean, cov_inv and channel_indices
Arguments:
-
device
- The device where to run the model.
| fit(dataloader: torch.utils.data.DataLoader) -> None
Fit the model (i.e. mean and cov_inv) to data.
Arguments:
-
dataloader
- A pytorch dataloader, with sample dimensions (B, D, H, W),
containing normal images.
| predict(batch: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]
Make a prediction on test images.
Arguments:
-
batch
- A batch of test images, with dimension (B, D, h, w).
Returns:
-
image_scores
- A tensor with the image level scores, with dimension (B). -
score_map
- A tensor with the patch level scores, with dimension (B, H, W)
| evaluate(dataloader: torch.utils.data.DataLoader) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]
Run predict on all images in a dataloader and return the results.
Arguments:
-
dataloader
- A pytorch dataloader, with sample dimensions (B, D, H, W),
containing normal images.
Returns:
-
images
- An array containing all input images. -
image_classifications_target
- An array containing the target
classifications on image level. -
masks_target
- An array containing the target classifications on patch level. -
image_scores
- An array containing the predicted scores on image level. -
score_maps
- An array containing the predicted scores on patch level.