ComfyUI Node

NNT Image To Tensor

Your bridge from pixels to training data

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Image To Tensor
  • image
  • TENSOR
width512
height512
cropFalse
color_modeRGB
flattenFalse

ComfyUI thinks in IMAGE tensors - [batch, height, width, channels], values 0–1. The Neural Network Toolkit thinks in TENSORs it can train on. NNT Image To Tensor is the adapter between those two worlds: it takes a ComfyUI image and converts it into the tensor shape and layout your NNT model expects. If you're building a model that classifies images you already have in your ComfyUI workflow, this node is your front door.

What it actually does

It takes the input IMAGE (shape [B, H, W, C], range 0–1), and for each image in the batch: converts to a PIL image, applies color mode, resizes, optionally crops, and repacks it as a tensor. The output TENSOR is channels-first ([C, H, W] per image, batched up) - the convention your conv layers expect. Resizing uses LANCZOS by default for quality downsampling.

Inputs that matter

  • image - any ComfyUI image output: a LoadImage result, a generated image, whatever.
  • width / height - the target size, both defaulting to 512. Resize happens first; if you also want a crop, read on.
  • crop - False (default) just stretches the image to width×height. True does a center crop: it first resizes to fit, then crops a width×height window from the middle. Use True when you don't want distortion but need an exact size.
  • color_mode - RGB or Grayscale. Grayscale is the classic choice for MNIST-style tasks and for shrinking your model's input; set it and your tensor drops to a single channel.
  • flatten - True squashes each image into a flat vector, which is what you want if you're skipping conv layers and going straight to a dense classifier (the pack's linear regression example takes a similar route).

The one gotcha

Channel order. ComfyUI images are [H, W, C]; NNT tensors need [C, H, W]. The node handles that swap for you, so you don't think about it - but if you ever hand a tensor to a node that expects ComfyUI's layout back, remember the order is different. Also mind the 0-1 scale: the conversion keeps values in 0–1 (it doesn't blindly multiply to 0–255), so don't add your own normalization unless your model needs it. For a model built with NntInputLayer as [3, 224, 224], set width/height to 224 and feed the output straight in.

Installing NNT

Part of inventorado/ComfyUI_NNT. ComfyUI Manager (search "ComfyUI Neural Network Toolkit") or:

cd ComfyUI/custom_nodes
git clone https://github.com/inventorado/ComfyUI_NNT.git
cd ComfyUI_NNT
pip install -r requirements.txt

Restart ComfyUI after. The requirements are a heavy scientific stack (torch, numpy, scikit-learn, Pillow, pandas, transformers), so the first install is chunky. The pack's example workflows also want ComfyUI-Jjk-Nodes for text output; Manager's "Install Missing Custom Nodes" covers it.

CategoryNNT Neural Network Toolkit/Tensors

Inputs (6)

NameTypeDefaultDescription
imageIMAGE
widthINT5121–4096
heightINT5121–4096
cropCOMBOFalse2 options: False, True
color_modeCOMBORGB2 options: RGB, Grayscale
flattenCOMBOFalse2 options: False, True

Outputs (1)

NameTypeDescription
TENSORTENSOR