unaiverse.modules.cnu.psi
What this module does 🟢
Provides the psi feature-mapping function and 1D/2D resize helpers used by the CNU module to project input features into the fixed-size key space via interpolation and normalization.
psi
¶
█████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
A Collectionless AI Project (https://collectionless.ai)
Registration/Login: https://unaiverse.io
Code Repositories: https://github.com/collectionlessai/
Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
psi
¶
Apply a projection function (psi) that maps a tensor to a fixed-size key vector.
The psi function is a configurable projection layer used in Contextual Neural Units
(CNU) to encode an input tensor into a fixed-length representation of size
key_size. Several projection strategies are supported, ranging from flat
identity mappings to spatially-aware resizing and sign-binarisation. After
projection an optional L2 normalisation is applied along the feature dimension.
The mode determines which internal helper is called:
"identity": flatten the input and use it as-is."sign": flatten and binarise each element viatorch.sign."resize1d": 1-D linear interpolation (seeresize1d)."resize2d": 2-D bilinear interpolation then flatten (seeresize2d)."resize2d_sign": same as"resize2d"followed bytorch.sign.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input tensor of shape |
required | |
mode
|
Projection strategy. Must be one of |
required | |
key_size
|
Target dimensionality of the output key vector. The projected tensor must have exactly this many features after flattening. |
required | |
normalize
|
If |
True
|
Returns:
| Type | Description |
|---|---|
|
A tensor of shape |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
AssertionError
|
If the projection result does not match |
Examples:
>>> import torch
>>> x = torch.randn(4, 3, 8, 8) # batch of 4 RGB 8x8 images
>>> key = psi(x, mode="resize2d", key_size=64)
>>> key.shape
torch.Size([4, 64])
>>> key_bin = psi(x, mode="resize2d_sign", key_size=64, normalize=False)
>>> key_bin.shape
torch.Size([4, 64])
Source code in unaiverse/modules/cnu/psi.py
resize1d
¶
Resize a 1-D feature tensor to a target length via linear interpolation.
If the input already has the required number of features, it is returned
unchanged. Otherwise torch.nn.functional.interpolate is used with
mode="linear" to up- or down-sample along the feature dimension. The
channel dimension expected by interpolate is handled transparently with
unsqueeze/squeeze so the caller does not need to reshape the tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
I
|
Input tensor of shape |
required | |
key_size
|
Target number of features in the output. |
required |
Returns:
| Type | Description |
|---|---|
|
A tensor of shape |
|
|
interpolation. |
Source code in unaiverse/modules/cnu/psi.py
resize2d
¶
Resize a 4-D image tensor to a target flat key size via bilinear interpolation.
The target key_size is divided evenly across the channel dimension to yield
a per-channel spatial budget (spatial_key_size = key_size // c). A new
(height, width) is computed that keeps the original aspect ratio as closely
as possible while matching that budget. The image is then resized with bilinear
interpolation and flattened. If the aspect-ratio rounding causes the flattened
size to fall short of key_size, the remainder is zero-padded so the output
always has exactly key_size features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
I
|
Input tensor of shape |
required | |
key_size
|
Target number of features after flattening. Must be divisible by the number of channels for an exact spatial split. |
required |
Returns:
| Type | Description |
|---|---|
|
A tensor of shape |
|
|
resampled region (if any) are zero-padded. |