C#中用TensorFlow读取模型

读取模型

flatBufferModel = new FlatBufferModel("detect.tflite");
interpreter = new Interpreter(flatBufferModel);
interpreter.AllocateTensors();
int[] input = interpreter.InputIndices;
//读取模型时要通过其他语言提前得到tensor在哪里
inputTensor = interpreter.GetTensor(260);
outputTensors = interpreter.Outputs;

传入图像 并得到输出

// An highlighted block
Mat ddmat = new Mat();
//ddmat = dmat.Clone();
CvInvoke.Resize(dmat, ddmat, new Size(300,300));
Image<Bgr, Byte> image2 = ddmat.ToImage<Bgr, Byte>();
if (ddmat.Depth != Emgu.CV.CvEnum.DepthType.Cv32F)
{
 ddmat.ConvertTo(ddmat, Emgu.CV.CvEnum.DepthType.Cv32F);
 ddmat /= 255;
}
ReadTensorFromMatBgr(
image: ddmat,
tensor: inputTensor,
inputHeight: 300,
inputWidth: 300
);
interpreter.Invoke();

Tensor boxes;
Tensor classes;
Tensor scores;

try
{
boxes = outputTensors[0];
classes = outputTensors[1];
scores = outputTensors[2];
}

图像传入后的处理函数

public static void ReadTensorFromMatBgr(Mat image, Tensor tensor, int inputHeight = -1, int inputWidth = -1, float inputMean = 0.0f, float scale = 1.0f)
            {
                //if (image.NumberOfChannels != 3)
                //{
                //    throw new ArgumentException("Input must be 3 channel BGR image");
                //}

                Emgu.CV.CvEnum.DepthType depth = image.Depth;
                if (!(depth == Emgu.CV.CvEnum.DepthType.Cv8U || depth == Emgu.CV.CvEnum.DepthType.Cv32F))
                {
                    throw new ArgumentException("Input image must be 8U or 32F");
                }

                //resize
                int finalHeight = inputHeight == -1 ? image.Height : inputHeight;
                int finalWidth = inputWidth == -1 ? image.Width : inputWidth;
                Size finalSize = new Size(finalWidth, finalHeight);

                //int[] dim = tensor.Dims;
                //if (dim[0] != 1)
                //    throw new ArgumentException("First dimension of the tensor must be 1 (batch size)");

                //if (dim[1] != finalHeight)
                //    throw new ArgumentException("Second dimension of the tensor must match the input height");

                //if (dim[2] != finalWidth)
                //    throw new ArgumentException("Third dimension of the tensor must match the input width");

                //if (dim[3] != 3)
                //    throw new ArgumentException("Fourth dimension of the tensor must be 3 (BGR has 3 channels)");

                if (image.Size != finalSize)
                {
                    using (Mat tmp = new Mat())
                    {
                        CvInvoke.Resize(image, tmp, finalSize);
                        ReadTensorFromMatBgr(tmp, inputMean, scale, tensor);
                        tmp.Dispose();
                    }
                }
                else
                {
                    ReadTensorFromMatBgr(image, inputMean, scale, tensor);
                }
            }
            private static void ReadTensorFromMatBgr(Mat image, float inputMean, float scale, Tensor t)
            {
                DataType type = t.Type;
                if (type == DataType.Float32)
                {
                    using (Mat matF = new Mat(image.Size, Emgu.CV.CvEnum.DepthType.Cv32F, 3, t.DataPointer, sizeof(float) * 3 * image.Width))
                    {
                        image.ConvertTo(matF, Emgu.CV.CvEnum.DepthType.Cv32F);
                    }
                }
                else if (type == DataType.UInt8)
                {
                    using (Mat matB = new Mat(image.Size, Emgu.CV.CvEnum.DepthType.Cv8U, 3, t.DataPointer, sizeof(byte) * 3 * image.Width))
                    {
                        if (scale == 1.0f && inputMean == 0)
                        {
                            image.CopyTo(matB);
                        }
                        else
                            CvInvoke.ConvertScaleAbs(image, matB, scale, -inputMean * scale);
                    }
                }
                else
                {
                    throw new Exception(String.Format("Data Type of {0} is not supported.", type));
                }
            }

        }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值