When loading data, convert semantic labels into binary matrices
Since the framework implements asynchronous loading of data, the following code in the data loading phase will not affect the GPU computing speed.
# 图像标签的稠密矩阵
shape = img_l.shape # img_l为图像标签
img_ld = numpy.zeros((19,) + shape) # img_ld为稠密矩阵
for i in range(19):
img_ld[i] = (img_l == i)
img_ld.astype('float32')
mIou calculation function
Applied in pytorch framework
class mIou(nn.Module):
def __init__(self):
super().__init__()
def forward(self, outputs, targets):
ma = torch.amax(outputs, dim=1)
outputs = outputs - ma
outputs = (outputs == 0).type_as(outputs)
a = outputs + targets
tt = torch.sum(a == 2, dim=[0, 2, 3])
tf = torch.sum(a > 0, dim=[0, 2, 3])
tt[tf == 0] = 1
tf[tf == 0] = 1
return torch.mean(tt / tf)
Join the mIou evaluation
Instantiate evaluation function
evaluate = util.loss.mIou().cuda() # 评估函数
m = 0
Calculate miou during training
e = evaluate(y_, z)
m += e.item()
if batch % 30 == 0: # 每30个批次进行一次评估
print('loss:', f / 30, 'mIou:', m / 30)
m = 0
If you have any more efficient algorithms or shortcomings, please leave a comment.