Skip to content

Allow sampling multiple patches from the same stack - #35

Merged
ziw-liu merged 4 commits into
21d-upscale-decoderfrom
multi-sample-collate
Aug 21, 2023
Merged

ziw-liu merged 4 commits into
21d-upscale-decoderfrom
multi-sample-collate

Conversation

@ziw-liu

@ziw-liu ziw-liu commented Aug 18, 2023

Copy link
Copy Markdown
Contributor

@mattersoflight and I had a discussion about potential data utilization problem when only sampling one patch from a stack at each training step. While with longer training schedule, all ROIs will still get sampled, this does waste a lot of I/O bandwidth, since most of the data loaded from disk is not used in the batch. This problem becomes more prominent when the stack depth increases, where even local NVME pools cannot keep the GPU fed with data.

This PR implements a new data module argument train_patches_per_stack, allowing sampling multiple patches from a single stack, effective only in training. The batch size has to be divisible by this number for collation to work. When I/O-bound, this can greatly speed up data-loading. Below is a test of single-precision batch tensor with size (32, 2, 5, 2048, 2048), worker number equals number of batches loaded (all 5 batches loaded in parallel, sharing I/O bandwidth). Data is stored on a Lustre server in the local infiniband network, and cache is warmed up by running the test several times before recording the result. Increasing train_patches_per_stack results in decreased total time. However the decrease is sub-linear, as the bottleneck shifts to compute.

train_patches_per_stack=1: 15869 function calls (15786 primitive calls) in 89.330 seconds
train_patches_per_stack=2: 13055 function calls (12972 primitive calls) in 49.898 seconds
train_patches_per_stack=4: 14321 function calls (14238 primitive calls) in 33.688 seconds
train_patches_per_stack=8: 14110 function calls (14027 primitive calls) in 24.769 seconds

However the train_patches_per_stack cannot be arbitrarily large. Sampling too many patches from the same stack reduces the variance of the batch, effectively reducing the batch size (in terms of statistical power). Also the chance of sampling background will increase. In practice this should be evaluated with batch size to avoid having too few stacks for a batch.

Below is the first 16 samples of the same batch when train_patches_per_stack=4 showing some 'batch effect' for samples from the same stack.

image

@ziw-liu ziw-liu added the enhancement New feature or request label Aug 18, 2023
@ziw-liu
ziw-liu marked this pull request as ready for review August 21, 2023 04:36
@ziw-liu

ziw-liu commented Aug 21, 2023

Copy link
Copy Markdown
Contributor Author

@mattersoflight I misinterpreted the training speed because as the epochs have train_patches_per_stack-times more batches. When taking that into account, this PR does make actual training faster.

@ziw-liu
ziw-liu requested review from edyoshikun and mattersoflight and removed request for edyoshikun and mattersoflight August 21, 2023 04:38
@ziw-liu
ziw-liu merged commit b232721 into 21d-upscale-decoder Aug 21, 2023
@ziw-liu
ziw-liu deleted the multi-sample-collate branch August 21, 2023 04:39
mattersoflight added a commit that referenced this pull request Aug 30, 2023
* pixelshuffle decoder

* Allow sampling multiple patches from the same stack (#35)

* sample multiple patches from one stack

* do not use type annotations from future
it breaks jsonargparse

* fix channel stacking for non-training samples

* remove batch size from model
the metrics will be automatically reduced by lightning

* add flop counting script

* 3d ouput head

* add datamodule target dims mode

* remove unused argument and configure drop path

* move architecture argument to model level

* DLMBL 2023 excercise (#36)

* updated intro and paths

* updated figures, tested data loader

* setup.sh fetches correct dataset

* finalized the exercise outline

* semi-final exercise

* parts 1 and 2 tested, part 3 outline ready

* clearer variables, train with larger patch size

* fix typo

* clarify variable names

* trying to log graph

* match example size with training

* reuse globals

* fix reference

* log sample images from the first batch

* wider model

* low LR solution

* fix path

* seed everything

* fix test dataset without masks

* metrics solution
this needs a new test dataset

* fetch test data, compute metrics

* byass cellpose import error due to numpy version conflicts

* final exercise

* moved files

* fixed formatting - ready for review

* viscy -> VisCy (#34) (#39)

Introducing capitalization to highlight vision and single-cell aspects of the pipeline.

* trying to log graph

* log graph

* black

---------

Co-authored-by: Shalin Mehta <shalin.mehta@gmail.com>
Co-authored-by: Shalin Mehta <shalin.mehta@czbiohub.org>

* fix channel dimension size for example input

#40

* fix argument linking

* 3D prediction writer
sliding windows are blended with uniform average

* update network diagram

* upgrade flop counting

* shallow 3D (2.5D) SSIM metric

* ms-ssim

* mixed loss

* fix arguments

* fix inheritance

* fix weight checking

* squeeze metric

* aggregate metrics

* optinal clamp to stabilize gradient of MS-SSIM

* fix calling

* increase epsilon

* disable autocast for loss

* restore relu for clamping

* plot all architectures with network_diagram script

---------
Co-authored-by: Shalin Mehta <shalin.mehta@czbiohub.org>
ziw-liu added a commit that referenced this pull request Nov 1, 2023
* pixelshuffle decoder

* Allow sampling multiple patches from the same stack (#35)

* sample multiple patches from one stack

* do not use type annotations from future
it breaks jsonargparse

* fix channel stacking for non-training samples

* remove batch size from model
the metrics will be automatically reduced by lightning

* add flop counting script

* 3d ouput head

* add datamodule target dims mode

* remove unused argument and configure drop path

* move architecture argument to model level

* DLMBL 2023 excercise (#36)

* updated intro and paths

* updated figures, tested data loader

* setup.sh fetches correct dataset

* finalized the exercise outline

* semi-final exercise

* parts 1 and 2 tested, part 3 outline ready

* clearer variables, train with larger patch size

* fix typo

* clarify variable names

* trying to log graph

* match example size with training

* reuse globals

* fix reference

* log sample images from the first batch

* wider model

* low LR solution

* fix path

* seed everything

* fix test dataset without masks

* metrics solution
this needs a new test dataset

* fetch test data, compute metrics

* byass cellpose import error due to numpy version conflicts

* final exercise

* moved files

* fixed formatting - ready for review

* viscy -> VisCy (#34) (#39)

Introducing capitalization to highlight vision and single-cell aspects of the pipeline.

* trying to log graph

* log graph

* black

---------

Co-authored-by: Shalin Mehta <shalin.mehta@gmail.com>
Co-authored-by: Shalin Mehta <shalin.mehta@czbiohub.org>

* fix channel dimension size for example input

#40

* fix argument linking

* 3D prediction writer
sliding windows are blended with uniform average

* update network diagram

* upgrade flop counting

* shallow 3D (2.5D) SSIM metric

* ms-ssim

* mixed loss

* fix arguments

* fix inheritance

* fix weight checking

* squeeze metric

* aggregate metrics

* optinal clamp to stabilize gradient of MS-SSIM

* fix calling

* increase epsilon

* disable autocast for loss

* shuffle validation data for logging

this hurts cache hit rate, but can avoid logging neighboring windows

* simplify decoder structure

* pop-head

* fix head expansion

* init conv weights

* update diagnostic scripts

* fix center slice metrics for 3D output (#51)

* Configure the number of  image samples logged at each epoch and batch (#49)

* log sample size at epoch and batch levels

* update example configs

* do not shuffle validation dataset

* fix upsampling weight initialization

* fix merge

* fix merge error

* fix formatting

---------

Co-authored-by: Shalin Mehta <shalin.mehta@gmail.com>
Co-authored-by: Shalin Mehta <shalin.mehta@czbiohub.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant