def intersectWithGroundTruth(A, gt): s = len(A) R = np.zeros((s, s)) for i in range(s): for j in range(s): maxIou, maxOv, maxCov = 0., 0., [0., []] for k in gt: iou = det.IoU(A[i][j], k) ov = det.overlap(k, A[i][j]) cov = det.overlap(A[i][j], k) if iou > maxIou: maxIou = iou if ov > maxOv: maxOv = ov if cov > maxCov[0]: maxCov = [cov, k] #R[i,j] = maxOv #continue if maxIou >= 0.7: # Relative size is roughly the same R[i, j] = 1. #elif maxOv >= 0.7: # The object covers this area almost completely # R[i,j] = 1. elif maxCov[0] >= 0.7: # The object is covered by the area ox = maxCov[1][0] + (maxCov[1][2] - maxCov[1][0]) / 2. oy = maxCov[1][1] + (maxCov[1][3] - maxCov[1][1]) / 2. aw = A[i][j][2] - A[i][j][0] ah = A[i][j][3] - A[i][j][1] ax = A[i][j][0] + aw / 2. ay = A[i][j][1] + ah / 2. r = 0.2 if (ax - r * aw <= ox and ox <= ax + r * aw) and (ay - r * ah <= oy and oy <= ay + r * ah): R[i, j] = 1. return R
def coverSample(self, idx): self.control['DONE'][idx] = True self.cover = self.boxes[idx][:] conflicts = [] for j in range(len(self.control['DONE'])): if not self.control['DONE'][j]: ov = det.overlap(self.cover, self.boxes[j]) if ov > 0.0: conflicts.append((j, ov)) conflicts.sort(key=lambda x: x[1], reverse=True) for j, ov in conflicts: nov = det.overlap(self.cover, self.boxes[j]) if nov < 0.5: ib = det.intersect(self.cover, self.boxes[j]) iw = ib[2] - ib[0] + 1 ih = ib[3] - ib[1] + 1 if iw > ih: # Cut height first if self.cover[1] >= ib[1]: self.cover[1] = ib[3] if self.cover[3] <= ib[3]: self.cover[3] = ib[1] else: # Cut width first if self.cover[0] >= ib[0]: self.cover[0] = ib[2] if self.cover[2] <= ib[2]: self.cover[2] = ib[0] elif nov >= 0.5: # Assume the example is done for now self.control['DONE'][j] = True self.control['SKIP'][j] = True
def intersectWithGroundTruth(A,gt): s = len(A) R = np.zeros((s,s)) for i in range(s): for j in range(s): maxIou,maxOv,maxCov = 0.,0.,[0.,[]] for k in gt: iou = det.IoU( A[i][j], k ) ov = det.overlap( k,A[i][j] ) cov = det.overlap( A[i][j], k ) if iou > maxIou: maxIou = iou if ov > maxOv: maxOv = ov if cov > maxCov[0]: maxCov = [cov,k] #R[i,j] = maxOv #continue if maxIou >= 0.7: # Relative size is roughly the same R[i,j] = 1. #elif maxOv >= 0.7: # The object covers this area almost completely # R[i,j] = 1. elif maxCov[0] >= 0.7: # The object is covered by the area ox = maxCov[1][0] + (maxCov[1][2]-maxCov[1][0])/2. oy = maxCov[1][1] + (maxCov[1][3]-maxCov[1][1])/2. aw = A[i][j][2] - A[i][j][0] ah = A[i][j][3] - A[i][j][1] ax = A[i][j][0] + aw/2. ay = A[i][j][1] + ah/2. r = 0.2 if (ax-r*aw <= ox and ox <= ax+r*aw) and (ay-r*ah <= oy and oy <= ay+r*ah): R[i,j] = 1. return R
def coverSample(self, idx): self.control['DONE'][idx] = True self.cover = self.boxes[idx][:] conflicts = [] for j in range(len(self.control['DONE'])): if not self.control['DONE'][j]: ov = det.overlap(self.cover, self.boxes[j]) if ov > 0.0: conflicts.append( (j,ov) ) conflicts.sort(key=lambda x:x[1], reverse=True) for j,ov in conflicts: nov = det.overlap(self.cover, self.boxes[j]) if nov < 0.5: ib = det.intersect(self.cover, self.boxes[j]) iw = ib[2] - ib[0] + 1 ih = ib[3] - ib[1] + 1 if iw > ih: # Cut height first if self.cover[1] >= ib[1]: self.cover[1] = ib[3] if self.cover[3] <= ib[3]: self.cover[3] = ib[1] else: # Cut width first if self.cover[0] >= ib[0]: self.cover[0] = ib[2] if self.cover[2] <= ib[2]: self.cover[2] = ib[0] elif nov >= 0.5: # Assume the example is done for now self.control['DONE'][j] = True self.control['SKIP'][j] = True
def findRelation(box, gt, iou): if iou >= 0.6: return 'tight' else: ov = det.overlap(gt,box) if ov >= 1.0 and iou <= 0.3 and iou >= 0.2: return 'inside' ov = det.overlap(box,gt) if ov >= 0.9 and iou <= 0.4 and iou >= 0.2: return 'big' return None
def inside(box,gt): ov = ldet.overlap(gt,box) iou = ldet.IoU(box,gt) if ov >= 0.9 and iou <= 1.0 and iou >= 0.01: return ov else: return 0.0
def findBigMatches(img, big, tight): layouts = [] idealMatch = [0.25, 1.0] for i in range(len(big)): b = big[i] for j in range(len(tight)): t = tight[j] r = [det.IoU(b[1:], t[1:]), det.overlap(b[1:], t[1:])] if 0.8 >= r[0] and r[1] >= 0.8: s = b[0] + t[0] ol = ObjectLayout(img) ol.addRootAndContext(t, b, r) layouts.append(ol) layouts.sort(key=lambda x: x.getScore(), reverse=True) return layouts
def findBigMatches(img, big, tight): layouts = [] idealMatch = [0.25, 1.0] for i in range(len(big)): b = big[i] for j in range(len(tight)): t = tight[j] r = [ det.IoU(b[1:], t[1:]), det.overlap(b[1:], t[1:])] if 0.8 >= r[0] and r[1] >= 0.8: s = b[0] + t[0] ol = ObjectLayout(img) ol.addRootAndContext(t,b,r) layouts.append(ol) layouts.sort(key=lambda x: x.getScore(), reverse=True) return layouts
def findInsideMatches(inside, layouts): idealMatch = [0.25, 1.0] for i in range(len(layouts)): t = layouts[i].root candidates = [] for j in range(len(inside)): n = inside[j] r = [ det.IoU(n[1:], t[1:]), det.overlap(t[1:], n[1:]) ] if 0.8 >= r[0] and r[1] >= 0.8: s = np.exp( -dist(idealMatch, r) ) candidates.append( [j,s,n[0],r] ) if len(candidates) > 0: candidates.sort(key=lambda x:x[1]+x[2],reverse=True) for k in range( min(MAX_NUMBER_OF_PARTS,len(candidates)) ): layouts[i].addPart( inside[ candidates[k][0] ], candidates[k][-1] ) layouts.sort(key=lambda x: x.getScore(), reverse=True) return layouts
def findInsideMatches(inside, layouts): idealMatch = [0.25, 1.0] for i in range(len(layouts)): t = layouts[i].root candidates = [] for j in range(len(inside)): n = inside[j] r = [det.IoU(n[1:], t[1:]), det.overlap(t[1:], n[1:])] if 0.8 >= r[0] and r[1] >= 0.8: s = np.exp(-dist(idealMatch, r)) candidates.append([j, s, n[0], r]) if len(candidates) > 0: candidates.sort(key=lambda x: x[1] + x[2], reverse=True) for k in range(min(MAX_NUMBER_OF_PARTS, len(candidates))): layouts[i].addPart(inside[candidates[k][0]], candidates[k][-1]) layouts.sort(key=lambda x: x.getScore(), reverse=True) return layouts
def inside(box, gt, a=1.0, b=0.3, c=0.2): ov = det.overlap(gt, box) iou = det.IoU(box, gt) return ov >= a and iou <= b and iou >= c
def bigOverlap(box, gt): if ldet.overlap(box,gt) > 0.5 and ldet.IoU(box,gt) < 0.5: return 1.0 else: return 0.0 # Main Program if __name__ == "__main__": params = cu.loadParams("overlap groundTruth detections output") indexData = [x.split() for x in open(params['groundTruth'])] detectionsData = [x.split() for x in open(params['detections'])] overlapLimit = 1.0 if params['overlap'].startswith('big'): minOverlap = float(params['overlap'].replace('big','')) overlapMeasure = lambda x,y: np.exp( -( (1.0-ldet.overlap(x,y))**2 + (0.25-ldet.IoU(x,y))**2 ) ) #overlapMeasure = bigOverlap elif params['overlap'].startswith('tight'): minOverlap = float(params['overlap'].replace('tight','')) overlapMeasure = ldet.IoU elif params['overlap'].startswith('inside'): minOverlap = float(params['overlap'].replace('inside','')) overlapMeasure = lambda x,y: np.exp( -( (1.0-ldet.overlap(y,x))**2 + (0.25-ldet.IoU(x,y))**2 ) ) elif params['overlap'].startswith('OV'): overlapMeasure = ldet.overlap minOverlap = float(params['overlap'].replace('OV','')) elif params['overlap'].startswith('IN'): overlapMeasure = inside minOverlap = float(params['overlap'].replace('IN','')) else: overlapMeasure = ldet.IoU
def bigOverlap(box, gt): if ldet.overlap(box,gt) > 0.5 and ldet.IoU(box,gt) < 0.5: return 1.0 else: return 0.0
def background(box,gt): ov = det.overlap(box,gt) iou = det.IoU(box,gt) return ov < 0.3 and iou < 0.3
def big(box, gt, a=0.9, b=0.3, c=0.2): ov = det.overlap(box,gt) iou = det.IoU(box,gt) return ov >= a and iou <= b and iou >= c
import os, sys import utils as cu import libDetection as ldet import numpy as np params = cu.loadParams('scoresFile groundTruth relation output') scores = [x.split() for x in open(params['scoresFile'])] ground = cu.loadBoxIndexFile(params['groundTruth']) scores.sort(key=lambda x: float(x[1]), reverse=True) if params['relation'] == 'big': operator = lambda x, y: np.exp(-((1.0 - ldet.overlap(x, y))**2 + (0.25 - ldet.IoU(x, y))**2)) >= 0.7 if params['relation'] == 'inside': operator = lambda x, y: np.exp(-((1.0 - ldet.overlap(y, x))**2 + (0.25 - ldet.IoU(x, y))**2)) >= 0.7 if params['relation'] == 'tight': operator = lambda x, y: ldet.IoU(x, y) >= 0.5 out = open(params['output'], 'w') for s in scores: box = map(float, s[2:7]) img = s[0] try: gtBoxes = ground[img] except: gtBoxes = [] match = '0' for gt in gtBoxes: if operator(box, gt): match = '1'
def inside(box, gt, a=1.0, b=0.3, c=0.2): ov = det.overlap(gt,box) iou = det.IoU(box,gt) return ov >= a and iou <= b and iou >= c
def big(box, gt, a=0.9, b=0.3, c=0.2): ov = det.overlap(box, gt) iou = det.IoU(box, gt) return ov >= a and iou <= b and iou >= c
import os,sys import utils as cu import libDetection as ldet import numpy as np params = cu.loadParams('scoresFile groundTruth relation output') scores = [x.split() for x in open(params['scoresFile'])] ground = cu.loadBoxIndexFile(params['groundTruth']) scores.sort(key=lambda x:float(x[1]), reverse=True) if params['relation'] == 'big': operator = lambda x,y: np.exp( -( (1.0-ldet.overlap(x,y))**2 + (0.25-ldet.IoU(x,y))**2 ) ) >= 0.7 if params['relation'] == 'inside': operator = lambda x,y: np.exp( -( (1.0-ldet.overlap(y,x))**2 + (0.25-ldet.IoU(x,y))**2 ) ) >= 0.7 if params['relation'] == 'tight': operator = lambda x,y: ldet.IoU(x,y) >= 0.5 out = open(params['output'],'w') for s in scores: box = map(float,s[2:7]) img = s[0] try: gtBoxes = ground[img] except: gtBoxes = [] match = '0' for gt in gtBoxes: if operator(box,gt): match = '1' out.write(' '.join(s) + ' ' + match + '\n') out.close()
def background(box, gt): ov = det.overlap(box, gt) iou = det.IoU(box, gt) return ov < 0.3 and iou < 0.3
def validRegion(b1, b2): ov = det.overlap(b1, b2) if ov > 0.9: return ov else: return det.IoU(b1, b2)
def validRegion(b1,b2): ov = det.overlap(b1,b2) if ov > 0.9: return ov else: return det.IoU(b1,b2)