ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

5 dimensions? Yeah right, I can handle three, maybe four, but once you go quantum I'm out

Open serget2 opened this issue 5 months ago • 2 comments

Custom Node Testing

Expected Behavior

To make some qwen images

Actual Behavior

I did manage to make some images with this but randomly it throws this out:

FaceDetailer

Expected NHWC tensor, but found 5 dimensions

Apparently my comfyUI makes contact with the twilight zone? Or the outer Limits? The multiverse?

Steps to Reproduce

Press queue prompt wait and hope it happens to you I guess?

Apparently that cropped refined and cropped_enhanced alpha from the facedetailer are the bits causing trouble? Sometimes they work sometimes they don;t, no idea what it does except make 2 black images and two images that are the same but different but still same?

Image

Debug Logs

2025-09-07T14:52:38.520022 - Requested to load WanVAE
2025-09-07T14:52:38.708015 - loaded completely 723.5501098632812 242.02829551696777 True
2025-09-07T14:52:38.833655 - [Impact Pack] vae decoded in 0.3s2025-09-07T14:52:38.833655 - 
2025-09-07T14:52:38.835652 - !!! Exception during processing !!! Expected NHWC tensor, but found 5 dimensions
2025-09-07T14:52:38.836652 - Traceback (most recent call last):
  File "D:\ComfyUI_windows_portable\ComfyUI\execution.py", line 496, in execute
    output_data, output_ui, has_subgraph, has_pending_tasks = await get_output_data(prompt_id, unique_id, obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb, hidden_inputs=hidden_inputs)
  File "D:\ComfyUI_windows_portable\ComfyUI\execution.py", line 315, in get_output_data
    return_values = await _async_map_node_over_list(prompt_id, unique_id, obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb, hidden_inputs=hidden_inputs)
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Lora-Manager\py\metadata_collector\metadata_hook.py", line 165, in async_map_node_over_list_with_metadata
    results = await original_map_node_over_list(
  File "D:\ComfyUI_windows_portable\ComfyUI\execution.py", line 289, in _async_map_node_over_list
    await process_inputs(input_dict, i)
  File "D:\ComfyUI_windows_portable\ComfyUI\execution.py", line 277, in process_inputs
    result = f(**inputs)
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\impact_pack.py", line 646, in doit
    enhanced_img, cropped_enhanced, cropped_enhanced_alpha, mask, cnet_pil_list = FaceDetailer.enhance_face(
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\impact_pack.py", line 600, in enhance_face
    DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for_bbox, max_size, seed, steps, cfg,
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\impact_pack.py", line 360, in do_detail
    enhanced_image, cnet_pils = core.enhance_detail(cropped_image, model, clip, vae, guide_size, guide_size_for_bbox, max_size,
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\core.py", line 402, in enhance_detail
    refined_image = utils.tensor_resize(refined_image, w, h)
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\utils.py", line 129, in tensor_resize
    _tensor_check_image(image)
  File "D:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack\modules\impact\utils.py", line 204, in _tensor_check_image
    raise ValueError(f"Expected NHWC tensor, but found {image.ndim} dimensions")
ValueError: Expected NHWC tensor, but found 5 dimensions

2025-09-07T14:52:38.838753 - Prompt executed in 276.36 seconds


## Attached Workflow
Please make sure that workflow does not contain any sensitive information such as API keys or passwords.

Workflow too large. Please manually upload the workflow from local file system.


## Additional Context
(Please add any additional context or steps to reproduce the error here)

Other

No response

serget2 avatar Sep 07 '25 12:09 serget2

Have you updated ComfyUI-Impact-Pack lately? https://github.com/ltdrdata/ComfyUI-Impact-Pack/issues/1066

Ken-g6 avatar Sep 12 '25 03:09 Ken-g6

Pasting what fixed it for me for when I need this again, I don't know jack squat about what I did just trusting Claude on this one.

Fix for NHWC 5D Tensor Error in AnimateDiff with Impact Pack

The Problem

When using AnimateDiff with Impact Pack's SEGSPaste node, you get this error:

ValueError: Expected NHWC tensor, but found 5 dimensions

This happens because AnimateDiff works with video frames stored as 5D tensors [batch, frames, height, width, channels], but SEGSPaste expects regular 4D images [batch, height, width, channels].

The Fix

In /modules/impact/animatediff_nodes.py, around line 189 in the DetailerForEachPipeForAnimateDiff.doit() method, we need to:

  1. Flatten the video frames from 5D to 4D by combining batch and frames into one dimension
  2. Flatten the cropped images inside each SEG object the same way
  3. Run SEGSPaste with the 4D data (which it expects)
  4. Reshape back to 5D to continue processing as video

This lets SEGSPaste work with AnimateDiff video tensors without breaking the existing workflow.

Code Changes

Replace the SEGSPaste call around line 189:

image_frames = SEGSPaste.doit(image_frames, enhanced_seg, feather, alpha=255)[0]

With this:

# FIX: Convert 5D to 4D for SEGSPaste, then back to 5D
original_ndim = image_frames.ndim
original_shape = image_frames.shape

if image_frames.ndim == 5:
    b, f, h, w, c = image_frames.shape
    image_frames = image_frames.reshape(b * f, h, w, c)

# Also fix enhanced_seg images to be 4D
fixed_enhanced_segs = []
for seg in enhanced_seg[1]:
    if seg.cropped_image is not None and seg.cropped_image.ndim == 5:
        sb, sf, sh, sw, sc = seg.cropped_image.shape
        fixed_cropped_image = seg.cropped_image.reshape(sb * sf, sh, sw, sc)
        fixed_seg = SEG(fixed_cropped_image, seg.cropped_mask, seg.confidence, 
                       seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper)
        fixed_enhanced_segs.append(fixed_seg)
    else:
        fixed_enhanced_segs.append(seg)

fixed_enhanced_seg = (enhanced_seg[0], fixed_enhanced_segs)

image_frames = SEGSPaste.doit(image_frames, fixed_enhanced_seg, feather, alpha=255)[0]

if original_ndim == 5:
    image_frames = image_frames.reshape(original_shape)

This resolves the dimension mismatch and allows AnimateDiff workflows to work properly with SEGSPaste.

Mister-Link avatar Nov 13 '25 21:11 Mister-Link