【Atlas500】入門(九)——yolov3訓練自己的數據集/安全帽佩戴檢測

yolov3項目:https://github.com/wizyoung/YOLOv3_TensorFlow
數據集:安全帽佩戴數據集
感謝二位的工作。

yolov3訓練

1. voc數據格式轉換

  • 修改misc/parse_voc_xml.py中的相關參數

2. anchor參數設置

  • Using the kmeans algorithm to get the prior anchors:
    python get_kmeans.py

3. 開始訓練

  • 修改args.py中的相關參數
  • 訓練
    python train.py

4. 報錯

  • AssertionError: Annotation error! Please check your annotation file. Make sure there is at least one target object in each image.
    檢查發現train.txt中有圖片沒有目標。
    問題: 項目中對數據集做了diffcult過濾
    解決: 進行篩選,過濾這部分

5. 測試

  • 修改test_single_image.py中的參數
  • 修改預訓練的模型位置,注意不帶後綴。如下:
parser.add_argument("--restore_path", type=str, default="./checkpoint/best_model_Epoch_20_step_21167_mAP_0.6431_loss_13.9490_lr_0.0001",
                    help="The path of the weights to restore.")
  • 結果
    在這裏插入圖片描述

Atlas轉換

1. 轉pd模型

參考:https://bbs.huaweicloud.com/forum/thread-45383-1-1.html

import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.platform import gfile


def freeze_graph(input_path, output_path):
    output_node_names = "yolov3/yolov3_head/feature_map_1,yolov3/yolov3_head/feature_map_2,yolov3/yolov3_head/feature_map_3"
    saver = tf.train.import_meta_graph(input_path+".meta", clear_devices=True)
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    with tf.Session() as sess:
        saver.restore(sess, input_path)
        output_graph_def = graph_util.convert_variables_to_constants(
                           sess=sess,
                           input_graph_def=input_graph_def,   # = sess.graph_def,
                           output_node_names=output_node_names.split(","))

        with tf.gfile.GFile(output_path, 'wb') as fgraph:
            fgraph.write(output_graph_def.SerializeToString())

if __name__=="__main__":
    input_path = "./checkpoint/best_model_Epoch_20_step_21167_mAP_0.6431_loss_13.9490_lr_0.0001"
    output_path = "./checkpoint/yolov3_helmet.pb"
    freeze_graph(input_path, output_path)

2. 轉om模型

omg --framework 3 --model ./yolov3_helmet.pb --output yolov3_helmet --insert_op_conf ./aipp_yolov3_picture.cfg

  • 問題1
    [ERROR] FMK:2020-06-22-14:08:11.056.383 Parse:framework/domi/omg/…/omg/parser/tensorflow/tensorflow_parser.cpp:779:“Unsupport op type Iterator”
    The pre-checking report has been saved to check_result.json.
    [ERROR] FMK:2020-06-22-14:08:11.099.514 Generate:framework/domi/omg/omg.cpp:732:“OMG model parse ret fail. Error Code:0x3000004()”
    [ERROR] FMK:2020-06-22-14:08:11.102.277 main:framework/domi/omg_main/main.cpp:797:“OMG Generate execute failed!!”
    OMG generate offline model failed. Please see the log or pre-checking report for more details.
    [INFO] RUNTIME:2020-06-22-14:08:11.104.122 22840 runtime/feature/src/driver.cc:57 ~Driver:deconstruct driver

解決: 暫無
https://github.com/JesseYule/ObjectDetection-YOLOv3試一下。
猜想: 沒有換,想着這個問題,心裏就不開心,難受。

  • 項目模型訓練的輸入,是一個iterator,image_ids, image, y_true_13, y_true_26, y_true_52 = iterator.get_next()
  • https://github.com/JesseYule/ObjectDetection-YOLOv3項目中使用的是placeholder進行佔位的
  • 如我所想
  • 下面是tensorflow模型轉om模型的代碼
# coding: utf-8
# for more details about the yolo darknet weights file, refer to
# https://itnext.io/implementing-yolo-v3-in-tensorflow-tf-slim-c3c55ff59dbe

from __future__ import division, print_function

import os
import sys
import tensorflow as tf
import numpy as np

from model import yolov3
from utils.misc_utils import parse_anchors, load_weights, freeze_graph

num_class = 2      # 0. 類別和輸入尺寸和訓練時保持一直
img_size = 416
ckpt_path = "./checkpoint/best_model_Epoch_20_step_21167_mAP_0.6431_loss_13.9490_lr_0.0001"          # 1. 訓練得到的模型位置
save_path = './checkpoint/yolov3_helmet.pd'
anchors = parse_anchors('./data/my_data/helmet_anchors.txt')      # 2. 使用訓練時的anchors文件

model = yolov3(num_class, anchors)
with tf.Session() as sess:
    inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3])     # 3. 輸入用placeholder佔位

    with tf.variable_scope('yolov3'):
        feature_map = model.forward(inputs, is_training=False)

    saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))
    saver.restore(sess, ckpt_path)

    feature_map_1, feature_map_2, feature_map_3 = feature_map
    our_out_node_names = ["yolov3/yolov3_head/feature_map_1", "yolov3/yolov3_head/feature_map_2", "yolov3/yolov3_head/feature_map_3"]

    freeze_graph(sess, save_path, our_out_node_names)
    print('TensorFlow model checkpoint has been saved to {}'.format(save_path))
  • 轉om模型
    omg --framework 3 --model yolov3_helmet.pd --output yolov3_helmet --insert_op_conf aipp_yolov3_picture.cfg --input_shape "Placeholder:1,416,416,3"

2.1 atlas500測試

1. 修改源碼/編譯

  • 修改文件InferObjectDection/ObjectDetectionEngine.h

    • const int CLASS_NUM = 2 類別數量
    • static float g_biases[BIASES_NUM] = {5,5, 6,7, 7,9, 10,11, 13,15, 19,21, 27,31, 43,50, 79,93} 訓練用的anchor參數
  • 編譯

cd $InferObjectDection
bash ./build.sh A500

2. 掛載共享文件夾(可選)

mount -t nfs -o nolock -o tcp -o rsize=32768,wsize=32768 192.168.143.106:/home/yangna/deepblue/36_HUAWEI/2_Atlas/1_Atlas500/samples/Samples/InferObjectDetection ./dbface_out

3. 測試

./ObjectDetection -i 000009.jpg -t 2 -m ./yolov3_helmet.om -g ./graph.config -s 0 -e 0

4. 結果

***start device 0, end device 0***
device = 0, graphID = 1 init success
[mainG_MODEL]:g_modelType: 2
[INF] channel 0, frame 0 have 3 object
 #0, bbox( 247,    9,  361,  148) confidence: 0.978974 classId is 0 
 #1, bbox( 361,    0,  462,  130) confidence: 0.985772 classId is 1 
 #2, bbox(  57,   75,  158,  217) confidence: 0.898902 classId is 1 

[INFO] graphID:1 is over!
  • 可視化結果
    • tensorflow結果
      在這裏插入圖片描述
    • atlas結果
      在這裏插入圖片描述
  • atlas結果
    在這裏插入圖片描述
  • tensorflow結果
    在這裏插入圖片描述
    問題1 結果有點問題。
    測試的時候將t參數賦值爲2了。正確的命令:
    ./ObjectDetection -i 000009.jpg -t 1 -m ./yolov3_helmet.om -g ./graph.config -s 0 -e 0
    在這裏插入圖片描述
    問題: 輸出結果和tensorflow還是有一點兒差別。

猜想

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