EfficientNet v2筆記

2022-01-04 Geek部落格

前篇:EfficientNet v1筆記

個人總結

亮點1:改進了efficientnet模型

亮點2:提出了漸進式學習方法(個人覺得思路很棒,因為類似於人類的學習方式,隨著年齡增加,我們學習內容的難度也是由簡單到困難)

    訓練前期,輸入較小尺寸的圖片,使用較弱的正則化策略(Dropout、RandAugment、Mixup);

    訓練後期,輸入較大尺寸的圖片,使用較強的正則化策略。

    通過漸進式學習,訓練時間有很大的縮減,且模型性能有稍許提高:


EfficientNetV2: Smaller Models and Faster Training

谷歌 2021 CVPR


介紹

    首先指出了EfficientNet v1存在的問題:

    (1)訓練圖像的尺寸很大時,訓練速度非常慢

    (2)在網絡淺層中使用DW卷積速度會非常慢

    (3)同等放大每個stage是次優的

    

    本文使用訓練感知神經結構搜索和縮放,共同優化訓練速度和參數效率。該模型從增加了新的操作(Fused-MBConv)的搜索空間中搜索。實驗表明,EffecientNet v2模型的訓練速度比最先進的模型快得多,而體積卻小了6.8倍。

    本文還通過漸進學習加快訓練速度:在早期訓練階段,我們用小圖像尺寸和弱正則化(如dropout和data augmentation)訓練網絡,然後我們逐漸增加圖像尺寸和增強正則化。基於漸進式調整(Howard,2018),我們的方法可以在不導致準確性下降的情況下加快訓練速度。

性能圖:

網絡框架(EfficientNetV2-S):

附(EfficienctNetv1):

與v1相比:

(1)混合使用了MBConv和Fused-MBConv(Fused-MBConv在實際代碼中無SE模塊);

(2)使用了較小的expansion rate;

(3)kernel size也變小了;

(4)移除了v1中最後一個步距為1的stage,即v1中的stage8。

代碼實現

SE模塊:

class SqueezeExcite(nn.Module):    def __init__(self,                 input_c: int,   # MBblock input channel                 expand_c: int,  # block expand channel(DW卷積的輸出channel)                 se_ratio: float = 0.25):        super(SqueezeExcite, self).__init__()        squeeze_c = int(input_c * se_ratio)        self.conv_reduce = nn.Conv2d(expand_c, squeeze_c, 1)        self.act1 = nn.SiLU()  # Swish激活函數        self.conv_expand = nn.Conv2d(squeeze_c, expand_c, 1)        self.act2 = nn.Sigmoid()
def forward(self, x: Tensor) -> Tensor: scale = x.mean((2, 3), keepdim=True) scale = self.conv_reduce(scale) scale = self.act1(scale) scale = self.conv_expand(scale) scale = self.act2(scale) return scale * x

MBConv(有捷徑分支的時候才有Dropout層):

dropout層:

def drop_path(x, drop_prob: float = 0., training: bool = False):    """    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).    "Deep Networks with Stochastic Depth", https://arxiv.org/pdf/1603.09382.pdf
This function is taken from the rwightman. It can be seen here: https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/drop.py#L140 """ if drop_prob == 0. or not training: return x keep_prob = 1 - drop_prob shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device) random_tensor.floor_() # binarize output = x.div(keep_prob) * random_tensor return output

class DropPath(nn.Module): """ Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). "Deep Networks with Stochastic Depth", https://arxiv.org/pdf/1603.09382.pdf """ def __init__(self, drop_prob=None): super(DropPath, self).__init__() self.drop_prob = drop_prob
def forward(self, x): return drop_path(x, self.drop_prob, self.training)

MBConv:

class MBConv(nn.Module):    def __init__(self,                 kernel_size: int,                 input_c: int,                 out_c: int,                 expand_ratio: int,                 stride: int,                 se_ratio: float,                 drop_rate: float,                 norm_layer: Callable[..., nn.Module]):        super(MBConv, self).__init__()
if stride not in [1, 2]: raise ValueError("illegal stride value.")
self.has_shortcut = (stride == 1 and input_c == out_c)
activation_layer = nn.SiLU expanded_c = input_c * expand_ratio
# 在EfficientNetV2中,MBConv中不存在expansion=1的情況所以conv_pw肯定存在 assert expand_ratio != 1 # Point-wise expansion self.expand_conv = ConvBNAct(input_c, expanded_c, kernel_size=1, norm_layer=norm_layer, activation_layer=activation_layer)
# Depth-wise convolution self.dwconv = ConvBNAct(expanded_c, expanded_c, kernel_size=kernel_size, stride=stride, groups=expanded_c, norm_layer=norm_layer, activation_layer=activation_layer)
self.se = SqueezeExcite(input_c, expanded_c, se_ratio)
# Point-wise linear projection self.project_conv = ConvBNAct(expanded_c, out_planes=out_c, kernel_size=1, norm_layer=norm_layer, activation_layer=nn.Identity) # 注意這裡沒有激活函數,所有傳入Identity
self.out_channels = out_c
# 只有在使用shortcut連接時才使用dropout層 self.drop_rate = drop_rate if self.has_shortcut and drop_rate > 0: self.dropout = DropPath(drop_rate)
def forward(self, x: Tensor) -> Tensor: result = self.expand_conv(x) result = self.dwconv(result) result = self.se(result) result = self.project_conv(result)
if self.has_shortcut: if self.drop_rate > 0: result = self.dropout(result) result += x
return result

Fused-MBConv:

class FusedMBConv(nn.Module):    def __init__(self,                 kernel_size: int,                 input_c: int,                 out_c: int,                 expand_ratio: int,                 stride: int,                 se_ratio: float,                 drop_rate: float,                 norm_layer: Callable[..., nn.Module]):        super(FusedMBConv, self).__init__()
assert stride in [1, 2] assert se_ratio == 0
self.has_shortcut = stride == 1 and input_c == out_c self.drop_rate = drop_rate
self.has_expansion = expand_ratio != 1
activation_layer = nn.SiLU # alias Swish expanded_c = input_c * expand_ratio
# 只有當expand ratio不等於1時才有expand conv if self.has_expansion: # Expansion convolution self.expand_conv = ConvBNAct(input_c, expanded_c, kernel_size=kernel_size, stride=stride, norm_layer=norm_layer, activation_layer=activation_layer)
self.project_conv = ConvBNAct(expanded_c, out_c, kernel_size=1, norm_layer=norm_layer, activation_layer=nn.Identity) # 注意沒有激活函數 else: # 當只有project_conv時的情況 self.project_conv = ConvBNAct(input_c, out_c, kernel_size=kernel_size, stride=stride, norm_layer=norm_layer, activation_layer=activation_layer) # 注意有激活函數
self.out_channels = out_c
# 只有在使用shortcut連接時才使用dropout層 self.drop_rate = drop_rate if self.has_shortcut and drop_rate > 0: self.dropout = DropPath(drop_rate)
def forward(self, x: Tensor) -> Tensor: if self.has_expansion: result = self.expand_conv(x) result = self.project_conv(result) else: result = self.project_conv(x)
if self.has_shortcut: if self.drop_rate > 0: result = self.dropout(result)
result += x
return result

配置文件:

def efficientnetv2_s(num_classes: int = 1000):    """    EfficientNetV2    https://arxiv.org/abs/2104.00298    """    # train_size: 300, eval_size: 384
# repeat, kernel, stride, expansion, in_c, out_c, operator, se_ratio model_config = [[2, 3, 1, 1, 24, 24, 0, 0], [4, 3, 2, 4, 24, 48, 0, 0], [4, 3, 2, 4, 48, 64, 0, 0], [6, 3, 2, 4, 64, 128, 1, 0.25], [9, 3, 1, 6, 128, 160, 1, 0.25], [15, 3, 2, 6, 160, 256, 1, 0.25]]
model = EfficientNetV2(model_cnf=model_config, num_classes=num_classes, dropout_rate=0.2) return model
def efficientnetv2_m(num_classes: int = 1000): """ EfficientNetV2 https://arxiv.org/abs/2104.00298 """ # train_size: 384, eval_size: 480
# repeat, kernel, stride, expansion, in_c, out_c, operator, se_ratio model_config = [[3, 3, 1, 1, 24, 24, 0, 0], [5, 3, 2, 4, 24, 48, 0, 0], [5, 3, 2, 4, 48, 80, 0, 0], [7, 3, 2, 4, 80, 160, 1, 0.25], [14, 3, 1, 6, 160, 176, 1, 0.25], [18, 3, 2, 6, 176, 304, 1, 0.25], [5, 3, 1, 6, 304, 512, 1, 0.25]]
model = EfficientNetV2(model_cnf=model_config, num_classes=num_classes, dropout_rate=0.3) return model

def efficientnetv2_l(num_classes: int = 1000): """ EfficientNetV2 https://arxiv.org/abs/2104.00298 """ # train_size: 384, eval_size: 480
# repeat, kernel, stride, expansion, in_c, out_c, operator, se_ratio model_config = [[4, 3, 1, 1, 32, 32, 0, 0], [7, 3, 2, 4, 32, 64, 0, 0], [7, 3, 2, 4, 64, 96, 0, 0], [10, 3, 2, 4, 96, 192, 1, 0.25], [19, 3, 1, 6, 192, 224, 1, 0.25], [25, 3, 2, 6, 224, 384, 1, 0.25], [7, 3, 1, 6, 384, 640, 1, 0.25]]
model = EfficientNetV2(model_cnf=model_config, num_classes=num_classes, dropout_rate=0.4) return model

整體架構:

class EfficientNetV2(nn.Module):    def __init__(self,                 model_cnf: list,                 num_classes: int = 1000,                 num_features: int = 1280,                 dropout_rate: float = 0.2,                 drop_connect_rate: float = 0.2):        super(EfficientNetV2, self).__init__()
for cnf in model_cnf: assert len(cnf) == 8
norm_layer = partial(nn.BatchNorm2d, eps=1e-3, momentum=0.1)
stem_filter_num = model_cnf[0][4]
self.stem = ConvBNAct(3, stem_filter_num, kernel_size=3, stride=2, norm_layer=norm_layer) # 激活函數默認是SiLU
total_blocks = sum([i[0] for i in model_cnf]) block_id = 0 blocks = [] for cnf in model_cnf: repeats = cnf[0] op = FusedMBConv if cnf[-2] == 0 else MBConv for i in range(repeats): blocks.append(op(kernel_size=cnf[1], input_c=cnf[4] if i == 0 else cnf[5], out_c=cnf[5], expand_ratio=cnf[3], stride=cnf[2] if i == 0 else 1, se_ratio=cnf[-1], drop_rate=drop_connect_rate * block_id / total_blocks, norm_layer=norm_layer)) block_id += 1 self.blocks = nn.Sequential(*blocks)
head_input_c = model_cnf[-1][-3] head = OrderedDict()
head.update({"project_conv": ConvBNAct(head_input_c, num_features, kernel_size=1, norm_layer=norm_layer)}) # 激活函數默認是SiLU
head.update({"avgpool": nn.AdaptiveAvgPool2d(1)}) head.update({"flatten": nn.Flatten()})
if dropout_rate > 0: head.update({"dropout": nn.Dropout(p=dropout_rate, inplace=True)}) head.update({"classifier": nn.Linear(num_features, num_classes)})
self.head = nn.Sequential(head)
# initial weights for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode="fan_out") if m.bias is not None: nn.init.zeros_(m.bias) elif isinstance(m, nn.BatchNorm2d): nn.init.ones_(m.weight) nn.init.zeros_(m.bias) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) nn.init.zeros_(m.bias)
def forward(self, x: Tensor) -> Tensor: x = self.stem(x) x = self.blocks(x) x = self.head(x)
return x

參考:

    原論文

    霹靂吧啦Wz(bilibili)

相關焦點

  • EfficientNet代碼和論文解析
    其中更多細節,大家可以參考下代碼包括從efficientnet-b0到efficientnet-b7的一些參數設定(寬度/深度/解析度相對於efficientnet-b0的放大比例,dropout的比例),如下
  • EfficientNetV2:更小,更快!
    參考EfficientNet: Rethinking Model Scaling for Convolutional Neural NetworksEfficientNetV2: Smaller Models and Faster Traininggoogle/automl/efficientnetv2
  • EfficientNet詳解:當前最強網絡
    參考論文連結:https://arxiv.org/abs/1905.11946官方TensorFlow代碼:https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet第三方PyTorch代碼:https://github.com/lukemelas/EfficientNet-PyTorch
  • 萬萬沒想到,EfficientNet居然這麼火!
    點擊我愛計算機視覺標星,更快獲取CVML新技術上周52CV曾經第一時間報導了谷歌新出的算法
  • 技術解讀EfficientNet系列模型——圖片分類的領域的扛把子
    8 當前的代碼資源有關efficientnet系列的代碼早已經開源。在Github上也可以搜索到許多有關efficientnet的工程。大多都是關於EfficientNet-B0到EfficientNet-B7的。其中包括了TensorFlow版本和PyTorch版本。
  • 【AI論技】EfficientNet的原理和應用
    Mark Sandler Andrew Howard Menglong Zhu Andrey Zhmoginov Liang-Chieh Chen,MobileNetV2: Inverted Residuals and Linear Bottlenecks4.https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet
  • 獨家 | 使EfficientNet更有效率的三種方法(附連結)
    Jégou, Fixing the train-test resolution discrepancy: FixEfficientNet (2020), NeurIPS 2019How we made EfficientNet more efficienthttps://towardsdatascience.com/how-we-made-efficientnet-more-efficient
  • 時隔兩年,EfficientNet v2來了!更快,更小,更強!
    paper: https://arxiv.org/abs/2104.00298code: https://github.com/google/automl/efficientnetv2本文是谷歌的MingxingTan與Quov V.Le對EfficientNet的一次升級
  • 有效提高視覺模型精度:EfficientNet-Lite 發布
    ImageClassifierDataLoader.from_folder(flower_path)train_data, test_data = data.split(0.9)# Customize the pre-trained TensorFlow modelmodel = image_classifier.create(train_data, model_spec=efficienetnet_lite0
  • 經驗總結 | Docker 使用筆記
    刪除所有正在運行和已停止的容器docker stop $(docker ps -a -q)docker rm $(docker ps -a -q)刪除所有容器,沒有任何標準docker container rm $(docker container
  • 《ASP.NET Core 與 RESTful API 開發實戰》-- (第10章)-- 讀書筆記
    (下)《ASP.NET Core 與 RESTful API 開發實戰》-- (第9章)-- 讀書筆記(上)《ASP.NET Core 與 RESTful API 開發實戰》-- (第8章)-- 讀書筆記(尾)《ASP.NET Core 與 RESTful API 開發實戰》-- (第8章)-- 讀書筆記(下)《ASP.NET
  • C#/.NET/.NET Core推薦學習書籍
    《深入理解C#(第3版)》《CLR via C# 第4版框架設計》《ASP.NET Core微服務實戰》《.NET 微服務 - 體系結構電子書》《ASP.NET Core開發實戰》《C#高級編程(第11版)》《.NET高級調試》《C#8.0和.NET Core 3.0高級編程》《.net
  • 終極釣魚模擬器v2.10.2.470-PC
    分享地說明【養老推薦……】自帶中文【升級檔兩個v2.10.2.469和v2.10.2.470】大概意思就是可以在世界各地使用各種技術去釣魚,emmmm,差不多就是如果你因為在等魚上鉤的過程在電腦面前睡著了…………你可以在冬天的冰面上釣魚,拿個什麼鑽頭挖個窟窿這樣有兩種模式,困難模式比普通模式禁用了一些輔助功能。
  • Kali linux 學習筆記(一)
    Kali linux 學習筆記(一)
  • 英文讀書筆記 | 《高效人士的七個習慣》01-品格的培養永遠比糾正行為更重要
    什麼是effective,我理解這個詞彙的時候,我會拿這個詞語和efficient作比較,在原著林肯書中提到The act(法案)took effect. 比起我們一直喊著口號我們要達成什麼,不如發現問題,到底什麼在拖延我們,對於學生來說,無法記牢東西背後可能是是筆記的習慣。對於考試失策的人來說,那就可能是懶筋作祟,表裡不如一。對於膽怯不敢嘗試的演講的人來說,也許就是勇氣的品格。
  • 收藏 | 最強分類模型EfficientNet詳解
  • 皇室戰爭更新 2v2模式全新卡牌聯賽系統
    自上周末開始,《皇室戰爭》官 方陸續公布了接下來的一系列更新內容,具體包括部落戰2v2模式、四張全新卡牌、聯賽系統、全新競技場等內容
  • 谷歌提出1個小時訓練EfficientNet,準確率高達83%!
    在這個排行榜的第10位,我們看到了一個孤獨的名字——AmoebaNet-D N6F256,根據排行榜的數據,它用1/4個TPUv2 Pod和1小時的時間在ImageNet上達到了93.03%的top-5準確率。實際上,該架構在2018年4月就由谷歌提出,在相同的硬體條件下,AmoebaNet-D N6F256的訓練時間比ResNet-50要短很多。