如何使用 TensorFlowSharp 从 Tensor 编码 .JPG 文件?

How to encode .JPG file from a Tensor with TensorFlowSharp?

我想用 TensorFlowSharp 从张量中保存 .JPG 图像文件。我尝试 GetValue() 获取张量的值,但我总是遇到一个问题 InvalidCastException: Specified cast is not valid.

unity中显示的问题如下:

InvalidCastException: Specified cast is not valid. TensorFlowModel.CallTFmodel.CallTFmodel_Start (System.Byte[] originalbytes, System.String SendPathName) (at Assets/Sample/Glass/Scripts/CallTFmodel.cs:84) Camera2picture+d__9.MoveNext () (at Assets/Sample/Glass/Scripts/Camera2picture.cs:71) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

var runner = session.GetRunner();

// "input", "ps";
runner.AddInput(graph["images"][0], tensorNormalized).Fetch(graph["Tanh"][0]); 
var output = runner.Run();
var result = output[0];

// the issue is happened here. 
byte[] result_bytes = (byte[])result.GetValue(jagged:true); 
// my target is to use GetValue() to transform tensor to array with type byte.
// this line is to write the byte array to a image file.
File.WriteAllBytes(SendPathName, result_bytes); 

我的目标是找到一种使用 TensorFlowSharp 将张量保存为 .JPG 图像文件的方法,但不幸的是我尝试了很多次都找不到解决方案。

在 TensorflowSharp 中你可以这样做:

    private byte[] TensorToBitmap(TFTensor image)
    {
        var graph = new TFGraph();
        var input = graph.Placeholder(TFDataType.Float);
        var output = graph.Cast(input, TFDataType.UInt8);
        output = graph.EncodeJpeg(output);

        using (var session = new TFSession(graph))
        {
            var result = session.Run(
                inputs: new[] { input },
                inputValues: new[] { image },
                outputs: new[] { output });

            var tensor = result[0];
            byte[] buffer = new byte[(int)tensor.TensorByteSize - 10];
            System.Runtime.InteropServices.Marshal.Copy(tensor.Data + 10, buffer, 0, buffer.Length);
            return buffer;
        }
    }

我不知道为什么,但你必须跳过前 10 个字节。