Pytorch-lightning: How to access validation logs data inside the on_validation_batch_end callback

Created on 21 Sep 2020  ยท  11Comments  ยท  Source: PyTorchLightning/pytorch-lightning

โ“ Questions and Help

What is your question?

How can I have access to validation logs data inside the on_validation_batch_end callback?

For the on_batch_endcallback, I have access to the train metrics by using the class property callback_metricsof the Trainerbut when querying the same property inside the on_validation_batch_endcallback I still have the same train logs data.

I've tried to loop all properties of the Trainer but couldn't find any reference for any validation log data.

Code

Processing batch:

def validation_step(self, image_ray_batch, batch_idx):
    ...
    output = {
        "val_loss": loss,
        "validation/loss": loss,
        "validation/coarse_loss": coarse_loss,
        "validation/psnr": psnr,
    }

    if rgb_fine is not None:
        output["validation/fine_loss"] = fine_loss

    return output

Custom Logging:

def on_validation_batch_end(self, trainer, pl_module):
    print("on_validation_batch_end")
    # Gather trainer metrics
    metrics = trainer.callback_metrics
    print(metrics)

What's your environment?

Using version [0.7.6]

question

Most helpful comment

I guess you could do a hack like: self.trainer.logger.log_metrics(...) in validation_step? (Not sure :P)

All 11 comments

Hi! thanks for your contribution!, great first issue!

@DomainFlag hey, first of all we would prefer that you update to the latest version. I know there are some changes from 0.7.6 in the API, but now it is being kept stable and should remain unchanged for the most part.

We don't currently have this and discussed about making this available within hooks.

@ananyahjha93 Hey, thanks for the reply, I've upgraded to 0.9.0 and currently I am returning like so when overriding the validation_step:

output = {
    "loss": loss,
    "log": {
        "val_loss": loss,
        "validation/loss": loss,
        "validation/coarse_loss": coarse_loss,
        "validation/psnr": psnr
    },
}

return output

The issue is that in on_validation_batch_end by accessing the callback_metrics property of the trainer I can have access to the validation logs but they aren't updated for each individual processed batch, thus I have access only to the last batch results. Is that part related to the "making this available within hook"?

Alright, so with some research done, I've found out the callback_metricsproperty gets updated only with the logs from processing the last val batch, thus the only way to have access to all val batch logs is to override the validation_epoch_end and store the list of batch results as dict property and access it by overriding the on_validation_epoch_end for pytorch_lightning.callbacks Callback. Is not the same as on_validation_batch_end but it's still something.

With some digging, I noticed the trainer has the property evaluation_loop that has subsequently the outputs property but there isn't any way to access it directly from the callback? I mean I am working with time-consuming val batch processing, and having logs for each step will be very helpful...

You could implement a custom logger. It should receive the logged data.
Example:

class MyLogger(pytorch_lightning.loggers.LightningLoggerBase):
    def log_metrics(self, metrics, step=None):
        print(metrics)

    ... # implement other methods as well

@EspenHa How would I go about logging for each step, instead of each epoch as it's now?

I guess you could do a hack like: self.trainer.logger.log_metrics(...) in validation_step? (Not sure :P)

@DomainFlag you should checkout the results object API in the new version: https://pytorch-lightning.readthedocs.io/en/stable/results.html

With the Results API, you should have access to both the step wise and epoch wise logs for validation.

Is this all you require? 'having logs for each step will be very helpful', if yes then you don't have to worry about hooks and having logs available within the hooks. If you do require the logs for a different purpose, then we'll have to specifically make it available to the hooks.

@ananyahjha93 How about custom logging, as it's referred from the thread, I need full control over the logging...

I'm wondering if get_batch_log_metrics could help you. See Result.get_batch_log_metrics documentation

It's available on 1.0.2 now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

williamFalcon picture williamFalcon  ยท  3Comments

williamFalcon picture williamFalcon  ยท  3Comments

chuong98 picture chuong98  ยท  3Comments

versatran01 picture versatran01  ยท  3Comments

monney picture monney  ยท  3Comments