Yolo v4中SAT和DropBlock介紹

2021-10-14 13:47:40 字數 4353 閱讀 3175

sat類似資料增強

對抗樣本的定義:以影象樣本為例,在原樣本上加入一些輕微的擾動,使得在人眼分辨不出差別的情況下,誘導模型進行錯誤分類。

如圖所示,input(panda影象)+ 噪音點 = output(誤判為gibbon影象)

github:

根據**結果可知,在coco資料集上可以將準確率提高1.6%

# **實現

import torch

import torch.nn.functional as f

from torch import nn

class dropblock2d(nn.module):

r"""randomly zeroes 2d spatial blocks of the input tensor.

as described in the *****

`dropblock: a regularization method for convolutional networks`_ ,

dropping whole blocks of feature map allows to remove semantic

information as compared to regular dropout.

args:

drop_prob (float): probability of an element to be dropped.

block_size (int): size of the block to drop

shape:

- input: `(n, c, h, w)`

- output: `(n, c, h, w)`

.. _dropblock: a regularization method for convolutional networks:

"""def __init__(self, drop_prob, block_size):

super(dropblock2d, self).__init__()

self.drop_prob = drop_prob

self.block_size = block_size

def forward(self, x):

# shape: (bsize, channels, height, width)

assert x.dim() == 4, \

"expected input with 4 dimensions (bsize, channels, height, width)"

if not self.training or self.drop_prob == 0.:

return x

else:

# get gamma value

gamma = self._compute_gamma(x)

# sample mask

mask = (torch.rand(x.shape[0], *x.shape[2:]) < gamma).float()

# place mask on input device

mask = mask.to(x.device)

# compute block mask

block_mask = self._compute_block_mask(mask)

out = x * block_mask[:, none, :, :]

# scale output

out = out * block_mask.numel() / block_mask.sum()

return out

def _compute_block_mask(self, mask):

block_mask = f.max_pool2d(input=mask[:, none, :, :],

kernel_size=(self.block_size, self.block_size),

stride=(1, 1),

padding=self.block_size // 2)

if self.block_size % 2 == 0:

block_mask = block_mask[:, :, :-1, :-1]

block_mask = 1 - block_mask.squeeze(1)

return block_mask

def _compute_gamma(self, x):

return self.drop_prob / (self.block_size ** 2)

class dropblock3d(dropblock2d):

r"""randomly zeroes 3d spatial blocks of the input tensor.

an extension to the concept described in the *****

`dropblock: a regularization method for convolutional networks`_ ,

dropping whole blocks of feature map allows to remove semantic

information as compared to regular dropout.

args:

drop_prob (float): probability of an element to be dropped.

block_size (int): size of the block to drop

shape:

- input: `(n, c, d, h, w)`

- output: `(n, c, d, h, w)`

.. _dropblock: a regularization method for convolutional networks:

"""def __init__(self, drop_prob, block_size):

super(dropblock3d, self).__init__(drop_prob, block_size)

def forward(self, x):

# shape: (bsize, channels, depth, height, width)

assert x.dim() == 5, \

"expected input with 5 dimensions (bsize, channels, depth, height, width)"

if not self.training or self.drop_prob == 0.:

return x

else:

# get gamma value

gamma = self._compute_gamma(x)

# sample mask

mask = (torch.rand(x.shape[0], *x.shape[2:]) < gamma).float()

# place mask on input device

mask = mask.to(x.device)

# compute block mask

block_mask = self._compute_block_mask(mask)

out = x * block_mask[:, none, :, :, :]

# scale output

out = out * block_mask.numel() / block_mask.sum()

return out

def _compute_block_mask(self, mask):

block_mask = f.max_pool3d(input=mask[:, none, :, :, :],

kernel_size=(self.block_size, self.block_size, self.block_size),

stride=(1, 1, 1),

padding=self.block_size // 2)

if self.block_size % 2 == 0:

block_mask = block_mask[:, :, :-1, :-1, :-1]

block_mask = 1 - block_mask.squeeze(1)

return block_mask

def _compute_gamma(self, x):

return self.drop_prob / (self.block_size ** 3)

Yolov4煙火檢測

作者開源的資料集 以上資料集包含煙霧影象,但是標註只有1類 開源模型只有fire類,如果要同時檢測煙霧,需要自己在影象上標註煙霧類別。採用yolov5煙霧 火災2類檢測模型,為自己的資料預標註,然後修正即可!煙火檢測資料集包含的場景型別 大火 小火,建築 草原 森林 車輛 汽車 卡車 電單車 電動車...

YOLOv4閱讀筆記

中英對照翻譯參考 中首先總結了目標檢測的一般性結構 通常,傳統的目標檢測器是離線訓練的。因此,研究人員總是喜歡利用這一優勢,開發更好的訓練方法,使目標檢測器在不增加推理成本的情況下獲得更好的精度。我們將這些方法稱為 bag of freebies 這些方法僅改變訓練策略或僅增加訓練成本。資料擴充 資...

YOLOv4學習資源

redmon寫完yolov3,退出計算機視覺領域之後,又有其他大佬推動著這一目標檢測神器的發展。yolov4我沒有看原 而是找了一些部落格資源進行學習,現在把我找到的部落格資源分享在此。看完這些部落格之後,yolov4其實並沒有引入特別創新的idea,作者的目標就是為了設計乙個用於實際工作環境的快速...