mmseg实现
mmseg有基于numpy的eval和 基于 tensor的 preval两种模式,好在都是基于
total_area_intersect, total_area_union, total_area_pred_label, total_area_label
这四个值去计算的,在
/mmseg/core/evaluation/metrics.py
里面,同时
freq = np.array(total_area_label / total_area_label.sum())
那在 得到
ret_metrics_class
和
freq
后就简单了
ret_metrics_class.update({'freq': [np.round(x * 100, 2) for x in freq]})
fwiou = sum(freq * np.nan_to_num(ret_metrics_class['IoU']))
fwiou = np.round(fwiou, 2)
ret_metrics_summary.update({'fwiou': fwiou})
还没在train log里测过,单独测可以的
import mmcv
config_path = 'test.py'
from mmseg.datasets import build_dataloader, build_dataset
cfg = mmcv.Config.fromfile(config_path)
dataset = build_dataset(cfg.data.test)
results = mmcv.load('/home/user/workplace/python/mmsegmentation-0.19.0/results.pkl')
metric = dataset.evaluate(results)
效果就是
per class results:
+-------+-------+-------+-------+
| Class | IoU | Acc | freq |
+-------+-------+-------+-------+
| 0 | nan | nan | 0.0 |
| 1 | 81.94 | 92.16 | 60.02 |
| 2 | 58.06 | 69.5 | 10.26 |
| 3 | 19.32 | 24.99 | 0.58 |
| 4 | 57.82 | 67.98 | 9.09 |
| 5 | 69.95 | 82.31 | 19.66 |
| 6 | 39.31 | 49.66 | 0.39 |
+-------+-------+-------+-------+
Summary:
+-------+------+-------+-------+
| aAcc | mIoU | mAcc | fwiou |
+-------+------+-------+-------+
| 85.14 | 54.4 | 64.43 | 74.41 |
+-------+------+-------+-------+
手撸一个fwiou
from tqdm import tqdm
import numpy as np
from PIL import Image
import os
def fast_hist(a, b, n):
k = (a >= 0) & (a < n)
return np.bincount(n * a[k].astype(int) + b[k], minlength=n*n).reshape(n, n)
truth_path = '/dev/shm/tmp/gt'
res_path = '/home/user/workplace/python/mmsegmentation-0.19.0/.format_cityscapes'
n_class = 7
hist = np.zeros((n_class, n_class), dtype=np.float)
for name in tqdm(os.listdir(truth_path)):
truth_img = Image.open(os.path.join(truth_path, name))
truth_img = np.array(truth_img).flatten()
res_img = Image.open(os.path.join(res_path, name.replace('LT.tif', 'GF.png')))
res_img = np.array(res_img).flatten()
hist += fast_hist(truth_img, res_img, n_class)
iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist) + 1e-8)
freq = hist.sum(1) / hist.sum()
res = (freq[freq > 0] * iu[freq > 0]).sum()
iu[0] = res
output = ' '.join(['{:.3f}'.format(x) for x in iu])
print(output)
版权声明:本文为carry_hjr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。