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.63k stars 646 forks source link

[Task]: Anomaly heat map not generated correctly for DFM model in classification #1521

Open JakeNail opened 9 months ago

JakeNail commented 9 months ago

What is the motivation for this task?

When running inference on a custom dataset for a DFM model with the classification task I found out that the anomaly heatmaps were not generated properly, they are just empty. I reproduced this problem with the MVtec dataset using the default config.yaml file for the model only changing task from segmentation to classification and save_images to True. Here are two examples. Screenshot 2023-12-04 153208 Screenshot 2023-12-04 153307

Describe the solution you'd like

However when using the task segmentation the anomaly heatmaps are generated as expected. Would it be possible to have them for the classification task for the DFM model instead of empty ones? Just like it is the case for other models in anomalib. Here are the two corresponding images using segmentation. Screenshot 2023-12-04 154305 Screenshot 2023-12-04 154243

Additional context

No response

blaz-r commented 9 months ago

There is some work in progress in https://github.com/openvinotoolkit/anomalib/pull/1378 to also add classification score to segmentation visualization.

The mask is still displayed in visualizer, so that's not a bug. But I identified the reason for mask being all zeros in case of classification. It is because of these lines: https://github.com/openvinotoolkit/anomalib/blob/1f50c952fc65a165884b2b94e178821dcebfbbef/src/anomalib/models/components/base/anomaly_module.py#L164-L167

Since mask is not added to batch in classification, mask threshold is set to same value as image threshold, which is completely wrong. So when normalization happens, all masks become 0. I think a solution would be to add mask to dataset even in case of classification here: https://github.com/openvinotoolkit/anomalib/blob/1f50c952fc65a165884b2b94e178821dcebfbbef/src/anomalib/data/base/dataset.py#L123-L125 skipping the separate if branch and loading mask for all types.

@samet-akcay I'm not sure what to do about this, we could include mask even in classification, since visualizer displays the heat map?