目标检测中几个指标的衡量
import os import sys sys.path.insert(0, "/home/xx/faceDetect/caffe/python") import cv2 from xml.etree import ElementTree as ET import numpy as np import caffe caffe.set_device(0) caffe.set_mode_gpu() model = "ssh_6.5M_400/model" config = "ssh_6.5M_400/modelconfig" net = caffe.Net(config, model, caffe.TEST) transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2, 0, 1)) transformer.set_mean('data', np.array([104,117,123])) # mean pixel transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1] transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB # set net to batch size of 1 image_resize = 400 net.blobs['data'].reshape(1,3,image_resize,image_resize) gtRoot = "shoulder_test/gt" imgRoot = "shoulder_test/image" testFile = "shoulder_test/test.txt" def IoUs(rect, boxs, type=0): x1 = rect[0] y1 = rect[1] x2 = rect[2] y2 = rect[3] xs1 = boxs[:,0] ys1 = boxs[:,1] xs2 = boxs[:,2] ys2 = boxs[:,3] rect_area = (x2 - x1 + 1) * (y2 - y1 + 1) areas = (xs2 - xs1 + 1) * (ys2 - ys1 + 1) ws = np.maximum(0, np.minimum(x2, xs2) - np.maximum(x1, xs1) + 1) hs = np.maximum(0, np.minimum(y2, ys2) - np.maximum(y1, ys1) + 1) inters = ws * hs if type==0: unions = areas + rect_area - inters else: unions = np.minimum(areas, rect_area) return inters/unions tp = 0 fn = 0 tfp = 0 tp_fn = 0 count = 0 with open(testFile) as f: for line in f: name = line.strip() imgFile = imgRoot + "/" + name + ".jpg" gtFile = gtRoot + "/" + name + ".xml" img = cv2.imread(imgFile) tree = ET.parse(gtFile) root = tree.getroot() gtRects = [] for bndbox in root.iter(tag="bndbox"): xmin = int(bndbox[0].text) ymin = int(bndbox[1].text) xmax = int(bndbox[2].text) ymax = int(bndbox[3].text) rect = [xmin, ymin, xmax, ymax] gtRects.append(rect) cv2.rectangle(img, (xmin,ymin),(xmax,ymax),(0,0,255),2) tp_fn += 1 gtRects = np.array(gtRects) image = caffe.io.load_image(imgFile) transformed_image = transformer.preprocess('data', image) net.blobs['data'].data[...] = transformed_image detections = net.forward()['detection_out'] det_label = detections[0,0,:,1] det_conf = detections[0,0,:,2] det_xmin = detections[0,0,:,3] det_ymin = detections[0,0,:,4] det_xmax = detections[0,0,:,5] det_ymax = detections[0,0,:,6] top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6] top_conf = det_conf[top_indices] top_xmin = det_xmin[top_indices] top_ymin = det_ymin[top_indices] top_xmax = det_xmax[top_indices] top_ymax = det_ymax[top_indices] tfp += top_conf.shape[0] tp_idx = [] for i in xrange(top_conf.shape[0]): xmin = round(top_xmin[i] * img.shape[1]) ymin = round(top_ymin[i] * img.shape[0]) xmax = round(top_xmax[i] * img.shape[1]) ymax = round(top_ymax[i] * img.shape[0]) rect = [xmin, ymin, xmax, ymax] iou = IoUs(rect, gtRects) # cv2.rectangle(img, (int(xmin),int(ymin)),(int(xmax),int(ymax)),(255,0,0),2) # cv2.imshow("ha", img) # cv2.waitKey() if np.max(iou) > 0.5: tp += 1 # idx = np.where(iou>0.5) # tp_idx = np.append(tp_idx, idx) count += 1 print count fn = tp_fn - tp print "tp: ",tp print "tfp: ", tfp print "tp_fn: ", tp_fn print "precision: ", float(tp)/(float(tfp)) print "miss rate: ", float(fn)/tp_fn # cv2.imshow("", img) # cv2.waitKey()