def loadAllGroundTruths(dir): groundTruths = {} for f in os.listdir(dir): if os.path.isfile(dir + '/' + f): category = f.split('_')[0] groundTruths[category] = cu.loadBoxIndexFile(dir + '/' + f) return groundTruths
def readPositivesData(positivesFeatures): #pos,posIdx = cu.loadMatrixAndIndex(positivesFeatures) pos = cu.loadMatrixNoCompression(positivesFeatures) posIdx = cu.loadBoxIndexFile(positivesFeatures.replace('fc6_neuron_cudanet_out','idx')) ari,osi = [],[] print 'Positive Matrix loaded ('+str(pos.shape[0])+' instances)' return (pos,posIdx,ari,osi)
def __init__(self, environment=None, groundTruthFile=None): Task.__init__(self, environment) if groundTruthFile is not None: self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.epochRecall = [] self.epochMaxIoU = [] self.epochLandmarks = []
def __init__(self, environment=None, groundTruthFile=None): Task.__init__(self, environment) if groundTruthFile is not None: self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.epochRecall = [] self.epochMaxIoU = [] self.epochLandmarks = []
def readPositivesData(positivesFeatures): #pos,posIdx = cu.loadMatrixAndIndex(positivesFeatures) pos = cu.loadMatrixNoCompression(positivesFeatures) posIdx = cu.loadBoxIndexFile( positivesFeatures.replace('fc6_neuron_cudanet_out', 'idx')) ari, osi = [], [] print 'Positive Matrix loaded (' + str(pos.shape[0]) + ' instances)' return (pos, posIdx, ari, osi)
def __init__(self, scoresDir, proposals): self.dir = scoresDir self.files = os.listdir(scoresDir) self.files.sort() self.categories = [] for f in self.files: names = f.split('_')[0:2] cat = '_'.join(names) self.categories.append( cat ) self.categories.sort() self.imgBoxes = {} self.scores = {} boxes = cu.loadBoxIndexFile(proposals) for img in boxes.keys(): self.imgBoxes[img] = np.array(boxes[img]) self.scores[img] = -10*np.ones( (len(boxes[img]), len(self.categories)) )
def __init__(self, imageList, mode, groundTruthFile=None): self.mode = mode self.cnn = cn.ConvNet() self.testRecord = None self.idx = -1 self.imageList = [x.strip() for x in open(imageList)] self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) #self.imageList = self.rankImages() #self.imageList = self.imageList[0:10] allImgs = set([x.strip() for x in open(config.get('allImagesList'))]) self.negativeSamples = list(allImgs.difference(set(self.groundTruth.keys()))) self.negativeEpisode = False if self.mode == 'train': self.negativeProbability = config.getf('negativeEpisodeProb') random.shuffle(self.imageList) self.loadNextEpisode()
def __init__(self, imageList, mode, groundTruthFile=None): self.mode = mode self.cnn = cn.ConvNet() self.testRecord = None self.idx = -1 self.imageList = [x.strip() for x in open(imageList)] self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) #self.imageList = self.rankImages() #self.imageList = self.imageList[0:10] allImgs = set([x.strip() for x in open(config.get('allImagesList'))]) self.negativeSamples = list( allImgs.difference(set(self.groundTruth.keys()))) self.negativeEpisode = False if self.mode == 'train': self.negativeProbability = config.getf('negativeEpisodeProb') random.shuffle(self.imageList) self.loadNextEpisode()
def __init__(self, scoresDir, proposals): self.dir = scoresDir self.files = os.listdir(scoresDir) self.files.sort() self.categories = [] for f in self.files: names = f.split('_')[0:2] cat = '_'.join(names) self.categories.append(cat) self.categories.sort() self.imgBoxes = {} self.scores = {} boxes = cu.loadBoxIndexFile(proposals) for img in boxes.keys(): self.imgBoxes[img] = np.array(boxes[img]) self.scores[img] = -10 * np.ones( (len(boxes[img]), len(self.categories)))
return result def reformatGroundTruth(gt, category): result = [] for img in gt.keys(): for box in gt[img]: result.append( [img, category + '_tight'] + box ) return result def saveResults(outputFile, results): outputFile = open(outputFile,'w') for r in results: outputFile.write(r[0] + ' ' + r[1] + ' ' + ' '.join(map(str,map(int,r[2:]))) + '\n') outputFile.close() if __name__ == "__main__": params = cu.loadParams("proposalsFile groundTruthDir outputFile") proposals = cu.loadBoxIndexFile(params['proposalsFile']) records = [] files = os.listdir(params['groundTruthDir']) files.sort() for f in files: category = f.split('_')[0] print category groundTruth = cu.loadBoxIndexFile(params['groundTruthDir'] + '/' + f) records += selectRegions(proposals, groundTruth, category, big) records += selectRegions(proposals, groundTruth, category, inside) records += reformatGroundTruth(groundTruth, category) saveResults(params['outputFile'], records)
for i in range(parts): nb = [gt[0] + i * step, y1, gt[0] + i * step + adjustedSize, y2] scaleBoxes.append(shiftBox(nb, w, h)) else: x1, x2 = 0, adjustedSize parts = int(round(bh / adjustedSize + 0.3)) step = (bh - adjustedSize) / max(parts - 1, 1) for i in range(parts): nb = [x1, gt[1] + i * step, x2, gt[1] + i * step + adjustedSize] scaleBoxes.append(shiftBox(nb, w, h)) return scaleBoxes if __name__ == "__main__": params = cu.loadParams("groundTruthBoxes imageDir outputDir cropSize") groundTruthBoxes = cu.loadBoxIndexFile(params["groundTruthBoxes"]) cropSize = int(params["cropSize"]) projections = {} overlaps = [] ious = [] for img in groundTruthBoxes.keys(): print img name = img.split("/")[1] if not os.path.isfile(params["imageDir"] + "/" + name + ".JPEG"): continue im = Image.open(params["imageDir"] + "/" + name + ".JPEG") w, h = im.size try: p = projections[img] except:
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 params = cu.loadParams( 'bigDetections tightDetections insideDetections imageDir outputDir') big = cu.loadBoxIndexFile(params['bigDetections']) tight = cu.loadBoxIndexFile(params['tightDetections']) inside = cu.loadBoxIndexFile(params['insideDetections']) print 'Images:', len(big), len(tight), len(inside) allLayouts = [] for k in big.keys(): layouts = findBigMatches(k, big[k], tight[k]) layouts = findInsideMatches(inside[k], layouts) allLayouts += [layouts[0]] allLayouts.sort(key=lambda x: x.getScore(), reverse=True) matchCounter = 0 for ol in allLayouts: img, boxes, scores = ol.getLayoutData()
def __init__(self, environment, groundTruthFile): Task.__init__(self, environment) self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.prevAction = 0 self.prevPos = (0, 0, 0)
def __init__(self, environment, groundTruthFile): Task.__init__(self, environment) self.groundTruth = cu.loadBoxIndexFile(groundTruthFile)
scaleBoxes.append(shiftBox(nb, w, h)) else: x1, x2 = 0, adjustedSize parts = int(round(bh / adjustedSize + 0.3)) step = (bh - adjustedSize) / max(parts - 1, 1) for i in range(parts): nb = [ x1, gt[1] + i * step, x2, gt[1] + i * step + adjustedSize ] scaleBoxes.append(shiftBox(nb, w, h)) return scaleBoxes if __name__ == "__main__": params = cu.loadParams("groundTruthBoxes imageDir outputDir cropSize") groundTruthBoxes = cu.loadBoxIndexFile(params['groundTruthBoxes']) cropSize = int(params['cropSize']) projections = {} overlaps = [] ious = [] for img in groundTruthBoxes.keys(): print img name = img.split('/')[1] if not os.path.isfile(params['imageDir'] + '/' + name + '.JPEG'): continue im = Image.open(params['imageDir'] + '/' + name + '.JPEG') w, h = im.size try: p = projections[img] except:
def __init__(self, environment, groundTruthFile): Task.__init__(self, environment) self.groundTruth = cu.loadBoxIndexFile(groundTruthFile)
def __init__(self, environment, groundTruthFile): Task.__init__(self, environment) self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.prevAction = 0 self.prevPos = (0,0,0)
nFeat = r[0].shape[1] featureMatrix = np.zeros( (nBoxes,nFeat) ) i = 0 outputFile = open(outputDir + '/' + category + '.idx','w') for r in result: featureMatrix[i:i+r[0].shape[0]] = r[0] for box in r[1]: outputFile.write(box[0] + ' ' + ' '.join(map(str,map(int,box[1:]))) + '\n') i += r[0].shape[0] outputFile.close() cu.saveMatrix(featureMatrix,outputDir + '/' + category + '.' + featExt) print 'Total of',nBoxes,'positive examples collected for',category if __name__ == "__main__": params = cu.loadParams("imageList featuresDir groundTruthFile outputDir featuresExt category operation") groundTruths = cu.loadBoxIndexFile(params['groundTruthFile']) imageList = [x.replace('\n','') for x in open(params['imageList'])] operator = None if params['operation'] == 'big': operator = big elif params['operation'] == 'tight': operator = tight elif params['operation'] == 'inside': operator = inside elif params['operation'] == 'background': operator = background else: print 'Select a valid operation: [big | tight | inside | background]' sys.exit() selectRegions(imageList, params['featuresDir'], groundTruths, params['outputDir'], params['featuresExt'], params['category'], operator)
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 params = cu.loadParams('bigDetections tightDetections insideDetections imageDir outputDir') big = cu.loadBoxIndexFile( params['bigDetections'] ) tight = cu.loadBoxIndexFile( params['tightDetections'] ) inside = cu.loadBoxIndexFile( params['insideDetections'] ) print 'Images:',len(big),len(tight),len(inside) allLayouts = [] for k in big.keys(): layouts = findBigMatches(k, big[k], tight[k]) layouts = findInsideMatches(inside[k], layouts) allLayouts += [ layouts[0] ] allLayouts.sort(key=lambda x: x.getScore(), reverse=True) matchCounter = 0 for ol in allLayouts: img, boxes, scores = ol.getLayoutData()
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()
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 def saveCandidates(candidates, output): out = open(output, 'w') for k in candidates: out.write(k[0] + ' ' + ' '.join(map(str, map(int, k[2:6]))) + '\n') out.close() if __name__ == "__main__": params = cu.loadParams("detectionsFile groundTruths output") detectionsData = [x.split() for x in open(params['detectionsFile'])] detections = eval.loadDetections(detectionsData) groundTruth = cu.loadBoxIndexFile(params['groundTruths']) candidates = selectBestBoxes(detections, groundTruth, 0.5) print 'Selected candidates:', len(candidates) saveCandidates(candidates, params['output'])
def reformatGroundTruth(gt, category): result = [] for img in gt.keys(): for box in gt[img]: result.append([img, category + '_tight'] + box) return result def saveResults(outputFile, results): outputFile = open(outputFile, 'w') for r in results: outputFile.write(r[0] + ' ' + r[1] + ' ' + ' '.join(map(str, map(int, r[2:]))) + '\n') outputFile.close() if __name__ == "__main__": params = cu.loadParams("proposalsFile groundTruthDir outputFile") proposals = cu.loadBoxIndexFile(params['proposalsFile']) records = [] files = os.listdir(params['groundTruthDir']) files.sort() for f in files: category = f.split('_')[0] print category groundTruth = cu.loadBoxIndexFile(params['groundTruthDir'] + '/' + f) records += selectRegions(proposals, groundTruth, category, big) records += selectRegions(proposals, groundTruth, category, inside) records += reformatGroundTruth(groundTruth, category) saveResults(params['outputFile'], records)