-
Notifications
You must be signed in to change notification settings - Fork 355
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
[Feature] Support calculating loss during validation #1503
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hope to merge val loss into mmengine as soon as possible, which is a very useful feature |
HAOCHENYE
previously approved these changes
Mar 5, 2024
HAOCHENYE
previously approved these changes
May 6, 2024
zhouzaida
reviewed
May 17, 2024
zhouzaida
approved these changes
May 17, 2024
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Since early stopping requires validation loss as a possible metric, mmengine currently does not support calculating and parsing validation loss as a metric.
However, due to the inconsistency of model implementations and the fact that calculating validation loss is not a common requirement, the process of calculating validation loss should not be initiated by mmengine, but rather, initiated by the model and returned by mmengine with a convention to be parsed and returned as a metric.
Thus this PR aims to implement this return-and-resolve convention without introducing breaking change.
Design
In order not to introduce breaking change, we chose to return the loss computed by the model at
val_step
(model.forward
withmode='predict'
orpredict
) wrapped byBaseDataElement
and append after the val step result.Therefore, mmengine needs to get the last item of the result of
val_step
inValLoop
and determine whether it is validation loss or not. If it is validation loss, it will perform the relevant computation and return it at the end of theValLoop
, and then compute other metrics based on the items other than the validation loss, e.g., the accuracy, etc. If it is not a val loss, it will not be processed.Adaptation
Custom Model
Take https://github.com/open-mmlab/mmengine/blob/02f80e8bdd38f6713e04a872304861b02157905a/examples/distributed_training.py#L14-#L25 as an example.
MMPreTrain
Take https://github.com/open-mmlab/mmpretrain/blob/17a886cb5825cd8c26df4e65f7112d404b99fe12/mmpretrain/models/classifiers/image.py#L248-L249 as an example.
MMPose
Calculating loss in this way maybe not correct.
Take https://github.com/open-mmlab/mmpose/blob/5a3be9451bdfdad2053a90dc1199e3ff1ea1a409/mmpose/models/pose_estimators/topdown.py#L99-#L120 as an example.
In addition, you should add
dict(type='GenerateTarget', encoder=codec)
toval_pipeline
similar totrain_pipeline
.