class_docstr: DSR: A Dual Subspace Re-Projection Network for Surface Anomaly Detection
  
  Args:
      latent_anomaly_strength (float, optional): Strength of the generated anomalies in the latent space.
docstr: Training Step of DSR.
  
  Feeds the original image and the simulated anomaly mask during first phase. During
  second phase, feeds a generated anomalous image to train the upsampling module.
  
  Args:
      batch (dict[str, str | Tensor]): Batch containing image filename, image, label and mask
  
  Returns:
      STEP_OUTPUT: Loss dictionary
docstr_tok: ["Training","Step","of","DSR",".","Feeds","the","original","image","and","the","simulated","anomaly","mask","during","first","phase",".","During","second","phase",",","feeds","a","generated","anomalous","image","to","train","the","upsampling","module",".","Args",":","batch","(","dict","[","str",",","str","|","Tensor","]",")",":","Batch","containing","image","filename",",","image",",","label","and","mask","Returns",":","STEP_OUTPUT",":","Loss","dictionary"]
code: def training_step(
      self, batch: dict[str, str | Tensor]
  ) -> STEP_OUTPUT:
      """Training Step of DSR.
  
      Feeds the original image and the simulated anomaly mask during first phase. During
      second phase, feeds a generated anomalous image to train the upsampling module.
  
      Args:
          batch (dict[str, str | Tensor]): Batch containing image filename, image, label and mask
  
      Returns:
          STEP_OUTPUT: Loss dictionary
      """
      ph1_opt, ph2_opt = self.optimizers()
  
      if self.current_epoch < self.second_phase:
          # we are not yet training the upsampling module: we are only using the first optimizer
          input_image = batch["image"]
          # Create anomaly masks
          anomaly_mask = self.quantized_anomaly_generator.augment_batch(
              input_image
          )
          # Generate model prediction
          model_outputs = self.model(input_image, anomaly_mask)
          # Compute loss
          loss = self.second_stage_loss(
              model_outputs["recon_feat_hi"],
              model_outputs["recon_feat_lo"],
              model_outputs["embedding_bot"],
              model_outputs["embedding_top"],
              input_image,
              model_outputs["obj_spec_image"],
              model_outputs["anomaly_map"],
              model_outputs["true_anomaly_map"],
          )
  
          # compute manual optimizer step
          ph1_opt.zero_grad()
          self.manual_backward(loss)
          ph1_opt.step()
  
      else:
          # we are training the upsampling module
          input_image = batch["image"]
          # Generate anomalies
          (
              input_image,
              anomaly_maps,
          ) = self.perlin_generator.augment_batch(input_image)
          # Get model prediction
          model_outputs = self.model(input_image)
          # Calculate loss
          loss = self.third_stage_loss(
              model_outputs["anomaly_map"], anomaly_maps
          )
  
          # compute manual optimizer step
          ph2_opt.zero_grad()
          self.manual_backward(loss)
          ph2_opt.step()
  
      self.log(
          "train_loss",
          loss.item(),
          on_epoch=True,
          prog_bar=True,
          logger=True,
      )
      return {"loss": loss}
  
imports: from __future__ import annotations
  import logging
  from pathlib import Path
  import torch
  from omegaconf import DictConfig, ListConfig
  from pytorch_lightning.utilities.types import STEP_OUTPUT
  from torch import Tensor
  from anomalib.data.utils import DownloadInfo, download_and_extract
  from anomalib.data.utils.augmenter import Augmenter
  from anomalib.models.components import AnomalyModule
  from anomalib.models.dsr.anomaly_generator import DsrAnomalyGenerator
  from anomalib.models.dsr.loss import DsrSecondStageLoss, DsrThirdStageLoss
  from anomalib.models.dsr.torch_model import DsrModel
label_desc: Use image node_id 6 for calling the Dsr obj's underlying member method code with example usage: obj.training_step(batch) and returns: dict