From 465348d62cec8afc00b84a5e540783168d6705c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Jan 2024 19:11:20 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pl_bolts/callbacks/data_monitor.py | 2 ++ src/pl_bolts/callbacks/verification/base.py | 6 ++++-- .../callbacks/verification/batch_gradient.py | 6 ++++-- src/pl_bolts/callbacks/vision/image_generation.py | 1 + src/pl_bolts/callbacks/vision/sr_image_logger.py | 5 +++-- src/pl_bolts/datamodules/imagenet_datamodule.py | 4 ++-- src/pl_bolts/datamodules/stl10_datamodule.py | 1 + src/pl_bolts/datamodules/vocdetection_datamodule.py | 2 ++ src/pl_bolts/datasets/kitti_dataset.py | 1 + src/pl_bolts/datasets/utils.py | 1 + .../models/detection/yolo/darknet_network.py | 6 ++++-- src/pl_bolts/models/detection/yolo/loss.py | 1 + src/pl_bolts/models/detection/yolo/torch_networks.py | 12 ++++++++---- src/pl_bolts/models/detection/yolo/utils.py | 5 +++-- src/pl_bolts/models/detection/yolo/yolo_module.py | 12 ++++++++---- src/pl_bolts/models/rl/ppo_model.py | 3 +-- .../models/self_supervised/moco/moco_module.py | 1 + src/pl_bolts/models/self_supervised/moco/utils.py | 1 + 18 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/pl_bolts/callbacks/data_monitor.py b/src/pl_bolts/callbacks/data_monitor.py index 7a39a1e709..df822e95f2 100644 --- a/src/pl_bolts/callbacks/data_monitor.py +++ b/src/pl_bolts/callbacks/data_monitor.py @@ -73,6 +73,7 @@ def log_histograms(self, batch: Any, group: str = "") -> None: Otherwise the histograms get labelled with an integer index. Each label also has the tensors's shape as suffix. group: Name under which the histograms will be grouped. + """ if not self._log or (self._train_batch_idx + 1) % self._log_every_n_steps != 0: # type: ignore[operator] return @@ -220,6 +221,7 @@ def __init__(self, log_every_n_steps: int = None) -> None: # log histogram of training data passed to `LightningModule.training_step` trainer = Trainer(callbacks=[TrainingDataMonitor()]) + """ super().__init__(log_every_n_steps=log_every_n_steps) diff --git a/src/pl_bolts/callbacks/verification/base.py b/src/pl_bolts/callbacks/verification/base.py index 49e2e3593a..1f6d59c0c8 100644 --- a/src/pl_bolts/callbacks/verification/base.py +++ b/src/pl_bolts/callbacks/verification/base.py @@ -77,6 +77,7 @@ def _model_forward(self, input_array: Any) -> Any: Returns: The output of the model. + """ if isinstance(input_array, tuple): return self.model(*input_array) @@ -105,8 +106,8 @@ def __init__(self, warn: bool = True, error: bool = False) -> None: self._raise_error = error def message(self, *args: Any, **kwargs: Any) -> str: - """The message to be printed when the model does not pass the verification. If the message for warning and - error differ, override the :meth:`warning_message` and :meth:`error_message` methods directly. + """The message to be printed when the model does not pass the verification. If the message for warning and error + differ, override the :meth:`warning_message` and :meth:`error_message` methods directly. Arguments: *args: Any positional arguments that are needed to construct the message. @@ -114,6 +115,7 @@ def message(self, *args: Any, **kwargs: Any) -> str: Returns: The message as a string. + """ def warning_message(self, *args: Any, **kwargs: Any) -> str: diff --git a/src/pl_bolts/callbacks/verification/batch_gradient.py b/src/pl_bolts/callbacks/verification/batch_gradient.py index 834184c152..107c647d9f 100644 --- a/src/pl_bolts/callbacks/verification/batch_gradient.py +++ b/src/pl_bolts/callbacks/verification/batch_gradient.py @@ -91,6 +91,7 @@ class BatchGradientVerificationCallback(VerificationCallbackBase): """The callback version of the :class:`BatchGradientVerification` test. Verification is performed right before training begins. + """ def __init__( @@ -211,12 +212,13 @@ def collect_batches(tensor: Tensor) -> Tensor: @under_review() @contextmanager def selective_eval(model: nn.Module, layer_types: Iterable[Type[nn.Module]]) -> None: - """A context manager that sets all requested types of layers to eval mode. This method uses an ``isinstance`` - check, so all subclasses are also affected. + """A context manager that sets all requested types of layers to eval mode. This method uses an ``isinstance`` check, + so all subclasses are also affected. Args: model: A model which has layers that need to be set to eval mode. layer_types: The list of class objects for which all layers of that type will be set to eval mode. + """ to_revert = [] try: diff --git a/src/pl_bolts/callbacks/vision/image_generation.py b/src/pl_bolts/callbacks/vision/image_generation.py index a30e78972b..0e860292de 100644 --- a/src/pl_bolts/callbacks/vision/image_generation.py +++ b/src/pl_bolts/callbacks/vision/image_generation.py @@ -31,6 +31,7 @@ class TensorboardGenerativeModelImageSampler(Callback): from pl_bolts.callbacks import TensorboardGenerativeModelImageSampler trainer = Trainer(callbacks=[TensorboardGenerativeModelImageSampler()]) + """ def __init__( diff --git a/src/pl_bolts/callbacks/vision/sr_image_logger.py b/src/pl_bolts/callbacks/vision/sr_image_logger.py index f27bd294e2..4a96330013 100644 --- a/src/pl_bolts/callbacks/vision/sr_image_logger.py +++ b/src/pl_bolts/callbacks/vision/sr_image_logger.py @@ -17,8 +17,8 @@ @under_review() class SRImageLoggerCallback(Callback): - """Logs low-res, generated high-res, and ground truth high-res images to TensorBoard Your model must implement - the ``forward`` function for generation. + """Logs low-res, generated high-res, and ground truth high-res images to TensorBoard Your model must implement the + ``forward`` function for generation. Requirements:: @@ -30,6 +30,7 @@ class SRImageLoggerCallback(Callback): from pl_bolts.callbacks import SRImageLoggerCallback trainer = Trainer(callbacks=[SRImageLoggerCallback()]) + """ def __init__(self, log_interval: int = 1000, scale_factor: int = 4, num_samples: int = 5) -> None: diff --git a/src/pl_bolts/datamodules/imagenet_datamodule.py b/src/pl_bolts/datamodules/imagenet_datamodule.py index 90f2bb641d..3fec1bab68 100644 --- a/src/pl_bolts/datamodules/imagenet_datamodule.py +++ b/src/pl_bolts/datamodules/imagenet_datamodule.py @@ -167,12 +167,12 @@ def train_dataloader(self) -> DataLoader: return loader def val_dataloader(self) -> DataLoader: - """Uses the part of the train split of imagenet2012 that was not used for training via - `num_imgs_per_val_class` + """Uses the part of the train split of imagenet2012 that was not used for training via `num_imgs_per_val_class` Args: batch_size: the batch size transforms: the transforms + """ transforms = self.val_transform() if self.val_transforms is None else self.val_transforms diff --git a/src/pl_bolts/datamodules/stl10_datamodule.py b/src/pl_bolts/datamodules/stl10_datamodule.py index 158baee0bc..bff8113256 100644 --- a/src/pl_bolts/datamodules/stl10_datamodule.py +++ b/src/pl_bolts/datamodules/stl10_datamodule.py @@ -139,6 +139,7 @@ def train_dataloader_mixed(self) -> DataLoader: batch_size: the batch size transforms: a sequence of transforms + """ transforms = self._default_transforms() if self.train_transforms is None else self.train_transforms diff --git a/src/pl_bolts/datamodules/vocdetection_datamodule.py b/src/pl_bolts/datamodules/vocdetection_datamodule.py index de8a84b0f3..d6435bcc91 100644 --- a/src/pl_bolts/datamodules/vocdetection_datamodule.py +++ b/src/pl_bolts/datamodules/vocdetection_datamodule.py @@ -166,6 +166,7 @@ def train_dataloader(self, image_transforms: Optional[Callable] = None) -> DataL Args: image_transforms: custom image-only transforms + """ transforms = [ _prepare_voc_instance, @@ -181,6 +182,7 @@ def val_dataloader(self, image_transforms: Optional[Callable] = None) -> DataLoa Args: image_transforms: custom image-only transforms + """ transforms = [ _prepare_voc_instance, diff --git a/src/pl_bolts/datasets/kitti_dataset.py b/src/pl_bolts/datasets/kitti_dataset.py index 0e6674224d..92b08ba9c3 100644 --- a/src/pl_bolts/datasets/kitti_dataset.py +++ b/src/pl_bolts/datasets/kitti_dataset.py @@ -86,6 +86,7 @@ def encode_segmap(self, mask): It also sets all of the valid pixels to the appropriate value between 0 and `len(valid_labels)` (the number of valid classes), so it can be used properly by the loss function when comparing with the output. + """ for voidc in self.void_labels: mask[mask == voidc] = self.ignore_index diff --git a/src/pl_bolts/datasets/utils.py b/src/pl_bolts/datasets/utils.py index e3b085fbc1..52a25d46ea 100644 --- a/src/pl_bolts/datasets/utils.py +++ b/src/pl_bolts/datasets/utils.py @@ -60,6 +60,7 @@ def to_tensor(arrays: TArrays) -> torch.Tensor: Returns: Tensor of the integers + """ return torch.tensor(arrays) diff --git a/src/pl_bolts/models/detection/yolo/darknet_network.py b/src/pl_bolts/models/detection/yolo/darknet_network.py index 7a38a0f5d2..78f87d62d9 100644 --- a/src/pl_bolts/models/detection/yolo/darknet_network.py +++ b/src/pl_bolts/models/detection/yolo/darknet_network.py @@ -145,6 +145,7 @@ def read(tensor: Tensor) -> int: """Reads the contents of ``tensor`` from the current position of ``weight_file``. Returns the number of elements read. If there's no more data in ``weight_file``, returns 0. + """ np_array = np.fromfile(weight_file, count=tensor.numel(), dtype=np.float32) num_elements = np_array.size @@ -275,8 +276,8 @@ def convert(key: str, value: str) -> Union[str, int, float, List[Union[str, int, def _create_layer(config: CONFIG, num_inputs: List[int], **kwargs: Any) -> CREATE_LAYER_OUTPUT: - """Calls one of the ``_create_(config, num_inputs)`` functions to create a PyTorch module from the - layer config. + """Calls one of the ``_create_(config, num_inputs)`` functions to create a PyTorch module from the layer + config. Args: config: Dictionary of configuration options for this layer. @@ -285,6 +286,7 @@ def _create_layer(config: CONFIG, num_inputs: List[int], **kwargs: Any) -> CREAT Returns: module (:class:`~torch.nn.Module`), num_outputs (int): The created PyTorch module and the number of channels in its output. + """ create_func: Dict[str, Callable[..., CREATE_LAYER_OUTPUT]] = { "convolutional": _create_convolutional, diff --git a/src/pl_bolts/models/detection/yolo/loss.py b/src/pl_bolts/models/detection/yolo/loss.py index 44ac5b0f11..6bcaadd0de 100644 --- a/src/pl_bolts/models/detection/yolo/loss.py +++ b/src/pl_bolts/models/detection/yolo/loss.py @@ -205,6 +205,7 @@ def _target_labels_to_probs( Returns: An ``[M, C]`` matrix of target class probabilities. + """ if targets.ndim == 1: # The data may contain a different number of classes than what the model predicts. In case a label is diff --git a/src/pl_bolts/models/detection/yolo/torch_networks.py b/src/pl_bolts/models/detection/yolo/torch_networks.py index ee5358ac7f..9e59eec796 100644 --- a/src/pl_bolts/models/detection/yolo/torch_networks.py +++ b/src/pl_bolts/models/detection/yolo/torch_networks.py @@ -31,6 +31,7 @@ def run_detection( detections: A list where a tensor containing the detections will be appended to. losses: A list where a tensor containing the losses will be appended to, if ``targets`` is given. hits: A list where the number of targets that matched this layer will be appended to, if ``targets`` is given. + """ output, preds = detection_layer(layer_input, image_size) detections.append(output) @@ -69,6 +70,7 @@ def run_detection_with_aux_head( detections: A list where a tensor containing the detections will be appended to. losses: A list where a tensor containing the losses will be appended to, if ``targets`` is given. hits: A list where the number of targets that matched this layer will be appended to, if ``targets`` is given. + """ output, preds = detection_layer(layer_input, image_size) detections.append(output) @@ -1132,8 +1134,8 @@ def forward(self, x: Tensor, targets: Optional[TARGETS] = None) -> NETWORK_OUTPU class YOLOV5Network(nn.Module): - """The YOLOv5 network architecture. Different variants (n/s/m/l/x) can be achieved by adjusting the ``depth`` - and ``width`` parameters. + """The YOLOv5 network architecture. Different variants (n/s/m/l/x) can be achieved by adjusting the ``depth`` and + ``width`` parameters. Args: num_classes: Number of different classes that this model predicts. @@ -1176,6 +1178,7 @@ class YOLOV5Network(nn.Module): class_loss_multiplier: Classification loss will be scaled by this value. xy_scale: Eliminate "grid sensitivity" by scaling the box coordinates by this factor. Using a value > 1.0 helps to produce coordinate values close to one. + """ def __init__( @@ -1613,8 +1616,8 @@ def forward(self, x: Tensor) -> Tensor: class YOLOXNetwork(nn.Module): - """The YOLOX network architecture. Different variants (nano/tiny/s/m/l/x) can be achieved by adjusting the - ``depth`` and ``width`` parameters. + """The YOLOX network architecture. Different variants (nano/tiny/s/m/l/x) can be achieved by adjusting the ``depth`` + and ``width`` parameters. Args: num_classes: Number of different classes that this model predicts. @@ -1657,6 +1660,7 @@ class YOLOXNetwork(nn.Module): class_loss_multiplier: Classification loss will be scaled by this value. xy_scale: Eliminate "grid sensitivity" by scaling the box coordinates by this factor. Using a value > 1.0 helps to produce coordinate values close to one. + """ def __init__( diff --git a/src/pl_bolts/models/detection/yolo/utils.py b/src/pl_bolts/models/detection/yolo/utils.py index d981fadceb..66996fc4d8 100644 --- a/src/pl_bolts/models/detection/yolo/utils.py +++ b/src/pl_bolts/models/detection/yolo/utils.py @@ -102,8 +102,8 @@ def aligned_iou(wh1: Tensor, wh2: Tensor) -> Tensor: def iou_below(pred_boxes: Tensor, target_boxes: Tensor, threshold: float) -> Tensor: - """Creates a binary mask whose value will be ``True``, unless the predicted box overlaps any target - significantly (IoU greater than ``threshold``). + """Creates a binary mask whose value will be ``True``, unless the predicted box overlaps any target significantly + (IoU greater than ``threshold``). Args: pred_boxes: The predicted corner coordinates. Tensor of size ``[height, width, boxes_per_cell, 4]``. @@ -112,6 +112,7 @@ def iou_below(pred_boxes: Tensor, target_boxes: Tensor, threshold: float) -> Ten Returns: A boolean tensor sized ``[height, width, boxes_per_cell]``, with ``False`` where the predicted box overlaps a target significantly and ``True`` elsewhere. + """ shape = pred_boxes.shape[:-1] pred_boxes = pred_boxes.view(-1, 4) diff --git a/src/pl_bolts/models/detection/yolo/yolo_module.py b/src/pl_bolts/models/detection/yolo/yolo_module.py index 429ed087b7..558cb496b7 100644 --- a/src/pl_bolts/models/detection/yolo/yolo_module.py +++ b/src/pl_bolts/models/detection/yolo/yolo_module.py @@ -144,8 +144,8 @@ def __init__( def forward( self, images: Union[Tensor, IMAGES], targets: Optional[TARGETS] = None ) -> Union[Tensor, Tuple[Tensor, Tensor]]: - """Runs a forward pass through the network (all layers listed in ``self.network``), and if training targets - are provided, computes the losses from the detection layers. + """Runs a forward pass through the network (all layers listed in ``self.network``), and if training targets are + provided, computes the losses from the detection layers. Detections are concatenated from the detection layers. Each detection layer will produce a number of detections that depends on the size of the feature map and the number of anchors per feature map cell. @@ -161,6 +161,7 @@ def forward( provided, a dictionary of losses. Detections are shaped ``[batch_size, anchors, classes + 5]``, where ``anchors`` is the feature map size (width * height) times the number of anchors per cell. The predicted box coordinates are in `(x1, y1, x2, y2)` format and scaled to the input image size. + """ self.validate_batch(images, targets) images_tensor = images if isinstance(images, Tensor) else torch.stack(images) @@ -185,6 +186,7 @@ def configure_optimizers(self) -> Tuple[List[optim.Optimizer], List[LRScheduler] If weight decay is specified, it will be applied only to convolutional layer weights, as they contain much more parameters than the biases and batch normalization parameters. Regularizing all parameters could lead to underfitting. + """ if ("weight_decay" in self.optimizer_params) and (self.optimizer_params["weight_decay"] != 0): defaults = copy(self.optimizer_params) @@ -574,12 +576,13 @@ def __init__( class ResizedVOCDetectionDataModule(VOCDetectionDataModule): - """A subclass of ``VOCDetectionDataModule`` that resizes the images to a specific size. YOLO expectes the image - size to be divisible by the ratio in which the network downsamples the image. + """A subclass of ``VOCDetectionDataModule`` that resizes the images to a specific size. YOLO expectes the image size + to be divisible by the ratio in which the network downsamples the image. Args: width: Resize images to this width. height: Resize images to this height. + """ def __init__(self, width: int = 608, height: int = 608, **kwargs: Any): @@ -609,6 +612,7 @@ def _resize(self, image: Tensor, target: TARGET) -> Tuple[Tensor, TARGET]: Returns: Resized image tensor. + """ device = target["boxes"].device height, width = image.shape[-2:] diff --git a/src/pl_bolts/models/rl/ppo_model.py b/src/pl_bolts/models/rl/ppo_model.py index 21bc0873c0..b861a66619 100644 --- a/src/pl_bolts/models/rl/ppo_model.py +++ b/src/pl_bolts/models/rl/ppo_model.py @@ -319,8 +319,7 @@ def configure_optimizers(self) -> List[Optimizer]: return optimizer_actor, optimizer_critic def optimizer_step(self, *args, **kwargs): - """Run ``num_optim_iters`` number of iterations of gradient descent on actor and critic for each data - sample.""" + """Run ``num_optim_iters`` number of iterations of gradient descent on actor and critic for each data sample.""" for _ in range(self.num_optim_iters): super().optimizer_step(*args, **kwargs) diff --git a/src/pl_bolts/models/self_supervised/moco/moco_module.py b/src/pl_bolts/models/self_supervised/moco/moco_module.py index 86d01147c3..c42f1a5dde 100644 --- a/src/pl_bolts/models/self_supervised/moco/moco_module.py +++ b/src/pl_bolts/models/self_supervised/moco/moco_module.py @@ -228,6 +228,7 @@ def configure_optimizers(self) -> Tuple[List[optim.Optimizer], List[optim.lr_sch ``self.lr_scheduler_params``. If weight decay is specified, it will be applied only to convolutional layer weights. + """ if ( ("weight_decay" in self.optimizer_params) diff --git a/src/pl_bolts/models/self_supervised/moco/utils.py b/src/pl_bolts/models/self_supervised/moco/utils.py index 116b52f979..030ec7079d 100644 --- a/src/pl_bolts/models/self_supervised/moco/utils.py +++ b/src/pl_bolts/models/self_supervised/moco/utils.py @@ -101,6 +101,7 @@ def concatenate_all(tensor: Tensor) -> Tensor: """Performs ``all_gather`` operation to concatenate the provided tensor from all devices. This function has no gradient. + """ gathered_tensor = [torch.zeros_like(tensor) for _ in range(torch.distributed.get_world_size())] torch.distributed.all_gather(gathered_tensor, tensor.contiguous())