fovi.arch.knnresnet

class fovi.arch.knnresnet.KNNResNetBasicBlock(in_channels, out_channels, k, in_res, stride, fov, cmf_a, style='isotropic', conv_layer=KNNConvLayer, cart_res=None, norm_type='batch', arch_flag='', sample_cortex=True, device='cuda', auto_match_cart_resources=0)[source]

Bases: Module

Basic block for KNN-based ResNet architecture.

This block implements a residual connection with two KNN convolution layers, following the standard ResNet basic block design but using KNN convolutions instead of standard convolutions.

Parameters:
  • in_channels (int) – Number of input channels.

  • out_channels (int) – Number of output channels.

  • k (int) – Number of nearest neighbors for KNN convolution.

  • in_res (int) – Input resolution.

  • stride (int) – Stride for the first convolution layer.

  • fov (float) – Field of view parameter.

  • cmf_a (float) – Cortical magnification factor parameter.

  • style (str, optional) – Sampling style. Defaults to ‘isotropic’.

  • conv_layer (class, optional) – Convolution layer class to use. Defaults to KNNConvLayer.

  • cart_res (int, optional) – Cartesian resolution. Defaults to None.

  • norm_type (str, optional) – Normalization type. Defaults to ‘batch’.

  • arch_flag (str, optional) – Architecture flag. Defaults to ‘’.

  • sample_cortex (bool, optional) – Whether to sample cortex. Defaults to True.

  • device (str, optional) – Device to use. Defaults to ‘cuda’.

  • auto_match_cart_resources (int, optional) – Auto-match cartesian resources. Defaults to 0.

__init__(in_channels, out_channels, k, in_res, stride, fov, cmf_a, style='isotropic', conv_layer=KNNConvLayer, cart_res=None, norm_type='batch', arch_flag='', sample_cortex=True, device='cuda', auto_match_cart_resources=0)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Forward pass through the basic block.

Parameters:

x (torch.Tensor) – Input tensor.

Returns:

Output tensor after applying residual connection.

Return type:

torch.Tensor

class fovi.arch.knnresnet.KNNResNet(block=KNNResNetBasicBlock, layers=[2, 2, 2, 2], in_conv_stride=2, in_pool_stride=2, fov=16, cmf_a=0.5, in_res=64, out_res=1, style='isotropic', conv_layer=KNNConvLayer, pool_layer=KNNPoolingLayer, norm_type='batch', arch_flag='', sample_cortex=True, device='cuda', auto_match_cart_resources=0, num_classes=None)[source]

Bases: Module

KNN-based ResNet architecture.

A ResNet implementation that uses KNN convolutions instead of standard convolutions, designed for foveated vision processing.

Parameters:
  • block (class, optional) – Block class to use for layers. Defaults to KNNResNetBasicBlock.

  • layers (list, optional) – Number of blocks in each layer. Defaults to [2, 2, 2, 2].

  • in_conv_stride (int, optional) – Stride for initial convolution. Defaults to 2.

  • in_pool_stride (int, optional) – Stride for initial pooling. Defaults to 2.

  • fov (float, optional) – Field of view parameter. Defaults to 16.

  • cmf_a (float, optional) – Cortical magnification factor parameter. Defaults to 0.5.

  • in_res (int, optional) – Input resolution. Defaults to 64.

  • out_res (int, optional) – Output resolution. Defaults to 1.

  • style (str, optional) – Sampling style. Defaults to ‘isotropic’.

  • conv_layer (class, optional) – Convolution layer class to use. Defaults to KNNConvLayer.

  • pool_layer (class, optional) – Pooling layer class to use. Defaults to KNNPoolingLayer.

  • norm_type (str, optional) – Normalization type. Defaults to ‘batch’.

  • arch_flag (str, optional) – Architecture flag. Defaults to ‘’.

  • sample_cortex (bool, optional) – Whether to sample cortex. Defaults to True.

  • device (str, optional) – Device to use. Defaults to ‘cuda’.

  • auto_match_cart_resources (int, optional) – Auto-match cartesian resources. Defaults to 0.

  • num_classes (int, optional) – Number of output classes for classification. If None, no classification head is added. Defaults to None.

__init__(block=KNNResNetBasicBlock, layers=[2, 2, 2, 2], in_conv_stride=2, in_pool_stride=2, fov=16, cmf_a=0.5, in_res=64, out_res=1, style='isotropic', conv_layer=KNNConvLayer, pool_layer=KNNPoolingLayer, norm_type='batch', arch_flag='', sample_cortex=True, device='cuda', auto_match_cart_resources=0, num_classes=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

_make_layer(block, planes, blocks, stride=1)[source]

Create a layer with the specified number of blocks.

Parameters:
  • block (class) – Block class to use for the layer.

  • planes (int) – Number of output channels for the layer.

  • blocks (int) – Number of blocks in the layer.

  • stride (int, optional) – Stride for the first block. Defaults to 1.

Returns:

Sequential container with the layer blocks.

Return type:

nn.Sequential

forward(x)[source]

Forward pass through the KNN ResNet.

Parameters:

x (torch.Tensor) – Input tensor of shape (batch_size, 3, height, width).

Returns:

Output tensor. If num_classes is specified, returns

classification logits. Otherwise, returns feature embeddings.

Return type:

torch.Tensor