Exemplo n.º 1
0
                    31,
                    m["fy"],
                    m["fx"],
                    usemrf=cfg.usemrf,
                    usefather=cfg.usefather)
                #models[idm]["ra"]=w[cumsize[idm+1]-1]
            util.save("%s%d.model" % (testname, it), models)
            if cfg.deform:
                print m["df"]

            if True:
                print "Show model"
                for idm, m in enumerate(models):
                    pylab.figure(100 + idm)
                    pylab.clf()
                    util.drawModel(m["ww"])
                    pylab.draw()
                    pylab.show()
                    pylab.savefig("%s_hog%d_cl%d.png" % (testname, it, idm))
                    if cfg.deform:
                        pylab.figure(110 + idm)
                        pylab.clf()
                        util.drawDeform(m["df"])
                        pylab.draw()
                        pylab.show()
                        pylab.savefig(
                            "%s_def%d_cl%d.png" % (testname, it, idm))

            sts.report(testname + ".rpt.txt", "a", "Before Filtering")
            #sort based on score
            #sort=True
Exemplo n.º 2
0
def detectflip(f,m,gtbbox=None,auxdir=".",hallucinate=1,initr=1,ratio=1,deform=False,bottomup=False,usemrf=False,numneg=0,thr=-2,posovr=0.7,minnegincl=0.5,small=True,show=False,cl=0,mythr=-10,nms=0.5,inclusion=False,usefather=True,mpos=1,useprior=False,K=1.0,occl=False,trunc=0):
    """Detect objects with RL flip
        used for both test --> gtbbox=None
        and trainig --> gtbbox = list of bounding boxes
    """
    #build flip
    pyrHOG2.setK(K)
    m1=flip(m)
    t=time.time()        
    if gtbbox!=None and gtbbox!=[] and useprior:
        pr=f.buildPrior(gtbbox,m["fy"],m["fx"])
    else:
        pr=None
    t=time.time()      
    f.resetHOG()
    if deform:
        if bottomup:
            scr,pos=f.scanRCFLDefBU(m,initr=initr,ratio=ratio,small=small,usemrf=usemrf)
            scr1,pos1=f.scanRCFLDefBU(m1,initr=initr,ratio=ratio,small=small,usemrf=usemrf)
        else:
            #scr,pos=f.scanRCFLDefThr(m,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mythr=mythr)
            scr,pos=f.scanRCFLDef(m,initr=initr,ratio=ratio,small=small,usemrf=usemrf,trunc=trunc)
            #scr1,pos1=f.scanRCFLDefThr(m1,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mythr=mythr)
            scr1,pos1=f.scanRCFLDef(m1,initr=initr,ratio=ratio,small=small,usemrf=usemrf,trunc=trunc)
    else:
        scr,pos=f.scanRCFL(m,initr=initr,ratio=ratio,small=small,trunc=trunc)
        scr1,pos1=f.scanRCFL(m1,initr=initr,ratio=ratio,small=small,trunc=trunc)
    lr=[]
    fscr=[]
    for idl,l in enumerate(scr):
        auxscr=numpy.zeros((l.shape[0],l.shape[1],2),numpy.float32)
        auxscr[:,:,0]=scr[idl]
        auxscr[:,:,1]=scr1[idl]
        fscr.append(numpy.max(auxscr,2))
        lr.append(numpy.argmax(auxscr,2))
        if 0:
            pylab.figure(101)
            pylab.clf()
            print auxscr[:,:,0]
            print auxscr[:,:,1]
            pylab.imshow(lr[-1])
            pylab.show()
            raw_input()
    if deform:
        tr=TreatDefRL(f,fscr,pos,pos1,lr,initr,m["fy"],m["fx"],occl=occl,trunc=trunc)
    else:
        tr=TreatRL(f,fscr,pos,pos1,lr,initr,m["fy"],m["fx"],occl=occl,trunc=trunc)

    numhog=f.getHOG()
    if gtbbox==None:
        if show==True:
            showlabel="Parts"
        else:
            showlabel=False
        ref=0#enable TD+BU
        if ref:
            t1=time.time()
            det=tr.doall(thr=thr,rank=100,refine=True,rawdet=False,cluster=False,show=False,inclusion=inclusion,cl=cl)
            detR=[];detL=[]
            for d in det:
                if d["rl"]==1:
                    detR.append(d)
                else:
                    detL.append(d)
            samplesR=tr.goodsamples(detR,initr=initr,ratio=ratio)
            samplesL=tr.goodsamples(detL,initr=initr,ratio=ratio)
            scrR,posR=f.scanRCFLDefBU(m,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mysamples=samplesR)
            scrL,posL=f.scanRCFLDefBU(m1,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mysamples=samplesL)
            lr=[];fscr=[]
            for idl,l in enumerate(scrR):
                auxscr=numpy.zeros((l.shape[0],l.shape[1],2),numpy.float32)
                auxscr[:,:,0]=scrR[idl]
                auxscr[:,:,1]=scrL[idl]
                fscr.append(numpy.max(auxscr,2))
                lr.append(numpy.argmax(auxscr,2))
            print "Refine Time:",time.time()-t1
            tr=TreatDefRL(f,fscr,posR,posL,lr,initr,m["fy"],m["fx"],occl=occl)
            det=tr.doall(thr=thr,rank=100,refine=True,rawdet=False,cluster=nms,show=False,inclusion=inclusion,cl=cl)
        else:
            det=tr.doall(thr=thr,rank=100,refine=True,rawdet=False,cluster=nms,show=False,inclusion=inclusion,cl=cl)
        dettime=time.time()-t
        if show:
            tr.show(det,parts=showlabel,thr=-1.0,maxnum=0)  
        print "Detect: %.3f"%(time.time()-t)
        return tr,det,dettime,numhog
    else:
        best1,worste1=tr.doalltrain(gtbbox,thr=thr,rank=1000,show=show,mpos=mpos,numpos=1,posovr=posovr,numneg=numneg,minnegovr=0,minnegincl=minnegincl,cl=cl)        
        ipos=[];ineg=[]
        if False and show and len(best1)>0:
            import util
            ds=tr.descr(best1,flip=False,usemrf=usemrf,usefather=usefather,k=K)
            pylab.figure(120)
            pylab.clf()
            mm=tr.model(ds[0],0,len(m1["ww"]),31)
            util.drawModel(mm["ww"])
            pylab.show()
            #raw_input()

        print "Detect: %.3f"%(time.time()-t)
        return tr,best1,worste1,ipos,ineg
Exemplo n.º 3
0
            dd.append(lowd)
            rho = 0
        models.append({
            "ww": ww,
            "rho": rho,
            "df": dd,
            "fy": ww[0].shape[0],
            "fx": ww[0].shape[1]
        })

    if False:
        print "Show model"
        for idm, m in enumerate(models):
            pylab.figure(100 + idm)
            pylab.clf()
            util.drawModel(m["ww"])
            pylab.draw()
            pylab.show()
        raw_input()

    #fulltrpos=[]
    #fulltrposcl=[]
    trneg = []
    trpos = []
    trposcl = []
    dtrpos = {}
    trnegcl = []
    newtrneg = []
    newtrnegcl = []
    negratio = [-1]
    posratio = [-1]
Exemplo n.º 4
0
def detectflip(f,
               m,
               gtbbox=None,
               auxdir=".",
               hallucinate=1,
               initr=1,
               ratio=1,
               deform=False,
               bottomup=False,
               usemrf=False,
               numneg=0,
               thr=-2,
               posovr=0.7,
               minnegincl=0.5,
               small=True,
               show=False,
               cl=0,
               mythr=-10,
               nms=0.5,
               inclusion=False,
               usefather=True,
               mpos=1,
               useprior=False,
               K=1.0,
               occl=False,
               trunc=0):
    """Detect objects with RL flip
        used for both test --> gtbbox=None
        and trainig --> gtbbox = list of bounding boxes
    """
    #build flip
    pyrHOG2.setK(K)
    m1 = flip(m)
    t = time.time()
    if gtbbox != None and gtbbox != [] and useprior:
        pr = f.buildPrior(gtbbox, m["fy"], m["fx"])
    else:
        pr = None
    t = time.time()
    f.resetHOG()
    if deform:
        if bottomup:
            scr, pos = f.scanRCFLDefBU(m,
                                       initr=initr,
                                       ratio=ratio,
                                       small=small,
                                       usemrf=usemrf)
            scr1, pos1 = f.scanRCFLDefBU(m1,
                                         initr=initr,
                                         ratio=ratio,
                                         small=small,
                                         usemrf=usemrf)
        else:
            #scr,pos=f.scanRCFLDefThr(m,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mythr=mythr)
            scr, pos = f.scanRCFLDef(m,
                                     initr=initr,
                                     ratio=ratio,
                                     small=small,
                                     usemrf=usemrf,
                                     trunc=trunc)
            #scr1,pos1=f.scanRCFLDefThr(m1,initr=initr,ratio=ratio,small=small,usemrf=usemrf,mythr=mythr)
            scr1, pos1 = f.scanRCFLDef(m1,
                                       initr=initr,
                                       ratio=ratio,
                                       small=small,
                                       usemrf=usemrf,
                                       trunc=trunc)
    else:
        scr, pos = f.scanRCFL(m,
                              initr=initr,
                              ratio=ratio,
                              small=small,
                              trunc=trunc)
        scr1, pos1 = f.scanRCFL(m1,
                                initr=initr,
                                ratio=ratio,
                                small=small,
                                trunc=trunc)
    lr = []
    fscr = []
    for idl, l in enumerate(scr):
        auxscr = numpy.zeros((l.shape[0], l.shape[1], 2), numpy.float32)
        auxscr[:, :, 0] = scr[idl]
        auxscr[:, :, 1] = scr1[idl]
        fscr.append(numpy.max(auxscr, 2))
        lr.append(numpy.argmax(auxscr, 2))
        if 0:
            pylab.figure(101)
            pylab.clf()
            print auxscr[:, :, 0]
            print auxscr[:, :, 1]
            pylab.imshow(lr[-1])
            pylab.show()
            raw_input()
    if deform:
        tr = TreatDefRL(f,
                        fscr,
                        pos,
                        pos1,
                        lr,
                        initr,
                        m["fy"],
                        m["fx"],
                        occl=occl,
                        trunc=trunc)
    else:
        tr = TreatRL(f,
                     fscr,
                     pos,
                     pos1,
                     lr,
                     initr,
                     m["fy"],
                     m["fx"],
                     occl=occl,
                     trunc=trunc)

    numhog = f.getHOG()
    if gtbbox == None:
        if show == True:
            showlabel = "Parts"
        else:
            showlabel = False
        ref = 0  #enable TD+BU
        if ref:
            t1 = time.time()
            det = tr.doall(thr=thr,
                           rank=100,
                           refine=True,
                           rawdet=False,
                           cluster=False,
                           show=False,
                           inclusion=inclusion,
                           cl=cl)
            detR = []
            detL = []
            for d in det:
                if d["rl"] == 1:
                    detR.append(d)
                else:
                    detL.append(d)
            samplesR = tr.goodsamples(detR, initr=initr, ratio=ratio)
            samplesL = tr.goodsamples(detL, initr=initr, ratio=ratio)
            scrR, posR = f.scanRCFLDefBU(m,
                                         initr=initr,
                                         ratio=ratio,
                                         small=small,
                                         usemrf=usemrf,
                                         mysamples=samplesR)
            scrL, posL = f.scanRCFLDefBU(m1,
                                         initr=initr,
                                         ratio=ratio,
                                         small=small,
                                         usemrf=usemrf,
                                         mysamples=samplesL)
            lr = []
            fscr = []
            for idl, l in enumerate(scrR):
                auxscr = numpy.zeros((l.shape[0], l.shape[1], 2),
                                     numpy.float32)
                auxscr[:, :, 0] = scrR[idl]
                auxscr[:, :, 1] = scrL[idl]
                fscr.append(numpy.max(auxscr, 2))
                lr.append(numpy.argmax(auxscr, 2))
            print "Refine Time:", time.time() - t1
            tr = TreatDefRL(f,
                            fscr,
                            posR,
                            posL,
                            lr,
                            initr,
                            m["fy"],
                            m["fx"],
                            occl=occl)
            det = tr.doall(thr=thr,
                           rank=100,
                           refine=True,
                           rawdet=False,
                           cluster=nms,
                           show=False,
                           inclusion=inclusion,
                           cl=cl)
        else:
            det = tr.doall(thr=thr,
                           rank=100,
                           refine=True,
                           rawdet=False,
                           cluster=nms,
                           show=False,
                           inclusion=inclusion,
                           cl=cl)
        dettime = time.time() - t
        if show:
            tr.show(det, parts=showlabel, thr=-1.0, maxnum=0)
        print "Detect: %.3f" % (time.time() - t)
        return tr, det, dettime, numhog
    else:
        best1, worste1 = tr.doalltrain(gtbbox,
                                       thr=thr,
                                       rank=1000,
                                       show=show,
                                       mpos=mpos,
                                       numpos=1,
                                       posovr=posovr,
                                       numneg=numneg,
                                       minnegovr=0,
                                       minnegincl=minnegincl,
                                       cl=cl)
        ipos = []
        ineg = []
        if False and show and len(best1) > 0:
            import util
            ds = tr.descr(best1,
                          flip=False,
                          usemrf=usemrf,
                          usefather=usefather,
                          k=K)
            pylab.figure(120)
            pylab.clf()
            mm = tr.model(ds[0], 0, len(m1["ww"]), 31)
            util.drawModel(mm["ww"])
            pylab.show()
            #raw_input()

        print "Detect: %.3f" % (time.time() - t)
        return tr, best1, worste1, ipos, ineg