NntDatasetToImageTensor
NNT Dataset To Image Tensor — turn a dataset's image column into a trainable tensor
- dataset
- image_tensor
- info
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 useimage; 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, orcustom.0-1is whatToTensorgives you for free.-1-1shifts the range, which some architectures prefer.customuses yourcustom_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_firstis[N, C, H, W], what PyTorch models want.channels_lastgives[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: Resize → ToTensor → 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_columndoesn't exist. Check the dataset's schema first (the loader's info output lists columns). - Shape surprises downstream - if your model expects
channels_firstand you left the default, you're fine; but if you flipdata_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.
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| dataset | DATASET | — | |
| image_column | STRING | image | — |
| target_size | INT | 22416–4096 | — |
| normalization | COMBO | 0-1 | 4 options: None, 0-1, -1-1, custom |
| custom_mean | STRING | [0.485, 0.456, 0.406] | — |
| custom_std | STRING | [0.229, 0.224, 0.225] | — |
| num_channels | INT | 31–4 | — |
| interpolation | COMBO | bilinear | 4 options: nearest, bilinear, bicubic, lanczos |
| data_format | COMBO | channels_first | 2 options: channels_first, channels_last |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image_tensor | TENSOR | — |
| info | STRING | — |