python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法_第1頁
python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法_第2頁
python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法_第3頁
python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法_第4頁
python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第python目標(biāo)檢測YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強方法目錄什么是Mosaic數(shù)據(jù)增強方法實現(xiàn)思路全部代碼

什么是Mosaic數(shù)據(jù)增強方法

Yolov4的mosaic數(shù)據(jù)增強參考了CutMix數(shù)據(jù)增強方式,理論上具有一定的相似性!

CutMix數(shù)據(jù)增強方式利用兩張圖片進行拼接。

但是mosaic利用了四張圖片,根據(jù)論文所說其擁有一個巨大的優(yōu)點是豐富檢測物體的背景!且在BN計算的時候一下子會計算四張圖片的數(shù)據(jù)!就像下圖這樣:

實現(xiàn)思路

1、每次讀取四張圖片。

2、分別對四張圖片進行翻轉(zhuǎn)、縮放、色域變化等,并且按照四個方向位置擺好。

3、進行圖片的組合和框的組合

全部代碼

全部代碼構(gòu)成如下:

fromPILimportImage,ImageDraw

importnumpyasnp

frommatplotlib.colorsimportrgb_to_hsv,hsv_to_rgb

importmath

defrand(a=0,b=1):

returnnp.random.rand()*(b-a)+a

defmerge_bboxes(bboxes,cutx,cuty):

merge_bbox=[]

foriinrange(len(bboxes)):

forboxinbboxes[i]:

tmp_box=[]

x1,y1,x2,y2=box[0],box[1],box[2],box[3]

ifi==0:

ify1cutyorx1cutx:

continue

ify2=cutyandy1=cuty:

y2=cuty

ify2-y15:

continue

ifx2=cutxandx1=cutx:

x2=cutx

ifx2-x15:

continue

ifi==1:

ify2cutyorx1cutx:

continue

ify2=cutyandy1=cuty:

y1=cuty

ify2-y15:

continue

ifx2=cutxandx1=cutx:

x2=cutx

ifx2-x15:

continue

ifi==2:

ify2cutyorx2cutx:

continue

ify2=cutyandy1=cuty:

y1=cuty

ify2-y15:

continue

ifx2=cutxandx1=cutx:

x1=cutx

ifx2-x15:

continue

ifi==3:

ify1cutyorx2cutx:

continue

ify2=cutyandy1=cuty:

y2=cuty

ify2-y15:

continue

ifx2=cutxandx1=cutx:

x1=cutx

ifx2-x15:

continue

tmp_box.append(x1)

tmp_box.append(y1)

tmp_box.append(x2)

tmp_box.append(y2)

tmp_box.append(box[-1])

merge_bbox.append(tmp_box)

returnmerge_bbox

defget_random_data(annotation_line,input_shape,random=True,hue=.1,sat=1.5,val=1.5,proc_img=True):

'''randompreprocessingforreal-timedataaugmentation'''

h,w=input_shape

min_offset_x=0.4

min_offset_y=0.4

scale_low=1-min(min_offset_x,min_offset_y)

scale_high=scale_low+0.2

image_datas=[]

box_datas=[]

index=0

place_x=[0,0,int(w*min_offset_x),int(w*min_offset_x)]

place_y=[0,int(h*min_offset_y),int(w*min_offset_y),0]

forlineinannotation_line:

#每一行進行分割

line_content=line.split()

#打開圖片

image=Image.open(line_content[0])

image=image.convert("RGB")

#圖片的大小

iw,ih=image.size

#保存框的位置

box=np.array([np.array(list(map(int,box.split(','))))forboxinline_content[1:]])

#image.save(str(index)+".jpg")

#是否翻轉(zhuǎn)圖片

flip=rand().5

ifflipandlen(box)0:

image=image.transpose(Image.FLIP_LEFT_RIGHT)

box[:,[0,2]]=iw-box[:,[2,0]]

#對輸入進來的圖片進行縮放

new_ar=w/h

scale=rand(scale_low,scale_high)

ifnew_ar1:

nh=int(scale*h)

nw=int(nh*new_ar)

else:

nw=int(scale*w)

nh=int(nw/new_ar)

image=image.resize((nw,nh),Image.BICUBIC)

#進行色域變換

hue=rand(-hue,hue)

sat=rand(1,sat)ifrand().5else1/rand(1,sat)

val=rand(1,val)ifrand().5else1/rand(1,val)

x=rgb_to_hsv(np.array(image)/255.)

x[...,0]+=hue

x[...,0][x[...,0]1]-=1

x[...,0][x[...,0]0]+=1

x[...,1]*=sat

x[...,2]*=val

x[x1]=1

x[x0]=0

image=hsv_to_rgb(x)

image=Image.fromarray((image*255).astype(np.uint8))

#將圖片進行放置,分別對應(yīng)四張分割圖片的位置

dx=place_x[index]

dy=place_y[index]

new_image=Image.new('RGB',(w,h),(128,128,128))

new_image.paste(image,(dx,dy))

image_data=np.array(new_image)/255

#Image.fromarray((image_data*255).astype(np.uint8)).save(str(index)+"distort.jpg")

index=index+1

box_data=[]

#對box進行重新處理

iflen(box)0:

np.random.shuffle(box)

box[:,[0,2]]=box[:,[0,2]]*nw/iw+dx

box[:,[1,3]]=box[:,[1,3]]*nh/ih+dy

box[:,0:2][box[:,0:2]0]=0

box[:,2][box[:,2]w]=w

box[:,3][box[:,3]h]=h

box_w=box[:,2]-box[:,0]

box_h=box[:,3]-box[:,1]

box=box[np.logical_and(box_w1,box_h1)]

box_data=np.zeros((len(box),5))

box_data[:len(box)]=box

image_datas.append(image_data)

box_datas.append(box_data)

img=Image.fromarray((image_data*255).astype(np.uint8))

forjinrange(len(box_data)):

thickness=3

left,top,right,bottom=box_data[j][0:4]

draw=ImageDraw.Draw(img)

foriinrange(thickness):

draw.rectangle([left+i,top+i,right-i,bottom-i],outline=(255,255,255))

img.show()

#將圖片分割,放在一起

cutx=np.random.randint(int(w*min_offset_x),int(w*(1-min_offset_x)))

cuty=np.random.randint(int(h*min_offset_y),int(h*(1-min_offset_y)))

new_image=np.zeros([h,w,3])

new_image[:cuty,:cutx,:]=image_datas[0][:cuty,:cutx,:]

new_image[cuty:,:cutx,:]=image_datas[1][cuty:,:cutx,:]

new_image[cuty:,cutx:,:]=image_datas[2][cuty:,cutx:,:]

new_image[:cuty,cutx:,:]=image_datas[3][:cuty,cutx:,:]

#對框進行進一步的處理

new_boxes=merge_bboxes(box_datas,cutx,cuty)

returnnew_image,new_boxes

defnormal_(annotation_line,input_shape):

'''randompreprocessingforreal-timedataaugmentation'''

line=annotation_line.split()

image=Image.open(line[0])

box=np.array([np.array(list(map(int,box.split(','))))forboxinline[1:]])

iw,ih=image.size

image=image.transpose(Image.FLIP_LEFT_RIGHT)

box[:,[0,2]]=iw-box[:,[2,0]]

returnimage,box

if__name__=="__main__":

withopen("2007_train.txt")asf:

lines=f.readlines()

a=np.random.randint(0,len(lines))

#index=0

#line_all=lines[a:a+4]

#forlineinline_all:

#image_data,box_data=normal_(line,[416,416])

#img=image_data

#forjinrange(len(box_data)):

#thickness=3

#left,top,right,bottom=box_data[j][0:4]

#draw=ImageDraw.Draw(img)

#foriinrange(thickness):

#draw.rectangle

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論