如何将 Byte[](解码为 PNG 或 JPG)转换为 Tensorflows Tensor [英] How to transform Byte[](decoded as PNG or JPG) to Tensorflows Tensor

查看:132
本文介绍了如何将 Byte[](解码为 PNG 或 JPG)转换为 Tensorflows Tensor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Unity 的项目中使用 Tensorflowsharp.

I'am trying to use Tensorflowsharp in a Project in Unity.

我面临的问题是,对于转换,您通常使用第二个 Graph 将输入转换为张量.Android 不支持使用的函数 DecodeJpg 和 DecodePng 那么如何将该输入转换为张量?

The problem i'm facing is that for the transform you usually use a second Graph to transform the input into a tensor. The used functions DecodeJpg and DecodePng are not supported on Android so how can you transform that input into a tensor ?

private static void ConstructGraphToNormalizeImage(out TFGraph graph, out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
{

    const int W = 224;
    const int H = 224;
    const float Mean = 117;
    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);
}

其他解决方案似乎会产生不准确的结果.

Other solutions seem to create non accurate results.

也许以某种方式使用 Mat 对象?

Maybe somehow with a Mat object?

和我的我在 Unity 中的 c# 中实现了一些比较,它部分工作.它根本不准确.我要如何找出均值?我找不到有关 rgb 订单的任何信息.?我对此很陌生,所以也许我只是忽略了它.(在 Tensorflow.org 上)使用在 1.4 中训练的 MobileNet.

and my I implemented something comparabel in c# in Unity and it works partially. It is just not accurate at all. How am i gonna find out the Mean? And i could not find anything about the rgb order.? I'm really new to this so maybe i have just overlooked it. (on Tensorflow.org) Using MobileNet trained in 1.4.

  public TFTensor transformInput(Color32[] pic, int texturewidth, int textureheight)
    {
        const int W = 224;
        const int H = 224;
        const float imageMean = 128;
        const float imageStd = 128;

        float[] floatValues = new float[texturewidth * textureheight * 3];

        for (int i = 0; i < pic.Length; ++i)
        {
            var color = pic[i];
            var index = i * 3;

            floatValues[index] = (color.r - imageMean) / imageStd;
            floatValues[index + 1] = (color.g - imageMean) / imageStd;
            floatValues[index + 2] = (color.b - imageMean) / imageStd;

        }
        TFShape shape = new TFShape(1, W, H, 3);
        return TFTensor.FromBuffer(shape, floatValues, 0, floatValues.Length);
    }

推荐答案

你可以输入实际的浮点数组,而不是先输入字节数组然后使用 DecodeJpeg,你可以这样得到:

Instead of feeding the byte array and then use DecodeJpeg, you could feed the actual float array, which you can get like this:

https://github.com/tensorflow/tensorflow/blob/3f4662e7ca8724f760db4a5ea6e241c99e66e588/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageClassifier.java#L134

float[] floatValues = new float[inputSize * inputSize * 3];
int[] intValues = new int[inputSize * inputSize];

bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
      final int val = intValues[i];
      floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - imageMean) / imageStd;
      floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd;
      floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd;
}

Tensor<Float> input = Tensors.create(floatValues);

要使用Tensors.create()",您至少需要 Tensorflow 1.4 版.

In order to use "Tensors.create()" you need to have at least Tensorflow version 1.4.

这篇关于如何将 Byte[](解码为 PNG 或 JPG)转换为 Tensorflows Tensor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆