使用TransferLearning實現環視圖像的角點檢測——Tensorflow+MobileNetv2_SSD


環境說明

  • 依賴環境安裝eIQ官方指南:
name: eiq_auto 
channels:
  - conda-forge
  - defaults
dependencies:
  - numpy=1.18.1=py36h4f9e942_0
  - onnx==1.6.0
  - opencv==4.2.0
  - pandas=0.24.2=py36he6710b0_0
  - pillow=7.0.0=py36hb39fc2d_0
  - protobuf=3.9.2=py36he6710b0_0
  - pytest=5.3.0=py36_0
  - python=3.6.10=h0371630_0
  - tensorflow=1.14.0=mkl_py36h2526735_0
pip:
   - onnxruntime==1.0.0

==================================================================================

1.安裝tensorflow object detection API

sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
pip install --user Cython
pip install --user contextlib2
pip install --user jupyter
pip install --user matplotlib
  • 下載models
git clone https://github.com/tensorflow/models.git
  • 安裝cocoAPI
pip install --user pycocotools
  • 使用Protobuf Compilation
protoc object_detection/protos/*.proto --python_out=.
  • 添加到PYTHONPATH
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:/mnt/d/0-WORK/models/models-master/research:/mnt/d/0-WORK/models/models-master/research/slim
source ~/.bashrc
  • 注意以上絕對路徑填正確
  • 測試是否完成
python object_detection/builders/model_builder_tf1_test.py
  • 如果出現以下結果表示API已成功安裝:

2.使用mobilenetV2_SSD進行訓練和預測

官方使用的版本(ssd_mobilenet_v2_coco_2018_03_29)

  • 首先使用以下flowchart幫助理解transferLearning
  • step1:進入Model目錄,執行如下命令:
cd models/research/
python setup.py build
python setup.py install
  • step2:配置model並進行訓練,首先在object_detection/目錄下創建目錄ssd_model:
    將下載好的model解壓后放在自定義路徑下(如object_detection/ssd_model/),下載鏈接[http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz]
    mobileNetv2_SSD使用tfrecord格式的數據進行訓練,數據集是使用labelImg工具進行標注的xml格式,需要完成xml轉csv再轉為record文件。數據集轉換工具詳見datitran:[https://github.com/datitran/raccoon_dataset]
    把制作好的數據集tfrecords放在路徑下(制作步驟詳見文末)。復制訓練數據用到的文件,我們在這個基礎上修改配置,訓練我們的數據.coco數據集共有90個class。我們在APA數據標注中使用了3個class,因此打開配置文件ssd_mobilenet_v2_coco.config.需要修改的內容如下:
./object_detection/ssd_model/data
cp object_detection/data/mscoco_label_map.pbtxt object_detection/ssd_model/ cp object_detection/samples/configs/ssd_mobilenet_v2_coco.config object_detection/ssd_model/
# 修改ssd_mobilenet_v2_coco.config
num_classes: 3 # 自定義的class數目
num_steps: 200000 # 設置多少個step后停止,可以mark此行不使用,loss值沒有持續下降,可以CTRL-C停止
batch_size: 8 # 根據算力設置
fine_tune_checkpoint:/mnt/.../ssd_model/mobilenet_v2_1.4_224/model.ckpt # 上述step中下載的 pre-trained model path,最后固定接上mode.ckpt
train_input_reader: {
  tf_record_input_reader {
    input_path: "/mnt/.../ssd_model/data/train.record"
        # 之前dataset產生的TFRecord train.record路徑

eval_input_reader: {
  tf_record_input_reader {
    input_path: "/mnt/.../ssd_model/data/test.record"
        # 之前dataset產生的TFRecord test.record路徑

label_map_path:"/mnt/.../ssd_model/data/mscoco_label_map.pbtxt" # 注意train和eval兩處都需要更改.
  • step3:訓練開始,新版的API中train.py在legacy目錄下,先把它copy到research下。
    回到research目錄下 執行
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=ssd_model/data/ssd_mobilenet_v2_coco.config

訓練過程如下 (沒有GPU時間會比較長,可以在觀察到loss不再下降的時候CTRL+C停止訓練)



訓練完成后,結果會在-–train_dir指定的path下:


  • step4:模型效果評估:
    我們使用tensorboard工具查看訓練效果。首先browser打開tensorbord的address,即可看到training及validate的信息:
    執行以下命令(=后添加剛剛訓練的路徑)
tensorboard --logdir=/mnt/d/0-WORK/models/models-master/research/training

成功打開會出現以下地址:



將地址復制粘貼到瀏覽器中即可看到訓練可視化結果:http://desktop-0vqus2j:6006/#scalars


  • step5: 使用eavl.py查看在驗證集上的效果

  • step6:保存模型:
python object_detection/export_inference_graph.py --pipeline_config_path=/mnt/d/0-WORK/models/models-master/research/object_detection/ssd_model/data/ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix=/mnt/d/0-WORK/models/models-master/research/training/model.ckpt-77133 --output_directory /mnt/d/0-WORK/models/models-master/research/training/

執行完畢后出現:

OK,得到pb模型啦。

Model_output
- saved_model
  - saved_model.pb
- checkpoint
- frozen_inference_graph.pb     # Main model 
- model.ckpt.data-00000-of-00001
- model.ckpt.index
- model.ckpt.meta
- pipeline.config

保存前述data中的mscoco_label_map.pbtxt和本步驟中的frozen_inference_graph.pb,后續使用。

  • step7:使用訓練好的模型進行預測:
    使用如下腳本進行單幀圖片檢測:
# test.py
import numpy as np
import tensorflow as tf
import cv2 as cv

model_path = "/mnt/d/0-WORK/models/models-master/research/training/frozen_inference_graph.pb"
pbtxt_path = "/mnt/d/0-WORKmodels/models-master/research/object_detection/ssd_model/data/mscoco_label_map.pbtxt"
testimg = "/mnt/d/0-WORK/models/models-master/research/testing/114.jpg"

# Read the graph.
#with tf.compat.v1.gfile.FastGFile(model_path, 'rb') as f:
#    graph_def = tf.compat.v1.GraphDef()
#    graph_def.ParseFromString(f.read())

with tf.gfile.FastGFile(model_path, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())


with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

    # Read and preprocess an image.
    img = cv.imread(testimg)
    rows = img.shape[0]
    cols = img.shape[1]
    inp = cv.resize(img, (450, 450))
    inp = inp[:, :, [2, 1, 0]]  # BGR2RGB

    # Run the model
    out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                    sess.graph.get_tensor_by_name('detection_scores:0'),
                    sess.graph.get_tensor_by_name('detection_boxes:0'),
                    sess.graph.get_tensor_by_name('detection_classes:0')],
                   feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})

    # Visualize detected bounding boxes.
    num_detections = int(out[0][0])
    for i in range(num_detections):
        classId = int(out[3][0][i])
        

        score = float(out[1][0][i])
        bbox = [float(v) for v in out[2][0][i]]

        if score > 0.5:

            x = bbox[1] * cols
            y = bbox[0] * rows
            right = bbox[3] * cols
            bottom = bbox[2] * rows
            cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
            print(classId, "-->", score, x, y)
            
  
cv.imwrite('/mnt/d/0-WORK/models/models-master/research/testing/result_114.jpg', img)
cv.waitKey()

效果如下:


  • step7: 模型評測

附:voc轉tfrecord

參考博文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM