FQAopencvhaartraining.docx
《FQAopencvhaartraining.docx》由会员分享,可在线阅读,更多相关《FQAopencvhaartraining.docx(6页珍藏版)》请在冰豆网上搜索。
![FQAopencvhaartraining.docx](https://file1.bdocx.com/fileroot1/2022-11/30/7b7b644f-328a-4378-be43-e022573216f0/7b7b644f-328a-4378-be43-e022573216f01.gif)
FQAopencvhaartraining
FAQ:
OpenCVHaartraining
Postedon:
10-11-2009|By:
rhondasw|In:
OpenCV
HiAll,beforepostingyourquestion,pleaselookatthisFAQcarefully!
AlsoyoucanreadOpenCVhaartrainingarticle. Ifyouaresure,thereisnoanswertoyourquestion,feelfreetopostcomment. Alsoplease,putcommentsaboutimprovementofthispost. Thispostwillbeupdated,ifneeded.
Positiveimages
Whypositiveimagesarenamedso?
Becauseapositiveimagecontainsthetargetobjectwhichyouwantmachinetodetect.Unlikethem,anegativeimagedoesn’tcontainsuchtargetobjects.
What’svecfileinOpenCVhaartraining?
Duringhaartrainingpositivesamplesshouldhavethesamewidthandheightasyoudefineincommand“-w-hsize”. Sooriginalpositiveimagesareresized andpackedasthumbstovecfile.Vecfilehasheader:
numberofpositivesamples,width,heightandcontainpositivethumbsinbody.
Isitpossibletomergevecfiles?
Yes,useGoogle,therearefreetools,writtenbyOpenCV’scommunity.
Ihave positiveimages,howcreatevecfileofpositivesamples?
ThereistoolinC:
\ProgramFiles\OpenCV\apps\HaarTraining\srccreatesamples.cpp. Usage:
createsamples-infopositive_description.txt-vecsamples.vec-w20-h20
What’spositivedescriptionfile?
Thematteristhat,oneachpositiveimage,therecanbeseveralobjects.Theyhaveboundingrectangles:
x,y,width,height. Soyoucanwritesuchdescriptioninfoofimage:
positive_image_name num_of_objectsxywidthheightxywidthheight…
Textfile,whichcontainssuchinfoaboutpositiveimagesiscalleddescriptionfile.Soduringvecfilegeneration, reallyobjectsarepacked,butnotwholeimage.Essentiallyvecfileisneededtospeedupmachinelearning.
DoIalwaysneeddescriptionfile,evenifIhaveonlyoneobjectonaimage?
Yes,withcreatesamplesyouneeddescriptionfile. Ifyouhaveonlyoneobject,it’sboundingrectanglemaybeboundingrectangleofwholeimage.Ifyouwant,writeyourowntoolforvecfilegeneration=)
Shouldlightningconditionsandbackgroundbevariousonpositiveimages?
Yes,it’sveryimportant.Oneachpositiveimage,besideobject,thereisbackground.Trytofillthisbackgroundwithrandomnoise,avoidconstantbackground.
Howmuchbackgroundshouldbeonpositiveimage?
Ifyouhavemuchbackgroundpixelsonyourpositiveimagesincomparisonwithobject’spixels–it’sbadsincethehaartrainingcouldrememberthebackgroundasfeatureofpositiveimage.
Ifyoudon’thavebackgroundpixelsatall–it’salsobad.Thereshouldbesmallbackgroundframeonpositiveimage
Shouldalloriginalpositiveimageshavethesamesize?
No,originalimagescanhaveanysize. Butit’simportantthatwidth,heightofthisrectanglehavethesameaspectratioas-w-h.
What’s -wand-hshouldIputincreatesamples?
Shoulditbealwayssquare?
Youcanputanyvalueto-wand-hdependonaspectratioofthetargetobjectwhichyouwanttodetect. Butobjectsofsmallersizewillnotbedetected!
Forfaces,commonlyusedvaluesare24×24,20×20.Butyoumayuse24×20,20×24,etc.
Errorsduringvecfilegeneration:
Incorrectsizeofinputarray,0kbvecfile,
-Firstcheckyoudescriptionfile:
positive_image_nameshouldbeabsolutepathnamewithoutspaceslike“C:
\content\image.jpg”not“C:
\content\image.jpg”orrelativepathname.
-Avoidemptylinesindescriptionfile
-Resolutionoforiginalpositiveimagefileshouldbenotless,then-w-hparametersyouput.
-Checkthatpositiveimagesareavailableinyourfilesystemsandnotcorrupted.
-Therecanbeunsupportedformats.Jpeg,Bmp,PPMaresupported!
Exampleofvecfilegeneration!
Let’sworkingdirectorybeC:
\haartraining.Initthereiscreatesamples.exe.Thereisfolder
C:
\haartraining\positives.Socreatedescriptionfilepositive_desc.txt.
positives\image1.jpg110102020
positives\image2.jpg23030505060607070
or
C:
\haartraining\positives\image1.jpg110102020
C:
\haartraining\positives\image2.jpg23030505060607070
Youshouldavoidemptylinesandemptyspaceinimage’spath
createsamples-infopositive_desc.txt-vecsamples.vec-w20-h20
Negativeimages
WhatnegativeimagesshouldItake?
YoucanuseanyimageofOpenCVsupportedformats,whichdoesnotcontaintargetobjects(whicharepresentonpositiveimages).Buttheyshouldbevarious–it’simportant!
Goodenoughdatabaseishere
Shouldnegativeimageshavethesamesize?
No.Butthesizeshouldnotbelessthan-w-h,whichwereputduringvecfilegeneration.
What’sdescriptionfilefornegativeimage?
It’sjusttextfile,oftencallednegative.dat,whichcontainsfullpathtonegativeimageslike:
image_name1.jpg
image_name2.jpg
Avoidemptylinesinit.
Howmanynegative/positiveimageshouldItake?
Itdependsonyourtask. Forrealcascadesthereshouldbeabout1000positiveimagesand2000negativeimagese.g.
Goodenoughproportionis positive:
negative=1:
2,butit’snothardrule!
Iwouldrecommendfirsttousesmallnumberofsamples,generatecascade,testit,thenenlargenumberofsamples.
Launchhaartraining.exe(OpenCV\apps\HaarTraining\src)
Exampleoflaunching
WorkingdirectoryisC:
\haartrainingwithhaartraining.exetoolandsamples.vecfile.
Let’snegativeimagesareinC:
\haartraining\negative,inthiscasenegative.datshouldbelikethis:
negative\neg1.jpg
negative\neg2.jpg
…
SoinC:
\haartraininglaunchthis:
haartraining-datahaarcascade-vecsamples.vec-bgnegatives.dat-nstages20 -minhitrate0.999-maxfalsealarm0.5-npos1000-nneg2000-w20-h20-nonsym-mem1024
∙w h isthesame,youputduringvecfilegeneration
∙nposnneg –numberofpositivesamplesandnegativesamples
∙mem–RAMmemory,thatprogrammayuse
∙maxfalsealarm–maximumfalsealarm,thatstagemayhave.Ifbigfalsealarm–itcouldbebaddetectionsystem
∙minhitrate–minimalhitrate,thatshouldstagehaveatleast
∙nstage–numberofstagesincascade
What’sfalsealarmandhitrateofstage?
Youshouldreadtheoryofadaboostaboutstrongclassifier.Stageisstrongclassifier.Inshort:
∙Forexampleyouhave1000positivesamples.Youwantyoursystemtodetect900ofthem.Sodesiredhitrate=900/1000=0.9.Commonly,putminhitrate=0.999
∙Forexampleyouhave1000negativesamples.Becauseit’snegative,youdon’twantyoursystemtodetectthem.Butyoursystem,becauseithaserror,willdetectsomeofthem.Leterrorbeabout490samples,sofalsealarm=490/1000=0.49.Commonly,putfalsealarm =0.5
Arefalsealarmandhitratedependoneachother?
Yes,thereisdependency.Youcouldnotputminhitrate=1.0andmaxfalsealarm=0.0..
Firstly,thesystembuildsclassifierwithdesiredhitrate,thenitwillcalculateit’sfalsealarm,ifthefalsealarmishigherthanmaxfalsealarm,thesystemwillrejectsuchclassifierandwillbuildthenextone.Duringhaartrainingyoumayseesuch:
N|%SMP|F|ST.THR|HR|FA|EXP.ERR|
+—-+—-+-+———+———+———+———+
|0|25%|-|-1423.312590|1.000000|1.000000|0.876272|
HR–hitrate
FA–falsealarm
What’sfalsealarmandhitrateofwholecascade?
Cascadeislinkedlist(orthree)ofstages.That’swhy:
∙Falsealarmofcascade=falsealarmof stage1*falsealarmof stage2*…
∙Hitrate=hitrateof stage1*hitrateofstage2*…
Howmanystagesshouldbeused?
∙Ifyousetbignumberofstages,thenyouwillachievebetterfalsealarm,butitwilltakemoretimeforgeneratingcascade.
∙Ifyousetbignumberofstages,thenthedetectiontimecouldbeslower
∙Ifyousetbignumberofstages,thentheworsehitratewillbe(0.99*0.99*…etc).Commonly14-25stagesareenough
∙It’suselesstosetmanystage,ifyouhavesmallnumberofpositive,negativesamples
What’sweighttrimming,eqw,bt,nonsymoptions?
ReallyalltheseparametersarerelatedtoAdaboost,readtheory.Inshort:
∙nonsym–IfyoupositivesamplesarenotXorYsymmetric,put-nonsym,-symisdefault!
∙eqw–ifyouhavedifferentnumberofposandnegimages,it’sbettertoputnoeqw
∙weighttrimming–forcalculationoptimization.Itcanreducecalculationtimealittle,butqualitymaybeworse
∙bt–whatAdaboostalgorithmtouse:
RealAB,GentleAB,etc.
What’s minpos,nsplits,maxtreesplitsoptions?
Theseparametersarerelatedtoclustering.InAdaboostdifferentweekclassifiermaybeused:
stump-basedortree-based. Ifyouchoosensplits>0,tree-basedwillbeusedandyoushouldsetupminposandmaxtreesplits.
∙nsplits–minimunnumberofnodesintree
∙maxtreesplits–maximumnumberofnodesintree.Ifmaxtreesplits∙minpos–numberofpositiveimages,thatcanbeusedbyonenodeduringtraining. Allpositiveimagesaresplittedbetweennodes.Generallyminpos shouldbenotlessthan npos/nsplits.
Errorsandstrangesduringhaartraining!
∙Error(validonlyforDiscreteandRealAdaBoost):
misclass–it’swarning,butnoerror. SomeoptionsarespecifictoDandRAdaboost. Soyourhaartrainingisok.
∙Screenisfilledwithsuch|1000|25%|-|-1423.312590|1.000000|1.000000|0.876272|–yourtrainingiscycled,restartit.Firstcolumnshouldhavevalue<100
∙cvAllocfails.Ourofmemory–yougivetoomuchnegativeimagesorsample.vecistoobig.AllthesepicturesareloadedtoRAM.
∙Payattentionyouputthesame-wand-h,asduringvecfilegeneration
∙Payattention,thatnumberofpositivesamplesandnegativesamples,youputin-npos-nnegarereallyavailable
∙Avoidemptylineinnegative.datfile
∙Requiredleaffalsealarmrateachieved.Branchtrainingterminated–it’simpossibletobuildclassifierwithgoodfalsealarmonthisnegativeimages.Checkyournegativeimagesarereall