最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python目標(biāo)檢測(cè)YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強(qiáng)方法

 更新時(shí)間:2022年05月09日 11:16:54   作者:Bubbliiiing  
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強(qiáng)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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

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

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

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

實(shí)現(xiàn)思路

1、每次讀取四張圖片。

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

3、進(jìn)行圖片的組合和框的組合

全部代碼

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

from PIL import Image, ImageDraw
import numpy as np
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
import math
def rand(a=0, b=1):
    return np.random.rand()*(b-a) + a
def merge_bboxes(bboxes, cutx, cuty):
    merge_bbox = []
    for i in range(len(bboxes)):
        for box in bboxes[i]:
            tmp_box = []
            x1,y1,x2,y2 = box[0], box[1], box[2], box[3]
            if i == 0:
                if y1 > cuty or x1 > cutx:
                    continue
                if y2 >= cuty and y1 <= cuty:
                    y2 = cuty
                    if y2-y1 < 5:
                        continue
                if x2 >= cutx and x1 <= cutx:
                    x2 = cutx
                    if x2-x1 < 5:
                        continue
            if i == 1:
                if y2 < cuty or x1 > cutx:
                    continue
                if y2 >= cuty and y1 <= cuty:
                    y1 = cuty
                    if y2-y1 < 5:
                        continue
                if x2 >= cutx and x1 <= cutx:
                    x2 = cutx
                    if x2-x1 < 5:
                        continue
            if i == 2:
                if y2 < cuty or x2 < cutx:
                    continue
                if y2 >= cuty and y1 <= cuty:
                    y1 = cuty
                    if y2-y1 < 5:
                        continue
                if x2 >= cutx and x1 <= cutx:
                    x1 = cutx
                    if x2-x1 < 5:
                        continue
            if i == 3:
                if y1 > cuty or x2 < cutx:
                    continue
                if y2 >= cuty and y1 <= cuty:
                    y2 = cuty
                    if y2-y1 < 5:
                        continue
                if x2 >= cutx and x1 <= cutx:
                    x1 = cutx
                    if x2-x1 < 5:
                        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)
    return merge_bbox
def get_random_data(annotation_line, input_shape, random=True, hue=.1, sat=1.5, val=1.5, proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    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]
    for line in annotation_line:
        # 每一行進(jìn)行分割
        line_content = line.split()
        # 打開(kāi)圖片
        image = Image.open(line_content[0])
        image = image.convert("RGB") 
        # 圖片的大小
        iw, ih = image.size
        # 保存框的位置
        box = np.array([np.array(list(map(int,box.split(',')))) for box in line_content[1:]])
        # image.save(str(index)+".jpg")
        # 是否翻轉(zhuǎn)圖片
        flip = rand()<.5
        if flip and len(box)>0:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)
            box[:, [0,2]] = iw - box[:, [2,0]]
        # 對(duì)輸入進(jìn)來(lái)的圖片進(jìn)行縮放
        new_ar = w/h
        scale = rand(scale_low, scale_high)
        if new_ar < 1:
            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)
        # 進(jìn)行色域變換
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)
        val = rand(1, val) if rand()<.5 else 1/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[x>1] = 1
        x[x<0] = 0
        image = hsv_to_rgb(x)
        image = Image.fromarray((image*255).astype(np.uint8))
        # 將圖片進(jìn)行放置,分別對(duì)應(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 = []
        # 對(duì)box進(jìn)行重新處理
        if len(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_w>1, box_h>1)]
            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))
        for j in range(len(box_data)):
            thickness = 3
            left, top, right, bottom  = box_data[j][0:4]
            draw = ImageDraw.Draw(img)
            for i in range(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:, :]
    # 對(duì)框進(jìn)行進(jìn)一步的處理
    new_boxes = merge_bboxes(box_datas, cutx, cuty)
    return new_image, new_boxes
def normal_(annotation_line, input_shape):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])
    box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
    iw, ih = image.size
    image = image.transpose(Image.FLIP_LEFT_RIGHT)
    box[:, [0,2]] = iw - box[:, [2,0]]
    return image, box
if __name__ == "__main__":
    with open("2007_train.txt") as f:
        lines = f.readlines()
    a = np.random.randint(0,len(lines))
    # index = 0
    # line_all = lines[a:a+4]
    # for line in line_all:
    #     image_data, box_data = normal_(line,[416,416])
    #     img = image_data
    #     for j in range(len(box_data)):
    #         thickness = 3
    #         left, top, right, bottom  = box_data[j][0:4]
    #         draw = ImageDraw.Draw(img)
    #         for i in range(thickness):
    #             draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))
    #     img.show()
    #     # img.save(str(index)+"box.jpg")
    #     index = index+1
    line = lines[a:a+4]
    image_data, box_data = get_random_data(line,[416,416])
    img = Image.fromarray((image_data*255).astype(np.uint8))
    for j in range(len(box_data)):
        thickness = 3
        left, top, right, bottom  = box_data[j][0:4]
        draw = ImageDraw.Draw(img)
        for i in range(thickness):
            draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))
    img.show()
    # img.save("box_all.jpg")

以上就是python目標(biāo)檢測(cè)YoloV4當(dāng)中的Mosaic數(shù)據(jù)增強(qiáng)方法的詳細(xì)內(nèi)容,更多關(guān)于YoloV4 Mosaic數(shù)據(jù)增強(qiáng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python爬蟲(chóng)代理池搭建的方法步驟

    Python爬蟲(chóng)代理池搭建的方法步驟

    這篇文章主要介紹了Python爬蟲(chóng)代理池搭建的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 在tensorflow中實(shí)現(xiàn)屏蔽輸出的log信息

    在tensorflow中實(shí)現(xiàn)屏蔽輸出的log信息

    今天小編就為大家分享一篇在tensorflow中實(shí)現(xiàn)屏蔽輸出的log信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python編寫(xiě)第一個(gè)交互程序步驟示例教程

    python編寫(xiě)第一個(gè)交互程序步驟示例教程

    這篇文章主要為大家介紹了python編寫(xiě)第一個(gè)交互程序示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Python的pdfplumber庫(kù)將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)

    Python的pdfplumber庫(kù)將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)

    本文主要介紹了Python的pdfplumber庫(kù)將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • python GUI實(shí)現(xiàn)小球滿屏亂跑效果

    python GUI實(shí)現(xiàn)小球滿屏亂跑效果

    這篇文章主要為大家詳細(xì)介紹了python GUI實(shí)現(xiàn)小球滿屏亂跑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python網(wǎng)絡(luò)爬蟲(chóng)神器PyQuery的基本使用教程

    Python網(wǎng)絡(luò)爬蟲(chóng)神器PyQuery的基本使用教程

    這篇文章主要給大家介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲(chóng)神器PyQuery的基本使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)使用PyQuery具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02
  • 在Python3中初學(xué)者應(yīng)會(huì)的一些基本的提升效率的小技巧

    在Python3中初學(xué)者應(yīng)會(huì)的一些基本的提升效率的小技巧

    這篇文章主要介紹了在Python3中的一些基本的小技巧,有利于剛剛上手Python的初學(xué)者提升開(kāi)發(fā)效率,需要的朋友可以參考下
    2015-03-03
  • Django實(shí)現(xiàn)接口token檢測(cè)的方法詳解

    Django實(shí)現(xiàn)接口token檢測(cè)的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Django實(shí)現(xiàn)接口token檢測(cè),文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • 如何使用?Python為你的在線會(huì)議創(chuàng)建一個(gè)假的攝像頭

    如何使用?Python為你的在線會(huì)議創(chuàng)建一個(gè)假的攝像頭

    這篇文章主要介紹了使用?Python為你的在線會(huì)議創(chuàng)建一個(gè)假的攝像頭,在?Python?的幫助下,不再?gòu)?qiáng)制開(kāi)啟攝像頭,將向你展示如何為你的在線會(huì)議創(chuàng)建一個(gè)假的攝像頭,需要的朋友可以參考下
    2022-08-08
  • Pandas DataFrame分組求和、分組乘積的實(shí)例

    Pandas DataFrame分組求和、分組乘積的實(shí)例

    這篇文章主要介紹了Pandas DataFrame分組求和、分組乘積的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評(píng)論

隆子县| 西畴县| 海丰县| 朝阳县| 康乐县| 高平市| 宁津县| 老河口市| 昌吉市| 新巴尔虎右旗| 宜黄县| 万荣县| 五家渠市| 巴楚县| 怀仁县| 志丹县| 宜昌市| 桐城市| 报价| 陆川县| 南通市| 特克斯县| 濮阳县| 隆尧县| 全州县| 武清区| 定兴县| 长葛市| 龙口市| 电白县| 洪江市| 车险| 黄大仙区| 白山市| 丰城市| 孝昌县| 湘潭县| 灵山县| 绥棱县| 瓦房店市| 都江堰市|