Nodes/ComfyDL/Residual Block
ComfyUI Node

Residual Block

The Lego brick of every modern CNN, as a node

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Residual Block
    • block
    num_channels64
    use_1x1convfalse
    strides1

    Every serious CNN you've ever heard of - ResNet, and everything that came after it - is built from one idea: the residual block. CdlResidual builds a single one as a ComfyDL model, ready to stack into a network. It's the Residual class from Dive into Deep Learning, exposed as a node, and it's your first real taste of architecting a network instead of just running one.

    The residual trick is almost insultingly simple and it changed deep learning: instead of forcing a layer to learn H(x) from scratch, let the block learn the difference F(x) = H(x) - x, and add the input back. Because the network can always fall back to "copy the input through" (the identity, F(x) = 0), deeper networks stopped degrading - that's the entire reason ResNet-50 has 50 layers where pre-residual nets choked at ~20. ComfyDL lets you hold that mechanism in your hand, one node at a time.

    What the block contains

    • Two Conv2d + BatchNorm + ReLU pairs, sequentially.
    • An optional 1×1 shortcut convolution that reshapes the input to match the output (needed when channel count or spatial size changes).
    • The skip connection that adds input to output.

    The inputs

    • num_channels - output channels for the block (default 64). This defines the block's width.
    • use_1x1conv - toggle the shortcut conv. Turn this on whenever strides > 1 or the channels change between input and output; without it, the shapes won't match and the add will fail. This is the textbook convention and it's the setting beginners get wrong first.
    • strides - convolution stride (1–4). Stride 2 is the classic downsampling move: halves the spatial size, and forces use_1x1conv = True.

    Output: block, a cdlModel you can stack, feed data through with a forward-pass node, or hand to an optimizer.

    Installing ComfyDL

    Same one-line install as the whole pack:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Cynthia-lxx/ComfyDL
    cd ComfyDL && pip install -r requirements.txt
    

    Restart ComfyUI; it's under ComfyDL → CV Models. Or search "ComfyDL" in ComfyUI Manager. Only dependency is matplotlib; the model weights are random-initialized at build time, so there's nothing to download.

    The part people get wrong

    The 1×1 conv is not optional decoration - it's the shape-matching mechanism. Build a block with num_channels=128 feeding from a 64-channel input, or a stride-2 block, and you will hit a shape error unless use_1x1conv is on. The d2l convention: toggle it whenever stride or channels change. When stride is 1 and channels stay the same, the identity shortcut works as-is and you leave it off.

    Also remember these are untrained, random-init blocks - this is about understanding and building architectures, not loading a pretrained ResNet. If you want a whole network in one node rather than stacking blocks by hand, the pack's ResNet-18 node does exactly that with the same math underneath. Build a block by hand first, though - the point of this pack is that you see the pieces.

    Categoryd2l/CV Models

    Inputs (3)

    NameTypeDefaultDescription
    num_channelsINT641–2048
    use_1x1convBOOLEANfalse
    stridesINT11–4

    Outputs (1)

    NameTypeDescription
    blockcdlModel