forked from marcopede/CFdet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testPASCAL.py
160 lines (147 loc) · 5.03 KB
/
testPASCAL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import matplotlib
matplotlib.use("Agg") #if run out of ipython do not show any graph
#from procedures import *
from database import *
from multiprocessing import Pool
import util
import pyrHOG2
import VOCpr
import time
import copy
import itertools
from trainPASCAL import *
if __name__=="__main__":
cfg=config()
cfg.cls="bicycle"
it=7
import sys
if len(sys.argv)>1:
cfg.cls=sys.argv[1]
cfg.numcl=2#2
if len(sys.argv)>2:
it=int(sys.argv[2])
#testname="./data/11_02_26/%s_%d_comp_bias2"%(cfg.cls,cfg.numcl)
testname="./data/finalRL/%s%d_test"%(cfg.cls,cfg.numcl)
#testname="./data/11_04_02/%s%d_RLrigth"%(cfg.cls,cfg.numcl)
cfg=util.load(testname+".cfg")
cfg.mythr=-10
#cfg.mpos=1
estimate=False
if len(sys.argv)>3:
cfg.mythr=float(sys.argv[3])
if len(sys.argv)>4:
select=sys.argv[4]
else:
select="all"
#cfg.bottomup=False
#cfg.small=1
cfg.small=False
#cfg.year="2007"
#cfg.maxtest=16#5000
#cfg.initr=0
cfg.show=True
select="pos"
if cfg.show:
cfg.multipr=False
else:
cfg.multipr=8
cfg.savefeat=False
cfg.loadfeat=False
#cfg.inclusion=True
cfg.thr=-2
cfg.auxdir="/home/databases/VOC2007/VOCdevkit/local/VOC2007/"#"/state/partition1/marcopede/"
#cfg.test=True
models=util.load("%s%d.model"%(testname,it))
import util
w=[]
rho=[]
for l in range(cfg.numcl):
w.append(util.ModeltoW(models[l],cfg.usemrf,cfg.usefather,cfg.k,lastlev=1))
rho.append(models[l]["rho"])
#cfg.mythr=cfg.mythr*numpy.mean([numpy.sum(x**2) for x in w])#-numpy.mean(rho)
#raw_input()
#cfg.mythr=cfg.mythr#-numpy.mean(rho)
if cfg.multipr==1:
numcore=None
else:
numcore=cfg.multipr
mypool = Pool(numcore)
if cfg.cls=="inria":
if select=="pos":
tsImages=getRecord(InriaTestData(basepath=cfg.dbpath),cfg.maxtest)
else:
tsImages=getRecord(InriaTestFullData(basepath=cfg.dbpath),cfg.maxtest)
else:
tsImages=getRecord(VOC07Data(select=select,cl="%s_test.txt"%cfg.cls,
basepath=cfg.dbpath,#"/home/databases/",
usetr=True,usedf=False),cfg.maxtest)
mypool = Pool(numcore)
print "Test"
numhog=0
initime=time.time()
detlist=[]
mycfg=copy.copy(cfg)
mycfg.numneg=0
arg=[[i,tsImages[i]["name"],None,models,mycfg] for i in range(len(tsImages))]
t=time.time()
if not(cfg.multipr):
itr=itertools.imap(detectWrap,arg)
else:
itr=mypool.imap(detectWrap,arg)
for ii,res in enumerate(itr):
totneg=0
fuse=[]
for mix in res:
tr=mix[0]
fuse+=mix[1]
numhog+=mix[3]
#for h in fuse:
# h["scr"]+=models[h["cl"]]["ra"]
rfuse=tr.rank(fuse,maxnum=300)
nfuse=tr.cluster(rfuse,ovr=0.3,inclusion=False)
print "----Test Image %d----"%ii
for l in nfuse:
detlist.append([tsImages[ii]["name"].split("/")[-1].split(".")[0],l["scr"],l["bbox"][1],l["bbox"][0],l["bbox"][3],l["bbox"][2]])
print "Detections:",len(nfuse)
if cfg.show:
if cfg.show==True:
showlabel="Parts"
else:
showlabel=False
pylab.figure(20)
pylab.ioff()
pylab.clf()
pylab.axis("off")
img=util.myimread(tsImages[ii]["name"])
pylab.imshow(img,interpolation="nearest",animated=True)
pylab.gca().set_ylim(0,img.shape[0])
pylab.gca().set_xlim(0,img.shape[1])
pylab.gca().set_ylim(pylab.gca().get_ylim()[::-1])
tr.show(nfuse,parts=showlabel,thr=-0.8,maxnum=10)
pylab.show()
#raw_input()
del itr
#tp,fp,scr,tot=VOCpr.VOCprlistfastscore(tsImages,detlist,numim=cfg.maxpostest,show=False,ovr=0.5)
#tp,fp,scr,tot=VOCpr.VOCprRecord_wrong(tsImages,detlist,show=False,ovr=0.5)
tp,fp,scr,tot=VOCpr.VOCprRecord(tsImages,detlist,show=False,ovr=0.5)
pylab.figure(15)
pylab.clf()
rc,pr,ap=VOCpr.drawPrfast(tp,fp,tot)
pylab.draw()
pylab.show()
pylab.savefig("%s_ap%d_test%s%.1f.png"%(testname,it,select,cfg.mythr))
tottime=((time.time()-initime))
print "Threshold used:",cfg.mythr
print "Total number of HOG:",numhog
print "AP(it=",it,")=",ap
print "Testing Time: %.3f s"%tottime#/3600.0)
results={"det":detlist,"ap":ap,"tp":tp,"fp":fp,"pr":pr,"rc":rc,"numhog":numhog,"mythr":cfg.mythr,"time":tottime}
util.savemat("%s_ap%d_test_thr_%.3f.mat"%(testname,it,cfg.mythr),results)
#util.save("%s_ap%d_test_thr_%.3f.dat"%(testname,it,cfg.mythr),results)
#util.savedetVOC(detlist,"%s_ap%d_test_thr_%.3f.txt"%(testname,it,cfg.mythr))
fd=open("%s_ap%d_test%s.txt"%(testname,it,select),"a")
fd.write("Threshold used:%f\n"%cfg.mythr)
fd.write("Total number of HOG:%d\n"%numhog)
fd.write("Average precision:%f\n"%ap)
fd.write("Testing Time: %.3f s\n\n"%tottime)
fd.close()