Nodes/ComfyUI-Pt-Wrapper/Ptn Instance Norm 2d
ComfyUI Node

Ptn Instance Norm 2d

InstanceNorm2d for image feature maps, as a ComfyUI node

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Instance Norm 2d
    • PTMODEL
    num_features1
    affinetrue
    track_running_statstrue
    momentum0.1000

    For image work, InstanceNorm is the normalization layer you pick when batch statistics don't make sense - style transfer, per-image normalization, or any setup where each sample should stand alone. PtnInstanceNorm2d wraps nn.InstanceNorm2d and drops it into your model graph as a PTMODEL brick. If your feature maps are 4D (batch, channels, height, width), this normalizes each channel of each image independently, which is a different deal from BatchNorm and often the more sensible one for per-image tasks.

    How it works

    For every sample in the batch, it computes mean and variance per channel over the spatial dimensions (H and W) and normalizes against them. That means a batch of 32 images is treated as 32 separate normalizations, not one pooled statistic - the property that makes it the go-to for tasks like neural style transfer, where batch-wide stats blur the per-image identity. If affine is on, each channel gets a learned scale and shift on top. With track_running_stats on, it also keeps a running mean/var for use in eval mode - but during training, the current sample's stats are always what's used, regardless of the flag.

    The inputs that matter

    • num_features - the channel count of your input. For [8, 4, 256, 256], that's 4. Get this wrong and shapes break immediately.
    • affine (default True) - learned per-channel scale and bias. Keep it on unless you're using this purely for normalization.
    • track_running_stats (default True) - maintain EMA running statistics for eval-mode normalization; momentum controls how much weight the current batch gets (higher momentum = more responsive).
    • momentum (default 0.1) - the EMA coefficient, only meaningful with track_running_stats.

    One PTMODEL output, ready to chain into a conv stack.

    How you'd use it

    Chain it after a Ptn Conv 2d block to stabilize training on image data where BatchNorm's batch-dependence is undesirable. Combined with the pack's PtnPreAddChannelAxis wrapper, it slots cleanly into models fed by the image-pipeline nodes. For most classification pipelines you'll reach for BatchNorm first, but when per-image statistics matter, this is the one.

    Installing

    Same as every node in this pack. ComfyUI Manager → search "Pt-Wrapper", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
    

    Restart ComfyUI; the pack's requirements install on first launch.

    Where people get burned

    num_features mismatches are the classic runtime crash - it must equal your channel count, not your spatial size. And a quieter trap: with track_running_stats=True in eval mode, the node normalizes against running statistics you may not have trained long enough to be meaningful. If you're doing per-image inference-style work, consider False so eval behaves like training.

    CategoryTraining

    Inputs (4)

    NameTypeDefaultDescription
    num_featuresINT11–1000000
    affineBOOLEANtrue
    track_running_statsBOOLEANtrue
    momentumFLOAT0.10000–1

    Outputs (1)

    NameTypeDescription
    PTMODELPTMODEL