关于tensorflow.net推理mnist图片问题
using System;
using Tensorflow;
using Tensorflow.Lite;
using OpenCvSharp;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
public static unsafe void test()
{
var modelPath = "mnist.tflite";
var imagePath = "1.jpg";
var model = c_api_lite.TfLiteModelCreateFromFile(modelPath);
var options = c_api_lite.TfLiteInterpreterOptionsCreate();
var interpreter = c_api_lite.TfLiteInterpreterCreate(model, options);
TfLiteStatus status = c_api_lite.TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor inputTensor = c_api_lite.TfLiteInterpreterGetInputTensor(interpreter, 0);
Mat img = Cv2.ImRead(imagePath, ImreadModes.Grayscale);
var bytes = new byte[img.Total() * 1];
Marshal.Copy(img.Data, bytes, 0, bytes.Length);
fixed (byte* addr = &bytes[0])
{
c_api_lite.TfLiteTensorCopyFromBuffer(inputTensor, new IntPtr(addr), img.Cols*img.Rows*img.Channels() * sizeof(byte));
}
c_api_lite.TfLiteInterpreterInvoke(interpreter);
var output_tensor = c_api_lite.TfLiteInterpreterGetOutputTensor(interpreter, 0);
var output = new byte[10];
fixed (byte* addr = &output[0])
{
c_api_lite.TfLiteTensorCopyToBuffer(output_tensor, new IntPtr(addr), 10 * sizeof(byte));
}
for (int i = 0; i< output.Length;i++)
{
Console.WriteLine(output[i]);
}
c_api_lite.TfLiteInterpreterDelete(interpreter.DangerousGetHandle());
}
static void Main(string[] args)
{
test();
}
}
}
推理结果为什么为零啊
