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

The purpose of collecting running stats when updating EMA before FID computation, image or network saving #21

Closed
EndlessSora opened this issue Mar 16, 2022 · 2 comments

Comments

@EndlessSora
Copy link

EndlessSora commented Mar 16, 2022

Hi @SushkoVadim @edgarschnfld,

Thanks for the excellent work. I noted that when updating EMA, you collect the running stats for BatchNorm before FID computation, image or network saving (see https://github.com/boschresearch/OASIS/blob/master/utils/utils.py#L133).

May I ask about the purpose and intuition of this operation? How significant would it affect the model performance? Thank you in advance.

@SushkoVadim
Copy link
Contributor

Hi,

This operation is needed for the correct use of BatchNorm at test time. When updating the weights of EMA, our code iterates over the state_dict of the generator:

with torch.no_grad():
        for key in model.module.netEMA.state_dict():
            model.module.netEMA.state_dict()[key].data.copy_(
                model.module.netEMA.state_dict()[key].data * opt.EMA_decay +
                model.module.netG.state_dict()[key].data   * (1 - opt.EMA_decay)
            )

which contains all the network's weights. However, the estimates of mean and variance of batches are not copied, since these statistics are updated only during a forward pass through the network (see PyTorch documentation).

Therefore, for correct testing, before switching to evaluation mode netEMA.eval(), we need to collect some running stats for the batch norm in the netEMA.train() regime, using some label maps from the train set. Without this stats accumulation, the images produced by netEMA usually look like colorful noise, so this step is important.

Hope this answers your question!
Vadim

@EndlessSora
Copy link
Author

Great. Thank you for the prompt response.

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

No branches or pull requests

2 participants