C#中的张量流再训练图(Tensorflowsharp)

时间:2018-01-02 18:22:15

标签: tensorflow

我只是想在Unity中的Tensorflow中使用重新训练的初始模型。 重新训练的模型是使用optimize_for_inference准备的,并且像python中的魅力一样工作。 但它在c#中非常不准确。

代码的工作方式如下: 首先我得到图片

//webcamtexture transformed to picture in jpg
var pic = _texture.EncodeToJpg();
//added Picture to queue for the object detection thread
_detectedObjects.addTens(pic);

之后,线程将处理每个收集的图片

public void HandlePicture(byte[] picture)
{
    var tensor = ImageUtil.CreateTensorFromImageFile(picture);
        var runner = session.GetRunner();

        runner.AddInput(g_input, tensor).Fetch(g_output);
        var output = runner.Run();

        var bestIdx = 0;
        float best = 0;
        var result = output[0];
        var rshape = result.Shape;
        var probabilities = ((float[][])result.GetValue(jagged: true))[0];
        for (int r = 0; r < probabilities.Length; r++)
        {
            if (probabilities[r] > best)
            {
                bestIdx = r;
                best = probabilities[r];
            }
        }
        Debug.Log("Tensorflow thinks this is: " + labels[bestIdx] + " Prob : " + best * 100);
}

所以我的猜测是:

1.它与重新训练的图形有关(因为我找不到任何应用/测试它被使用和工作)。

2.它与我如何处理图片转换成张量有关?! (但如果那是错的,我可能需要帮助,代码进一步向下)

转换图片我也使用像tensorsharp示例中使用的图表

public static class ImageUtil
{
    // Convert the image in filename to a Tensor suitable as input to the Inception model.
    public static TFTensor CreateTensorFromImageFile(byte[] contents, TFDataType destinationDataType = TFDataType.Float)
    {
        // DecodeJpeg uses a scalar String-valued tensor as input.
        var tensor = TFTensor.CreateString(contents);

        TFGraph graph;
        TFOutput input, output;

        // Construct a graph to normalize the image
        ConstructGraphToNormalizeImage(out graph, out input, out output, destinationDataType);

        // Execute that graph to normalize this one image
        using (var session = new TFSession(graph))
        {
            var normalized = session.Run(
                     inputs: new[] { input },
                     inputValues: new[] { tensor },
                     outputs: new[] { output });

            return normalized[0];
        }
    }

    // The inception model takes as input the image described by a Tensor in a very
    // specific normalized format (a particular image size, shape of the input tensor,
    // normalized pixel values etc.).
    //
    // This function constructs a graph of TensorFlow operations which takes as
    // input a JPEG-encoded string and returns a tensor suitable as input to the
    // inception model.
    private static void ConstructGraphToNormalizeImage(out TFGraph graph, out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
    {
        // Some constants specific to the pre-trained model at:
        // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
        //
        // - The model was trained after with images scaled to 224x224 pixels.
        // - The colors, represented as R, G, B in 1-byte each were converted to
        //   float using (value - Mean)/Scale.

        const int W = 299;
        const int H = 299;
        const float Mean = 128;
        const float Scale = 1;
        graph = new TFGraph();


        input = graph.Placeholder(TFDataType.String);

        output = graph.Cast(graph.Div(
            x: graph.Sub(
                x: graph.ResizeBilinear(
                    images: graph.ExpandDims(
                        input: graph.Cast(
                            graph.DecodeJpeg(contents: input, channels: 3), DstT: TFDataType.Float),
                        dim: graph.Const(0, "make_batch")),
                    size: graph.Const(new int[] { W, H }, "size")),
                y: graph.Const(Mean, "mean")),
            y: graph.Const(Scale, "scale")), destinationDataType);
    }
}

0 个答案:

没有答案
相关问题