SimpleNet Documentation

repository·main·Indexed 20 days ago

https://github.com/donaldrr/simplenet

A lightweight PyTorch-based framework for image anomaly detection and localization. SimpleNet utilizes a feature encoder, generator, and discriminator to identify defects. It supports the MvTecAD dataset and provides a chained Click CLI for configuring network architecture (via the `net` command), dataset parameters (via the `dataset` command), and global execution settings (via the `main` command).

Tokens
1.2K
Snippets
3
Records
8
Agent score
20%

What's inside SimpleNet

  1. Overview of SimpleNet architecture

    main

    SimpleNet is a network for image anomaly detection and localization implemented in PyTorch. It is designed to be conceptually simple, avoiding complex network designs or external data sources. The architecture consists of three primary components:

    1. Feature encoder
    2. Feature generator
    3. Defect discriminator
  2. Set up the SimpleNet environment

    main

    SimpleNet requires Python 3.8. While other versions may work, the following package versions are recommended for compatibility:

    • torch==1.12.1
    • torchvision==0.13.1
    • numpy==1.22.4
    • opencv-python==4.5.1
    pip install torch==1.12.1 torchvision==0.13.1 numpy==1.22.4 opencv-python==4.5.1
  3. Train SimpleNet using run.sh

    main

    Training is managed via the run.sh shell script. Before running the training demo, you must manually edit run.sh to specify the following:

    • Dataset path: Edit line 1 to point to your dataset location.
    • Log folder: Edit line 10 to specify where logs should be saved.

    Once configured, execute the training process using:

    bash run.sh
    bash run.sh
  4. Run SimpleNet via CLI

    main

    SimpleNet is executed using a chained Click CLI. You can chain the net and dataset commands to configure the model architecture and the data loading pipeline, respectively. The main entrypoint main handles the execution flow, including training or testing, result logging, and saving metrics to CSV.

    To run the full pipeline, you must provide configuration for both the network and the dataset. The execution follows this pattern:

    1. Define the network architecture using net.
    2. Define the dataset and data augmentation using dataset.
    3. The main command then orchestrates the training/testing process.
    # Example conceptual usage (actual command structure):
    # python main.py --results_path ./res --run_name my_run net --backbone_names resnet50 dataset mvtec ./data --subdatasets bottle
  5. Configure datasets with the `dataset` command

    main

    Use the dataset command to specify the data source and augmentation parameters. The command supports the mvtec dataset type.

    Arguments:

    • name: The dataset type (e.g., mvtec).
    • data_path: Path to the dataset directory.

    Options:

    • --subdatasets / -d: Required. List of subdataset names (e.g., bottle, cable).
    • --train_val_split: Float determining the split for training/validation. If < 1, a validation set is created.
    • --batch_size: Number of images per batch.
    • --imagesize: Target image size for resizing.
    • --augment: Flag to enable data augmentation.
    • Augmentation parameters: --rotate_degrees, --translate, --scale, --brightness, --contrast, --saturation, --gray, --hflip, --vflip.
  6. Configure the SimpleNet architecture with the `net` command

    main

    Use the net command to define the backbone and training hyperparameters for the SimpleNet model.

    Key options include:

    • --backbone_names / -b: List of backbones to use. Supports a .seed- suffix (e.g., resnet50.seed-42) to set a specific backbone seed.
    • --layers_to_extract_from / -le: Specifies which layers to extract features from. If multiple backbones are used, provide corresponding layers for each.
    • --patchsize: Size of the patch for embedding.
    • --embedding_size: Dimension of the embedding.
    • --meta_epochs, --aed_meta_epochs, --gan_epochs: Number of epochs for different training stages.
    • --dsc_layers, --dsc_hidden, --dsc_margin, --dsc_lr: Parameters for the Discriminative Score Coding (DSC) component.
    • --train_backbone: Flag to enable training of the backbone.
    • --cos_lr: Flag to enable cosine learning rate scheduling.
  7. Configure global execution settings in `main`

    main

    The main command accepts global options that control the execution environment and logging:

    • --results_path: Directory where results and models will be saved.
    • --gpu: Integer list of GPU IDs to use (e.g., --gpu 0 --gpu 1).
    • --seed: Random seed for reproducibility.
    • --log_group: Group name for logging.
    • --log_project: Project name for logging.
    • --run_name: Name of the specific run.
    • --test: Flag to run in testing mode (Note: The script currently warns that test should be set to True by default).
    • --save_segmentation_images: Flag to save the generated segmentation masks.