训练深度学习_深度学习训练tricks整理1
深度學習訓練tricks整理1
環境:pytorch1.4.0 + Ubuntu16.04
參考:
數據增強策略(一)?mp.weixin.qq.comhttps://zhuanlan.zhihu.com/p/104992391?zhuanlan.zhihu.com深度神經網絡模型訓練中的 tricks(原理與代碼匯總)?mp.weixin.qq.com一、data_augmentation
基本的數據增強調用torchvision.transforms庫中的就可以了,我整理一下其他的。
參考:
Pytorch 中的數據增強方式最全解釋?cloud.tencent.com1.1 單圖操作(圖像遮擋)
1.Cutout
對CNN 第一層的輸入使用剪切方塊Mask
論文參考:
Improved Regularization of Convolutional Neural Networks with Cutout?arxiv.org代碼鏈接:
https://github.com/uoguelph-mlrg/Cutout?github.comCutout示意圖2.Random Erasing
用隨機值或訓練集的平均像素值替換圖像的區域
論文參考:
https://arxiv.org/abs/1708.04896?arxiv.org代碼參考:
https://github.com/zhunzhong07/Random-Erasing/blob/master/transforms.py?github.comRandom Erasing示意圖3.Hide-and-Seek
圖像分割成一個由 SxS 圖像補丁組成的網格,根據概率設置隨機隱藏一些補丁,從而讓模型學習整個對象的樣子,而不是單獨一塊,比如不單獨依賴動物的臉做識別。
論文參考:
Hide-and-Seek: Forcing a Network to be Meticulous for Weakly-supervised Object and Action Localization?arxiv.org代碼參考:
https://github.com/kkanshul/Hide-and-Seek/blob/master/hide_patch.py?github.comHide-and-Seek示意圖4.GridMask
將圖像的區域隱藏在網格中,作用也是為了讓模型學習對象的整個組成部分
論文參考:
https://arxiv.org/pdf/2001.04086.pdf?arxiv.org代碼參考:
https://github.com/Jia-Research-Lab/GridMask/blob/master/imagenet_grid/utils/grid.py?github.comGridMask示意圖1.2 多圖組合
1.Mixup
通過線性疊加兩張圖片生成新的圖片,對應label也進行線性疊加用以訓練
論文參考:
https://arxiv.org/abs/1710.09412?arxiv.org理解與代碼參考:
目標檢測中圖像增強,mixup 如何操作??www.zhihu.comMixup 示意圖2.Cutmix
將另一個圖像中的剪切部分粘貼到當前圖像來進行圖像增強,圖像的剪切迫使模型學會根據大量的特征進行預測。
論文參考:
https://arxiv.org/abs/1905.04899?arxiv.org代碼參考:
https://github.com/clovaai/CutMix-PyTorch/blob/master/train.py?github.com代碼理解:
模型訓練技巧--CutMix_Guo_Python的博客-CSDN博客_cutmix loss?blog.csdn.netCutmix示意圖3.Mosaic data augmentation(用于檢測)
Cutmix中組合了兩張圖像,而在 Mosaic中使用四張訓練圖像按一定比例組合成一張圖像,使模型學會在更小的范圍內識別對象。其次還有助于顯著減少對batch-size的需求。
代碼參考:
https://zhuanlan.zhihu.com/p/163356279?zhuanlan.zhihu.comMosaic data augmentation示意圖二、Label Smoothing
參考論文:
https://arxiv.org/pdf/1812.01187.pdf?arxiv.org參考理解:
SoftMax原理介紹 及其 LabelSmooth優化?blog.csdn.net標簽平滑Label Smoothing?blog.csdn.nethttps://zhuanlan.zhihu.com/p/148487894?zhuanlan.zhihu.com在多分類訓練任務中,輸入圖片經過神經網絡的計算,會得到當前輸入圖片對應于各個類別的置信度分數,這些分數會被softmax進行歸一化處理,最終得到當前輸入圖片屬于每個類別的概率,最終在訓練網絡時,最小化預測概率和標簽真實概率的交叉熵,從而得到最優的預測概率分布.
網絡會驅使自身往正確標簽和錯誤標簽差值大的方向學習,在訓練數據不足以表征所以的樣本特征的情況下,這就會導致網絡過擬合。label smoothing的提出就是為了解決上述問題。最早是在Inception v2中被提出,是一種正則化的策略。其通過"軟化"傳統的one-hot類型標簽,使得在計算損失值時能夠有效抑制過擬合現象。
代碼:
class LabelSmoothCEloss(nn.Module):def __init__(self):super().__init__()def forward(self, pred, label, smoothing=0.1):pred = F.softmax(pred, dim=1)one_hot_label = F.one_hot(label, pred.size(1)).float()smoothed_one_hot_label = (1.0 - smoothing) * one_hot_label + smoothing / pred.size(1)loss = (-torch.log(pred)) * smoothed_one_hot_labelloss = loss.sum(axis=1, keepdim=False)loss = loss.mean()return loss ---------------------------------------------------------------------------------------------- 調用時criterion = nn.CrossEntropyLoss() 改為criterion = LabelSmoothCELoss()三、學習率調整
warm up最早來自于這篇文章:https://arxiv.org/pdf/1706.02677.pdf 。根據這篇文章,我們一般只在前5個epoch使用warm up。consine learning rate來自于這篇文章:https://arxiv.org/pdf/1812.01187.pdf 。通常情況下,把warm up和consine learning rate一起使用會達到更好的效果。 代碼實現:
class WarmUpLR(_LRScheduler):"""warmup_training learning rate schedulerArgs:optimizer: optimzier(e.g. SGD)total_iters: totoal_iters of warmup phase"""def __init__(self, optimizer, total_iters, last_epoch=-1):self.total_iters = total_iterssuper().__init__(optimizer, last_epoch)def get_lr(self):"""we will use the first m batches, and set the learningrate to base_lr * m / total_iters"""return [base_lr * self.last_epoch / (self.total_iters + 1e-8) for base_lr in self.base_lrs]# MultiStepLR without warm up scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.milestones, gamma=0.1)# warm_up_with_multistep_lr warm_up_with_multistep_lr = lambda epoch: epoch / args.warm_up_epochs if epoch <= args.warm_up_epochs else 0.1**len([m for m in args.milestones if m <= epoch]) scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=warm_up_with_multistep_lr)# warm_up_with_cosine_lr warm_up_with_cosine_lr = lambda epoch: epoch / args.warm_up_epochs if epoch <= args.warm_up_epochs else 0.5 * ( math.cos((epoch - args.warm_up_epochs) /(args.epochs - args.warm_up_epochs) * math.pi) + 1) scheduler = torch.optim.lr_scheduler.LambdaLR( optimizer, lr_lambda=warm_up_with_cosine_lr)四、蒸餾(distillation)
4.1 傳統蒸餾
論文參考:
https://arxiv.org/pdf/1503.02531.pdf?arxiv.org理解參考:
深度學習方法(十五):知識蒸餾(Distilling the Knowledge in a Neural Network),在線蒸餾?blog.csdn.net知識蒸餾(Distilling Knowledge )的核心思想?blog.csdn.net傳統蒸餾示意圖訓練的過程采用以下的步驟: 先用硬標簽訓練大型復雜網絡(Teacher Net); 采用值大的T,經訓練好的 TN 進行前向傳播獲得軟標簽; 分別采用值大的 T 和 T=1 兩種情況,讓小型網絡(Student Net)獲得兩種不同的輸出,加權計算兩種交叉熵損失,訓練SN; 采用訓練好的 SN 預測類別。2. 新的蒸餾方式:通道蒸餾
論文參考:
Channel Distillation: Channel-Wise Attention for Knowledge Distillation?arxiv.org代碼參考:
https://github.com/zhouzaida/channel-distillation?github.com通道蒸餾示意圖總結
以上是生活随笔為你收集整理的训练深度学习_深度学习训练tricks整理1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页html语言怎么看,怎样查看网页的c
- 下一篇: android 人脸识别_小模型,高精度