class
InstanceNorm2d
extends
_InstanceNormBaseInstanceNorm2d(num_features: int, eps: float = 1e-05, momentum: float = 0.1, affine: bool = False, track_running_stats: bool = False, device: DeviceLike = None, dtype: DTypeLike = None)Instance normalization for 4-D input (N, C, H, W).
Normalises each (n, c) slice over its spatial height and width
independently:
where and are computed over the spatial positions of sample , channel .
Unlike BatchNorm2d, the statistics do not mix across the
batch, making the output of each image entirely independent of the
other images in the batch. This property makes InstanceNorm2d
well-suited to image style transfer, generative models, and any
scenario where per-image normalization is desirable.
Parameters
num_featuresintNumber of feature channels .
epsfloat= 1e-05Numerical stability constant. Default:
1e-5.momentumfloat= 0.1EMA factor for optional running stats. Default:
0.1.affinebool= FalseIf
True, learns per-channel and .
Default: False.track_running_statsbool= FalseIf
True, maintains running mean/variance buffers.
Default: False.deviceDeviceLike= NoneDevice for parameters/buffers. Default:
None.dtypeDTypeLike= NoneData type for parameters/buffers. Default:
None.Attributes
weightParameter or NonePer-channel scale , shape
(num_features,).
None when affine=False.biasParameter or NonePer-channel shift , shape
(num_features,).
None when affine=False.running_meanTensor or NoneRunning mean per channel, shape
(num_features,).
None when track_running_stats=False.running_varTensor or NoneRunning variance per channel, shape
(num_features,).
None when track_running_stats=False.Notes
- Input:
- Output: — same shape.
- Instance Norm was introduced for style transfer, where each image should be normalized independently so that stylistic statistics from other images do not bleed through.
- At inference time,
weightandbiascan be replaced by style-conditioned tensors to implement Adaptive Instance Normalization (AdaIN).
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> norm = nn.InstanceNorm2d(64)
>>> x = lucid.randn(4, 64, 128, 128)
>>> out = norm(x)
>>> out.shape
(4, 64, 128, 128)
With affine parameters for conditional style transfer:
>>> norm_affine = nn.InstanceNorm2d(64, affine=True)
>>> out2 = norm_affine(x)
>>> out2.shape
(4, 64, 128, 128)