ComfyUI Node

NntDatasetToImageTensor

NNT Dataset To Image Tensor — turn a dataset's image column into a trainable tensor

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NntDatasetToImageTensor
  • dataset
  • image_tensor
  • info
image_columnimage
target_size224
normalization0-1
custom_mean[0.485, 0.456, 0.406]
custom_std[0.229, 0.224, 0.225]
num_channels3
interpolationbilinear
data_formatchannels_first

Image classification in the Neural Network Toolkit starts with a dataset, and datasets store images as PIL objects or file paths - not as tensors. NntDatasetToImageTensor is the bridge: it pulls the image column out of a HuggingFace-style dataset, resizes, normalizes, and stacks everything into one big tensor your model can train on.

The inputs

  • dataset - a DATASET input. This comes from the pack's data-loading nodes (NntHuggingFaceDataLoader, NntTorchvisionDataLoader, NntFileLoader), not from a raw file path. You load the dataset first, then feed it here.
  • image_column (default image) - which column holds the images. Most HF image datasets use image; check yours.
  • target_size (default 224) - images are resized to a square of this size. Match it to what your model expects (32 for MNIST-style, 224 for ImageNet-style).
  • normalization (default 0-1) - None, 0-1, -1-1, or custom. 0-1 is what ToTensor gives you for free. -1-1 shifts the range, which some architectures prefer. custom uses your custom_mean/custom_std (defaults are the ImageNet stats [0.485, 0.456, 0.406] / [0.229, 0.224, 0.225]).
  • num_channels (default 3) - 3 for RGB, 1 for grayscale.
  • interpolation - nearest, bilinear (default), bicubic, or lanczos for the resize.
  • data_format (default channels_first) - channels_first is [N, C, H, W], what PyTorch models want. channels_last gives [N, H, W, C], what ComfyUI images use. Pick per your downstream node.

Outputs: image_tensor (TENSOR, the stacked batch) and info (STRING, a summary of count, shape, normalization, and format).

How it works

Under the hood it's a torchvision transforms.Compose: ResizeToTensor → optional Normalize, then it loops the dataset converting each image (opening it if it's a path or bytes) and stacks the results. RGB vs grayscale conversion happens per num_channels. The whole thing runs with gradients enabled, matching how the rest of the pack's data nodes behave.

Common issues

  • Wrong column name - the node errors cleanly if image_column doesn't exist. Check the dataset's schema first (the loader's info output lists columns).
  • Shape surprises downstream - if your model expects channels_first and you left the default, you're fine; but if you flip data_format, remember the model needs the matching layout.
  • Big datasets are slow - it resizes and converts every image in the dataset, every run. For the toy datasets this pack targets it's fine; don't feed it a 100k-image set.
  • First-run downloads - if the dataset itself comes from HuggingFace, the loader already handled the download; this node itself is offline.

Install

Pack-level, nothing node-specific:

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

or ComfyUI Manager → "ComfyUI Neural Network Toolkit NNT", restart, and it's under NNT Neural Network Toolkit/Data Processing. The pack's CIFAR10 and MNIST workflows show the full chain: loader → this node → labels node → compile → train.

CategoryNNT Neural Network Toolkit/Data Processing

Inputs (9)

NameTypeDefaultDescription
datasetDATASET
image_columnSTRINGimage
target_sizeINT22416–4096
normalizationCOMBO0-14 options: None, 0-1, -1-1, custom
custom_meanSTRING[0.485, 0.456, 0.406]
custom_stdSTRING[0.229, 0.224, 0.225]
num_channelsINT31–4
interpolationCOMBObilinear4 options: nearest, bilinear, bicubic, lanczos
data_formatCOMBOchannels_first2 options: channels_first, channels_last

Outputs (2)

NameTypeDescription
image_tensorTENSOR
infoSTRING