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

Make metrics serializable #3001

Merged

Conversation

sadra-barikbin
Copy link
Collaborator

@sadra-barikbin sadra-barikbin commented Jul 15, 2023

Fixes #2999

@github-actions github-actions bot added the module: metrics Metrics module label Jul 15, 2023
@sadra-barikbin sadra-barikbin marked this pull request as ready for review July 16, 2023 11:56
@sadra-barikbin
Copy link
Collaborator Author

Shall I add _state_dict_all_req_keys attribute to all of metrics?
Shall I add tests for state_dict and load_state_dict for each metric?

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @sadra-barikbin !
The approach looks OK to me, let's update all metrics.

ignite/metrics/metric.py Outdated Show resolved Hide resolved
ignite/metrics/metric.py Show resolved Hide resolved
ignite/metrics/metric.py Outdated Show resolved Hide resolved
@sadra-barikbin sadra-barikbin self-assigned this Aug 7, 2023

to_save = {'trainer': trainer, 'model': model, 'optimizer': optimizer, 'lr_scheduler': lr_scheduler}
to_save = {'trainer': trainer, 'model': model, 'optimizer': optimizer, 'lr_scheduler': lr_scheduler, 'metric': metric}
Copy link
Collaborator

@vfdev-5 vfdev-5 Aug 10, 2023

Choose a reason for hiding this comment

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

Ideally, we would like to get that in a more automatic way. Also if we want to save multiple metrics adding them one by one to save/load can be a pain.
Let's propose something in a follow-up PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you please reiterate what you mean? If we have multiple metrics, is it cumbersome to do:

to_save = {..., 'metric1': metric1, 'metric2': metric2, 'metric3': metric3}

?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, it is too verbose, let's try to find a way to simplify that.

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @sadra-barikbin

@vfdev-5 vfdev-5 merged commit cf3fdd1 into pytorch:master Aug 10, 2023
@@ -87,6 +87,9 @@ def log_running_avg_metrics():
"""

required_output_keys = None
# TODO Shall we put `src` here? Then we should add a new branch for metric-typed attributes in `state_dict`
# and `load_state_dict`. Examples; This class; `Rouge` which has a `List[_BaseRouge]`.
_state_dict_all_req_keys = ("_value",)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vfdev-5 , What's your thoughts on this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add src and in state_dict/load_state_dict put an explicit check for isinstance(attr, Metric) and call state[attr_name].update(attr.state_dict) or something like that.
Do you have any other options ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: metrics Metrics module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add state_dict and load_state_dict to metrics
2 participants