转换为Tensorflows Tensor,第1张 c# – 如何将Byte [](解码为PNG或JPG)转换为Tensorflows Tensor,第1张](/aiimages/c%23+%E2%80%93+%E5%A6%82%E4%BD%95%E5%B0%86Byte+%5B%5D%28%E8%A7%A3%E7%A0%81%E4%B8%BAPNG%E6%88%96JPG%29%E8%BD%AC%E6%8D%A2%E4%B8%BATensorflows+Tensor.png)
我试图在Unity的项目中使用Tensorflowsharp.
我面临的问题是,对于变换,您通常使用第二个Graph将输入转换为张量.
Android上不支持使用的函数DecodeJpg和DecodePng,那么如何将输入转换为张量?
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对象?
和我的编辑:
我在Unity中用c#实现了一些比较,它可以部分工作.它完全不准确.我怎么会发现平均值?而且我找不到关于rgb命令的任何信息.我真的很新,所以也许我只是忽略了它. (在Tensorflow.org上)使用1.4中受过训练的MobileNet.
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,您可以提供实际的浮点数组,您可以这样:
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.
总结以上是内存溢出为你收集整理的c# – 如何将Byte [](解码为PNG或JPG)转换为Tensorflows Tensor全部内容,希望文章能够帮你解决c# – 如何将Byte [](解码为PNG或JPG)转换为Tensorflows Tensor所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)