简体   繁体   English

Python 到 ML.NET 图像中的自定义对象检测

[英]Python to ML.NET Custom Object Detection in image

I trained a custom model that can find custom objects in the image.我训练了一个自定义模型,可以在图像中找到自定义对象。 I used a terrific article我用了一篇很棒的文章

Many thanks to Evan EdjeElectronics .非常感谢Evan EdjeElectronics

This python code works fine:这个python代码工作正常:

    import cv2
    import numpy as np
    import tensorflow as tf       
    
    PATH_TO_CKPT = os.path.join(CWD_PATH, 'model.pb')
       
    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')
    
    # Path to image
    PATH_TO_IMAGE = "D:\\documents\\_1.jpg"
    
    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
    
        sess = tf.Session(graph=detection_graph)
    
# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a , whersingle-column arraye each item in the column has the pixel RGB value
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0)

# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: image_expanded})

Now i am trying to use my tensorflow model in ML.NET现在我正在尝试在 ML.NET 中使用我的 tensorflow 模型

// For checking tensor names, you can open the TF model .pb file with tools like Netron: https://github.com/lutzroeder/netron
public struct TensorFlowModelSettings
{
    // Input tensor name.
    public const string inputTensorName = "image_tensor:0";
    // Output tensor name.
    public const string outputTensorName = "detection_boxes:0";
}

/// <summary>
/// Setup ML.NET model  by tensorFlow .pb model file
/// </summary>
/// <param name="tensorFlowModelFilePath">Full path for .pb model file</param>
private ITransformer SetupMlnetModel(string tensorFlowModelFilePath)
{
    var pipeline = _mlContext.Transforms
        .ResizeImages(
            outputColumnName: TensorFlowModelSettings.inputTensorName, 
            imageWidth: ImageSettings.imageWidth, 
            imageHeight: ImageSettings.imageHeight, 
            inputColumnName: nameof(ImageInputData.Image))
        .Append(_mlContext.Transforms.ExtractPixels(
            outputColumnName: TensorFlowModelSettings.inputTensorName, 
            interleavePixelColors: ImageSettings.channelsLast, 
            offsetImage: ImageSettings.mean))
        .Append(_mlContext.Model.LoadTensorFlowModel(tensorFlowModelFilePath)
            .ScoreTensorFlowModel(outputColumnNames: new[]                                      { TensorFlowModelSettings.outputTensorName },
                                 inputColumnNames: new[] { TensorFlowModelSettings.inputTensorName }, 
                                 addBatchDimensionInput: false));

    ITransformer mlModel = pipeline.Fit(CreateEmptyDataView());

    return mlModel;
}

Using the instruction step by step - I get an error when I call method pipeline.Fit :逐步使用说明- 调用方法pipeline.Fit时出现错误:

System.ArgumentOutOfRangeException: 'Schema mismatch for input column 'image_tensor:0': expected Byte, got Single Arg_ParamName_Name System.ArgumentOutOfRangeException: '输入列 'image_tensor:0' 的架构不匹配:预期字节,得到单个 Arg_ParamName_Name

在此处输入图片说明

Link to image with code使用代码链接到图像

I solved the problem with TensorFlowSharp我用TensorFlowSharp解决了这个问题

using (var graph = new TFGraph ()) {
    var model = File.ReadAllBytes (modelFile);
    graph.Import (new TFBuffer (model));

    using (var session = new TFSession (graph)) {
        Console.WriteLine("Detecting objects");

        foreach (var tuple in fileTuples) {
            //var tensor = ImageUtil.CreateTensorFromImageFile (tuple.input, TFDataType.UInt8);
            var tensor = ImageUtil.CreateTensorFromImageFileAlt (tuple.input, TFDataType.UInt8);
            var runner = session.GetRunner ();


            runner
                .AddInput (graph ["image_tensor"] [0], tensor)
                .Fetch (
                graph ["detection_boxes"] [0],
                graph ["detection_scores"] [0],
                graph ["detection_classes"] [0],
                graph ["num_detections"] [0]);
            var output = runner.Run ();

            var boxes = (float [,,])output [0].GetValue (jagged: false);
            var scores = (float [,])output [1].GetValue (jagged: false);
            var classes = (float [,])output [2].GetValue (jagged: false);
            var num = (float [])output [3].GetValue (jagged: false);

            DrawBoxes (boxes, scores, classes, tuple.input, tuple.output, MIN_SCORE_FOR_OBJECT_HIGHLIGHTING);
            Console.WriteLine($"Done. See {_output_relative}");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM