:py:mod:`mipcandy.presets.segmentation`
=======================================

.. py:module:: mipcandy.presets.segmentation

.. autodoc2-docstring:: mipcandy.presets.segmentation
   :allowtitles:

Module Contents
---------------

Classes
~~~~~~~

.. list-table::
   :class: autosummary longtable
   :align: left

   * - :py:obj:`DeepSupervisionWrapper <mipcandy.presets.segmentation.DeepSupervisionWrapper>`
     -
   * - :py:obj:`SegmentationTrainer <mipcandy.presets.segmentation.SegmentationTrainer>`
     - .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer
          :summary:

API
~~~

.. py:class:: DeepSupervisionWrapper(loss: torch.nn.Module, *, weight_factors: typing.Sequence[float] | None = None)
   :canonical: mipcandy.presets.segmentation.DeepSupervisionWrapper

   Bases: :py:obj:`mipcandy.common.Loss`

   .. py:method:: forward(outputs: typing.Sequence[torch.Tensor], targets: typing.Sequence[torch.Tensor]) -> tuple[torch.Tensor, dict[str, float]]
      :canonical: mipcandy.presets.segmentation.DeepSupervisionWrapper.forward

      .. autodoc2-docstring:: mipcandy.presets.segmentation.DeepSupervisionWrapper.forward

.. py:class:: SegmentationTrainer(trainer_folder: str | os.PathLike[str], dataloader: torch.utils.data.DataLoader[tuple[torch.Tensor, torch.Tensor]], validation_dataloader: torch.utils.data.DataLoader[tuple[torch.Tensor, torch.Tensor]], *, recoverable: bool = True, profiler: bool = False, device: torch.device | str = 'cpu', console: rich.console.Console = Console())
   :canonical: mipcandy.presets.segmentation.SegmentationTrainer

   Bases: :py:obj:`mipcandy.training.Trainer`

   .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer

   .. rubric:: Initialization

   .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.__init__

   .. py:attribute:: num_classes
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.num_classes
      :type: int
      :value: 1

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.num_classes

   .. py:attribute:: include_background
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.include_background
      :type: bool
      :value: True

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.include_background

   .. py:attribute:: deep_supervision
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision
      :type: bool
      :value: False

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision

   .. py:attribute:: deep_supervision_scales
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision_scales
      :type: typing.Sequence[float] | None
      :value: None

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision_scales

   .. py:attribute:: deep_supervision_weights
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision_weights
      :type: typing.Sequence[float] | None
      :value: None

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.deep_supervision_weights

   .. py:method:: _save_preview(x: torch.Tensor, title: str, quality: float, *, is_label: bool = False) -> None
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer._save_preview

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer._save_preview

   .. py:method:: apply_non_linearity(x: torch.Tensor, channel_dim: int) -> torch.Tensor
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.apply_non_linearity

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.apply_non_linearity

   .. py:method:: save_preview(image: torch.Tensor, label: torch.Tensor, output: torch.Tensor, *, quality: float = 0.75) -> None
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.save_preview

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.save_preview

   .. py:method:: build_ema(model: torch.nn.Module) -> torch.nn.Module
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.build_ema

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.build_ema

   .. py:method:: build_criterion() -> torch.nn.Module
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.build_criterion

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.build_criterion

   .. py:method:: build_optimizer(params: mipcandy.types.Params) -> torch.optim.Optimizer
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.build_optimizer

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.build_optimizer

   .. py:method:: build_scheduler(optimizer: torch.optim.Optimizer, num_epochs: int) -> torch.optim.lr_scheduler.LRScheduler
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.build_scheduler

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.build_scheduler

   .. py:method:: backward(images: torch.Tensor, labels: torch.Tensor, toolbox: mipcandy.training.TrainerToolbox) -> tuple[float, dict[str, float]]
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.backward

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.backward

   .. py:method:: prepare_deep_supervision_targets(labels: torch.Tensor, output_shapes: list[tuple[int, ...]]) -> list[torch.Tensor]
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.prepare_deep_supervision_targets
      :staticmethod:

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.prepare_deep_supervision_targets

   .. py:method:: class_percentages(ids: torch.Tensor) -> dict[int, float]
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.class_percentages

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.class_percentages

   .. py:method:: format_class_percentages(percentages: dict[int, float], prefix: str) -> dict[str, float]
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.format_class_percentages
      :staticmethod:

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.format_class_percentages

   .. py:method:: validate_case(idx: int, image: torch.Tensor, label: torch.Tensor, toolbox: mipcandy.training.TrainerToolbox) -> tuple[float, dict[str, float], torch.Tensor]
      :canonical: mipcandy.presets.segmentation.SegmentationTrainer.validate_case

      .. autodoc2-docstring:: mipcandy.presets.segmentation.SegmentationTrainer.validate_case
