[CPU+目標檢測] openvino實現Robomaster自瞄

這篇文章爲大連理工大學Robomaster凌Bug戰隊的李樂恆同學成果!
他在CPU上利用openvino這樣的深度學習算法實現了Robomaster的自瞄,大大提高了robomaster自瞄的上界,且達到了良好的檢測效果。所有代碼全部開源,
github主頁如下:https://github.com/Len-Li/openvino-robomaster

0.introduction

在Robomaster比賽中,選手往往使用顏色分離,提取輪廓,匹配輪廓的方式來識別裝甲板,但往往會花費大量時間在現場調整參數,於是我們想:能否利用深度學習來做自瞄以提高其魯棒性?但深度學習算法在實時性上通常表現不好,在1080ti這樣的顯卡才能達到實時,但沒人會在機器人上帶一個煤氣竈吧。很多人會想到使用Tensor RT,或者模型剪枝/壓縮,低比特推理的方式提高深度學習算法在GPU上的速度,但沒有想到使用純CPU也能實時運行神經網絡。憑藉Intel團隊發佈的openvino,我們可以在Intel CPU或者計算棒上實時運行目標檢測算法。在這裏我們以CPU+計算棒的方式介紹完整的實現步驟。
How it works?
分爲三個步驟

  1. 訓練自己的模型或者使用官方的demo
  2. 將模型轉換至中間表示層
  3. 部署
    在這裏插入圖片描述
    根據官網上的信息,openvino對TensorFlow支持的最好,所以我們這裏以谷歌的模型庫爲示例,走通上述的pipeline。
    在這裏插入圖片描述
檢測demo

1 訓練自己的模型

本節以robomaster數據集爲例,利用TensorFlow Object Detection API訓練自己的模型。

1.1 使用的模型庫

鏈接:https://github.com/tensorflow/models/tree/master/research/object_detection

TensorFlow Object Detection API是谷歌爸爸開源的模型庫,包含了完整的訓練和評測代碼。

模型包括主流的檢測和分割網絡,有SSD,Faster rcnn,mask rcnn,主幹網包括mobilenet v1/v2/v3(看出谷歌爸爸的偏心了吧),inception v2,resnet 50/101。

在這裏插入圖片描述

SSD家族,map代表檢測準確度,越大越好

1.2 數據集

大疆在2020年初開源了一個俯視視角的數據集,具體特徵類似於大家看直播時的畫面。分辨率是1920*1080。官方的用意應該是給雷達站做目標檢測,放在自瞄這樣的平視場景會存在一定的gap,同時分辨率也過大,增大計算負擔。所以我們在官方數據集的基礎上進行了魔改,改動如下:

  1. 檢測紅色裝甲板和藍色裝甲板
  2. 爲了方便評測,我們將原有的VOC數據集格式改爲COCO格式
  3. 對原圖進行crop操作。針對一張圖的每一個物體,先給該物體中心點一個30個像素點以內的上下左右漂移,然後以此點爲中心,扣取400*300的圖像

在這裏插入圖片描述

裝甲板非常小

在這裏插入圖片描述

裝甲板可見
下載鏈接: https://pan.baidu.com/s/105vjTcDs6XZHtnXAgCx86g 提取碼:v8yg

1.3 訓練+評測

Prerequisite:顯卡:最好1080ti以上。單卡v100兩個小時訓練完畢。

pip install tensorflow-gpu==1.14 最好使用1.14版本的TF

Linux系統

1.3.1 安裝TensorFlow Object Detection API

請參考 官方安裝指令:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

1.3.2 修改配置文件

  1. 將coco數據集轉換爲tfrecord格式
python object_detection/dataset_tools/create_coco_tf_record.py --logtostderr \
  --train_image_dir="data/roco_train" \
  --val_image_dir="data/roco_val" \
  --test_image_dir="data/roco_val" \
  --train_annotations_file="data/train.json" \
  --val_annotations_file="data/val.json" \
  --testdev_annotations_file="data/val.json" \
  --output_dir="models/research/save_dir"

目錄根據自己的實際位置更改,其中test可以忽略

  1. 模型config

所有模型配置文件在models/research/object_detection/samples/configs目錄下,以ssd_mobilenet_v2_coco.config爲例。

需要修改的地方①num_classes: 2 ②image_resizer:height: 300 width: 400 ③fine_tune_checkpoint ④最後的數據位置
數據擴充:水平翻轉,亮度,對比度,飽和度隨機抖動

  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    random_adjust_brightness {
      max_delta: 0.2
    }
  }
  data_augmentation_options {
    random_adjust_contrast {
      min_delta: 0.7
      max_delta: 1.1
    }
  }
  data_augmentation_options {
    random_adjust_saturation {
      min_delta: 0.9
      max_delta: 1.1
    }
  }
  1. 數據config

在models/research/object_detection/data/*.pbtxt裏記錄了數據集的類別,這裏我們是兩類,所以將label_map_path中的文件替換爲以下字段:(注意文件名對應)

item {
  name: "/m/01g317"
  id: 1
  display_name: "armor_blue"
}
item {
  name: "/m/0199g"
  id: 2
  display_name: "armor_red"
}

訓練代碼

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
CUDA_VISIBLE_DEVICES=0 python object_detection/model_main.py \
--pipeline_config_path=object_detection/samples/configs/ssd_mobilenet_v2_coco.config \
    --model_dir='output_model' \
    --num_train_steps=500000 \
    --sample_1_of_n_eval_examples=10 \
    --alsologtostderr

v100訓練2個小時就收斂了,1080ti可能三小時。訓練過程中會邊訓練邊評測。

這裏我們關注的是mAP(0.5:0.95)和AP(0.5),可以看到mAP是0.537,AP是0.974,基本滿足需求。

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.537
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.974
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.531
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.529
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.613
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.220
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.618
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.619
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.607
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.684
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000

當然,我們也放出來了模型文件

下載鏈接:百度雲:https://pan.baidu.com/s/1-m1ovofM_X9rh4rlQEicFg
提取碼:4nra

2 Openvino模型轉換

2.1 安裝openvino

在Linux下的安裝請參閱 官方文檔(很簡單):https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html

同時還可以查看 b站視頻:https://www.bilibili.com/video/BV1fC4y1s7dt

2.2 模型轉換

仍然在TensorFlow models文件夾下,先提取inference_graph

python object_detection/export_inference_graph.py \
--input_type=image_tensor \ 
--pipeline_config_path=object_detection/samples/configs/ssdlite_mobilenet_v2_coco.config \ --trained_checkpoint_prefix=models/research/output_model/model.ckpt-18723 \--output_directory=models/research/exported_model

將inference_graph轉換爲openvino接受的格式也就是intermediate representation。這裏需要注意ssd mobilenetv2對應的是ssd_support_api_v1.15.json

python3 mo_tf.py --input_model=exported_model/frozen_inference_graph.pb --transformations_config /opt/intel/openvino/deployment_tools/model_optimizer/extensions/front/tf/ssd_support_api_v1.15.json --tensorflow_object_detection_api_pipeline_config exported_model/pipeline.config --reverse_input_channels 

Python測試模型

python3 object_detection_demo_ssd_async.py -m /home/lilelife/onnx/ssdv2/frozen_inference_graph.xml -i *.avi

C艹測試模型(記得先編譯源碼)

./object_detection_demo_ssd_async -i *.mp4 -m ssdv2/frozen_inference_graph.xml

結果就是開頭的GIF啦。

微信公衆號

歡迎大家關注我的個人公衆號,現階段主要總結Robomaster相關的計算機視覺知識:Qt,C++,CMake,OpenCV等等
公衆號名稱:三豐雜貨鋪
在這裏插入圖片描述

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