Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial docs string #9

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ repos:
args: [--config-file=tox.ini]
additional_dependencies: [types-PyYAML]
exclude: "tests/"

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
name: pydocstyle
entry: pydocstyle
language: python
types: [python]
args: [--config=tox.ini]
exclude: "tests|docs"
4 changes: 1 addition & 3 deletions anomalib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Anomalib library for research and benchmarking
"""
"""Anomalib library for research and benchmarking."""

# Copyright (C) 2020 Intel Corporation
#
Expand Down
4 changes: 1 addition & 3 deletions anomalib/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Utilities to get configurable parameters
"""
"""Utilities to get configurable parameters."""

# Copyright (C) 2020 Intel Corporation
#
Expand Down
30 changes: 12 additions & 18 deletions anomalib/config/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Get configurable parameters
"""
"""Get configurable parameters."""

# Copyright (C) 2020 Intel Corporation
#
Expand Down Expand Up @@ -28,16 +26,16 @@


def update_input_size_config(config: Union[DictConfig, ListConfig]) -> Union[DictConfig, ListConfig]:
"""
Convert integer image size parameters into tuples, calculate the effective input size based on image size and crop
size, and set tiling stride if undefined.
"""Update config with image size as tuple, effective input size and tiling stride.

Convert integer image size parameters into tuples, calculate the effective input size based on image size
and crop size, and set tiling stride if undefined.

Args:
config: Dictconfig: Configurable parameters object
config (Union[DictConfig, ListConfig]): Configurable parameters object

Returns:
Configurable parameters with updated values

"""
# handle image size
if isinstance(config.dataset.image_size, int):
Expand All @@ -55,8 +53,7 @@ def update_input_size_config(config: Union[DictConfig, ListConfig]) -> Union[Dic


def update_nncf_config(config: Union[DictConfig, ListConfig]) -> Union[DictConfig, ListConfig]:
"""
Set the NNCF input size based on the value of the crop_size parameter in the configurable parameters object.
"""Set the NNCF input size based on the value of the crop_size parameter in the configurable parameters object.

Args:
config: Dictconfig: Configurable parameters of the current run.
Expand All @@ -76,8 +73,9 @@ def update_nncf_config(config: Union[DictConfig, ListConfig]) -> Union[DictConfi


def update_multi_gpu_training_config(config: Union[DictConfig, ListConfig]) -> Union[DictConfig, ListConfig]:
"""Updates the config to change learning rate based on number of gpus assigned
and ensures only ddp accelerator is used
"""Updates the config to change learning rate based on number of gpus assigned.

Current behaviour is to ensure only ddp accelerator is used.

Args:
config (Union[DictConfig, ListConfig]): Configurable parameters for the current run
Expand Down Expand Up @@ -115,9 +113,7 @@ def update_multi_gpu_training_config(config: Union[DictConfig, ListConfig]) -> U


def update_device_config(config: Union[DictConfig, ListConfig], openvino: bool) -> Union[DictConfig, ListConfig]:
"""
Update XPU Device Config
This function ensures devices are configured correctly by the user.
"""Update XPU Device Config This function ensures devices are configured correctly by the user.

Args:
config (Union[DictConfig, ListConfig]): Input config
Expand Down Expand Up @@ -150,8 +146,7 @@ def get_configurable_parameters(
config_filename: Optional[str] = "config",
config_file_extension: Optional[str] = "yaml",
) -> Union[DictConfig, ListConfig]:
"""
Get configurable parameters
"""Get configurable parameters.

Args:
model_name: Optional[str]: (Default value = None)
Expand All @@ -163,7 +158,6 @@ def get_configurable_parameters(

Returns:
Configurable parameters in DictConfig object.

"""
if model_name is None and model_config_path is None:
raise ValueError(
Expand Down
4 changes: 1 addition & 3 deletions anomalib/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
This module holds common components such as callbacks, custom modules and utils
"""
"""This module holds common components such as callbacks, custom modules and utils."""

# Copyright (C) 2020 Intel Corporation
#
Expand Down
8 changes: 4 additions & 4 deletions anomalib/core/callbacks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Callbacks for Anomalib models"""
"""Callbacks for Anomalib models."""

import os
from importlib import import_module
Expand All @@ -23,12 +23,12 @@


def get_callbacks(config: Union[ListConfig, DictConfig]) -> List[Callback]:
"""Return base callbacks for all the lightning models
"""Return base callbacks for all the lightning models.

Args:
config (DictConfig): model config

Returns:
Return:
(List[Callback]): List of callbacks
"""
callbacks: List[Callback] = []
Expand Down Expand Up @@ -65,7 +65,7 @@ def get_callbacks(config: Union[ListConfig, DictConfig]) -> List[Callback]:
if config.optimization.compression.apply:
callbacks.append(
CompressModelCallback(
config=config,
input_size=config.model.input_size,
dirpath=os.path.join(config.project.path, "compressed"),
filename="compressed_model",
)
Expand Down
21 changes: 13 additions & 8 deletions anomalib/core/callbacks/compress.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
"""Callback that compresses a trained model by first exporting to .onnx format, and then converting to OpenVINO IR."""
import os
from typing import Union
from typing import Tuple

import torch
from omegaconf import DictConfig, ListConfig
from pytorch_lightning import Callback, LightningModule


class CompressModelCallback(Callback):
"""
Callback that compresses a trained model by first exporting to .onnx format, and then converting to OpenVINO IR.
"""Callback to compresses a trained model.

Model is first exported to .onnx format, and then converted to OpenVINO IR.

Args:
input_size (Tuple[int, int]): Tuple of image height, width
dirpath (str): Path for model output
filename (str): Name of output model
"""

def __init__(self, config: Union[ListConfig, DictConfig], dirpath: str, filename: str):
self.config = config
def __init__(self, input_size: Tuple[int, int], dirpath: str, filename: str):
self.input_size = input_size
self.dirpath = dirpath
self.filename = filename

def on_train_end(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when the train ends."""
"""Call when the train ends."""
os.makedirs(self.dirpath, exist_ok=True)
onnx_path = os.path.join(self.dirpath, self.filename + ".onnx")
height, width = self.config.model.input_size
height, width = self.input_size
torch.onnx.export(
pl_module.model, torch.zeros((1, 3, height, width)).to(pl_module.device), onnx_path, opset_version=11
)
Expand Down
2 changes: 1 addition & 1 deletion anomalib/core/callbacks/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def __init__(self, weights_path):
self.weights_path = weights_path

def on_test_start(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when the test begins."""
"""Call when the test begins."""
pl_module.load_state_dict(torch.load(self.weights_path)["state_dict"])
36 changes: 13 additions & 23 deletions anomalib/core/callbacks/nncf_callback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
NNCF Callback
"""
"""NNCF Callback."""

import os
from typing import Any, Dict, Iterator, Optional, Tuple
Expand All @@ -19,16 +17,7 @@


def criterion_fn(outputs, criterion):
"""
criterion_fn [summary]

Args:
outputs ([type]): [description]
criterion ([type]): [description]

Returns:
[type]: [description]
"""
"""Calls the criterion function on outputs."""
return criterion(outputs)


Expand All @@ -40,25 +29,26 @@ def __init__(self, data_loader: DataLoader):
self._data_loader_iter: Iterator

def __iter__(self):
"""Create iterator for dataloader."""
self._data_loader_iter = iter(self._data_loader)
return self

def __next__(self) -> Any:
"""Return next item from dataloader iterator."""
loaded_item = next(self._data_loader_iter)
return loaded_item["image"]

def get_inputs(self, dataloader_output) -> Tuple[Tuple, Dict]:
"""
"""Get input to model.

Returns:
(dataloader_output,), {}: Tuple[Tuple, Dict]: The current model call to be made during
the initialization process
"""
return (dataloader_output,), {}

def get_target(self, _):
"""
Parses the generic data loader output and returns a structure to be used as
ground truth in the loss criterion.
"""Return structure for ground truth in loss criterion based on dataloader output.

Returns:
None
Expand All @@ -69,8 +59,8 @@ def get_target(self, _):
class NNCFCallback(Callback):
"""Callback for NNCF compression.

Assumes that the pl module contains a 'model' attribute, which is the PyTorch module
that must be compressed.
Assumes that the pl module contains a 'model' attribute, which is
the PyTorch module that must be compressed.
"""

def __init__(self, config, dirpath, filename):
Expand All @@ -88,7 +78,7 @@ def __init__(self, config, dirpath, filename):
self.compression_scheduler: CompressionScheduler

def setup(self, _: pl.Trainer, pl_module: pl.LightningModule, __: Optional[str] = None) -> None:
"""Called when fit or test begins"""
"""Call when fit or test begins."""
if self.comp_ctrl is None:
init_loader = InitLoader(self.train_loader)
nncf_config = register_default_init_args(
Expand All @@ -101,13 +91,13 @@ def setup(self, _: pl.Trainer, pl_module: pl.LightningModule, __: Optional[str]
def on_train_batch_start(
self, trainer, _pl_module: pl.LightningModule, _batch: Any, _batch_idx: int, _dataloader_idx: int
) -> None:
"""Called when the train batch begins."""
"""Call when the train batch begins."""
self.compression_scheduler.step()
if self.comp_ctrl is not None:
trainer.model.loss_val = self.comp_ctrl.loss()

def on_train_end(self, _trainer, _pl_module: pl.LightningModule) -> None:
"""Called when the train ends."""
"""Call when the train ends."""
os.makedirs(self.dirpath, exist_ok=True)
onnx_path = os.path.join(self.dirpath, self.filename + ".onnx")
if self.comp_ctrl is not None:
Expand All @@ -118,5 +108,5 @@ def on_train_end(self, _trainer, _pl_module: pl.LightningModule) -> None:
def on_train_epoch_end(
self, _trainer: pl.Trainer, _pl_module: pl.LightningModule, _unused: Optional[Any] = None
) -> None:
"""Called when the train epoch ends."""
"""Call when the train epoch ends."""
self.compression_scheduler.epoch_step()
17 changes: 8 additions & 9 deletions anomalib/core/callbacks/save_to_csv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
SaveToCSV Callback
"""
"""Callback to save metrics to CSV."""
from pathlib import Path

import numpy as np
Expand All @@ -11,21 +9,22 @@


class SaveToCSVCallback(Callback):
"""
Callback that saves the inference results of a model. The callback generates a csv file that saves different
performance metrics and results.
"""Callback that saves the inference results of a model.

The callback generates a csv file that saves different performance
metrics and results.
"""

def __init__(self):
"""SaveToCSV callback"""
"""Callback to save metrics to CSV."""

def on_test_epoch_end(self, _trainer: Trainer, pl_module: AnomalyModule) -> None:
"""Save Results at the end of training
"""Save Results at the end of training.

Args:
_trainer (Trainer): Pytorch lightning trainer object (unused)
pl_module (LightningModule): Lightning modules derived from BaseAnomalyLightning object.
"""

results = pl_module.results
data_frame = pd.DataFrame(
{
Expand Down
10 changes: 5 additions & 5 deletions anomalib/core/callbacks/timer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Callback to measure training and testing time of a PyTorch Lightning module"""
"""Callback to measure training and testing time of a PyTorch Lightning module."""
import time

from pytorch_lightning import Callback, LightningModule
Expand All @@ -11,17 +11,17 @@ def __init__(self):
self.start = None

def on_fit_start(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when fit begins"""
"""Call when fit begins."""
self.start = time.time()

def on_fit_end(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when fit ends"""
"""Call when fit ends."""
print(f"Training took {time.time() - self.start} seconds")

def on_test_start(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when the test begins."""
"""Call when the test begins."""
self.start = time.time()

def on_test_end(self, trainer, pl_module: LightningModule) -> None: # pylint: disable=W0613
"""Called when the test ends."""
"""Call when the test ends."""
print(f"Testing took {time.time() - self.start} seconds.")
Loading