rohban-lab / Knowledge_Distillation_AD

82 stars 46 forks source link

Visualization #6

Closed Nirvana-feng closed 2 years ago

Nirvana-feng commented 2 years ago

Hello, I would like to ask how to visualize the located abnormal part. Similar to the following picture ![Uploading image.png…]()

SMSD75 commented 2 years ago

Hi,

I appreciate your interest in our research. There are some localization functions in the codes to visualize gradient maps equal to the provided heatmaps.

Nirvana-feng commented 2 years ago

Thank you for your reply. I did not see the visualization after the code was completed, only the results of AUC. I don't know where the problem occurred. So I want to ask you how to get a visual heat map. The code I run is for location detection. Looking forward to your reply, thank you again

SMSD75 commented 2 years ago

There is a "localization_test" function returning the gradient matrixes. If you want heat maps, you should plot the gradient maps. Of course, choosing the best threshold and using morphological filters could improve the visualizations. Gradient maps are good enough to localize defections in the pixel-level anomaly detection datasets, as mentioned in the paper.

I hope your problem has been solved.

Nirvana-feng commented 2 years ago

Sorry, I still don't know how to draw heat maps. Can you recommend me to refer to the case of visualizing heat maps? Excuse me. Feel sorry

Nirvana-feng commented 2 years ago

I converted the gradient matrix (109,128,128) into a matrix of (2,8192). Since drawing the heat map requires integer parameters, I multiplied the matrix by 255 and then converted it into an unsigned integer. But the drawn image seems to be wrong, as shown in the figure: image This should be the gradient of the first abnormal image of the capsule。

Soroosh-Bsl commented 2 years ago

Hi there,

You are doing it in the wrong way. In the (109, 128, 128) batch of gradients, the first element of shape shows the batch_size. It means this array has the 128x128 gradient maps of 109 images. Hence, to get a heatmap you only need to select the i-th element of this array (e.g. the first one). Also, there is no need to convert the map to integers as the gradient maps' pixels given by the final functions are in [0 1] and matplotlib supports this interval for images. Kindly follow the functions in the test parts, it will help to have a better view of the procedures. Hope your issue gets resolved.

Nirvana-feng commented 2 years ago

Thank you for your answer, but I found that the size of the original picture is (1000,1000) and the heat map is (640,480) and there are a lot of blank spaces in the heat map. So I manually cropped the blank areas in the heat map. Then convert the heat map and the original image into a picture with a size of (372,372), and the effect of overlaying the heat map to the original image is as follows:. It's not whether you have any better suggestions to overlay the heat map on the original image. image

SMSD75 commented 2 years ago

Yeah, You are right. We have applied some filters and post-processing, which you can find in the codes.

Nirvana-feng commented 2 years ago

Thank you for your answer

wangxin-fighting commented 11 months ago

@Nirvana-feng Hello, I went to test.py to generate a visualization of the anomaly localization after the training, following the closed way that you have asked , and I got the following error.What is the reason for this?Looking forward to your reply very much, thank you!

Traceback (most recent call last): File "D:/1-study/2-code/Knowledge_Distillation_AD-main/test.py", line 31, in main() File "D:/1-study/2-code/Knowledge_Distillation_AD-main/test.py", line 20, in main config=config) File "D:\1-study\2-code\Knowledge_Distillation_AD-main\test_functions.py", line 79, in localization_test return compute_localization_auc(grad, ground_truth) File "D:\1-study\2-code\Knowledge_Distillation_AD-main\test_functions.py", line 344, in compute_localization_auc tp_map = np.multiply(grad_t, x_ground_comp) ValueError: operands could not be broadcast together with shapes (167,128,128) (141,128,128)