在YOLOV5優(yōu)化算法當(dāng)中,根據(jù)不同的數(shù)據(jù)信息,通常會(huì)事先設(shè)定固定Anchor,接下來本文關(guān)鍵為大家介紹了有關(guān)yolov5中anchors設(shè)定的資料,原文中根據(jù)案例編碼推薦的十分詳盡,必須的小伙伴可以借鑒一下
yolov5中增強(qiáng)了響應(yīng)式導(dǎo)向框(AutoLearningBoundingBoxAnchors),但是其他yolo系類是不存在的。
一、默認(rèn)設(shè)置導(dǎo)向框
Yolov5中默認(rèn)設(shè)置保留了某些對(duì)于coco數(shù)據(jù)信息的預(yù)置導(dǎo)向框,在yolov5的環(huán)境變量*.yaml里已經(jīng)預(yù)置了640×640圖像尺寸下導(dǎo)向框的規(guī)格(以yolov5s.yaml為例子):
#anchors anchors: -[10,13,16,30,33,23]#P3/8 -[30,61,62,45,59,119]#P4/16 -[116,90,156,198,373,326]#P5/32
anchors主要參數(shù)一共有四行,每排9個(gè)標(biāo)值;且每一行意味著運(yùn)用不同類型的特征圖;
1、首行要在最大的一個(gè)特點(diǎn)圖中錨框
2、下一頁是在正中間的的特點(diǎn)圖中錨框
3、第三行要在最小特點(diǎn)圖中錨框;
在物體檢測(cè)任務(wù)時(shí),一般希望能在大一點(diǎn)的特征圖上來檢驗(yàn)個(gè)人目標(biāo),但大特征圖才帶有大量個(gè)人目標(biāo)信息內(nèi)容,因而大特點(diǎn)圖中anchor標(biāo)值一般設(shè)為小標(biāo)值,而大特點(diǎn)圖中標(biāo)值設(shè)定為大標(biāo)值檢驗(yàn)大總體目標(biāo)。
二、自定導(dǎo)向框
1、訓(xùn)練的時(shí)候快速計(jì)算導(dǎo)向框
yolov5中并不只是應(yīng)用默認(rèn)設(shè)置導(dǎo)向框,在進(jìn)行練習(xí)之前都會(huì)對(duì)模型集中標(biāo)注信息內(nèi)容進(jìn)行核實(shí),估算此數(shù)據(jù)信息標(biāo)明信息內(nèi)容對(duì)于默認(rèn)設(shè)置導(dǎo)向框的最好均方誤差,當(dāng)最好均方誤差大于等于0.98,一般不必須升級(jí)導(dǎo)向框;假如最好均方誤差低于0.98,就需要重算合乎此數(shù)據(jù)信息的導(dǎo)向框。
審查導(dǎo)向框適合不適合標(biāo)準(zhǔn)的函數(shù)公式在/utils/autoanchor.py報(bào)告中:
defcheck_anchors(dataset,model,thr=4.0,imgsz=640):
在其中thr就是指數(shù)據(jù)信息集中標(biāo)注框高寬較大閥值,默認(rèn)設(shè)置是采用超參文檔hyp.scratch.yaml里的“anchor_t”變量值。
審查關(guān)鍵編碼如下所示:
defmetric(k):#computemetric r=wh[:,None]/k[None] x=torch.min(r,1./r).min(2)[0]#ratiometric best=x.max(1)[0]#best_x aat=(x>1./thr).float().sum(1).mean()#anchorsabovethreshold bpr=(best>1./thr).float().mean()#bestpossiblerecall returnbpr,aat bpr,aat=metric(m.anchor_grid.clone().cpu().view(-1,2)) 在其中2個(gè)指標(biāo)值必須說明一下(bpr和aat): bpr(bestpossiblerecall) aat(anchorsabovethreshold)
其中bpr主要參數(shù)就是說確定是否必須重算導(dǎo)向框的根據(jù)(是不是低于0.98)。
重算合乎此數(shù)據(jù)信息標(biāo)明框的導(dǎo)向框,是運(yùn)用kmean聚類方法達(dá)到的,編碼在/utils/autoanchor.py文件中:
def kmean_anchors(path='./data/coco128.yaml',n=9,img_size=640,thr=4.0,gen=1000,verbose=True): """Creates kmeans-evolved anchors from training dataset Arguments: path:path to dataset*.yaml,or a loaded dataset n:number of anchors img_size:image size used for training thr:anchor-label wh ratio threshold hyperparameter hyp['anchor_t']used for training,default=4.0 gen:generations to evolve anchors using genetic algorithm verbose:print all results Return: k:kmeans evolved anchors Usage: from utils.autoanchor import*;_=kmean_anchors() """ thr=1./thr prefix=colorstr('autoanchor:') def metric(k,wh):#compute metrics r=wh[:,None]/k[None] x=torch.min(r,1./r).min(2)[0]#ratio metric #x=wh_iou(wh,torch.tensor(k))#iou metric return x,x.max(1)[0]#x,best_x def anchor_fitness(k):#mutation fitness _,best=metric(torch.tensor(k,dtype=torch.float32),wh) return(best*(best>thr).float()).mean()#fitness def print_results(k): k=k[np.argsort(k.prod(1))]#sort small to large x,best=metric(k,wh0) bpr,aat=(best>thr).float().mean(),(x>thr).float().mean()*n#best possible recall,anch>thr print(f'{prefix}thr={thr:.2f}:{bpr:.4f}best possible recall,{aat:.2f}anchors past thr') print(f'{prefix}n={n},img_size={img_size},metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best,' f'past_thr={x[x>thr].mean():.3f}-mean:',end='') for i,x in enumerate(k): print('%i,%i'%(round(x[0]),round(x[1])),end=','if i<len(k)-1 else'n')#use in*.cfg return k if isinstance(path,str):#*.yaml file with open(path)as f: data_dict=yaml.load(f,Loader=yaml.SafeLoader)#model dict from utils.datasets import LoadImagesAndLabels dataset=LoadImagesAndLabels(data_dict['train'],augment=True,rect=True) else: dataset=path#dataset #Get label wh shapes=img_size*dataset.shapes/dataset.shapes.max(1,keepdims=True) wh0=np.concatenate([l[:,3:5]*s for s,l in zip(shapes,dataset.labels)])#wh #Filter i=(wh0<3.0).any(1).sum() if i: print(f'{prefix}WARNING:Extremely small objects found.{i}of{len(wh0)}labels are<3 pixels in size.') wh=wh0[(wh0>=2.0).any(1)]#filter>2 pixels #wh=wh*(np.random.rand(wh.shape[0],1)*0.9+0.1)#multiply by random scale 0-1 #Kmeans calculation print(f'{prefix}Running kmeans for{n}anchors on{len(wh)}points...') s=wh.std(0)#sigmas for whitening k,dist=kmeans(wh/s,n,iter=30)#points,mean distance k*=s wh=torch.tensor(wh,dtype=torch.float32)#filtered wh0=torch.tensor(wh0,dtype=torch.float32)#unfiltered k=print_results(k) #Plot #k,d=[None]*20,[None]*20 #for i in tqdm(range(1,21)): #k[i-1],d[i-1]=kmeans(wh/s,i)#points,mean distance #fig,ax=plt.subplots(1,2,figsize=(14,7),tight_layout=True) #ax=ax.ravel() #ax[0].plot(np.arange(1,21),np.array(d)**2,marker='.') #fig,ax=plt.subplots(1,2,figsize=(14,7))#plot wh #ax[0].hist(wh[wh[:,0]<100,0],400) #ax[1].hist(wh[wh[:,1]<100,1],400) #fig.savefig('wh.png',dpi=200) #Evolve npr=np.random f,sh,mp,s=anchor_fitness(k),k.shape,0.9,0.1#fitness,generations,mutation prob,sigma pbar=tqdm(range(gen),desc=f'{prefix}Evolving anchors with Genetic Algorithm:')#progress bar for _ in pbar: v=np.ones(sh) while(v==1).all():#mutate until a change occurs(prevent duplicates) v=((npr.random(sh)<mp)*npr.random()*npr.randn(*sh)*s+1).clip(0.3,3.0) kg=(k.copy()*v).clip(min=2.0) fg=anchor_fitness(kg) if fg>f: f,k=fg,kg.copy() pbar.desc=f'{prefix}Evolving anchors with Genetic Algorithm:fitness={f:.4f}' if verbose: print_results(k) return print_results(k)
對(duì)kmean_anchors()函數(shù)中的主要參數(shù)做個(gè)簡(jiǎn)單的解釋(編碼中有了英語注解):
path:包括數(shù)據(jù)信息目標(biāo)文件夾等信息的yaml文檔(例如coco128.yaml),或是數(shù)據(jù)信息偏微分(yolov5快速計(jì)算導(dǎo)向框時(shí)就是通過的這種方法,先將數(shù)據(jù)信息標(biāo)簽信息載入再加工)
n:導(dǎo)向框的總數(shù),既有幾個(gè);初始值是9
img_size:圖像尺寸。估算數(shù)據(jù)信息樣版標(biāo)簽框的高寬時(shí),也是需要縮放進(jìn)img_size尺寸之后再計(jì)算出來的;初始值是640
thr:數(shù)據(jù)信息集中標(biāo)注框高寬較大閥值,默認(rèn)設(shè)置是采用超參文檔hyp.scratch.yaml里的“anchor_t”變量值;初始值是4.0;快速計(jì)算時(shí),就會(huì)自動(dòng)根據(jù)自己所采用的數(shù)據(jù)信息,進(jìn)行計(jì)算適宜的閥值。
gen:kmean聚類算法迭代次數(shù),初始值是1000
verbose:是不是打印全部數(shù)值,初始值是true
如果不想快速計(jì)算導(dǎo)向框,還可以在train.py中設(shè)定主要參數(shù)就可以:
parser.add_argument('--noautoanchor',action='store_true',help='disableautoanchorcheck')
2、練習(xí)前手動(dòng)式估算導(dǎo)向框
如果采用yolov5運(yùn)動(dòng)效果并不好(清除其他問題,只關(guān)心“預(yù)置導(dǎo)向框”這些因素),yolov5在審查默認(rèn)設(shè)置導(dǎo)向框是不是符合標(biāo)準(zhǔn)時(shí),計(jì)算出來的最好均方誤差超過0.98,并沒有快速計(jì)算導(dǎo)向框;這時(shí)也可以自己手動(dòng)式估算導(dǎo)向框。【就算自己的信息集中化總體目標(biāo)高寬最高值低于4,默認(rèn)設(shè)置導(dǎo)向框不一定是最理想的】
最先可以自己編寫一個(gè)程序,統(tǒng)計(jì)一下你所能鍛煉的數(shù)據(jù)信息全部標(biāo)簽框高寬,看看高寬關(guān)鍵遍布在哪些范疇、較大高寬多少錢?例如:你應(yīng)用的信息集中化總體目標(biāo)高寬較大達(dá)到5:1(乃至10:1),那還是需要重算導(dǎo)向框了,對(duì)于coco數(shù)據(jù)信息的主要高寬是4:1。
隨后在yolov5系統(tǒng)中構(gòu)建一個(gè)新的python文件test.py,手動(dòng)式估算導(dǎo)向框:
import utils.autoanchor as autoAC #對(duì)數(shù)據(jù)集重新計(jì)算anchors new_anchors=autoAC.kmean_anchors('./data/mydata.yaml',9,640,5.0,1000,True) print(new_anchors) 輸入信息如下(只截取了部分): autoanchor:Evolving anchors with Genetic Algorithm:fitness=0.6604:87%|████████▋|866/1000[00:00<00:00,2124.00it/s]autoanchor:thr=0.25:0.9839 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.662-mean/best,past_thr=0.476-mean:15,20,38,25,55,65,131,87,97,174,139,291,256,242,368,382,565,422 autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,39,26,54,64,127,87,97,176,142,286,257,245,374,379,582,424 autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,39,26,54,63,126,86,97,176,143,285,258,241,369,381,583,424 autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,39,26,54,63,127,86,97,176,143,285,258,241,369,380,583,424 autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,39,26,53,63,127,86,97,175,143,284,257,243,369,381,582,422 autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,40,26,53,62,129,85,96,175,143,287,256,240,370,378,582,419 autoanchor:Evolving anchors with Genetic Algorithm:fitness=0.6605:100%|██████████|1000/1000[00:00<00:00,2170.29it/s] Scanning'..coco128labelstrain2017.cache'for images and labels...128 found,0 missing,2 empty,0 corrupted:100%|██████████|128/128[00:00<?,?it/s] autoanchor:thr=0.25:0.9849 best possible recall,3.84 anchors past thr autoanchor:n=9,img_size=640,metric_all=0.267/0.663-mean/best,past_thr=0.476-mean:15,20,40,26,53,62,129,85,96,175,143,287,256,240,370,378,582,419 [[14.931 20.439] [39.648 25.53] [53.371 62.35] [129.07 84.774] [95.719 175.08] [142.69 286.95] [256.46 239.83] [369.9 378.3] [581.87 418.56]] Process finished with exit code 0
輸出的9組新的錨定框即是根據(jù)自己的數(shù)據(jù)集來計(jì)算的,可以按照順序替換到你所使用的配置文件*.yaml中(比如yolov5s.yaml)。就可以重新訓(xùn)練了。
參考的博文(表示感謝!):
https://github.com/ultralytics/yolov5
https://blog.csdn.net/flyfish1986/article/details/117594265
https://zhuanlan.zhihu.com/p/183838757
https://blog.csdn.net/aabbcccffffd01/article/details/109578614
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/130263.html
如今yolov5的neck用的都是PANet,在efficient文章中給出了BiFPN結(jié)構(gòu),也有更為很不錯(cuò)的特性,接下來本文關(guān)鍵為大家介紹了對(duì)于如何將yolov5里的PANet層改成BiFPN的資料,需用的小伙伴可以借鑒一下 一、Add 1.在common.py后放入如下所示編碼 #融合BiFPN設(shè)定可學(xué)習(xí)培訓(xùn)主要參數(shù)學(xué)習(xí)培訓(xùn)不一樣支系的權(quán)重值 #2個(gè)支系add實(shí)際操作 cl...
如今yolov5的neck用的都是PANet,在efficient文章中給出了BiFPN結(jié)構(gòu),也有更為很不錯(cuò)的特性,接下來本文關(guān)鍵為大家介紹了對(duì)于如何將yolov5里的PANet層改成BiFPN的資料,必須的小伙伴可以借鑒一下 一、Add 1.在common.py后放入如下所示編碼 #融合BiFPN設(shè)定可學(xué)習(xí)培訓(xùn)主要參數(shù)學(xué)習(xí)培訓(xùn)差異支系的權(quán)重值 #2個(gè)支系add實(shí)際操作 clas...
摘要:結(jié)論正確檢測(cè)小物體確實(shí)是一個(gè)挑戰(zhàn)。下載視覺實(shí)戰(zhàn)項(xiàng)目講在小白學(xué)視覺公眾號(hào)后臺(tái)回復(fù)視覺實(shí)戰(zhàn)項(xiàng)目,即可下載包括圖像分割口罩檢測(cè)車道線檢測(cè)車輛計(jì)數(shù)添加眼線車牌識(shí)別字符識(shí)別情緒檢測(cè)文本內(nèi)容提取面部識(shí)別等個(gè)視覺實(shí)戰(zhàn)項(xiàng)目,助力快速學(xué)校計(jì)算機(jī)視覺。 點(diǎn)擊上方小白學(xué)視覺,選擇加星標(biāo)或置頂 重磅干貨,第一時(shí)...
閱讀 892·2023-01-14 11:38
閱讀 837·2023-01-14 11:04
閱讀 688·2023-01-14 10:48
閱讀 1892·2023-01-14 10:34
閱讀 895·2023-01-14 10:24
閱讀 753·2023-01-14 10:18
閱讀 482·2023-01-14 10:09
閱讀 522·2023-01-14 10:02