-
Notifications
You must be signed in to change notification settings - Fork 13.1k
nodes_morphology: implement with max-pooling. #13389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maromri
wants to merge
3
commits into
Comfy-Org:master
Choose a base branch
from
maromri:feature/implement-morphology-with-max-pool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+18
−9
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Comfy-Org/ComfyUI
Length of output: 178
🏁 Script executed:
cat -n comfy_extras/nodes_morphology.py | head -100Repository: Comfy-Org/ComfyUI
Length of output: 4498
🏁 Script executed:
Repository: Comfy-Org/ComfyUI
Length of output: 114
🏁 Script executed:
Repository: Comfy-Org/ComfyUI
Length of output: 49344
🏁 Script executed:
Repository: Comfy-Org/ComfyUI
Length of output: 4960
🏁 Script executed:
Repository: Comfy-Org/ComfyUI
Length of output: 43
Handle even kernels and border padding explicitly in pooling helpers.
max_pool2d(..., padding=kernel_size // 2, stride=1)is not shape-preserving for even kernels—the output spatial dimensions increase by 1. Since the schema accepts evenkernel_sizevalues (line 34:step=1from 3 to 999), operations liketop_hatandbottom_hatthat subtract pooled outputs from the original image will fail at runtime with shape mismatch.Additionally, the negation trick in
_max_pool_erodeapplies implicit zero-padding, which differs from proper geodesic erosion semantics where borders should be treated as infinity.💡 Proposed fix
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step=2would make more sense here IMO).This is because
F.max_pool2dpads with-inf, not zero, so the border semantics already matchkornia's geodesic padding.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maromri, thank you for the correction and for verifying empirically.Point 2 — border padding: You're absolutely right.
F.max_pool2dinternally initializes its sliding window accumulator to-inf, not0. This means the negation trick (-F.max_pool2d(-x, ...)) automatically gives+inf-equivalent border semantics for the erosion case, which is exactly what Kornia's geodesic erosion does. My concern there was unfounded — I apologize for the noise.Point 1 — even kernels: The shape-mismatch risk is real, and I agree
step=2is the cleaner fix for this context. Morphological operations with even-sized kernels lack a well-defined center pixel anyway, so restricting the input to odd values viastep=2is both semantically correct and prevents any downstream shape issues. The currentstep=1withmin=3still allows even values like 4, 6, etc.(ᵔ◡ᵔ)∫
✏️ Learnings added
🧠 Learnings used