Skip to content
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

Fix train metrics #868

Merged
merged 18 commits into from
Jul 19, 2024
Merged

Fix train metrics #868

merged 18 commits into from
Jul 19, 2024

Conversation

VukW
Copy link
Contributor

@VukW VukW commented May 15, 2024

Fixes: N.A.

Proposed Changes

  • The PR addresses a few important bugs in how metrics are calculated.
  • Because of how the batches are structured, during multi-batch training, only the first batch was used to calculate metrics.
  • The current codebase is fine with batch size of 1.
  • This has no effect on validation/testing/inference runs.

Checklist

  • CONTRIBUTING guide has been followed.
  • PR is based on the current GaNDLF master .
  • Non-breaking change (does not break existing functionality): provide as many details as possible for any breaking change.
  • Function/class source code documentation added/updated (ensure typing is used to provide type hints, including and not limited to using Optional if a variable has a pre-defined value).
  • Code has been blacked for style consistency and linting.
  • If applicable, version information has been updated in GANDLF/version.py.
  • If adding a git submodule, add to list of exceptions for black styling in pyproject.toml file.
  • Usage documentation has been updated, if appropriate.
  • Tests added or modified to cover the changes; if coverage is reduced, please give explanation.
  • If customized dependency installation is required (i.e., a separate pip install step is needed for PR to be functional), please ensure it is reflected in all the files that control the CI, namely: python-test.yml, and all docker files [1,2,3].

VukW added 5 commits May 9, 2024 01:03
1. return output of whole batch, not just one item
2. make ground truth & predictions array to take into account `q_samples_per_volume` (the whole dataset size during 1 epoch is equal to len(data) * q_samples_per_volume; so if dataset df contains 100 records and q_samples_per_volume = 10 (by default) and batch size is 4, there would be 250 batches by 4 elements
3. make ground truth take into account that train_dataloader is shuffled. So now ground truth is sorted in the same order as predictions and as train_dataloader.
To ensure values in csv are always written in the same order as header
Copy link
Contributor

github-actions bot commented May 15, 2024

MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅

@sarthakpati sarthakpati self-requested a review May 19, 2024 03:12
@sarthakpati sarthakpati marked this pull request as draft May 19, 2024 03:12
@sarthakpati
Copy link
Collaborator

Converting to draft until the tests are passing.

@VukW VukW force-pushed the fix_train_metrics branch from 65f3d8a to 3987439 Compare May 21, 2024 20:51
@VukW
Copy link
Contributor Author

VukW commented May 21, 2024

@sarthakpati It seems to me I fixed the code for usual segmentation cases.
However, I found that the code is essentially broken for some specific architectures - deep_* and sdnet, where model returns list instead of just one Tensor. The reason is we strongly assume output is Tensor (say, when we aggregate segmentation results during validation step).
In master branch the code is not failing but calculates valid metrics in wrong way:

  • we take just first elem of the list (one tensor)
  • AFAIU for sdnet it works only if batch_size >= 5 (as prediction here is BxCxHxWxD, we take not 1st piece of output but 1st batch element prediction) and still not properly
  • for deep_* models here also prediction is BxCxHxWxD and not a list of the tensors

I can't imagine right now how to fix that easily without massive refactoring. I returned the crutch back (so at least code doesn't fail right now). From one side a train metrics are calculated like averaging by sample and thus are calculated properly. From other side, validation metrics are broken. I'd strongly prefer to disable / remove these list architectures from GaNDLF for now; but what do you think?

@VukW VukW force-pushed the fix_train_metrics branch from f94cc15 to 26b33a9 Compare May 21, 2024 22:14
@VukW VukW marked this pull request as ready for review May 23, 2024 11:55
@VukW VukW changed the title [WIP] Fix train metrics Fix train metrics May 23, 2024
Geeks-Sid
Geeks-Sid previously approved these changes Jun 3, 2024
@sarthakpati sarthakpati requested a review from a team as a code owner June 4, 2024 15:12
@sarthakpati
Copy link
Collaborator

@szmazurek - can you confirm if the BraTS training is working for you?

@szmazurek
Copy link
Collaborator

@szmazurek - can you confirm if the BraTS training is working for you?

Re-launched the training after pulling yesterday's merge by @Geeks-Sid. Will keep you updated if it runs, keeping the rest the same.

@szmazurek
Copy link
Collaborator

szmazurek commented Jun 4, 2024

@szmazurek - can you confirm if the BraTS training is working for you?

Re-launched the training after pulling yesterday's merge by @Geeks-Sid. Will keep you updated if it runs, keeping the rest the same.

Still negative. the output:

Looping over training data:   0%|          | 0/6255 [02:51<?, ?it/s]
ERROR: Traceback (most recent call last):
  File "/net/tscratch/people/plgmazurekagh/gandlf/gandlf_env/bin/gandlf_run", line 126, in <module>
    main_run(
  File "/net/tscratch/people/plgmazurekagh/gandlf/gandlf_env/lib/python3.10/site-packages/GANDLF/cli/main_run.py", line 92, in main_run
    TrainingManager_split(
  File "/net/tscratch/people/plgmazurekagh/gandlf/gandlf_env/lib/python3.10/site-packages/GANDLF/training_manager.py", line 173, in TrainingManager_split
    training_loop(
  File "/net/tscratch/people/plgmazurekagh/gandlf/gandlf_env/lib/python3.10/site-packages/GANDLF/compute/training_loop.py", line 445, in training_loop
    epoch_train_loss, epoch_train_metric = train_network(
  File "/net/tscratch/people/plgmazurekagh/gandlf/gandlf_env/lib/python3.10/site-packages/GANDLF/compute/training_loop.py", line 171, in train_network
    total_epoch_train_metric[metric] += metric_val
ValueError: operands could not be broadcast together with shapes (4,) (3,) (4,) 

I am using flexinet cosine annealing config as provided on brats 2021.

@sarthakpati @VukW @Geeks-Sid any ideas? Did you maybe succeed?

GANDLF/compute/forward_pass.py Outdated Show resolved Hide resolved
@VukW
Copy link
Contributor Author

VukW commented Jun 5, 2024

@szmazurek Can you plz show the exact config you're using? I'm not familiar with brats challenge:)

@sarthakpati
Copy link
Collaborator

@szmazurek - I would assume I will also get the same error. I am currently running other jobs so I don't have any free slots to queue up any other training.

@szmazurek
Copy link
Collaborator

Hey dears, so, apparently commenting one parameter in the config made it work! The problem was metric option dice_per_label, having that commented did not result in any error. I will tackle that further, I also sent you the example config via gmail @VukW .

@sarthakpati
Copy link
Collaborator

Hey dears, so, apparently commenting one parameter in the config made it work! The problem was metric option dice_per_label, having that commented did not result in any error. I will tackle that further, I also sent you the example config via gmail @VukW .

This will need more investigation - can you please open a new issue to track it?

(for one of classes per-label metrics are not counted thus metric shape may differ)
@VukW
Copy link
Contributor Author

VukW commented Jun 6, 2024

@sarthakpati @szmazurek I catch the bug with @szmazurek 's model config. The issue is when ignore_label_validation is given in the model config, metrics for this specific label is not evaluated, thus metrics output differ from what I assumed (N_CLASSES). Fix: d0d25fb
Now it works for me

@sarthakpati
Copy link
Collaborator

@szmazurek can you confirm the fix on your end?

@szmazurek
Copy link
Collaborator

@sarthakpati On it, training scheduled. Thanks @VukW for tackling that!

@szmazurek
Copy link
Collaborator

szmazurek commented Jun 7, 2024

@sarthakpati On it, training scheduled. Thanks @VukW for tackling that!

Hey, my initial tests failed, it turned out that the error spotted by @VukW was present also in the validation and test loops. Corrected it and successfully completed the entire training epoch, the changes are applied in commit 5148e86.

EDIT: I also now initialized the training with confing in the exact same way as you sent me @sarthakpati, will keep you noticed on the results.

@sarthakpati
Copy link
Collaborator

@VukW - I think the CLA bot it complaining because of 5148e86 ... Can you please remove this?

VukW added 2 commits July 18, 2024 14:52
- the same metric error was occuring in the loops in forward_pass.py -
  now it is fixed
- entire epoch completes successfully
Implemented by Szymon Mazurek [email protected]
@VukW VukW force-pushed the fix_train_metrics branch from e81b63c to 0829662 Compare July 18, 2024 11:55
@VukW
Copy link
Contributor Author

VukW commented Jul 18, 2024

😁What a crutch
@sarthakpati overrode branch's commits history, making myself as commit author (sorry, @szmazurek ), so failed check should be fixed now.
But isn't it strange Szymon's CLA agreement was lost?

@sarthakpati
Copy link
Collaborator

Thanks!

But isn't it strange Szymon's CLA agreement was lost?

Actually, I think he submitted a PR from a machine where git was improperly configured, and his username for that commit was registered as Mazurek, Szymon instead of szmazurek, and thus resulting in the failed CLA check. This usually happens because in the initial git setup step, git asks for Full Name when it should be asking for username, followed by email.

@sarthakpati
Copy link
Collaborator

Multiple experiments have shown the validity of this PR:

  1. 2D Histology binary segmentation:

image

  1. 3D Radiology multi-class segmentation:

image

Merging this PR in, and subsequent issues are to be addressed in more PRs.

@sarthakpati sarthakpati merged commit 8b9fb47 into mlcommons:master Jul 19, 2024
20 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants