这是我生成混淆矩阵的代码片段:我想知道如何使用 sklearn 更改混淆矩阵中那些不位于与热图相同的对角线中的框的颜色。
nb_classes = 15
confusion_matrix = torch.zeros(nb_classes, nb_classes)
with torch.no_grad():
for i, (inputs, target, classes, im_path) in enumerate(dataLoaders['test']):
inputs = inputs.to(device)
target = target.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
for t, p in zip(target.view(-1), preds.view(-1)):
confusion_matrix[t.long(), p.long()] += 1
num_classes = 15
class_names = ['A2CH', 'A3CH', 'A4CH_LV', 'A4CH_RV', 'A5CH', 'Apical_MV_LA_IAS',
'OTHER', 'PLAX_TV', 'PLAX_full', 'PLAX_valves', 'PSAX_AV', 'PSAX_LV',
'Subcostal_IVC', 'Subcostal_heart', 'Suprasternal']
plt.figure()
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
tick_marks = numpy.arange(num_classes)
classNames = class_names
thresh = confusion_matrix.max() / 2.
for …Run Code Online (Sandbox Code Playgroud) 我想知道如何计算多类分割的骰子系数。
这是计算二元分割任务的骰子系数的脚本。如何循环每个类并计算每个类的骰子?
先感谢您
import numpy
def dice_coeff(im1, im2, empty_score=1.0):
im1 = numpy.asarray(im1).astype(numpy.bool)
im2 = numpy.asarray(im2).astype(numpy.bool)
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
im_sum = im1.sum() + im2.sum()
if im_sum == 0:
return empty_score
# Compute Dice coefficient
intersection = numpy.logical_and(im1, im2)
return (2. * intersection.sum() / im_sum)
Run Code Online (Sandbox Code Playgroud)