Ptn Pre Add Channel Axis
Make grayscale images look like 4D tensors before your conv net sees them
- model
- PTMODEL
Conv layers expect 4D input: (batch, channels, height, width). Grayscale images don't naturally arrive that way - a single image comes in as 3D (batch, height, width) with no channel axis at all. PtnPreAddChannelAxis is the adapter that fixes it: it wraps a model and, right before the forward pass, adds a channel axis to any 3D input, turning (batch, h, w) into (batch, 1, h, w). If your image pipeline hands you channel-less tensors and your conv net keeps complaining about rank, this is the node you were looking for.
How it works
It's a wrapper module that holds your model and intercepts the input. In forward, if the input has rank 3 it inserts a singleton dimension at position 1 (torch.unsqueeze(inputs, 1)) - that's the channel axis - and then passes the result to the wrapped model. If the input is already rank 4, it passes it straight through untouched. Either way, what you get back is the wrapped model's output, and the PTMODEL it emits is a drop-in replacement for the model it wraps.
The inputs
- model - any
PTMODEL. That's the only input, and the only output is the wrapped model. There are no knobs to tune.
It's the sibling of PtnPreFlatten (which flattens instead of adding a channel) and works best at the front of an image-classification chain: image → add channel → conv stack → classifier.
How you'd use it
Feed it a single-channel dataset like Fashion-MNIST or MNIST and a Ptn Conv 2d-based model, and it bridges the gap between the 3D tensor the dataset produces and the 4D tensor the conv net demands. Combined with PtnPreFlatten on the other end of the model (for the dense head), it makes the classic conv-classifier pipeline assemble without any shape gymnastics on your part.
Installing
Same as every node in this pack. ComfyUI Manager → search "Pt-Wrapper", or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
Restart ComfyUI; the pack's requirements install on first launch.
Where people get burned
The error message - "Inputs is not a rank 3 or 4 tensor" - is your main clue when this node is in the wrong spot. It only adds a channel to rank-3 input; if you feed it already-4D data it passes through untouched, which is fine, and if you feed it 2D data it errors. Also note it always adds exactly one channel: if your input has three channels that need to be preserved as-is, this node isn't the right tool and you should leave the data rank 4.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| model | PTMODEL | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| PTMODEL | PTMODEL | — |