手把手教物體檢測——EfficientDet

目錄

摘要

訓練數據

1、下載Pytoch版的EfficientDet。

2、製作數據集。

3、下載EfficientNets預訓練模型。

4、安裝模型需要的包

5、放置數據集

6、修改train.py中的參數

測試

注意


摘要

谷歌大腦團隊 Quoc V. Le 等人系統研究了多種目標檢測神經網絡架構設計,提出了能夠提升模型效率的幾項關鍵優化。首先,他們提出加權雙向特徵金字塔網絡(weighted bi-directional feature pyramid network,BiFPN),從而輕鬆、快速地實現多尺度特徵融合;其次,他們提出一種複合縮放(compound scaling)方法,同時對所有主幹網絡、特徵網絡和邊界框/類別預測網絡的分辨率、深度和寬度執行統一縮放。基於這些優化,研究者開發了新型目標檢測器 EfficientDet。模型結構如下:

 

EfficientDet的特點:

  1. 使用EfficientNet網格作爲主幹網絡。
  2. 提出BiFPN,使模型實現高效的雙向跨尺度連接和加權特徵融合。下圖是集中常見的FPN結構圖:

 

aFPN 使用自上而下的路徑來融合多尺度特徵 level 3-7P3 - P7);bPANet FPN 的基礎上額外添加了自下而上的路徑;cNAS-FPN 使用神經架構搜索找出不規則特徵網絡拓撲;(d)-(f) 展示了該論文研究的三種替代方法。d 在所有輸入特徵和輸出特徵之間添加成本高昂的連接;e 移除只有一個輸入邊的節點,從而簡化 PANetf 是兼顧準確和效率的 BiFPN

  1. 模型縮放。提出一種目標檢測器複合縮放方法,即統一擴大所有主幹網絡、特徵網絡、邊界框/類別預測網絡的分辨率/深度/寬度。

基於以上的特點,EfficientDet-D7 COCO 數據集上實現了當前最優的 51.0 mAP,準確率超越之前最優檢測器(+0.3% mAP),其規模僅爲之前最優檢測器的 1/4,而後者的 FLOPS 更是 EfficientDet-D7 9.3 倍。

論文地址:https://arxiv.org/abs/1911.09070

訓練數據

1、下載Pytoch版的EfficientDet。

https://github.com/zylo117/Yet-Another-EfficientDet-Pytorch

2、製作數據集。

將標註好的:Labelme數據集轉爲COCO數據集。

參照https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/106255087

3、下載EfficientNets預訓練模型。

b0: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b0-355c32eb.pth

b1: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b1-f1951068.pth

b2: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b2-8bb594d6.pth

b3: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b3-5fb5a3c3.pth

b4: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b4-6ed6700e.pth

b5: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b5-b6417697.pth

b6: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b6-c76e70fd.pth

b7: https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b7-dcc49843.pth

將預訓練模型放到指定的目錄下面:

 

如果可以連接外網可以忽略這個步驟,運行時會自動下載預訓練模型。

4、安裝模型需要的包

  1. pycocotools
  2. pytorch1.2版本以上(模型用了pytorch的nms,在1.2以上的版本中才包含)
  3. python-opencv(pip install opencv-python)
  4. tensorboardX(pip install tensorboardx)
  5. webcolors(pip install webcolors)

結合本地環境,如果還有缺少的自行下載,唯一要注意的就是pytorch的版本。

  1. 修改coco.yml參數。

project_name: coco  # also the folder name of the dataset that under data_path folder
train_set: train2017#注意和COCO轉化時,選擇的年份一致。
val_set: val2017
num_gpus: 1

# mean and std in RGB order, actually this part should remain unchanged as long as your dataset is similar to coco.
mean: [0.485, 0.456, 0.406]
std: [0.229, 0.224, 0.225]

# this is coco anchors, change it if necessary
anchors_scales: '[2 ** 0, 2 ** (1.0 / 3.0), 2 ** (2.0 / 3.0)]'
anchors_ratios: '[(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)]'

# must match your dataset's category_id.
# category_id is one_indexed,
# for example, index of 'car' here is 2, while category_id of is 3

obj_list: ['aircraft', 'oiltank'] #類別順序和轉化COCO時候的類別順序一致。COCO中設置'aircraft':1, 'oiltank':2

5、放置數據集

將數據集放到datasets目錄下,如下圖:

 

6、修改train.py中的參數

主要需要修改的參數有compound_coef、batch_size、num_epochs、save_interval、lr
 、data_path、load_weights
compound_coef:0-7,選擇EfficientDet的模型,對應d0-d7,同時對應EfficientNet網絡的b0-b7。
batch_size:根據顯卡顯存的大小和類別的多少定義。
epochs:默認爲500,一般情況300即可。
save_interval:迭代多少次保存一個模型。
lr:學習率,默認爲10-4,這個模型不要用太大的學習率,經測試,學習率太大不收斂。
data_path:數據集的路徑,本例放在datasets路徑下面,就設置爲datasets。
load_weights:加載模型的路徑,如果沒有一次訓練完,再次訓練就要用到此參數。
參數配置如下:
def get_args():

    parser = argparse.ArgumentParser('Yet Another EfficientDet Pytorch: SOTA object detection network - Zylo117')

    parser.add_argument('-p', '--project', type=str, default='coco', help='project file that contains parameters')

    parser.add_argument('-c', '--compound_coef', type=int, default=4, help='coefficients of efficientdet')

    parser.add_argument('-n', '--num_workers', type=int, default=2, help='num_workers of dataloader')

    parser.add_argument('--batch_size', type=int, default=2, help='The number of images per batch among all devices')

    parser.add_argument('--head_only', type=boolean_string, default=False,

                        help='whether finetunes only the regressor and the classifier, '

                             'useful in early stage convergence or small/easy dataset')

    parser.add_argument('--lr', type=float, default=1e-4)

    parser.add_argument('--optim', type=str, default='adamw', help='select optimizer for training, '

                                                                   'suggest using \'admaw\' until the'

                                                                   ' very final stage then switch to \'sgd\'')

    parser.add_argument('--num_epochs', type=int, default=300)

    parser.add_argument('--val_interval', type=int, default=1, help='Number of epoches between valing phases')

    parser.add_argument('--save_interval', type=int, default=11970, help='Number of steps between saving')

    parser.add_argument('--es_min_delta', type=float, default=0.0,

                        help='Early stopping\'s parameter: minimum change loss to qualify as an improvement')

    parser.add_argument('--es_patience', type=int, default=0,

                        help='Early stopping\'s parameter: number of epochs with no improvement after which training will be stopped. Set to 0 to disable this technique.')

    parser.add_argument('--data_path', type=str, default='datasets', help='the root folder of dataset')

    parser.add_argument('--log_path', type=str, default='logs/')

    parser.add_argument('-w', '--load_weights', type=str, default=None,

                        help='whether to load weights from a checkpoint, set None to initialize, set \'last\' to load last checkpoint')

    parser.add_argument('--saved_path', type=str, default='logs/')

    parser.add_argument('--debug', type=boolean_string, default=False, help='whether visualize the predicted boxes of trainging, '

                                                                  'the output images will be in test/')
運行效果如下:

 

測試

修改efficientdet_test.py參數:
  1. compound_coef和訓練時設置的參數相對應,比如訓練時設置的是0,測試的時候也設置爲0
  2. img_path:測試圖片的路徑。
  3. threshod = 0.2iou_threshod = 0.35。這兩個參數在物體檢測中常見,一個過濾分數,一個設置重疊度。
  4. obj_ist =['aircraft', 'oitank']:類別,和訓練時的類別順序一致。
修改完成後運行efficientdet_test.py文件
會在test文件夾下面保存結果的圖片,結果如下:


 
運行coco_eval.py,會生成,測試模型得分。在這裏注意numpy的版本。如果太高會報一個不兼容的錯誤,需要降到17版本。我用的版本是1.17.4,結果如下:

 

注意

1、這個模型使用時,compound_coef在訓練集和測試集相對應,如果不仔細容易出現錯亂。

2、在使用COCO數據集時,注意類別的順序,如果出現錯亂會導致得分很低甚至爲零。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章