Beispiel #1
0
def mainLoop(modelType, modelArgs, positives, trainingList, featuresDir,
             featuresExt, modelOut, maxNegOverlap, iter):
    pos, posIdx, featSize, fmSize = positives
    featureSpace = pos.shape[1]
    startTime = cu.tic()
    if iter == 0:
        ## Random Negatives
        print ' >>> RANDOM NEGATIVES'
        N, negIdx = learn.getRandomNegs(featuresDir, trainingList, featuresExt,
                                        featSize, maxVectorsCache,
                                        maxNegativeImages)
        cellsPerImage = featSize / featureSpace
        N = N.reshape((N.shape[0], featureSpace, fmSize,
                       fmSize))  # Recover original feature layout
        neg = np.zeros((cellsPerImage * N.shape[0], featureSpace))
        for i in range(N.shape[0]):
            neg[i * cellsPerImage:(i + 1) * cellsPerImage] = N[i].T.reshape(
                (cellsPerImage, featureSpace))  # Unfold features
        hards = {'features': np.zeros((0, neg.shape[1])), 'index': []}
        lap = cu.toc(
            'Random negatives matrix (' + str(neg.shape[0]) + ' instances)',
            startTime)
    else:
        ## Mine hard negatives
        print ' >>> MINING HARD NEGATIVES'
        model = det.createDetector(modelType, modelArgs)
        model.load(modelOut + '.' + str(iter - 1))
        detList, detMatrix = maskDetector.detectObjects(
            model, trainingList, featuresDir, featuresExt, -10.0)
        hdnList, detMatrix = maskDetector.selectHardNegatives(
            detList, detMatrix, posIdx, maxNegativeVectors)
        neg = maskDetector.loadHardNegativesFromMatrix(featuresDir, hdnList,
                                                       detMatrix, featuresExt,
                                                       featureSpace,
                                                       maxNegativeVectors)
        hards = cu.loadMatrixNoCompression(modelOut + '.hards').item()
        lap = cu.toc(
            'Hard negatives (' + str(neg.shape[0]) + ' mined + ' +
            str(hards['features'].shape[0]) + ' previous instances)',
            startTime)

    ## Learn Detector
    neg = np.concatenate((neg, hards['features']))
    clf = det.createDetector(modelType, modelArgs)
    clf.learn(pos, neg)
    clf.save(modelOut + '.' + str(iter))
    lap = cu.toc('Classifier learned:', lap)

    ## Keep hard negatives for next iterations
    scores = clf.predict(neg)
    hardNegsIdx = np.argsort(scores)
    hardNeg = np.concatenate(
        (hards['features'], neg[hardNegsIdx[-cu.topHards:]]))
    cu.saveMatrixNoCompression({'features': hardNeg}, modelOut + '.hards')
    print ' ** Iteration', iter, 'done'
def mainLoop(modelType,modelArgs,positives,trueObjectBoxes,trainingList,featuresDir,featuresExt,modelOut,maxNegOverlap,iter):
  pos,posIdx,ari,osi = positives
  startTime = cu.tic()
  if iter == 0:
    ## Random Negatives
    print ' >>> RANDOM NEGATIVES'
    neg,negIdx = learn.getRandomNegs(featuresDir,trainingList,featuresExt,pos.shape[1],maxVectorsCache,maxNegativeImages)
    detectionsList = [ [x[0],'0.0']+x[1:]+['1'] for x in negIdx]
    hards = {'features':np.zeros((0,neg.shape[1])),'index':[]}
    lap = cu.toc('Random negatives matrix ('+str(neg.shape[0])+' instances)',startTime)
  else:
    ## Mine hard negatives
    print ' >>> MINING HARD NEGATIVES'
    model = det.createDetector(modelType,modelArgs)
    model.load(modelOut+'.'+ str( iter-1 ))
    detectionsList = detector.detectObjects(model,trainingList,featuresDir,featuresExt,0.3,-10.0)
    hards = cu.loadMatrixNoCompression(modelOut+'.hards').item()
    lap = cu.toc('Hard negatives matrix ('+str(hards['features'].shape[0])+' instances)',startTime)

  ## Rank and clean negative detections
  detectionsData = evaluation.loadDetections(detectionsList)
  groundTruth = evaluation.loadGroundTruthAnnotations(trueObjectBoxes)
  detectionsLog = evaluation.evaluateDetections(groundTruth,detectionsData,0.5,allowDuplicates=True) # overlapMeasure=validRegion,
  evaluation.computePrecisionRecall(len(posIdx),detectionsLog['tp'],detectionsLog['fp'],'tmp.txt')
  evaluation.computePrecAt(detectionsLog['tp'],[20,50,100,200,300,400,500])
  logData = learn.parseRankedDetectionsFile(detectionsLog['log'],maxNegOverlap,maxNegativeVectors)
  print ' >>> LOADING HARD NEGATIVES'
  neg,negIdx = learn.loadHardNegativesFromList(featuresDir,logData['negExamples'],featuresExt,pos.shape[1],logData['negTaken'])
  del(detectionsList,detectionsData,detectionsLog,logData)
  lap = cu.toc('Ranked negatives matrix ('+str(neg.shape[0])+' instances)',lap)
  neg = np.concatenate( (neg,hards['features']) )
  negIdx = negIdx + hards['index']

  ## Learn Detector
  clf = det.createDetector(modelType,modelArgs)
  clf.learn(pos,neg,posIdx,negIdx)
  clf.save(modelOut+'.'+str(iter))
  lap = cu.toc('Classifier learned:',lap)

  ## Keep hard negatives for next iterations
  scores = clf.predict(neg,negIdx)
  hardNegsIdx = np.argsort(scores)
  hardNeg = np.concatenate( (hards['features'], neg[hardNegsIdx[-cu.topHards:]]) )
  negIdx = hards['index'] + [negIdx[j] for j in hardNegsIdx[-cu.topHards:]]
  print 'Hard negatives:',hardNeg.shape[0]
  hards = {'features':hardNeg, 'index':negIdx}
  cu.saveMatrixNoCompression({'features':hardNeg,'index':negIdx},modelOut+'.hards')

  print ' ** Iteration',iter,'done'
  return {'detector':clf,'pos':pos,'posIdx':posIdx,'neg':neg,'negIdx':negIdx}
Beispiel #3
0
 def __init__(self, dir):
     allModels = []
     for f in os.listdir(dir):
         model = det.createDetector('linear')
         model.load(dir + f)
         allModels.append(model)
     feat = allModels[0].clf.coef_.shape[1]
     M = np.zeros((len(allModels), feat + 1))
     for i in range(len(allModels)):
         M[i, 0:feat] = allModels[i].clf.coef_[0, :]
         M[i, feat] = allModels[i].clf.intercept_[0]
     self.M = M
 def __init__(self, dir):
   allModels = []
   for f in os.listdir(dir):
     model = det.createDetector('linear')
     model.load(dir + f)
     allModels.append(model)
   feat = allModels[0].clf.coef_.shape[1]
   M = np.zeros( (len(allModels), feat + 1) )
   for i in range(len(allModels)):
     M[i,0:feat] = allModels[i].clf.coef_[0,:]
     M[i,feat] = allModels[i].clf.intercept_[0]
   self.M = M
            a = map(int,areas[i])
            smap[ a[1]:a[3], a[0]:a[2] ] += scores[i]
            
        a = fig.add_subplot(1,2,2)
        smap[0,0] = 20
        smap[ox-1,oy-1] = -20
        plt.imshow(smap)
        plt.savefig('/home/caicedo/data/rcnn/masksOut/'+image+'.png',bbox_inches='tight')        

########################################
## MAIN PROGRAM
########################################
if __name__ == "__main__":
    MIN_AREA = 99.0*99.0
    MAX_AREA = 227.0*227.0
    CONV_LAYER = 'conv3'
    ## Main Program Parameters
    params = cu.loadParams("modelFile testImageList proposalsFile featuresDir featuresExt threshold outputDir")
    model = det.createDetector('linear')
    model.load(params['modelFile'])
    imageList = [x.replace('\n','') for x in open(params['testImageList'])]
    proposals = mk.loadBoxIndexFile(params['proposalsFile'])
    threshold = float(params['threshold'])
    ## Make detections and transfer scores
    projector = PredictionsToImagePlane(proposals,CONV_LAYER,MIN_AREA,MAX_AREA,0.7)
    results = detectObjects(model,imageList,params['featuresDir'],params['featuresExt'],-10.0,projector)
    out = open(params['outputDir'],'w')
    for r in results:
        out.write(' '.join(map(str,r))+' 0\n')
    out.close()
Beispiel #6
0
        plt.imshow(smap)
        plt.savefig('/home/caicedo/data/rcnn/masksOut/' + image + '.png',
                    bbox_inches='tight')


########################################
## MAIN PROGRAM
########################################
if __name__ == "__main__":
    MIN_AREA = 99.0 * 99.0
    MAX_AREA = 227.0 * 227.0
    CONV_LAYER = 'conv3'
    ## Main Program Parameters
    params = cu.loadParams(
        "modelFile testImageList proposalsFile featuresDir featuresExt threshold outputDir"
    )
    model = det.createDetector('linear')
    model.load(params['modelFile'])
    imageList = [x.replace('\n', '') for x in open(params['testImageList'])]
    proposals = mk.loadBoxIndexFile(params['proposalsFile'])
    threshold = float(params['threshold'])
    ## Make detections and transfer scores
    projector = PredictionsToImagePlane(proposals, CONV_LAYER, MIN_AREA,
                                        MAX_AREA, 0.7)
    results = detectObjects(model, imageList, params['featuresDir'],
                            params['featuresExt'], -10.0, projector)
    out = open(params['outputDir'], 'w')
    for r in results:
        out.write(' '.join(map(str, r)) + ' 0\n')
    out.close()
Beispiel #7
0
def mainLoop(modelType, modelArgs, positives, trainingList, featuresDir,
             featuresExt, modelOut, maxNegOverlap, iter):
    pos, posIdx, ari, osi = positives
    startTime = cu.tic()
    if iter == 0:
        ## Random Negatives
        print ' >>> RANDOM NEGATIVES'
        neg, negIdx = learn.getRandomNegs(featuresDir, trainingList,
                                          featuresExt, pos.shape[1],
                                          maxVectorsCache, maxNegativeImages)
        detectionsList = [[x[0], '0.0'] + x[1:] + ['1'] for x in negIdx]
        hards = {'features': np.zeros((0, neg.shape[1])), 'index': []}
        lap = cu.toc(
            'Random negatives matrix (' + str(neg.shape[0]) + ' instances)',
            startTime)
    else:
        ## Mine hard negatives
        print ' >>> MINING HARD NEGATIVES'
        model = det.createDetector(modelType, modelArgs)
        model.load(modelOut + '.' + str(iter - 1))
        detectionsList = detector.detectObjects(
            model, trainingList, featuresDir, featuresExt, 1.0, -10.0
        )  # For RCNN the overlap parameter is 0.3 not 1.0(no suppression)
        hards = cu.loadMatrixNoCompression(modelOut + '.hards').item()
        lap = cu.toc(
            'Hard negatives matrix (' + str(hards['features'].shape[0]) +
            ' instances)', startTime)

    ## Rank and clean negative detections
    detectionsData = evaluation.loadDetections(detectionsList)
    groundTruth = evaluation.loadGroundTruthAnnotations(posIdx)
    detectionsLog = evaluation.evaluateDetections(
        groundTruth, detectionsData, 0.5,
        allowDuplicates=False)  #,overlapMeasure=det.overlap
    evaluation.computePrecisionRecall(len(posIdx), detectionsLog['tp'],
                                      detectionsLog['fp'], 'tmp.txt')
    evaluation.computePrecAt(detectionsLog['tp'],
                             [20, 50, 100, 200, 300, 400, 500])
    logData = learn.parseRankedDetectionsFile(detectionsLog['log'],
                                              maxNegOverlap,
                                              maxNegativeVectors)
    print ' >>> LOADING HARD NEGATIVES'
    neg, negIdx = learn.loadHardNegativesFromList(featuresDir,
                                                  logData['negExamples'],
                                                  featuresExt, pos.shape[1],
                                                  logData['negTaken'])
    del (detectionsList, detectionsData, detectionsLog, logData)
    lap = cu.toc(
        'Ranked negatives matrix (' + str(neg.shape[0]) + ' instances)', lap)
    neg = np.concatenate((neg, hards['features']))
    negIdx = negIdx + hards['index']

    ## Learn Detector
    clf = det.createDetector(modelType, modelArgs)
    clf.learn(pos, neg, posIdx, negIdx)
    clf.save(modelOut + '.' + str(iter))
    lap = cu.toc('Classifier learned:', lap)

    ## Keep hard negatives for next iterations
    scores = clf.predict(neg, negIdx)
    hardNegsIdx = np.argsort(scores)
    hardNeg = np.concatenate(
        (hards['features'], neg[hardNegsIdx[-cu.topHards:]]))
    negIdx = hards['index'] + [negIdx[j] for j in hardNegsIdx[-cu.topHards:]]
    print 'Hard negatives:', hardNeg.shape[0]
    hards = {'features': hardNeg, 'index': negIdx}
    cu.saveMatrixNoCompression({
        'features': hardNeg,
        'index': negIdx
    }, modelOut + '.hards')

    print ' ** Iteration', iter, 'done'
    return {
        'detector': clf,
        'pos': pos,
        'posIdx': posIdx,
        'neg': neg,
        'negIdx': negIdx
    }
  result = processData(imageList,featuresDir,featuresExt,task)
  if outputFile != None:
    outf = open(outputFile,'w')
    writeF = lambda x,y,b: outf.write(x + ' {:.8f} {:.0f} {:.0f} {:.0f} {:.0f} {:.0f}\n'.format(y,b[0],b[1],b[2],b[3],b[4]))
  else:
    writeF = lambda x,y,b: x
  detectionsList = []
  for data in result:
    img,filteredBoxes,filteredScores = data
    for i in range(len(filteredBoxes)):
      b = filteredBoxes[i]
      writeF(img,filteredScores[i],b)
      detectionsList.append( [img,filteredScores[i],b[0],b[1],b[2],b[3],b[4]] )
  if outputFile != None:
    outf.close()
  return detectionsList

########################################
## MAIN PROGRAM
########################################
if __name__ == "__main__":
  ## Main Program Parameters
  params = cu.loadParams("modelType modelFile testImageList featuresDir featuresExt maxOverlap threshold outputFile")
  model = det.createDetector(params['modelType'])
  model.load(params['modelFile'])
  imageList = [x.replace('\n','') for x in open(params['testImageList'])]
  maxOverlap = float(params['maxOverlap'])
  threshold = float(params['threshold'])
  detectObjects(model,imageList,params['featuresDir'],params['featuresExt'],maxOverlap,threshold,params['outputFile'])

Beispiel #9
0
        writeF = lambda x, y, b: x
    detectionsList = []
    for data in result:
        img, filteredBoxes, filteredScores = data
        for i in range(len(filteredBoxes)):
            b = filteredBoxes[i]
            writeF(img, filteredScores[i], b)
            detectionsList.append(
                [img, filteredScores[i], b[0], b[1], b[2], b[3], b[4]])
    if outputFile != None:
        outf.close()
    return detectionsList


########################################
## MAIN PROGRAM
########################################
if __name__ == "__main__":
    ## Main Program Parameters
    params = cu.loadParams(
        "modelType modelFile testImageList featuresDir featuresExt maxOverlap threshold outputFile"
    )
    model = det.createDetector(params['modelType'])
    model.load(params['modelFile'])
    imageList = [x.replace('\n', '') for x in open(params['testImageList'])]
    maxOverlap = float(params['maxOverlap'])
    threshold = float(params['threshold'])
    detectObjects(model, imageList, params['featuresDir'],
                  params['featuresExt'], maxOverlap, threshold,
                  params['outputFile'])