首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Byte[](解码为PNG或JPG)转换为Tensorflow张量

如何将Byte[](解码为PNG或JPG)转换为Tensorflow张量
EN

Stack Overflow用户
提问于 2018-01-05 16:36:51
回答 2查看 4.1K关注 0票数 3

我在联合的一个项目中尝试使用Tensorflowsharp。

我面临的问题是,对于转换,通常使用第二张图将输入转换为张量。安卓系统不支持使用的函数DecodeJpg和DecodePng,那么如何将输入转换为张量呢?

代码语言:javascript
复制
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);
}

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

也许是带着个Mat对象?

我的编辑:我在c#中实现了一个比较器,它部分地工作。只是一点都不准确。我要怎么找出那个卑鄙的人?我找不到任何关于rgb的命令。?我对这件事很陌生,所以我可能只是忽略了这一点。(关于Tensorflow.org)使用MobileNet进行了1.4级的培训。

代码语言:javascript
复制
  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);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-06 16:41:55

您可以输入实际的浮点数数组,而不是输入字节数组,然后使用DecodeJpeg,如下所示:

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

代码语言:javascript
复制
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。

票数 5
EN

Stack Overflow用户

发布于 2018-01-28 17:45:36

在将图像放入@sladomic函数之前,您可能没有裁剪和缩放图像。

我设法破解了一个用于对象分类的TensorflowSharp在统一中的应用实例。它适用于官方Tensorflow Android示例中的模型,也适用于我自己培训过的MobileNet模型。您所需要的只是替换模型并设置平均值和std,在我的例子中,它们都等于224。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48117704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档