【发布时间】:2018-06-15 12:11:38
【问题描述】:
我正在尝试在 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);
}
【问题讨论】:
-
您好,您能提供更多关于网络的信息吗?你是自己训练的还是使用预训练的网络?我在 tensorflow.org 上没有看到任何预训练的 MobilNets。
-
这是一个经过再训练的网络,使用他们的脚本与花本质上(更多的图片和“无”类别来否定 FP)。在 Python 中,这件事就像一个魅力。当我使用该脚本在 c# 中测试相同的图片时,它是不准确的(在 python 之外的任何脚本中都不准确)。它具有 1.0 224 架构。所有 TensorFlow 实例似乎都是 1.4 的某个版本。
标签: c# android opencv tensorflow tensorflowsharp