【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的问题

猜你喜欢

转载自blog.csdn.net/u011622208/article/details/106896235