Commit c434607
Fix TrainableBilateralFilter 3D input validation (Project-MONAI#7444) (Project-MONAI#8729)
- Fix dimension comparison to use spatial dims instead of total dims
- Add validation for minimum input dimensions
- Fix typo in error message (ken_spatial_sigma -> len_spatial_sigma)
- Move spatial dimension validation before unsqueeze operations
The forward() method was incorrectly comparing self.len_spatial_sigma
(number of spatial dimensions) with len(input_tensor.shape) (total
dimensions including batch and channel), causing valid 3D inputs to be
rejected.
Fixes Project-MONAI#7444
### Description
This PR fixes a validation bug in `TrainableBilateralFilter` that
incorrectly rejected valid 3D inputs with shape `(B, C, H, W, D)`.
**Root Cause:** The `forward()` method compared `self.len_spatial_sigma`
(spatial dimensions = 3) with `len(input_tensor.shape)` (total
dimensions = 5), causing a dimension mismatch error for valid inputs.
**Solution:** Calculate `spatial_dims = len(input_tensor.shape) - 2` to
exclude batch and channel dimensions, then compare against
`self.len_spatial_sigma`.
**Example of fixed behavior:**
```python
# Previously failed, now works
bf = TrainableBilateralFilter([1.0, 1.0, 1.0], 1.0)
x = torch.randn(1, 1, 10, 10, 10) # (B, C, H, W, D)
out = bf(x) # Success!
```
This fix also improves error messages and adds validation for inputs
with insufficient dimensions.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.
### Notes on Testing
The existing unit tests for `TrainableBilateralFilter` (24 tests)
require the C++ extension and were skipped locally (expected behavior
with `@skip_if_no_cpp_extension` decorator). These tests will run
automatically in CI.
I verified the fix logic with custom local tests for 1D, 2D, and 3D
cases (see examples in description above).
Linting and code formatting checks passed:
```bash
./runtests.sh --autofix # Passed
./runtests.sh --codeformat # Passed
```
No new tests were added as the existing 24 unit tests already cover the
behavior. No docstring or documentation changes were needed as this is
purely a bug fix in validation logic.
---------
Signed-off-by: Abdoulaye Diallo <abdoulayediallo338@gmail.com>
Signed-off-by: Abdoulaye Diallo <113793273+getrichthroughcode@users.noreply.github.com>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 252d26e commit c434607
1 file changed
Lines changed: 32 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
| 224 | + | |
| 225 | + | |
225 | 226 | | |
226 | 227 | | |
227 | 228 | | |
| |||
231 | 232 | | |
232 | 233 | | |
233 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
234 | 239 | | |
235 | 240 | | |
236 | 241 | | |
| |||
239 | 244 | | |
240 | 245 | | |
241 | 246 | | |
| 247 | + | |
242 | 248 | | |
243 | 249 | | |
244 | | - | |
| 250 | + | |
245 | 251 | | |
246 | | - | |
| 252 | + | |
247 | 253 | | |
248 | 254 | | |
249 | | - | |
250 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
251 | 259 | | |
252 | 260 | | |
253 | 261 | | |
254 | 262 | | |
255 | 263 | | |
256 | 264 | | |
257 | | - | |
| 265 | + | |
258 | 266 | | |
259 | | - | |
| 267 | + | |
260 | 268 | | |
261 | 269 | | |
262 | 270 | | |
| |||
389 | 397 | | |
390 | 398 | | |
391 | 399 | | |
392 | | - | |
| 400 | + | |
| 401 | + | |
393 | 402 | | |
394 | 403 | | |
395 | 404 | | |
| |||
399 | 408 | | |
400 | 409 | | |
401 | 410 | | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
402 | 415 | | |
403 | 416 | | |
404 | | - | |
| 417 | + | |
405 | 418 | | |
406 | 419 | | |
407 | 420 | | |
408 | 421 | | |
409 | 422 | | |
410 | | - | |
411 | | - | |
| 423 | + | |
412 | 424 | | |
413 | 425 | | |
414 | 426 | | |
| 427 | + | |
415 | 428 | | |
416 | 429 | | |
417 | | - | |
| 430 | + | |
418 | 431 | | |
419 | 432 | | |
420 | | - | |
| 433 | + | |
421 | 434 | | |
422 | 435 | | |
423 | 436 | | |
424 | | - | |
425 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
426 | 441 | | |
427 | 442 | | |
428 | 443 | | |
429 | 444 | | |
430 | 445 | | |
431 | 446 | | |
432 | | - | |
| 447 | + | |
433 | 448 | | |
434 | | - | |
| 449 | + | |
435 | 450 | | |
436 | 451 | | |
437 | 452 | | |
0 commit comments