Commit c575f22
committed
perf: skip redundant convert_to_tensor for MetaTensor strip
Several transforms invoke ``convert_to_tensor`` twice on the same input:
once with ``track_meta=get_track_meta()`` to keep the MetaTensor wrapper,
then again with ``track_meta=False`` to obtain a plain-tensor view for the
actual computation. The second call routes through
``_convert_tensor(data).to(dtype=..., device=..., memory_format=contiguous_format)``
(``monai/utils/type_conversion.py``) and is functionally equivalent to
calling ``MetaTensor.as_tensor()`` for the purpose intended here, since
the first call already enforced ``contiguous_format``.
Replace the second call with an explicit ``as_tensor()`` strip:
img_t = img.as_tensor() if isinstance(img, MetaTensor) else img
This avoids a redundant ``convert_to_tensor`` dispatch and a no-op
``Tensor.to(memory_format=contiguous_format)`` per ``__call__``. Sites
touched (all in transform ``__call__``, so per-sample per-epoch):
* ``KeepLargestConnectedComponent`` (post/array.py)
* ``RemoveSmallObjects`` (post/array.py)
* ``LabelToContour`` (post/array.py)
* ``ScaleIntensity`` (intensity/array.py)
* ``ScaleIntensityFixedMean`` (intensity/array.py)
* ``ClipIntensityPercentiles`` (intensity/array.py)
* ``NormalizeIntensity`` (intensity/array.py)
* ``SavitzkyGolaySmooth`` (intensity/array.py)
* ``RandHistogramShift`` (intensity/array.py)
* ``KSpaceSpikeNoise`` (intensity/array.py)
* ``IntensityRemap`` (intensity/array.py)
No behavioral change: the resulting tensor is the same underlying data
(``as_tensor()`` returns the wrapped ``torch.Tensor`` without copying).
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>1 parent a4d1a0d commit c575f22
2 files changed
Lines changed: 13 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
483 | 484 | | |
484 | 485 | | |
485 | 486 | | |
486 | | - | |
| 487 | + | |
487 | 488 | | |
488 | 489 | | |
489 | 490 | | |
| |||
542 | 543 | | |
543 | 544 | | |
544 | 545 | | |
545 | | - | |
| 546 | + | |
546 | 547 | | |
547 | 548 | | |
548 | 549 | | |
| |||
1168 | 1169 | | |
1169 | 1170 | | |
1170 | 1171 | | |
1171 | | - | |
| 1172 | + | |
1172 | 1173 | | |
1173 | 1174 | | |
1174 | 1175 | | |
| |||
1433 | 1434 | | |
1434 | 1435 | | |
1435 | 1436 | | |
1436 | | - | |
| 1437 | + | |
1437 | 1438 | | |
1438 | 1439 | | |
1439 | 1440 | | |
| |||
1530 | 1531 | | |
1531 | 1532 | | |
1532 | 1533 | | |
1533 | | - | |
| 1534 | + | |
1534 | 1535 | | |
1535 | 1536 | | |
1536 | 1537 | | |
| |||
1907 | 1908 | | |
1908 | 1909 | | |
1909 | 1910 | | |
1910 | | - | |
| 1911 | + | |
1911 | 1912 | | |
1912 | 1913 | | |
1913 | 1914 | | |
| |||
1952 | 1953 | | |
1953 | 1954 | | |
1954 | 1955 | | |
1955 | | - | |
| 1956 | + | |
1956 | 1957 | | |
1957 | 1958 | | |
1958 | 1959 | | |
| |||
2604 | 2605 | | |
2605 | 2606 | | |
2606 | 2607 | | |
2607 | | - | |
| 2608 | + | |
2608 | 2609 | | |
2609 | 2610 | | |
2610 | 2611 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
341 | | - | |
| 342 | + | |
342 | 343 | | |
343 | 344 | | |
344 | 345 | | |
| |||
497 | 498 | | |
498 | 499 | | |
499 | 500 | | |
500 | | - | |
| 501 | + | |
501 | 502 | | |
502 | 503 | | |
503 | 504 | | |
| |||
623 | 624 | | |
624 | 625 | | |
625 | 626 | | |
626 | | - | |
| 627 | + | |
627 | 628 | | |
628 | 629 | | |
629 | 630 | | |
| |||
0 commit comments