Custom Layers
Specialized neural network layers used as building blocks in core and readout modules.
Convolution Layers
STSeparableBatchConv3d
Spatio-temporal separable 3D convolution with batch-indexed temporal kernels.
STSeparableBatchConv3d
STSeparableBatchConv3d(
in_channels: int,
out_channels: int,
log_speed_dict: dict,
temporal_kernel_size: int,
spatial_kernel_size: int,
spatial_kernel_size2: int | None = None,
stride: int | tuple[int, int, int] = 1,
padding: int | str | tuple[int, ...] = 0,
num_scans: int = 1,
bias: bool = True,
subsampling_factor: int = 3,
)
Bases: Module
Spatio-temporal separable convolution layer for processing 3D data.
This layer applies convolution separately in the spatial and temporal dimensions, which is efficient for spatio-temporal data like video or medical images.
| ATTRIBUTE | DESCRIPTION |
|---|---|
in_channels |
Number of input channels.
TYPE:
|
out_channels |
Number of output channels.
TYPE:
|
temporal_kernel_size |
Size of the kernel in the temporal dimension.
TYPE:
|
spatial_kernel_size |
Size of the kernel in the spatial dimensions.
TYPE:
|
spatial_kernel_size2 |
Size of the kernel in the second spatial dimension.
TYPE:
|
stride |
Stride of the convolution.
TYPE:
|
padding |
Padding added to all sides of the input.
TYPE:
|
num_scans |
Number of scans for batch processing.
TYPE:
|
bias |
If True, adds a learnable bias to the output.
TYPE:
|
Initializes the STSeparableBatchConv3d layer.
| PARAMETER | DESCRIPTION |
|---|---|
in_channels
|
Number of channels in the input.
TYPE:
|
out_channels
|
Number of channels produced by the convolution.
TYPE:
|
log_speed_dict
|
Dictionary mapping data keys to log speeds.
TYPE:
|
temporal_kernel_size
|
Size of the temporal kernel.
TYPE:
|
spatial_kernel_size
|
Size of the spatial kernel.
TYPE:
|
spatial_kernel_size2
|
Size of the second spatial dimension of the kernel.
TYPE:
|
stride
|
Stride of the convolution. Defaults to 1.
TYPE:
|
padding
|
Zero-padding added to all sides of the input. Defaults to 0.
TYPE:
|
num_scans
|
Number of scans to process in batch. Defaults to 1.
TYPE:
|
bias
|
If True, adds a learnable bias to the output. Defaults to True.
TYPE:
|
Source code in openretina/modules/layers/convolutions.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
forward
forward(input_: tuple[Tensor, str] | Tensor) -> Tensor
Forward pass of the STSeparableBatchConv3d layer.
| PARAMETER | DESCRIPTION |
|---|---|
input_
|
Tuple containing the input tensor and the data key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
torch.Tensor: The output of the convolution. |
Source code in openretina/modules/layers/convolutions.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
TorchFullConv3D
TorchFullConv3D(
in_channels: int,
out_channels: int,
log_speed_dict: dict,
temporal_kernel_size: int,
spatial_kernel_size: int,
spatial_kernel_size2: Optional[int] = None,
stride: int = 1,
padding: int = 0,
bias: bool = True,
num_scans=1,
)
Bases: Module
Source code in openretina/modules/layers/convolutions.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
TorchSTSeparableConv3D
TorchSTSeparableConv3D(
in_channels: int,
out_channels: int,
log_speed_dict: dict,
temporal_kernel_size: int,
spatial_kernel_size: int,
spatial_kernel_size2: Optional[int] = None,
stride: int | tuple[int, int, int] = 1,
padding: int | tuple[int, int, int] | str = 0,
bias: bool = True,
num_scans=1,
)
Bases: Module
Source code in openretina/modules/layers/convolutions.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
compute_temporal_kernel
compute_temporal_kernel(
log_speed,
sin_weights,
cos_weights,
length: int,
subsampling_factor: int,
) -> Tensor
Computes the temporal kernel for the convolution.
| PARAMETER | DESCRIPTION |
|---|---|
log_speed
|
Logarithm of the speed factor.
TYPE:
|
sin_weights
|
Sinusoidal weights.
TYPE:
|
cos_weights
|
Cosine weights.
TYPE:
|
length
|
Length of the temporal kernel.
TYPE:
|
subsampling_factor
|
the factor by which to subsample the sin and cos weights
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
torch.Tensor: The temporal kernel. |
Source code in openretina/modules/layers/convolutions.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
Regularizers
Laplace
Laplace(
padding: int | None = None,
filter_size: int = 3,
persistent_buffer: bool = True,
)
Bases: Module
Laplace filter for a stack of data. Utilized as the input weight regularizer.
Laplace filter for a stack of data
Source code in openretina/modules/layers/regularizers.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
LaplaceL2norm
LaplaceL2norm(padding=None)
Bases: Module
Normalized Laplace regularizer for a 2D convolutional layer. returns |laplace(filters)| / |filters|
Source code in openretina/modules/layers/regularizers.py
202 203 204 | |
FlatLaplaceL23dnorm
FlatLaplaceL23dnorm(padding: int | None = None)
Bases: Module
Normalized Laplace regularizer for the spatial component of a separable 3D convolutional layer. returns |laplace(filters)| / |filters|
Source code in openretina/modules/layers/regularizers.py
153 154 155 | |
GaussianLaplaceL2
GaussianLaplaceL2(kernel, padding=None)
Bases: Module
Laplace regularizer, with a Gaussian mask, for a single 2D convolutional layer.
| PARAMETER | DESCRIPTION |
|---|---|
kernel
|
Size of the convolutional kernel of the filter that is getting regularized
|
padding
|
Controls the amount of zero-padding for the convolution operation.
TYPE:
|
Source code in openretina/modules/layers/regularizers.py
173 174 175 176 177 178 179 180 181 182 183 184 | |
Scaling Layers
Bias3DLayer
Bias3DLayer(channels: int, initial: float = 0.0, **kwargs)
Bases: Module
Source code in openretina/modules/layers/scaling.py
6 7 8 9 | |
Scale2DLayer
Scale2DLayer(
num_channels: int, initial: float = 1.0, **kwargs
)
Bases: Module
Source code in openretina/modules/layers/scaling.py
16 17 18 19 | |
FiLM
FiLM(num_features: int, cond_dim: int)
Bases: Module
FiLM (Feature-wise Linear Modulation) is a neural network module that applies conditional scaling and shifting to input features.
This module takes input features and a conditioning tensor, computes scaling (gamma) and shifting (beta) parameters from the conditioning tensor, and applies these parameters to the input features. The result is a modulated output that can adapt based on the provided conditions.
| PARAMETER | DESCRIPTION |
|---|---|
num_features
|
The number of features in the input tensor.
TYPE:
|
cond_dim
|
The dimensionality of the conditioning tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The modulated output tensor after applying the scaling and shifting. |
Source code in openretina/modules/layers/scaling.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
GRU Layers
ConvGRUCell
ConvGRUCell(
input_channels,
rec_channels,
input_kern: int,
rec_kern: int,
groups: int = 1,
gamma_rec: int = 0,
pad_input: bool = True,
**kwargs,
)
Bases: Module
Convolutional GRU cell from: https://github.com/sinzlab/Sinz2018_NIPS/blob/master/nips2018/architectures/cores.py
Source code in openretina/modules/layers/gru.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
GRU_Module
GRU_Module(
input_channels,
rec_channels,
input_kern,
rec_kern,
groups: int = 1,
gamma_rec: int = 0,
pad_input: bool = True,
**kwargs,
)
Bases: Module
A GRU module for video data to add between the core and the readout. Receives as input the output of a 3Dcore. Expected dimensions: - (Batch, Channels, Frames, Height, Width) or (Channels, Frames, Height, Width) The input is fed sequentially to a convolutional GRU cell, based on the frames channel. The output has the same dimensions as the input.
Source code in openretina/modules/layers/gru.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
forward
forward(input_)
Forward pass definition based on https://github.com/sinzlab/Sinz2018_NIPS/blob/3a99f7a6985ae8dec17a5f2c54f550c2cbf74263/nips2018/architectures/cores.py#L556 Modified to also accept 4 dimensional inputs (assuming no batch dimension is provided).
Source code in openretina/modules/layers/gru.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Ensemble
EnsembleModel
EnsembleModel(*members: Module)
Bases: Module
An ensemble model consisting of several individual ensemble members.
| ATTRIBUTE | DESCRIPTION |
|---|---|
*members |
PyTorch modules representing the members of the ensemble.
|
Initializes EnsembleModel.
Source code in openretina/modules/layers/ensemble.py
13 14 15 16 | |
forward
forward(x: Tensor, *args, **kwargs) -> Tensor
Calculates the forward pass through the ensemble.
The input is passed through all individual members of the ensemble and their outputs are averaged.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
A tensor representing the input to the ensemble.
TYPE:
|
*args
|
Additional arguments will be passed to all ensemble members.
DEFAULT:
|
**kwargs
|
Additional keyword arguments will be passed to all ensemble members.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A tensor representing the ensemble's output. |
Source code in openretina/modules/layers/ensemble.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Reducers
WeightedChannelSumLayer
WeightedChannelSumLayer(
init_channel_weights: tuple[float, ...],
trainable: bool = False,
)
Bases: Module
A layer that reduces multi-channel input to single-channel input by computing a weighted sum across the channel dimension using the provided weights. If the input only has a single channel, it will return it unchanged. One use case is of this layer is to convert a multi-color input into a grey-scale input to the model.
Source code in openretina/modules/layers/reducers.py
11 12 13 14 15 | |
forward
forward(x: Tensor) -> Tensor
If the input is not already single-channel (i.e. greyscale), take a weighted sum over channels .
Source code in openretina/modules/layers/reducers.py
17 18 19 20 21 22 23 24 25 | |