TensorFloat .netcoreCustomVision出现问题

问题描述

好吧,我一直在进行AI和对象检测方面的工作,最近我从CustomVision导出的模型遇到了一些问题。 对于那些知道我在说什么的人,当您从CustomVision导出模型时,会得到一个包含的.cs文件,该文件代表使用该模型所需的所有内容的类。 这开始了我的所有问题。.第一个也是最重要的一个问题是,在其中一种方法中,特别是在“ ExtractBoxes”方法中,该方法接收TensorFloat对象和锚点的float数组。 无论如何..在此方法中,有四个名为“ channels”,“ height”和“ width”的变量来自TensorFloat对象内部名为“ shape”的列表。 鉴于所有这些..我的问题在于TensorFloat对象,更具体地说,是在没有TensorFloat对象的情况下如何获取变量“ channels”,“ height”和“ width”的值。

下面,我将包含我正在谈论的.cs文件中的代码。 预先感谢!

public async Task<IList<PredictionModel>> PredictimageAsync(VideoFrame image)
{
    var imageWidth = image.softwareBitmap.PixelWidth;
    var imageHeight = image.softwareBitmap.PixelHeight;

    double ratio = Math.Sqrt((double)imageInputSize / (double)imageWidth / (double)imageHeight);
    int targetWidth = 32 * (int)Math.Round(imageWidth * ratio / 32);
    int targetHeight = 32 * (int)Math.Round(imageHeight * ratio / 32);

    using (var resizedBitmap = await ResizeBitmap(image.softwareBitmap,targetWidth,targetHeight))
    using (VideoFrame resizedVideoFrame = VideoFrame.CreateWithSoftwareBitmap(resizedBitmap))
    {
        var imageFeature = ImageFeatureValue.CreateFromVideoFrame(resizedVideoFrame);
        var bindings = new LearningModelBinding(this.session);
        bindings.Bind("input",imageFeature);

        var result = await this.session.EvaluateAsync(bindings,"");

        return Postprocess(result.Outputs["output"] as TensorFloat);
    }
}

private List<PredictionModel> Postprocess(TensorFloat predictionOutputs)
{
    var extractedBoxes = this.ExtractBoxes(predictionOutputs,ObjectDetection.Anchors);
    return this.SuppressNonMaximum(extractedBoxes);
}

private ExtractedBoxes ExtractBoxes(TensorFloat predictionOutput,float[] anchors)
{
            var shape = predictionOutput.Shape;
            Debug.Assert(shape.Count == 4,"The model output has unexpected shape");
            Debug.Assert(shape[0] == 1,"The batch size must be 1");

            IReadOnlyList<float> outputs = predictionOutput.GetAsvectorView();

            var numAnchor = anchors.Length / 2;
            var channels = shape[1];
            var height = shape[2];
            var width = shape[3];

            Debug.Assert(channels % numAnchor == 0);
            var numClass = (channels / numAnchor) - 5;

            Debug.Assert(numClass == this.labels.Count);

            var Boxes = new List<BoundingBox>();
            var probs = new List<float[]>();
            for (int gridY = 0; gridY < height; gridY++)
            {
                for (int gridX = 0; gridX < width; gridX++)
                {
                    int offset = 0;
                    int stride = (int)(height * width);
                    int baSEOffset = gridX + gridY * (int)width;

                    for (int i = 0; i < numAnchor; i++)
                    {
                        var x = (Logistic(outputs[baSEOffset + (offset++ * stride)]) + gridX) / width;
                        var y = (Logistic(outputs[baSEOffset + (offset++ * stride)]) + gridY) / height;
                        var w = (float)Math.Exp(outputs[baSEOffset + (offset++ * stride)]) * anchors[i * 2] / width;
                        var h = (float)Math.Exp(outputs[baSEOffset + (offset++ * stride)]) * anchors[i * 2 + 1] / height;

                        x = x - (w / 2);
                        y = y - (h / 2);

                        var objectness = Logistic(outputs[baSEOffset + (offset++ * stride)]);

                        var classprobabilities = new float[numClass];
                        for (int j = 0; j < numClass; j++)
                        {
                            classprobabilities[j] = outputs[baSEOffset + (offset++ * stride)];
                        }
                        var max = classprobabilities.Max();
                        for (int j = 0; j < numClass; j++)
                        {
                            classprobabilities[j] = (float)Math.Exp(classprobabilities[j] - max);
                        }
                        var sum = classprobabilities.Sum();
                        for (int j = 0; j < numClass; j++)
                        {
                            classprobabilities[j] *= objectness / sum;
                        }

                        if (classprobabilities.Max() > this.probabilityThreshold)
                        {
                            Boxes.Add(new BoundingBox(x,y,w,h));
                            probs.Add(classprobabilities);
                        }
                    }
                    Debug.Assert(offset == channels);
                }
            }

            Debug.Assert(Boxes.Count == probs.Count);
            return new ExtractedBoxes(Boxes,probs);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)