Nodes/ComfyUI-Pt-Wrapper/Ptn Max Pool 2d
ComfyUI Node

Ptn Max Pool 2d

Max pooling for 2D feature maps, as a string-typed node

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Max Pool 2d
    • PTMODEL
    kernel_size2
    stride2
    padding0
    dilation1

    Max pooling is the classic downsampler in conv nets - slide a window over the feature map and keep the maximum value in each patch. PtnMaxPool2d wraps nn.MaxPool2d so you can drop one into your model graph between conv blocks. It's a pure downsampling layer: no learned parameters, it just shrinks the spatial dimensions while preserving the channel count. You reach for it in the same place you'd reach for it in raw PyTorch - after a couple of conv layers, before the flatten-to-classifier step.

    How it works

    Each kernel_size × kernel_size window is replaced by its single maximum value, and the window slides by stride. The output width/height follows the usual formula: (W - kernel + 2*padding) / stride + 1. dilation spreads the window's taps apart to widen the receptive field without growing the kernel. It's a nn.MaxPool2d under the hood, so every behavior - including the fact that it only affects the last two dimensions and leaves batch and channel alone - is exactly PyTorch's.

    One quirk this node inherits from the pack: kernel_size, stride, padding, and dilation are all strings, not ints. They accept either a plain "2" or a bracketed list like "[2, 3]" for asymmetric kernels, parsed by the pack's str_to_dim helper.

    The inputs that matter

    • kernel_size (default "2") - window size; "2" or "[2, 3]" both work.
    • stride (default "2") - the slide step. Defaults to kernel_size in PyTorch when left unset, but here the node passes whatever you type, so if you change the kernel remember to check the stride too.
    • padding (default "0") - zeros added around the input before pooling.
    • dilation (default "1") - spacing between kernel taps; leave it unless you know you need it.

    Output is one PTMODEL, which chains straight into the next conv block or a flatten/classifier.

    How you'd use it

    The typical image-classification stack is conv → ReLU → max pool, repeated a couple of times, then PtnPreFlatten + PtnLinearModel. Pooling after each conv stage is what progressively shrinks a [8, 32, 64, 64] map down to something small enough for a dense head. In this pack it works best as one more brick in a Ptn Chained Model stack.

    Installing

    Same as every node in the 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

    The string-typed inputs trip people up more than anything else - it's easy to type 2 (an int) into a field that wants "2" (a string). And because stride doesn't auto-follow the kernel here the way it does in raw PyTorch's default, a non-square kernel_size with an unchanged stride produces a non-square output that breaks the next layer's expectations. If you see a shape error two blocks down the chain, check stride first.

    CategoryTraining

    Inputs (4)

    NameTypeDefaultDescription
    kernel_sizeSTRING2
    strideSTRING2
    paddingSTRING0
    dilationSTRING1

    Outputs (1)

    NameTypeDescription
    PTMODELPTMODEL