fovi.sensing.samplers
- class fovi.sensing.samplers.BaseGridSampler(*args: Any, **kwargs: Any)[source]
Bases:
ModuleBase class for grid samplers.
Note: objects of the BaseGridSampler family should not be used directly; it is much more convenient to use RetinalTransform, which stores a BaseGridSampler child, due to its handling of fixation parameters.
- _transform_fix_grid(img_shape, fix_loc, fixation_size, cmf_indices=None)[source]
Transform fixation grid to image coordinates.
- Parameters:
img_shape (tuple) – Image shape (height, width).
fix_loc (torch.Tensor) – Fixation location.
fixation_size (torch.Tensor) – Fixation size.
- Returns:
Transformed grid coordinates.
- Return type:
- selected_grid(cmf_indices: Tensor | None = None) Tensor[source]
Return a shared grid or gather per-image grids from a CMF bank.
- selected_mask(cmf_indices: Tensor | None = None) Tensor[source]
Return the scalar or per-image FoV mask.
- selected_radius(cmf_indices: Tensor | None = None) Tensor[source]
Return normalized eccentricity for color decay.
- _prep_grid_for_grid_sample(cartesian_grid)[source]
Prepare grid for torch.nn.functional.grid_sample.
- Parameters:
cartesian_grid (torch.Tensor) – (n,2) coordinates, where each 2-vector coordinate is specified in (x,y) in the typical math sense (normalized to [-1,1])
- Returns:
(1,1,n,2) coordinates, where each 2-vector coordinate is specified in (-y, x) or (row, col) format for grid_sample (normalized to [-1,1])
- Return type:
- _prepare_sampling_args(img, fix_loc, fixation_size, dtype=torch.float32)[source]
Return batched fixation tensors on
img.device.Keeping conversion in one place makes standalone samplers accept the numpy/list fixation forms that
RetinalTransformhistorically normalizes for them.
- _direct_pixel_coords(img_shape, fix_loc, fixation_size, cmf_indices=None)[source]
Compute canonical pixel-boundary coordinates for direct sampling.
- class fovi.sensing.samplers.GridSampler(fov, cmf_a, resolution, device='cuda', dtype=torch.float, mode='nearest', style='isotropic', coords=None, isotropic_plotting_type='v1like', backend='auto', output_dtype=None, fov_type='circular', field_geometry='planar', camera_model=None, gaze_convention='camera_xyz', radius_norm=2.0)[source]
Bases:
BaseGridSamplerGrid sampler for foveated vision using regular grid sampling.
This sampler uses standard grid sampling with nearest neighbor or bilinear interpolation to sample from the foveated sampling grid.
- __init__(fov, cmf_a, resolution, device='cuda', dtype=torch.float, mode='nearest', style='isotropic', coords=None, isotropic_plotting_type='v1like', backend='auto', output_dtype=None, fov_type='circular', field_geometry='planar', camera_model=None, gaze_convention='camera_xyz', radius_norm=2.0)[source]
Initialize the GridSampler.
- Parameters:
fov (float) – Field of view diameter in degrees.
cmf_a (float or sequence of float) – CMF parameter in degrees. A sequence builds a fixed-layout bank for warped Cartesian grids.
resolution (int) – Resolution parameter.
device (str, optional) – Device to run on. Defaults to ‘cuda’.
dtype (torch.dtype, optional) – Data type. Defaults to torch.float.
mode (str, optional) – Sampling mode (‘nearest’ or ‘bilinear’). Defaults to ‘nearest’.
style (str, optional) – Sampling style. Defaults to ‘isotropic’.
coords (SamplingCoords, optional) – Pre-computed sampling coordinates. Defaults to None.
backend (str, optional) – Sampling backend:
auto,torch,cuda, orcompiled(spherical geometry only). For floating inputs,torchselectstorch.grid_sample; for uint8 it selects direct indexing.autoprefers eligible native CUDA kernels. Sphericaltorchuses eager calibrated projection/gather;compiledselects the same operation throughtorch.compile. Inputs requiring gradients retain the Torch implementation.output_dtype (torch.dtype, optional) – Optional dtype for the compact output. This never rescales values. Nearest sampling otherwise preserves dtype; bilinear integer sampling naturally produces float32.
- property camera_model: CameraModel | None
- _torch_direct_sample(img, fix_loc, fixation_size, cmf_indices=None)[source]
Direct moving-grid gather used as the canonical-coordinate oracle.
- _torch_grid_sample(img, fix_loc, fixation_size, cmf_indices=None)[source]
Sample through grid_sample using the floating opmath coordinate dtype.
- property last_backend: str | None
Backend used by the most recent forward call, or None before sampling.
- calibrated_pixels(fix_loc: Tensor, rotation: Tensor | None = None, cmf_indices: Tensor | None = None) tuple[Tensor, Tensor][source]
Return gaze-dependent (B, N, 2) pixels and (B, N) validity.
Fixations are normalized (row, column) in the source image. An explicit (B, 3, 3) rotation maps retinal camera coordinates into source-camera coordinates and bypasses fixation-to-rotation conversion.
- _sample_calibrated(image: Tensor, pixels: Tensor, valid: Tensor) Tensor[source]
Gather compact samples without converting the full uint8 image.
- calibrated_forward(image: Tensor, fixation: Tensor, rotation: Tensor | None, cmf_indices: Tensor | None = None) tuple[Tensor, Tensor][source]
Eager calibrated sampling returning values and source pixels; supports autograd.
- forward(img, fix_loc=None, fixation_size=None, return_coords=False, direct=False, rotation=None, cmf_indices=None)[source]
Forward pass for grid sampling.
- Parameters:
img (torch.Tensor) – Input image tensor.
fix_loc (torch.Tensor, optional) – Fixation location. Defaults to None.
fixation_size (torch.Tensor, optional) – Fixation size. Defaults to None.
return_coords (bool, optional) – Whether to return sampling coordinates. Defaults to False.
direct (bool, optional) – Force the explicit Torch direct-index oracle. Defaults to False.
cmf_indices (torch.Tensor, optional) – Int64 level indices, shape (B,), on the image device. Required when sampling a CMF bank.
- Returns:
Sampled image tensor.
- Return type:
- class fovi.sensing.samplers.PoolingGridSampler(fov: float, cmf_a: float, resolution: int, res_mult: float = 3, cmf_a_mult: float = 1, fixation_size: int = 3000, k: int | None = None, style: str = 'isotropic', sample_cortex: bool | str = True, dtype: dtype = torch.float, device: str | device = 'cuda', isotropic_plotting_type: str = 'v1like', backend: str = 'auto', output_dtype: dtype | None = None, fov_type: str = 'circular', field_geometry: str = 'planar', radius_norm: float = 2.0, camera_model: CameraModel | CameraCalibration | None = None, gaze_convention: str = 'camera_xyz', pool_mode: str = 'avg', gauss_sigma: float = 10.0)[source]
Bases:
BaseGridSamplerHigh-resolution image sampling followed by retinal pooling.
This sampler pools a high-resolution sensor array into a lower-resolution sensor array:
_as_gridstyles use 2D windows (circularly padded in angle for log-polar grids) and sample-set styles use KNN neighborhoods. - highres_coords: akin to photoreceptors: there are more of them - coords: akin to retinal ganglion cells: there are less of them, and they integrate over a local pool of photoreceptors (highres_coords)- __init__(fov: float, cmf_a: float, resolution: int, res_mult: float = 3, cmf_a_mult: float = 1, fixation_size: int = 3000, k: int | None = None, style: str = 'isotropic', sample_cortex: bool | str = True, dtype: dtype = torch.float, device: str | device = 'cuda', isotropic_plotting_type: str = 'v1like', backend: str = 'auto', output_dtype: dtype | None = None, fov_type: str = 'circular', field_geometry: str = 'planar', radius_norm: float = 2.0, camera_model: CameraModel | CameraCalibration | None = None, gaze_convention: str = 'camera_xyz', pool_mode: str = 'avg', gauss_sigma: float = 10.0) None[source]
Initialize the pooling sampler.
- Parameters:
fov (float) – Field of view diameter in degrees.
cmf_a (float) – A parameter from the CMF: M(r)=1/(r+a). Smaller = stronger foveation.
resolution (int) – Resolution parameter.
res_mult (float, optional) – Resolution multiplier for photoreceptor layer vs. rgc layer. Defaults to 3.
cmf_a_mult (float, optional) – CMF_a multiplier for photoreceptor layer vs. rgc layer. Defaults to 1.
fixation_size (int, optional) – Fixation size in pixels. Defaults to 3000.
k (int, optional) – Neighborhood size; for grid2d, the squared kernel side. None uses the sample-count ratio or squared resolution ratio respectively.
style (str, optional) – Sampling style. Defaults to ‘isotropic’.
sample_cortex (bool, optional) – Whether to sample from cortex. Defaults to True.
dtype (torch.dtype, optional) – Data type. Defaults to torch.float.
device (str, optional) – Device to run on. Defaults to ‘cuda’.
camera_model – Source calibration required for spherical image sampling.
gaze_convention – Gaze rotation convention for calibrated sampling.
pool_mode – Average or Gaussian weighted pooling.
gauss_sigma – Gaussian sigma in normalized neighborhood-distance units.
- property camera_model: CameraModel | None
Calibration owned by the high-resolution image sampler.
- forward(img, fix_loc=None, fixation_size=None, direct=False, rotation=None)[source]
Sample the high-resolution retina and pool to the output grid.
- Parameters:
img (torch.Tensor) – Input image tensor.
fix_loc (torch.Tensor, optional) – Fixation location. Defaults to None.
fixation_size (torch.Tensor, optional) – Fixation size. Defaults to None.
- Returns:
Pooled samples from KNN grid sampling.
- Return type: