Python draws the entropy (uncertainty) of the two-segmentation prediction result and visualizes it

Here is the code directly:

import cv2
import numpy as np
import matplotlib.pyplot as plt

pred_path = "YOUR PRED PATH"
pred = cv2.imread(pred_path, 0)
pred = pred / 255	# 预测结果需要归一化到0~1的区间内
eps = 1e-8	
pred = np.clip(pred, eps, 1 - eps)	# 防止计算熵时log0出错
H = -pred * np.log2(pred) - (1 - pred) * np.log2(1 - pred)

fig, ax = plt.subplots()
im = ax.imshow(H, cmap='hot', interpolation='nearest')
ax.tick_params(axis='both', which='both', length=0, labelsize=0, labelcolor='w')
plt.colorbar(im, shrink=0.6)

plt.savefig('heatmap.png', bbox_inches='tight')
print("Entropy", H.sum() / pred.size)	# 整张图像的熵为所有像素熵的和除以像素数

Three examples are as follows.


Prediction result 1:
insert image description here

The entropy is 0.7879. Visualize a heatmap:
insert image description here


Prediction result 2:
insert image description here

The entropy is 0.2372. Visualize a heatmap:
insert image description here


Prediction result 3:
insert image description here

The entropy is 0.0645. Visualize a heatmap:
insert image description here

Guess you like

Origin blog.csdn.net/qq_40714949/article/details/129878215