Skip to content

Batched transforms#282

Merged
ziw-liu merged 70 commits into
mainfrom
batched-transforms
Sep 17, 2025
Merged

Batched transforms#282
ziw-liu merged 70 commits into
mainfrom
batched-transforms

Conversation

@ziw-liu

@ziw-liu ziw-liu commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Add more batched transforms.

Tested (now includes all DynaCLR transforms, along with #276):

  • Random adjust contrast
  • Random spatial crop
  • Center spatial crop
  • Random flip
  • Random Gaussian noise
  • Random scale intensity

See an illustration of these in the new example notebook.

Not tested (#284):

  • Random 3D elastic
  • Random histogram shift
  • Random local pixel shuffle
  • Random sharpen
  • Random Z stack shift

Also updated the triplet dataset and datamodule for end-to-end batching. Now the dataset only handles I/O (in thread workers), and all the transforms are applied to the batched tensor on the GPU in the main thread. Since tensorstore manages its own thread pool, it is recommended to set num_workers to 1.

ritvikvasan and others added 3 commits August 23, 2025 22:51
* Add batched transforms for MAE training

- BatchedRandFlipd
- BatchedRandSharpend
- BatchedRandLocalPixelShufflingd
- BatchedRandHistogramShiftd
- BatchedRandZStackShiftd
- BatchedRand3DElasticd

* Revert existing files to original state

Keep only new transform files and necessary imports

* Fix import sorting in new transform files

* Fix import formatting in __init__.py

* Format code with ruff formatter

* bugfix for device

* ruff format fix
@ziw-liu ziw-liu added the enhancement New feature or request label Aug 24, 2025
@ziw-liu ziw-liu added this to the v0.4.0 milestone Aug 24, 2025
@ziw-liu ziw-liu added the representation Representation learning (SSL) label Sep 6, 2025
@ziw-liu
ziw-liu marked this pull request as ready for review September 8, 2025 16:18
Comment thread viscy/data/triplet.py
Comment thread viscy/data/triplet.py Outdated
Comment thread viscy/data/triplet.py
Comment thread viscy/data/triplet.py
Comment thread viscy/data/cell_classification.py

@edyoshikun edyoshikun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont have tests for some of the transforms (ie 3d elastic, histogram shift, local pixel shuffle, sharpen, and z_stack shift).

Isn't the z_stack shift just an affine?

@edyoshikun edyoshikun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a better way to optimize the funciotns and I really liked the tests.

@ziw-liu

ziw-liu commented Sep 17, 2025

Copy link
Copy Markdown
Contributor Author

we dont have tests for some of the transforms (ie 3d elastic, histogram shift, local pixel shuffle, sharpen, and z_stack shift).

These were contributions from #284.

Isn't the z_stack shift just an affine?

Should be numerically equivalent to a translation. But if this is always aligned in index space then it avoids resampling, which would be faster than affine.

@ziw-liu
ziw-liu merged commit 1e5381d into main Sep 17, 2025
4 checks passed
@ziw-liu
ziw-liu deleted the batched-transforms branch September 17, 2025 18:29
edyoshikun added a commit that referenced this pull request Sep 19, 2025
* add test for scale intensity range percentiles transform

* randomize test data

* add decollate

* detect leading slashes

* configurable label column

* fix typo

* configurable example shape

* type hint

* fix example array

* format

* auto-align validation fov names

* Adding some batched transforms for MAE training (#284)

* Add batched transforms for MAE training

- BatchedRandFlipd
- BatchedRandSharpend
- BatchedRandLocalPixelShufflingd
- BatchedRandHistogramShiftd
- BatchedRandZStackShiftd
- BatchedRand3DElasticd

* Revert existing files to original state

Keep only new transform files and necessary imports

* Fix import sorting in new transform files

* Fix import formatting in __init__.py

* Format code with ruff formatter

* bugfix for device

* ruff format fix

* wip: random crop

* format

* use gather operation for cropping

* register batched random crop

* use unfold and bench

* add dict version for random spatial crop

* lint

* benchmark crop

* rename flip file

* use tensorstore to stack

* rename in register

* add notebook to showcase transforms

* batched center crop

* batched gaussian noise

* test and add to notebook

* fix docstring

* test random flip

* add map version of batched gaussian noise

* fix noise scaling and shifting

* per-sample random flip

* fix gaussian blur

* rename blur to smooth to match monai

* update smoothing tests

* sigma-based truncation

* print timer results

* random adjust contrast

* wrap monai

* random scale intensity

* update tests

* allow final crop override

* add center crop to example

* remove redundant call method

* check gamma value

* use batched center crop

* fix import

* wip: fix gathering

* limit worker to 1

* wip: execute augmentations in data hook

* use the full loop for profiling

* wip: update concat wrapper

* fix negative sampling

* update tests

* revert to use threads

* cache tensorstore arrays

* update default num_workers

* skip transforms for example array

* relax flaky randomness test

* explicitly configure cache pool

* use new iohub API

* only check cache when opening

* fix transform initialization

* remove unecessary tipletdatamodule inits

* explain num_workers in the docstring

* updating paths for profiling

* fixed batch randintensityscale in the notebook

---------

Co-authored-by: Ritvik <40371793+ritvikvasan@users.noreply.github.com>
Co-authored-by: Eduardo Hirata-Miyasaki <edhiratam@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request representation Representation learning (SSL)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants