ComfyUI Node

Load ResNet18

The boring load node that quietly downloads 45 MB your first run

By bemoregt·Created 6 months ago·Updated 6 months ago· 1
Load ResNet18
    • model
    pretrained

    Every node in the bemoregt/ComfyUI_AdversarialAttack pack needs a classifier to argue with, and this is the node that supplies it. LoadResNet18 is the most boring of the three - it doesn't attack anything, it doesn't classify anything, it just loads a ResNet18 and hands it downstream. But it's the one that does the heavy lifting of getting the model into the graph, and it's where the pack's only real download happens, so it's worth a minute of your time.

    What it is

    A single-output loader: pretrained is the only input (an enum with two choices), and the output is model, typed RESNET_MODEL. That model plugs into both Adversarial Attack (FGSM/PGD) and Classify Image (ResNet18). The pack ships one loader, and you typically create one instance and share it with both the attack and the classifier so they're all reasoning about the same network.

    Under the hood it's torchvision.models.resnet18() - the standard ImageNet pretrained architecture, 11 million parameters, 224×224 input. It's loaded in evaluation mode and carries the ImageNet label list with it, which is how the other two nodes turn raw class indices into names like "bald eagle" instead of 322.

    The download you'll hit

    Choose pretrained (the default choice in the enum) and the first time the graph runs, torchvision downloads the ImageNet weights from PyTorch's servers and caches them. That's tens of megabytes - nothing like the multi-GB checkpoints you're used to - but it does mean your very first run needs internet. If you're running ComfyUI somewhere offline or behind a proxy, this is where it'll choke, and the error will look like the loader failing even though the real problem is a blocked download. Fix the connection and re-run.

    The random trap

    The second enum choice, random, loads a ResNet18 with randomly initialized weights. That is a trap for almost everyone. A random network has no idea what anything is, class-name lookup falls back to bare indices, and running an adversarial attack against it is meaningless - you'd be testing noise against noise. Unless you're deliberately experimenting with untrained models, keep it on pretrained. There's a reason the README lists it as a curiosity, not a feature.

    How it actually loads

    One implementation detail worth knowing because it explains why the pack behaves well on current ComfyUI: ComfyUI runs nodes inside torch.inference_mode(), which freezes model parameters into tensors that can't be backpropagated through later. Since the attack node needs gradients, this loader deliberately spins up a fresh thread - PyTorch's grad mode is thread-local - so the model comes back with normal, gradient-capable parameters. If this pack ever breaks on a future ComfyUI update, it'll be because that dance stopped working.

    The flip side: the model loads on CPU and the pack has no device-switch node. The README's tip about "move the model to GPU for faster PGD" is real advice, but you have to do it some other way - out of the box, everything in this pack runs on CPU. That's fine for a single classification, but a 40-iteration PGD attack will take a noticeable while.

    Installation

    Install the pack once and all three nodes appear under the AdversarialAttack category. Easiest via ComfyUI Manager, searching "ComfyUI_AdversarialAttack". Manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/bemoregt/ComfyUI_AdversarialAttack
    

    then restart ComfyUI. Dependencies are torch>=2.0 and torchvision>=0.15 - both already present in any working ComfyUI, so there's no extra pip install to get wrong. If the loader errors on first use, check that the weights actually downloaded before you blame the node.

    CategoryAdversarialAttack

    Inputs (1)

    NameTypeDefaultDescription
    pretrainedCOMBO2 options: pretrained, random

    Outputs (1)

    NameTypeDescription
    modelRESNET_MODEL