class_docstr: Extracts regions from the image.
  
  Args:
      stage (RoiStage, optional): Processing stage from which rois are extracted.
      score_threshold (float, optional): Mimumum confidence score for the region proposals.
      min_size (int, optional): Minimum size in pixels for the region proposals.
      iou_threshold (float, optional): Intersection-Over-Union threshold used during NMS.
      max_detections_per_image (int, optional): Maximum number of region proposals per image.
class_docstr_tok: ["Extracts","regions","from","the","image",".","Args",":","stage","(","RoiStage",",","optional",")",":","Processing","stage","from","which","rois","are","extracted",".","score_threshold","(","float",",","optional",")",":","Mimumum","confidence","score","for","the","region","proposals",".","min_size","(","int",",","optional",")",":","Minimum","size","in","pixels","for","the","region","proposals",".","iou_threshold","(","float",",","optional",")",":","Intersection-Over-Union","threshold","used","during","NMS",".","max_detections_per_image","(","int",",","optional",")",":","Maximum","number","of","region","proposals","per","image","."]
docstr: Forward pass of the model.
  
  Args:
      input (Tensor): Batch of input images of shape [B, C, H, W].
  
  Raises:
      ValueError: When ``stage`` is not one of ``rcnn`` or ``rpn``.
  
  Returns:
      Tensor: Predicted regions, tensor of shape [N, 5] where N is the number of predicted regions in the batch,
           and where each row describes the index of the image in the batch and the 4 bounding box coordinates.
docstr_tok: ["Forward","pass","of","the","model",".","Args",":","input","(","Tensor",")",":","Batch","of","input","images","of","shape","[","B",",","C",",","H",",","W","]",".","Raises",":","ValueError",":","When","``","stage","``","is","not","one","of","``","rcnn","``","or","``","rpn","``",".","Returns",":","Tensor",":","Predicted","regions",",","tensor","of","shape","[","N",",","5","]","where","N","is","the","number","of","predicted","regions","in","the","batch",",","and","where","each","row","describes","the","index","of","the","image","in","the","batch","and","the","4","bounding","box","coordinates","."]
code: def forward(self, batch: Tensor) -> Tensor:
      """Forward pass of the model.
  
      Args:
          input (Tensor): Batch of input images of shape [B, C, H, W].
  
      Raises:
          ValueError: When ``stage`` is not one of ``rcnn`` or ``rpn``.
  
      Returns:
          Tensor: Predicted regions, tensor of shape [N, 5] where N is the number of predicted regions in the batch,
               and where each row describes the index of the image in the batch and the 4 bounding box coordinates.
      """
      if self.training:
          raise ValueError("Should not be in training mode")
  
      if self.stage == RoiStage.RCNN:
          # get rois from rcnn output
          predictions = self.faster_rcnn(batch)
          all_regions = [
              prediction["boxes"] for prediction in predictions
          ]
          all_scores = [
              prediction["scores"] for prediction in predictions
          ]
      elif self.stage == RoiStage.RPN:
          # get rois from region proposal network
          images, _ = self.faster_rcnn.transform(batch)
          features = self.faster_rcnn.backbone(images.tensors)
          proposals, _ = self.faster_rcnn.rpn(images, features)
          # post-process raw rpn predictions
          all_regions = [
              box_ops.clip_boxes_to_image(
                  boxes, images.tensors.shape[-2:]
              )
              for boxes in proposals
          ]
          all_regions = [
              scale_boxes(
                  boxes, images.tensors.shape[-2:], batch.shape[-2:]
              )
              for boxes in all_regions
          ]
          all_scores = [
              torch.ones(boxes.shape[0]).to(boxes.device)
              for boxes in all_regions
          ]
      else:
          raise ValueError(
              f"Unknown region extractor stage: {self.stage}"
          )
  
      regions = self.post_process_box_predictions(
          all_regions, all_scores
      )
  
      # convert from list of [N, 4] tensors to single [N, 5] tensor where each row is [index-in-batch, x1, y1, x2, y2]
      indices = torch.repeat_interleave(
          torch.arange(len(regions)),
          Tensor([rois.shape[0] for rois in regions]).int(),
      )
      regions = torch.cat(
          [indices.unsqueeze(1).to(batch.device), torch.cat(regions)],
          dim=1,
      )
      return regions
  
file_path: anomalib/src/anomalib/models/rkde/region_extractor.py
imports: from __future__ import annotations
  from enum import Enum
  import torch
  from torch import Tensor, nn
  from torchvision.models.detection import fasterrcnn_resnet50_fpn
  from torchvision.ops import boxes
  from anomalib.data.utils.boxes import scale_boxes
label_desc: Use image node_id 2 for calling the RegionExtractor obj's underlying member method code with example usage: obj.forward(batch) and returns: regions