### Run Training Demo with SimpleNet Source: https://github.com/donaldrr/simplenet/blob/main/README.md Execute the main training script for SimpleNet. Ensure dataset path and log folder are specified in run.sh. ```bash bash run.sh ``` -------------------------------- ### Initialize and Configure SimpleNet Model Source: https://context7.com/donaldrr/simplenet/llms.txt Initializes the SimpleNet model, loads a pretrained backbone, and configures model parameters. Ensure the device is set correctly and model directory is specified for saving checkpoints. ```python import torch import simplenet import backbones # Initialize SimpleNet device = torch.device("cuda:0") model = simplenet.SimpleNet(device) # Load a pretrained backbone (WideResNet50) backbone = backbones.load("wideresnet50") backbone.name = "wideresnet50" backbone.seed = None # Configure and load the model model.load( backbone=backbone, layers_to_extract_from=["layer2", "layer3"], device=device, input_shape=(3, 288, 288), pretrain_embed_dimension=1536, target_embed_dimension=1536, patchsize=3, embedding_size=256, meta_epochs=40, aed_meta_epochs=1, gan_epochs=4, noise_std=0.015, dsc_layers=2, dsc_hidden=1024, dsc_margin=0.5, dsc_lr=0.0002, train_backbone=False, cos_lr=True, pre_proj=1, proj_layer_type=0, mix_noise=1 ) # Set model directory for checkpoints and tensorboard logs model.set_model_dir("./models/0", "mvtec_bottle") ``` -------------------------------- ### Full Training Command for MVTec-AD Dataset Source: https://context7.com/donaldrr/simplenet/llms.txt This command demonstrates the complete configuration for training SimpleNet on the MVTec-AD dataset using a specific GPU and logging parameters. It includes network and dataset configurations. ```bash # Full training command for MVTec-AD dataset datapath=/data/MVTec_ad datasets=('screw' 'pill' 'capsule' 'carpet' 'grid' 'tile' 'wood' 'zipper' 'cable' 'toothbrush' 'transistor' 'metal_nut' 'bottle' 'hazelnut' 'leather') python3 main.py \ --gpu 0 \ --seed 0 \ --log_group simplenet_mvtec \ --log_project MVTecAD_Results \ --results_path results \ --run_name run \ net \ -b wideresnet50 \ -le layer2 \ -le layer3 \ --pretrain_embed_dimension 1536 \ --target_embed_dimension 1536 \ --patchsize 3 \ --meta_epochs 40 \ --embedding_size 256 \ --gan_epochs 4 \ --noise_std 0.015 \ --dsc_hidden 1024 \ --dsc_layers 2 \ --dsc_margin 0.5 \ --pre_proj 1 \ dataset \ --batch_size 8 \ --resize 329 \ --imagesize 288 \ -d bottle \ mvtec $datapath ``` -------------------------------- ### SimpleNet Initialization Source: https://context7.com/donaldrr/simplenet/llms.txt Initializes the SimpleNet model and configures the backbone and training hyperparameters. ```APIDOC ## SimpleNet.load ### Description Configures and loads the SimpleNet model with a specified backbone and training parameters. ### Parameters - **backbone** (object) - Required - Pretrained backbone network. - **layers_to_extract_from** (list) - Required - List of layer names to extract features from. - **device** (torch.device) - Required - Device to run the model on. - **input_shape** (tuple) - Required - Input image dimensions (C, H, W). - **pretrain_embed_dimension** (int) - Required - Embedding dimension of the backbone. - **target_embed_dimension** (int) - Required - Target embedding dimension. - **patchsize** (int) - Required - Patch size for feature processing. - **embedding_size** (int) - Required - Size of the embedding space. - **meta_epochs** (int) - Required - Number of meta-epochs for training. - **aed_meta_epochs** (int) - Required - AED meta-epochs. - **gan_epochs** (int) - Required - Number of GAN training epochs. - **noise_std** (float) - Required - Standard deviation for Gaussian noise. - **dsc_layers** (int) - Required - Number of discriminator layers. - **dsc_hidden** (int) - Required - Hidden dimension of the discriminator. - **dsc_margin** (float) - Required - Margin for the discriminator. - **dsc_lr** (float) - Required - Learning rate for the discriminator. - **train_backbone** (bool) - Required - Whether to train the backbone. - **cos_lr** (bool) - Required - Use cosine learning rate scheduler. - **pre_proj** (int) - Required - Pre-projection setting. - **proj_layer_type** (int) - Required - Type of projection layer. - **mix_noise** (int) - Required - Mix noise setting. ``` -------------------------------- ### Utility Functions for Reproducibility and Storage Source: https://context7.com/donaldrr/simplenet/llms.txt Demonstrates the use of utility functions for setting random seeds, configuring the PyTorch device, creating storage folders, and computing/storing final results. ```python from utils import fix_seeds, set_torch_device, create_storage_folder, compute_and_store_final_results # Set seeds for reproducibility fix_seeds(seed=42, with_torch=True, with_cuda=True) # Configure GPU device device = set_torch_device(gpu_ids=[0]) # Returns torch.device("cuda:0") device_cpu = set_torch_device(gpu_ids=[]) # Returns torch.device("cpu") # Create organized storage folders save_path = create_storage_folder( main_folder_path="./results", project_folder="MVTecAD", group_folder="simplenet", run_name="experiment_1", mode="overwrite" # or "iterate" for auto-incrementing ) print(f"Results will be saved to: {save_path}") # Output: Results will be saved to: ./results/MVTecAD/simplenet/experiment_1 # Store final results as CSV results = [ [0.995, 0.982, 0.943], # bottle [0.987, 0.975, 0.921], # cable [0.992, 0.968, 0.912], # capsule ] row_names = ["bottle", "cable", "capsule"] column_names = ["Instance AUROC", "Full Pixel AUROC", "PRO"] mean_metrics = compute_and_store_final_results( results_path=save_path, results=results, row_names=row_names, column_names=column_names ) print(f"Mean metrics: {mean_metrics}") # Output: Mean metrics: {'mean_Instance AUROC': 0.991, 'mean_Full Pixel AUROC': 0.975, 'mean_PRO': 0.925} ``` -------------------------------- ### Train on a Single Category Source: https://context7.com/donaldrr/simplenet/llms.txt This command trains SimpleNet on a single category ('bottle') from the MVTec-AD dataset, simplifying the configuration for testing or focused training. ```bash # Train on a single category python3 main.py --gpu 0 --seed 0 --results_path results --run_name bottle_test \ net -b wideresnet50 -le layer2 -le layer3 --meta_epochs 40 \ dataset --batch_size 8 --resize 329 --imagesize 288 -d bottle mvtec /data/MVTec_ad ``` -------------------------------- ### Load Pretrained Feature Extractors with backbones.load() Source: https://context7.com/donaldrr/simplenet/llms.txt Use `backbones.load()` to load various pretrained CNN and Vision Transformer models. Supported architectures include ResNet, WideResNet, EfficientNet, ViT, Swin Transformer, and DenseNet. ```python import backbones # Load WideResNet50 (recommended for anomaly detection) backbone = backbones.load("wideresnet50") # Load other supported backbones resnet50 = backbones.load("resnet50") efficientnet_b5 = backbones.load("efficientnet_b5") vit_base = backbones.load("vit_base") swin_large = backbones.load("vit_swin_large") densenet201 = backbones.load("densenet201") # Available backbone names: # CNN: resnet18, resnet50, resnet101, wideresnet50, wideresnet101 # EfficientNet: efficientnet_b1, efficientnet_b3, efficientnet_b5, efficientnet_b7 # ViT: vit_small, vit_base, vit_large, vit_swin_base, vit_swin_large # Others: densenet121, densenet201, vgg19, alexnet ``` -------------------------------- ### MVTecDataset - Dataset Loader for MVTec-AD Benchmark Source: https://context7.com/donaldrr/simplenet/llms.txt A PyTorch Dataset implementation for the MVTec Anomaly Detection dataset. Supports train/val/test splits, data augmentation, and automatic loading of ground truth segmentation masks. ```APIDOC ## MVTecDataset - Dataset Loader for MVTec-AD Benchmark ### Description Provides a PyTorch Dataset implementation for the MVTec Anomaly Detection dataset. It supports train/val/test splits, data augmentation, and automatic loading of ground truth segmentation masks for test images. ### Method `MVTecDataset(source: str, classname: str, resize: int, imagesize: int, split: DatasetSplit, train_val_split: float = 0.9, rotate_degrees: float = 0, translate: float = 0, brightness_factor: float = 0, contrast_factor: float = 0, h_flip_p: float = 0, v_flip_p: float = 0, scale: float = 0)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **source** (str) - Required - Path to the MVTec AD dataset directory. - **classname** (str) - Required - The specific class to load (e.g., 'carpet', 'bottle'). - **resize** (int) - Required - The size to resize images to before further processing. - **imagesize** (int) - Required - The final image size after cropping or resizing. - **split** (DatasetSplit) - Required - Specifies the dataset split (TRAIN, VAL, TEST). - **train_val_split** (float) - Optional - The proportion of the training data to use for validation (default: 0.9). - **rotate_degrees** (float) - Optional - Maximum degrees for random rotation augmentation (default: 0). - **translate** (float) - Optional - Maximum translation factor for augmentation (default: 0). - **brightness_factor** (float) - Optional - Factor for random brightness augmentation (default: 0). - **contrast_factor** (float) - Optional - Factor for random contrast augmentation (default: 0). - **h_flip_p** (float) - Optional - Probability of horizontal flip augmentation (default: 0). - **v_flip_p** (float) - Optional - Probability of vertical flip augmentation (default: 0). - **scale** (float) - Optional - Factor for random scaling augmentation (default: 0). ### Request Example ```python from datasets.mvtec import MVTecDataset, DatasetSplit from torch.utils.data import DataLoader # Create training dataset with data augmentation train_dataset = MVTecDataset( source="/data/MVTec_ad", classname="carpet", resize=329, imagesize=288, split=DatasetSplit.TRAIN, train_val_split=0.9, rotate_degrees=10, translate=0.1, brightness_factor=0.1, contrast_factor=0.1, h_flip_p=0.5, v_flip_p=0.5, scale=0.1 ) # Create test dataset test_dataset = MVTecDataset( source="/data/MVTec_ad", classname="carpet", resize=329, imagesize=288, split=DatasetSplit.TEST ) # Access a sample sample = train_dataset[0] print(f"Image shape: {sample['image'].shape}") print(f"Mask shape: {sample['mask'].shape}") print(f"Class: {sample['classname']}") print(f"Anomaly type: {sample['anomaly']}") print(f"Is anomaly: {sample['is_anomaly']}") ``` ### Response #### Success Response (200) Returns a dictionary containing: - **image** (torch.Tensor): The augmented or original image tensor. - **mask** (torch.Tensor): The ground truth anomaly mask tensor. - **classname** (str): The name of the object class. - **anomaly** (str): The type of anomaly ('good' or defect type). - **is_anomaly** (int): Binary indicator (1 if anomaly, 0 if normal). #### Response Example ```json { "image": "torch.Tensor of shape [3, 288, 288]", "mask": "torch.Tensor of shape [1, 288, 288]", "classname": "carpet", "anomaly": "good", "is_anomaly": 0 } ``` ``` -------------------------------- ### backbones.load() - Load Pretrained Feature Extractors Source: https://context7.com/donaldrr/simplenet/llms.txt Loads various pretrained CNN and Vision Transformer models for feature extraction. Supported architectures include ResNet variants, WideResNet, EfficientNet, ViT, Swin Transformer, and DenseNet. ```APIDOC ## backbones.load() ### Description Loads various pretrained CNN and Vision Transformer models for feature extraction. Supported architectures include ResNet variants, WideResNet, EfficientNet, ViT, Swin Transformer, and DenseNet. ### Method `backbones.load(name: str)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import backbones # Load WideResNet50 (recommended for anomaly detection) backbone = backbones.load("wideresnet50") # Load other supported backbones resnet50 = backbones.load("resnet50") efficientnet_b5 = backbones.load("efficientnet_b5") vit_base = backbones.load("vit_base") swin_large = backbones.load("vit_swin_large") densenet201 = backbones.load("densenet201") ``` ### Response #### Success Response (200) Returns a pretrained model object. #### Response Example ```python # Example response is the loaded model object itself ``` ### Available backbone names: - CNN: resnet18, resnet50, resnet101, wideresnet50, wideresnet101 - EfficientNet: efficientnet_b1, efficientnet_b3, efficientnet_b5, efficientnet_b7 - ViT: vit_small, vit_base, vit_large, vit_swin_base, vit_swin_large - Others: densenet121, densenet201, vgg19, alexnet ``` -------------------------------- ### SimpleNet.train() Source: https://context7.com/donaldrr/simplenet/llms.txt Trains the anomaly detector discriminator on normal images and evaluates performance on test data. ```APIDOC ## SimpleNet.train ### Description Trains the discriminator on normal images and returns evaluation metrics. ### Parameters - **train_loader** (DataLoader) - Required - DataLoader for training data. - **test_loader** (DataLoader) - Required - DataLoader for test data. ### Response - **image_auroc** (float) - Image-level detection AUROC score. - **pixel_auroc** (float) - Pixel-level detection AUROC score. - **pro_auroc** (float) - PRO metric score. ``` -------------------------------- ### SimpleNet Citation Information Source: https://github.com/donaldrr/simplenet/blob/main/README.md Bibliographic details for citing the SimpleNet paper. ```bibtex @inproceedings{liu2023simplenet, title={SimpleNet: A Simple Network for Image Anomaly Detection and Localization}, author={Liu, Zhikang and Zhou, Yiming and Xu, Yuansheng and Wang, Zilei}, booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition}, pages={20402--20411}, year={2023} } ``` -------------------------------- ### Load MVTec AD Dataset with MVTecDataset Source: https://context7.com/donaldrr/simplenet/llms.txt The `MVTecDataset` class is a PyTorch Dataset for the MVTec Anomaly Detection dataset. It supports train/val/test splits, data augmentation, and automatic loading of ground truth segmentation masks. ```python from datasets.mvtec import MVTecDataset, DatasetSplit from torch.utils.data import DataLoader # Create training dataset with data augmentation train_dataset = MVTecDataset( source="/data/MVTec_ad", classname="carpet", # or: bottle, cable, capsule, grid, hazelnut, leather, etc. resize=329, imagesize=288, split=DatasetSplit.TRAIN, train_val_split=0.9, # Use 90% for training rotate_degrees=10, translate=0.1, brightness_factor=0.1, contrast_factor=0.1, h_flip_p=0.5, v_flip_p=0.5, scale=0.1 ) # Create test dataset test_dataset = MVTecDataset( source="/data/MVTec_ad", classname="carpet", resize=329, imagesize=288, split=DatasetSplit.TEST ) # Access a sample sample = train_dataset[0] print(f"Image shape: {sample['image'].shape}") # torch.Size([3, 288, 288]) print(f"Mask shape: {sample['mask'].shape}") # torch.Size([1, 288, 288]) print(f"Class: {sample['classname']}") # 'carpet' print(f"Anomaly type: {sample['anomaly']}") # 'good' or defect type print(f"Is anomaly: {sample['is_anomaly']}") # 0 or 1 ``` -------------------------------- ### metrics.compute_imagewise_retrieval_metrics() - Image-Level Evaluation Source: https://context7.com/donaldrr/simplenet/llms.txt Computes image-level anomaly detection metrics including AUROC, FPR, TPR, and optimal thresholds. Takes anomaly prediction scores and ground truth binary labels as input. ```APIDOC ## metrics.compute_imagewise_retrieval_metrics() ### Description Computes image-level anomaly detection metrics including AUROC, FPR, TPR, and optimal thresholds. Takes anomaly prediction scores and ground truth binary labels as input. ### Method `compute_imagewise_retrieval_metrics(prediction_scores: np.ndarray, ground_truth_labels: np.ndarray)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **prediction_scores** (np.ndarray) - Required - An array of anomaly prediction scores (higher score indicates more anomalous). - **ground_truth_labels** (np.ndarray) - Required - An array of binary ground truth labels (0 for normal, 1 for anomaly). ### Request Example ```python import numpy as np from metrics import compute_imagewise_retrieval_metrics # Example predictions (higher score = more anomalous) prediction_scores = np.array([0.1, 0.2, 0.15, 0.9, 0.85, 0.95]) ground_truth_labels = np.array([0, 0, 0, 1, 1, 1]) # 0=normal, 1=anomaly results = compute_imagewise_retrieval_metrics(prediction_scores, ground_truth_labels) print(f"AUROC: {results['auroc']:.4f}") print(f"FPR curve length: {len(results['fpr'])}") print(f"TPR curve length: {len(results['tpr'])}") print(f"Thresholds: {results['threshold'][:3]}...") ``` ### Response #### Success Response (200) Returns a dictionary containing: - **auroc** (float): Area Under the Receiver Operating Characteristic Curve. - **fpr** (list): List of False Positive Rates. - **tpr** (list): List of True Positive Rates. - **threshold** (list): List of corresponding thresholds. #### Response Example ```json { "auroc": 1.0, "fpr": [0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0], "tpr": [0.0, 0.33, 0.67, 0.67, 1.0, 1.0, 1.0], "threshold": [1.0, 0.95, 0.9, 0.85, 0.2, 0.15, 0.1] } ``` ``` -------------------------------- ### Compute Per-Region Overlap (PRO) Metric Source: https://context7.com/donaldrr/simplenet/llms.txt Calculates the Per-Region Overlap (PRO) metric for anomaly localization, measuring overlap on a per-connected-component basis. This metric is more robust for small anomaly regions. ```python import numpy as np from metrics import compute_pro ``` -------------------------------- ### Compute Image-wise Retrieval Metrics Source: https://context7.com/donaldrr/simplenet/llms.txt Computes image-level anomaly detection metrics including AUROC, FPR, TPR, and optimal thresholds. Requires anomaly prediction scores and ground truth binary labels. ```python import numpy as np from metrics import compute_imagewise_retrieval_metrics # Example predictions (higher score = more anomalous) prediction_scores = np.array([0.1, 0.2, 0.15, 0.9, 0.85, 0.95]) ground_truth_labels = np.array([0, 0, 0, 1, 1, 1]) # 0=normal, 1=anomaly results = compute_imagewise_retrieval_metrics(prediction_scores, ground_truth_labels) print(f"AUROC: {results['auroc']:.4f}") print(f"FPR curve length: {len(results['fpr'])}") print(f"TPR curve length: {len(results['tpr'])}") print(f"Thresholds: {results['threshold'][:3]}...") # Output: # AUROC: 1.0000 # FPR curve length: 7 # TPR curve length: 7 # Thresholds: [1.95 0.95 0.9]... ``` -------------------------------- ### metrics.compute_pixelwise_retrieval_metrics() - Pixel-Level Evaluation Source: https://context7.com/donaldrr/simplenet/llms.txt Computes pixel-wise anomaly localization metrics for evaluating segmentation quality. Returns AUROC, FPR, TPR curves, and optimal thresholds for pixel-level predictions against ground truth masks. ```APIDOC ## metrics.compute_pixelwise_retrieval_metrics() ### Description Computes pixel-wise anomaly localization metrics for evaluating segmentation quality. Returns AUROC, FPR, TPR curves, and optimal thresholds for pixel-level predictions against ground truth masks. ### Method `compute_pixelwise_retrieval_metrics(anomaly_segmentations: np.ndarray, ground_truth_masks: np.ndarray)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **anomaly_segmentations** (np.ndarray) - Required - A numpy array of predicted anomaly heatmaps or segmentation masks. - **ground_truth_masks** (np.ndarray) - Required - A numpy array of ground truth binary masks. ### Request Example ```python import numpy as np from metrics import compute_pixelwise_retrieval_metrics # Example: batch of 2 images with 64x64 anomaly maps and masks anomaly_segmentations = np.random.rand(2, 64, 64) # Predicted heatmaps ground_truth_masks = np.zeros((2, 64, 64)) ground_truth_masks[0, 20:40, 20:40] = 1 # Anomaly region in first image ground_truth_masks[1, 10:30, 30:50] = 1 # Anomaly region in second image results = compute_pixelwise_retrieval_metrics(anomaly_segmentations, ground_truth_masks) print(f"Pixel AUROC: {results['auroc']:.4f}") print(f"Optimal threshold: {results['optimal_threshold']:.4f}") print(f"Optimal FPR: {results['optimal_fpr']:.4f}") print(f"Optimal FNR: {results['optimal_fnr']:.4f}") ``` ### Response #### Success Response (200) Returns a dictionary containing: - **auroc** (float): Pixel-wise Area Under the ROC Curve. - **optimal_threshold** (float): The threshold that optimizes a certain metric (e.g., balances FPR and FNR). - **optimal_fpr** (float): False Positive Rate at the optimal threshold. - **optimal_fnr** (float): False Negative Rate at the optimal threshold. #### Response Example ```json { "auroc": 0.95, "optimal_threshold": 0.5, "optimal_fpr": 0.1, "optimal_fnr": 0.05 } ``` ``` -------------------------------- ### Inference with SimpleNet.predict() Source: https://context7.com/donaldrr/simplenet/llms.txt Performs inference on images or dataloaders to obtain anomaly scores and segmentation masks. The `predict` method returns aggregated results, while `_predict` processes a single batch. ```python import numpy as np # Predict on a dataloader scores, segmentations, features, labels_gt, masks_gt = model.predict(test_loader) # scores: list of anomaly scores per image (higher = more anomalous) # segmentations: list of [H, W] anomaly heatmaps # labels_gt: list of ground truth labels (0=normal, 1=anomaly) # masks_gt: list of ground truth segmentation masks # Process a single batch of images batch = next(iter(test_loader)) images = batch["image"] # [B, 3, 288, 288] image_scores, masks, feats = model._predict(images) for i, (score, mask) in enumerate(zip(image_scores, masks)): print(f"Image {i}: anomaly_score={score:.4f}, mask_shape={mask.shape}") # Output: # Image 0: anomaly_score=0.1234, mask_shape=(288, 288) # Image 1: anomaly_score=0.9876, mask_shape=(288, 288) ``` -------------------------------- ### Train SimpleNet Anomaly Detector Source: https://context7.com/donaldrr/simplenet/llms.txt Trains the discriminator using normal images and evaluates on test data. Requires PyTorch DataLoaders for training and testing datasets. Returns AUROC scores for image-level, pixel-level detection, and PRO metric. ```python from torch.utils.data import DataLoader from datasets.mvtec import MVTecDataset, DatasetSplit # Create training dataset (normal images only) train_dataset = MVTecDataset( source="/data/MVTec_ad", classname="bottle", resize=329, imagesize=288, split=DatasetSplit.TRAIN, seed=0 ) # Create test dataset (normal + anomaly images with masks) test_dataset = MVTecDataset( source="/data/MVTec_ad", classname="bottle", resize=329, imagesize=288, split=DatasetSplit.TEST, seed=0 ) train_loader = DataLoader(train_dataset, batch_size=8, shuffle=True, num_workers=2) test_loader = DataLoader(test_dataset, batch_size=8, shuffle=False, num_workers=2) # Train the model and get evaluation metrics image_auroc, pixel_auroc, pro_auroc = model.train(train_loader, test_loader) print(f"Image-level AUROC: {image_auroc:.4f}") print(f"Pixel-level AUROC: {pixel_auroc:.4f}") print(f"PRO AUROC: {pro_auroc:.4f}") # Output: # Image-level AUROC: 0.9980 # Pixel-level AUROC: 0.9850 # PRO AUROC: 0.9420 ``` -------------------------------- ### Compute Pixel-wise Retrieval Metrics Source: https://context7.com/donaldrr/simplenet/llms.txt Computes pixel-wise anomaly localization metrics (AUROC, FPR, TPR, optimal thresholds) for evaluating segmentation quality against ground truth masks. ```python import numpy as np from metrics import compute_pixelwise_retrieval_metrics # Example: batch of 2 images with 64x64 anomaly maps and masks anomaly_segmentations = np.random.rand(2, 64, 64) # Predicted heatmaps ground_truth_masks = np.zeros((2, 64, 64)) ground_truth_masks[0, 20:40, 20:40] = 1 # Anomaly region in first image ground_truth_masks[1, 10:30, 30:50] = 1 # Anomaly region in second image results = compute_pixelwise_retrieval_metrics(anomaly_segmentations, ground_truth_masks) print(f"Pixel AUROC: {results['auroc']:.4f}") print(f"Optimal threshold: {results['optimal_threshold']:.4f}") print(f"Optimal FPR: {results['optimal_fpr']:.4f}") print(f"Optimal FNR: {results['optimal_fnr']:.4f}") ``` -------------------------------- ### metrics.compute_pro() - Per-Region Overlap Metric Source: https://context7.com/donaldrr/simplenet/llms.txt Computes the Per-Region Overlap (PRO) metric, which evaluates anomaly localization quality by measuring overlap between predictions and ground truth on a per-connected-component basis. This metric is more robust for small anomaly regions. ```APIDOC ## metrics.compute_pro() ### Description Computes the Per-Region Overlap (PRO) metric which evaluates anomaly localization quality by measuring overlap between predictions and ground truth on a per-connected-component basis. This metric is more robust for small anomaly regions. ### Method `compute_pro(anomaly_segmentations: np.ndarray, ground_truth_masks: np.ndarray)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **anomaly_segmentations** (np.ndarray) - Required - A numpy array of predicted anomaly heatmaps or segmentation masks. - **ground_truth_masks** (np.ndarray) - Required - A numpy array of ground truth binary masks. ### Request Example ```python import numpy as np from metrics import compute_pro # Example usage (assuming anomaly_segmentations and ground_truth_masks are defined) # anomaly_segmentations = ... # ground_truth_masks = ... # pro_score = compute_pro(anomaly_segmentations, ground_truth_masks) ``` ### Response #### Success Response (200) Returns the computed PRO score (float). #### Response Example ```json { "pro_score": 0.85 } ``` ``` -------------------------------- ### SimpleNet.predict() Source: https://context7.com/donaldrr/simplenet/llms.txt Performs inference to generate anomaly scores and segmentation masks. ```APIDOC ## SimpleNet.predict ### Description Runs inference on a dataloader to return anomaly scores, segmentation masks, features, and ground truth data. ### Parameters - **test_loader** (DataLoader) - Required - DataLoader containing test images. ### Response - **scores** (list) - Anomaly scores per image. - **segmentations** (list) - Anomaly heatmaps. - **features** (list) - Extracted features. - **labels_gt** (list) - Ground truth labels. - **masks_gt** (list) - Ground truth segmentation masks. ``` -------------------------------- ### Compute PRO Score Source: https://context7.com/donaldrr/simplenet/llms.txt Calculates the PRO score using ground truth masks and anomaly maps. Ensure masks and amaps are NumPy arrays of the same shape. ```python masks = np.zeros((10, 256, 256)) masks[:5, 50:100, 50:100] = 1 # 5 images with anomalies amaps = np.random.rand(10, 256, 256) * 0.3 # Background predictions amaps[:5, 45:105, 45:105] = np.random.rand(5, 60, 60) * 0.7 + 0.3 # Higher in anomaly regions pro_score = compute_pro(masks, amaps, num_th=200) print(f"PRO Score: {pro_score:.4f}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.