deeplab evaluate.py

#coding=utf-8
# import scipy.io
import numpy as np
# from skimage.io import imread
from PIL import Image
# data = scipy.io.loadmat('1.mat') 
# label = data['data']
# res=np.argmax(label,axis=2).astype(np.uint8)
# print res.shape
# res=res.reshape(res.shape[0],res.shape[1])
# lab = Image.open('1.png') 
# lab1 = Image.open('2.png')
# lab = lab.resize((lab1.size))
# lab1 = lab1.resize((lab1.size))
# lab = np.array(lab)
# lab1 = np.array(lab1)

def IOU(a, b, im, pixels):
	J = 0
	iou = 0
	U = pixels
	# calcu background pixels
	# num = str(a).count("0")
	# num1 = str(b).count("0")
	# U = pixels - max(num, num1)
	for i in range(a.shape[0]):
		for j in range(a.shape[1]):
			if a[i,j] == b[i,j]:
				J += 1
	iou = J / (U + 0.000000001)
	print iou
	return iou




lab_dir = "/path to label/SegmentationClass/"
label_file = "/pat to val ids/val_id.txt"
# only need change res label dir
res_dir = "/pat to res labels /"
ims = open(label_file)
iou = 0
count = 0
pixels = 263169  # 513*513
for im in ims:
	# print res_dir + im[:-1] + "_blob_0.png"
	lab = Image.open(lab_dir + im[:-1] + ".png")
	res = Image.open(res_dir + im[:-1] + "_blob_0.png")
	# resize
	lab = lab.resize(res.size)
	res = res.resize(res.size)
	lab = np.array(lab)
	res = np.array(res)
	count += 1
	iou += IOU(lab, res, im[:-1], pixels)
print iou, count 
print iou / count







deeplab_v2虽然是自带了evaluate的.m脚本 ,但是感觉用起来好费劲。我的理解就是直接算一个miou就可以了吧~以上代码仅供参考。。。

这里因为train net的时候进行了crop resize(531, 531),所以得到的res label的size可能是和img本身的gt size不一样的。所以需要做一个resize。我感觉就是这么简单粗暴吧。。。欢迎指正~

9.14更近版:

#coding=utf-8
import numpy as np
from PIL import Image
from scipy.io import loadmat as sio
import pdb  # bebug


def IOU(lab, res, im):
	cla_num = [0] * 21
	iou = [0] * 21
	for r in range(21):
		n = np.sum(lab == r)  # array 2 list, then count numbers of the cur eme.
		if n != 0:  # means has the r-ed class
			# pdb.set_trace() 
			cla_num[r] = 1
			n_res = np.sum(res == r)
			if n_res == 0:
				iou[r] = 0
			else:
				J = 0
				for i in range(lab.shape[0]):
					for j in range(lab.shape[1]):
						if res[i,j] == r and lab[i,j] == r:
							J += 1
				# print n, n_res, J, im[:-1], 'class=', r
				iou[r] = J / (n + n_res - J + 1e-9)
	print iou, cla_num
	return iou, cla_num





lab_dir = "/path to gt /"   
label_file = "/path to /val_id.txt"
# only need change res label dir
res_dir = "/path net result label/"  #.mat
ims = open(label_file)
ious =  [[0 for col in range(21)] for row in range(1449)]   # [1449, 21]
clsaaes = [[0 for col in range(21)] for row in range(1449)]
idx = 0
for im in ims:
	lab = Image.open(lab_dir + im[:-1] + ".png")
	mat_file=sio(res_dir + im[:-1] + "_blob_0")
	mat_file=mat_file['data']
	res=np.argmax(mat_file,axis=2).astype(np.uint8)
	lab = np.array(lab)
	# 表示这里很坑, Image.open读图会把图像的size h w 互换了......  手动转回来.....
	lab = np.transpose(lab, [1,0])
	# print lab.shape
	height = min(res.shape[0], lab.shape[0])
	width = min(res.shape[1], lab.shape[1])
	res = res[:height, :width, 0]
	lab = lab[:height, :width]
	# print lab.shape, res.shape, im[:-1]
	iou, cla_num = IOU(lab, res, im)
	ious[idx] = iou
	clsaaes[idx] = cla_num
	idx += 1

iou = map(sum, zip(*ious))  # sum in col
print iou
cla = map(sum, zip(*clsaaes))
print cla

res = 0
for i in range(21):
	res += iou[i] / cla[i]
print res / 21

所以踩坑记录就是:用Image.open()读图的时候,读进来后图像是否被transpose了这一点要特别注意,打印出size看下。。。

猜你喜欢

转载自blog.csdn.net/jiachen0212/article/details/82689482
今日推荐