コード例 #1
0
def buildDataSetWithHighOverlap(positivesFeatures, logData, topKPositives,
                                featuresDir, featuresExt):
    allPos, allPosIdx, ari, osi = training.readPositivesData(positivesFeatures)
    detections = [
        x[0:5] + [float(x[6]), x[7]] for x in logData[0:2 * topKPositives]
    ]
    detections = [x for x in detections if x[6] == '1' and x[5] >= minOverlap]
    pos = np.zeros((len(detections), allPos.shape[1]))
    posIdx = []
    for i in range(len(detections)):
        imgName = detections[i][0]
        box = map(float, detections[i][1:5])
        bestOverlap, bestIdx, idx = 0.0, 0, 0
        for truePos in allPosIdx:
            if truePos[0] == imgName:
                ov = det.IoU(box, map(float, truePos[1:5]))
                if ov > bestOverlap:
                    bestOverlap = ov
                    bestIdx = idx
            idx += 1
        pos[i, :] = allPos[bestIdx, :]
        posIdx.append(allPosIdx[bestIdx])
    print 'Positive Matrix with High Overlaping Detections (' + str(
        pos.shape[0]) + ' instances)'
    return (pos, posIdx, ari, osi)
コード例 #2
0
 def run(self,img,features,bboxes):
   if self.model:
     features,bboxes = self.rank(img,features,bboxes)
   if features == None:
     return ([],[],[],[])
   positives,negatives = [],[]
   imageData = self.groundTruths[img]
   for i in range( len(bboxes) ):
     isPositive,isNegative = False,False
     for j in imageData:
       o = det.IoU(j,map(float,bboxes[i][1:]))
       if o >= 0.85:
         isPositive = True
         break
       elif self.overlap >= o and o > 0:
         isNegative = True
     if isPositive: 
       positives.append(i)
     if isNegative:
       negatives.append(i)
   if self.model:
     negatives.reverse()
   else:
     cu.rnd.shuffle(negatives)
   posIdx = [bboxes[t] for t in positives]
   posFeat = [features[positives]]
   negIdx = [bboxes[t] for t in negatives[0:self.maxNegatives]]
   negFeat = [features[negatives[0:self.maxNegatives]]]
   return (posIdx,posFeat,negIdx,negFeat)
コード例 #3
0
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
コード例 #4
0
def scoreProposals(scoringAreas, imgProp):
    propScores = []
    for box in imgProp:
        boxScore = 0.0
        for sarea in scoringAreas:
            boxScore += det.IoU(box[1:], sarea[0:4]) * sarea[4]
        propScores.append(boxScore)
    return propScores
コード例 #5
0
 def matchBoxes(self, box, gt):
     maxIoU = -1.
     maxIdx = 0
     for i in range(len(gt)):
         iou = det.IoU(box, gt[i])
         if iou > maxIoU:
             maxIoU = iou
             maxIdx = i
     return (maxIoU, maxIdx)
コード例 #6
0
 def matchBoxes(self, box):
     maxIoU = -1.
     maxIdx = 0
     for i in range(len(self.boxes)):
         if self.control['DONE'][i]:
             continue
         iou = det.IoU(box, self.boxes[i])
         if iou > maxIoU:
             maxIoU = iou
             maxIdx = i
     return (maxIoU, maxIdx)
コード例 #7
0
def mapToGroundTruth(img, box, groundTruths):
  result = {}
  for cat in groundTruths.keys():
    try: annotations = groundTruths[cat][img]
    except: continue
    for gt in annotations:
      iou = det.IoU(box, gt)
      rel = findRelation(box, gt, iou)
      if rel != None:
        result[cat+'_'+rel] = iou
  #if len(result.keys()) > 1:
  #  print result
  return result
コード例 #8
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
コード例 #9
0
def selectBestBoxes(detections, groundTruth, minOverlap):
    candidates = []
    for d in detections:
        try:
            boxes = groundTruth[d[0]]
        except:
            continue
        bestIoU = 0.0
        for gt in boxes:
            iou = det.IoU(d[2:6], gt)
            if iou > bestIoU:
                bestIoU = iou
        print bestIoU
        if bestIoU > minOverlap:
            candidates.append(d)
    return candidates
コード例 #10
0
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
コード例 #11
0
def loadScores(memDir, featuresDir, categoryIndex):
    totalNumberOfBoxes = 0
    sumOfPercentBoxesUsed = 0
    totalImages = 0
    scoredDetections = {}
    for f in os.listdir(memDir):
        imageName = f.replace('.txt', '')
        totalImages += 1
        data = json.load(open(memDir + f, 'r'))
        features = scipy.io.loadmat(featuresDir + imageName + '.mat')
        boxes = []
        scores = []
        time = []
        t = 0
        for boxSet in data['boxes']:
            for box in boxSet:
                # Find scores for this box
                for i in range(features['boxes'].shape[0]):
                    iou = det.IoU(box, features['boxes'][i, :].tolist())
                    if iou == 1.0:
                        scores.append(features['scores'][i, categoryIndex])
                        break
                boxes.append(box)
                time.append(t)
            t += 1
        scoredDetections[imageName] = {
            'boxes': boxes,
            'scores': scores,
            'time': time
        }
        totalNumberOfBoxes += len(boxes)
        percentBoxesUsed = 100 * (float(len(boxes)) /
                                  features['boxes'].shape[0])
        sumOfPercentBoxesUsed += percentBoxesUsed
        print imageName, 'boxes:', len(boxes), '({:5.2f}% of {:4})'.format(
            percentBoxesUsed,
            features['boxes'].shape[0]), 'scores:', len(scores)
        #if totalImages > 5: break

    maxTime = np.max(time)
    print 'Average boxes per image: {:5.1f}'.format(totalNumberOfBoxes /
                                                    float(totalImages))
    print 'Average percent of boxes used: {:5.2f}%'.format(
        sumOfPercentBoxesUsed / float(totalImages))
    return scoredDetections, maxTime
コード例 #12
0
def createNegativeWindows(width, height, boxes, windowSize, stride):
    regions = []
    for j in range((height - windowSize) / stride):
        for i in range((width - windowSize) / stride):
            x1 = i * stride + 1
            y1 = j * stride + 1
            x2 = x1 + windowSize
            y2 = y1 + windowSize
            box = [x1,y1,x2,y2]
            maxOv = 0
            for b in boxes:
                ov1 = det.IoU(b, box)
                if ov1 > maxOv:
                    maxOv = ov1
            if maxOv >= 0.5:
                info = [1, maxOv]
            else:
                info = [0, maxOv]
            regions.append( info + box )
    return regions
コード例 #13
0
    #  b = filteredBoxes[i]
    #  print b[0] + ' {:.8f} {:} {:} {:} {:}\n'.format(filteredScores[i],b[1],b[2],b[3],b[4])
    det.showDetections('/home/caicedo/data/allimgs/' + testImage + '.jpg',
                       filteredBoxes, filteredScores, True)
    det.showDetections('/home/caicedo/data/allimgs/' + testImage + '.jpg',
                       candidateBoxes, candidateScores, False)
    det.showBestMatches('/home/caicedo/data/allimgs/' + testImage + '.jpg',
                        candidateBoxes, candidateScores,
                        groundTruth[testImage])

sys.exit()
import matplotlib.pyplot as plt
features = np.asmatrix(features)
K = features * features.T
N = np.diag(K)
D = np.tile(np.mat(N).T, (1, K.shape[0])) + np.tile(np.mat(N),
                                                    (K.shape[0], 1)) - 2 * K
plt.imshow(G)
plt.colorbar()
plt.show()

boxes = [map(float, x[1:]) for x in bboxes]
O = np.zeros(G.shape)
for i in range(O.shape[0]):
    for j in range(O.shape[1]):
        O[i, j] = det.IoU(boxes[i], boxes[j])

plt.imshow(O)
plt.colorbar()
plt.show()
コード例 #14
0
def validRegion(b1, b2):
    ov = det.overlap(b1, b2)
    if ov > 0.9:
        return ov
    else:
        return det.IoU(b1, b2)
コード例 #15
0
  try: overlapsImages[o[0]].append( o[1:] )
  except: overlapsImages[o[0]] = [ o[1:] ]

print 'Total images => Scores:',len(scoresImages),'Overlaps:',len(overlapsImages)

perfectMatches = 0
selectedRecords = {}
for img in scoresImages.keys():
  selectedRecords[img] = []
  for i in range(len(scoresImages[img])):
    b1 = map(float, scoresImages[img][i][1:5])
    maxIoU = 0.0
    bestMatch = -1
    for j in range(len(overlapsImages[img])):
      b2 = map(float, overlapsImages[img][j][0:4])
      iou = det.IoU(b1,b2)
      if iou > maxIoU:
        bestMatch = j
        maxIoU = iou
    if maxIoU == 1.0: perfectMatches += 1
    selectedRecords[img].append( scoresImages[img][i][1:5] +  
        [scoresImages[img][i][0]] + overlapsImages[img][bestMatch][-2:] )
    del overlapsImages[img][bestMatch]

print 'Perfect matches:',perfectMatches

out = open(outFile, 'w')
for img in selectedRecords.keys():
  for r in selectedRecords[img]:
    out.write(img + ' ' + ' '.join(r) + '\n' )
out.close()
コード例 #16
0
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'
コード例 #17
0
def tight(box, gt, a=0.9):
    iou = det.IoU(box, gt)
    return iou >= a
コード例 #18
0
  counts = {'big': {'tp':0,'tn':0,'fp':0,'fn':0}, 'tight':{'tp':0,'tn':0,'fp':0,'fn':0}, 'inside':{'tp':0,'tn':0,'fp':0,'fn':0}}
  allBoxes = 0
  for img in results.keys():
    try:
      boxes = groundTruths[img]
      imageOK = True
    except:
      imageOK = False
    if imageOK:
      out = open(params['outputDir']+'/'+img+'.region_rank','w')
      for box in results[img].keys():
        allBoxes += 1
        maxIoU,assigned = 0,[0,0,0,0]
        for gt in groundTruths[img]:
          iou = det.IoU(results[img][box]['box'],gt)
          if iou > maxIoU: 
            assigned = gt
            maxIoU = iou
        res = evaluateType(results[img][box],assigned,threshold)
        for r in res.keys():
          counts[r][res[r]] += 1
        if res[ results[img][box]['type'] ] == 'tp':
          correct = '1'
        else:
          correct = '0'
        out.write( img + ' ' + results[img][box]['score'] + ' ' + box + ' ' + results[img][box]['type'] + ' ' + correct + '\n')
      out.close()
  
  print 'AllBoxes:',allBoxes
  for type in counts.keys():
コード例 #19
0
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
コード例 #20
0
def background(box, gt):
    ov = det.overlap(box, gt)
    iou = det.IoU(box, gt)
    return ov < 0.3 and iou < 0.3
コード例 #21
0
    except:
        labels[d[0]] = [r]
    categories.add(d[1])
categories = list(categories)
categories.sort()
C = dict([(categories[c], c) for c in range(len(categories))])

print 'Identifying labeled boxes'
L = np.zeros((M['B'].shape[0], 60), np.int32)
for img in labels.keys():
    print img
    idx = T[img]
    for j in range(idx['s'], idx['e']):
        box = M['B'][j, :].tolist()
        for l in labels[img]:
            iou = det.IoU(box, l[1:])
            if iou == 1.0:
                L[j, C[l[0]]] = 1

archive['labels'] = L

scipy.io.savemat(params['outputDir'] + '/boxes.mat', {'boxes:': M['B']},
                 do_compression=True)
scipy.io.savemat(params['outputDir'] + '/scores.mat', {'scores:': M['S']})
scipy.io.savemat(params['outputDir'] + '/labels.mat', {'labels:': L})
scipy.io.savemat(params['outputDir'] + '/index.mat', {
    'index:': index,
    'images': T.keys()
},
                 do_compression=True)
コード例 #22
0
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
コード例 #23
0
 def rankImages(self):
     keys = self.groundTruth.keys()
     keys.sort()
     # Rank by number of objects in the scene (from many to few)
     objectCounts = [len(self.groundTruth[k]) for k in keys]
     countRank = np.argsort(objectCounts)[::-1]
     countDist = dict([(i, 0) for i in range(max(objectCounts) + 1)])
     for o in objectCounts:
         countDist[o] += 1
     print 'Distribution of object counts (# objects vs # images):', countDist
     print 'Images with largest number of objects:', [
         keys[i] for i in countRank[0:10]
     ]
     # Rank by object size (from small to large)
     minObjectArea = [min(map(det.area, self.groundTruth[k])) for k in keys]
     smallRank = np.argsort(minObjectArea)
     intervals = [(500 * 400 / i) for i in range(1, 21)]
     sizeDist = dict([(i, 0) for i in intervals])
     for a in minObjectArea:
         counted = False
         for r in intervals:
             if a >= r:
                 sizeDist[r] += 1
                 counted = True
                 break
         if not counted: sizeDist[r] += 1
     print 'Distribution of smallest objects area (area vs # images):', [
         (i, sizeDist[i]) for i in intervals
     ]
     print 'Images with the smallest objects:', [
         keys[i] for i in smallRank[0:10]
     ]
     # Rank by object size (from large to small)
     maxObjectArea = [max(map(det.area, self.groundTruth[k])) for k in keys]
     bigRank = np.argsort(minObjectArea)
     intervals = [(500 * 400 / i) for i in range(1, 21)]
     sizeDist = dict([(i, 0) for i in intervals])
     for a in maxObjectArea:
         counted = False
         for r in intervals:
             if a >= r:
                 sizeDist[r] += 1
                 counted = True
                 break
         if not counted: sizeDist[r] += 1
     print 'Distribution of biggest objects area (area vs # images):', [
         (i, sizeDist[i]) for i in intervals
     ]
     print 'Images with the biggest objects:', [
         keys[i] for i in bigRank[0:10]
     ]
     # Rank images by instance occlusion (from very occluded to isolated)
     maxInstanceOcclusion = []
     for k in keys:
         if len(self.groundTruth[k]) == 1:
             maxInstanceOcclusion.append(0)
         else:
             maxIoU = 0
             for i in range(len(self.groundTruth[k])):
                 for j in range(i + 1, len(self.groundTruth[k])):
                     iou = det.IoU(self.groundTruth[k][i],
                                   self.groundTruth[k][j])
                     if iou > maxIoU:
                         maxIoU = iou
             maxInstanceOcclusion.append(maxIoU)
     occlusionRank = np.argsort(maxInstanceOcclusion)[::-1]
     intervals = [1.0 / i for i in range(1, 21)]
     occlusionDist = dict([(i, 0) for i in intervals])
     for o in maxInstanceOcclusion:
         counted = False
         for r in intervals:
             if o >= r:
                 occlusionDist[r] += 1
                 counted = True
                 break
         if not counted: occlusionDist[r] += 1
     print 'Distribution of object occlusion (occlusion vs # images):', [
         (i, occlusionDist[i]) for i in intervals
     ]
     print 'Images with the most occluded objects:', [
         keys[i] for i in occlusionRank[0:10]
     ]
     # Rank combination
     rank = dict([(k, 0) for k in keys])
     for i in range(len(keys)):
         rank[keys[countRank[i]]] += i
         rank[keys[smallRank[i]]] += i
         rank[keys[occlusionRank[i]]] += i
     values = [rank[i] for i in keys]
     complexityRank = np.argsort(values)
     print 'More complex images:', [keys[i] for i in complexityRank[0:10]]
     return [keys[i] for i in occlusionRank]
コード例 #24
0
 mat = scipy.io.loadmat(params['matFilesDir'] + '/' + f)
 idx = mat['gt'] == 0
 mat['feat'] = mat['feat'][idx[:, 0], :]
 mat['gt'] = mat['gt'][idx[:, 0], :]
 mat['boxes'] = mat['boxes'][idx[:, 0], :]
 mat['overlap'] = np.zeros((mat['feat'].shape[0], len(categories)))
 mat['class'] = np.zeros((mat['feat'].shape[0], 1))
 duplicate = []
 try:
     groundTruths = relations[img]
 except:
     groundTruths = []
 for gt in groundTruths:
     for i in range(mat['boxes'].shape[0]):
         box = mat['boxes'][i, :].tolist()
         iou = det.IoU(box, gt[1:])
         if iou > mat['overlap'][i, categories[gt[0]]]:
             mat['overlap'][i, categories[gt[0]]] = iou
         if iou == 1:
             mat['gt'][i] = 1.0
             if mat['class'][i] == 0 or mat['class'][i] == categories[
                     gt[0]] + 1:
                 mat['class'][i] = categories[gt[0]] + 1
             else:
                 duplicate.append({
                     'row': i,
                     'class': categories[gt[0]] + 1
                 })
 shift = 0
 for d in duplicate:
     for key in ['feat', 'gt', 'boxes', 'overlap', 'class']: