openvinotoolkit / anomalib

An anomaly detection library comprising state-of-the-art algorithms and features such as experiment management, hyper-parameter optimization, and edge inference.
https://anomalib.readthedocs.io/en/latest/
Apache License 2.0
3.68k stars 654 forks source link

[Bug/Task]: Error in implementation of GANomaly's generator loss #2137

Open vonclites opened 3 months ago

vonclites commented 3 months ago

Describe the bug

The GANomaly paper and original implementation (https://github.com/samet-akcay/ganomaly/blob/master/lib/model.py#L318) use the feature representations of the discriminator for the generator's adversarial loss component. This makes sense as the generator's adversarial loss is some for of L1/L2/MSE, not binary cross entropy.

However, the anomalib implementation is calculating the adversarial loss based on the output probabilities of the discriminator, not the feature representations. See https://github.com/openvinotoolkit/anomalib/blob/main/src/anomalib/models/image/ganomaly/loss.py#L58

Other relevant lines of code to confirm the issue:

Output from discriminator: https://github.com/openvinotoolkit/anomalib/blob/main/src/anomalib/models/image/ganomaly/torch_model.py#L243 https://github.com/openvinotoolkit/anomalib/blob/main/src/anomalib/models/image/ganomaly/lightning_model.py#L150

Input to loss function: https://github.com/openvinotoolkit/anomalib/blob/main/src/anomalib/models/image/ganomaly/lightning_model.py#L154

If I'm correct that this is an error, let me know and I can make a pull request.

Thanks

Dataset

N/A

Model

GANomaly

Steps to reproduce the behavior

N/A. Error is evident from the code.

OS information

N/A

Expected behavior

Either the generator's adversarial loss should use the discriminator's feature representations, or the adversarial loss should be changed to binary cross entropy loss on the discriminator's predictions for the fake images.

Screenshots

No response

Pip/GitHub

pip

What version/branch did you use?

No response

Configuration YAML

N/A

Logs

N/A

Code of Conduct