code: def ransac(
      xsamples: Tensor,
      ysamples: Tensor,
      rdims: Tensor,
      config: Dict[str, Any],
      iters: int = 128,
      refit: bool = True,
  ) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
      DET_THR = config["detected_scale_rate_threshold"]
      MIN_CONFIDENCE = config["min_confidence"]
      dv: torch.device = config["device"]
  
      numransacs = rdims.shape[0]
      ransidx = torch.arange(numransacs, device=dv).repeat_interleave(
          rdims
      )
      idxoffsets = torch.cat(
          [
              torch.tensor([0], device=dv),
              torch.cumsum(rdims[:-1], dim=0),
          ],
          dim=0,
      )
  
      rand_samples_rel = draw_first_k_couples(iters, rdims, dv)
      rand_samples_abs = rand_samples_rel + idxoffsets
      sampled_x = torch.transpose(
          xsamples[rand_samples_abs], dim0=1, dim1=2
      )  # (niters, 2, numransacs, 2) -> (niters, numransacs, 2, 2)
      sampled_y = torch.transpose(
          ysamples[rand_samples_abs], dim0=1, dim1=2
      )
  
      # minimal fit for sampled_x @ A^T = sampled_y
      affinities_fit = torch.transpose(
          batch_2x2_inv(sampled_x, check_dets=True) @ sampled_y, -1, -2
      )
      if not refit:
          eigenvals, eigenvecs = batch_2x2_ellipse(affinities_fit)
          bad_ones = (eigenvals[..., 1] < 1 / DET_THR**2) | (
              eigenvals[..., 0] > DET_THR**2
          )
          affinities_fit[bad_ones] = torch.eye(2, device=dv)
      y_pred = (
          affinities_fit[:, ransidx] @ xsamples.unsqueeze(-1)
      ).squeeze(-1)
  
      residuals = torch.norm(
          y_pred - ysamples, dim=-1
      )  # (niters, numsamples)
  
      (
          inl_ransidx,
          inl_sampleidx,
          inl_counts,
          inl_iters,
          inl_confidence,
      ) = confidence_based_inlier_selection(
          residuals,
          ransidx,
          rdims,
          idxoffsets,
          dv=dv,
          min_confidence=MIN_CONFIDENCE,
      )
  
      if len(inl_sampleidx) == 0:
          # If no inliers have been found, there is nothing to re-fit!
          refit = False
  
      if not refit:
          return (
              inl_sampleidx,
              affinities_fit[
                  inl_iters, torch.arange(inl_iters.shape[0], device=dv)
              ],
              inl_confidence,
              inl_counts,
          )
  
      # Organize inliers found into a matrix for efficient GPU re-fitting.
      # Cope with the irregular number of inliers per sample by padding with zeros
      padded_inlier_x, padded_inlier_y = sample_padded_inliers(
          xsamples,
          ysamples,
          inl_counts,
          inl_ransidx,
          inl_sampleidx,
          numransacs,
          dv,
      )
  
      # A @ pad_x.T = pad_y.T
      # A = pad_y.T @ pad_x @ (pad_x.T @ pad_x)^-1
      refit_affinity = (
          padded_inlier_y.transpose(-2, -1)
          @ padded_inlier_x
          @ batch_2x2_inv(
              padded_inlier_x.transpose(-2, -1) @ padded_inlier_x,
              check_dets=True,
          )
      )
  
      # Filter out degenerate affinities with large scale changes
      eigenvals, eigenvecs = batch_2x2_ellipse(refit_affinity)
      bad_ones = (eigenvals[..., 1] < 1 / DET_THR**2) | (
          eigenvals[..., 0] > DET_THR**2
      )
      refit_affinity[bad_ones] = torch.eye(
          2, device=dv, dtype=refit_affinity.dtype
      )
      y_pred = (
          refit_affinity[ransidx] @ xsamples.unsqueeze(-1)
      ).squeeze(-1)
  
      residuals = torch.norm(y_pred - ysamples, dim=-1)
  
      (
          inl_ransidx,
          inl_sampleidx,
          inl_counts,
          inl_iters,
          inl_confidence,
      ) = confidence_based_inlier_selection(
          residuals.unsqueeze(0),
          ransidx,
          rdims,
          idxoffsets,
          dv=dv,
          min_confidence=MIN_CONFIDENCE,
      )
      return inl_sampleidx, refit_affinity, inl_confidence, inl_counts
  
file_path: kornia/kornia/feature/adalam/ransac.py
imports: from typing import Any, Dict, Tuple, Union
  import torch
  from kornia.core import Tensor
  from .utils import arange_sequence, batch_2x2_ellipse, batch_2x2_inv, draw_first_k_couples, piecewise_arange
label_desc: Use image node_id 5 for calling a global function with example usage: ransac(xsamples, ysamples, rdims, config, iters, refit) and returns: inl_sampleidx,  refit_affinity,  inl_confidence,  inl_counts, inl_sampleidx,  affinities_fit,  inl_confidence,  inl_counts