semseg-master加载数据、训练和计算mIOU
semseg-master的github链接:semseg一、数据处理在制作dataset的时候,label直接读取label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE)# GRAY 1 channel ndarray with shape H * W调试代码发现读取的是file:///media/D_4TB/zhouhongjie/1.few-s
semseg-master的github链接:semseg
一、数据处理
在制作dataset的时候,label直接读取label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
调试代码发现读取的是file:///media/D_4TB/zhouhongjie/1.few-shot%20segmentation/3.CaNet/CaNet-master/dataset/dir/VOCdevkit/VOC2012/SegmentationClassAug/2011_002227.png这张图片:
使用label_class = np.unique(label).tolist()
调试得到cv读取到的矩阵值:
[
0
,
6
,
15
,
255
]
\color{red}{[0, 6, 15, 255]}
[0,6,15,255],其中255为白色轮廓,0为黑色,6和15分别为两类。
二、训练
经过dataloader直接打包后,经过一些处理后,将input
和对应的label
送入训练:output, main_loss, aux_loss = model(input, target)
此处model可以选择和下载预训练权重:
三、输出误差和反向传播
四、mIOU计算
之后经过intersectionAndUnionGPU函数计算交集intersection和并集union、target:intersection, union, target = intersectionAndUnionGPU(output, target, args.classes, args.ignore_label)
classes=21,ignore_label=255
def intersectionAndUnionGPU(output, target, K, ignore_index=255):
# 'K' classes, output and target sizes are N or N * L or N * H * W, each value in range 0 to K - 1.
assert (output.dim() in [1, 2, 3])
assert output.shape == target.shape
output = output.view(-1)
target = target.view(-1)
output[target == ignore_index] = ignore_index
intersection = output[output == target] # I-iou
area_intersection = torch.histc(intersection, bins=K, min=0, max=K-1)
area_output = torch.histc(output, bins=K, min=0, max=K-1)
area_target = torch.histc(target, bins=K, min=0, max=K-1)
area_union = area_output + area_target - area_intersection
return area_intersection, area_union, area_target
需要注意此处交集和并集的计算:除掉忽略的255之后,交集=output[output == target],并集=output+target-area_intersection
area_intersection = torch.histc(intersection, bins=K, min=0, max=K-1)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)