Example #1
0
        static void Main(string[] args)
        {
            var modelPath = "models/resnet50.model";
            var width     = 224;

            var tensor = JpegToTensor(GetDataPath("datasets/towers/et1.jpg"), width, width);

            var model = Sequential.Load(GetDataPath(modelPath));

            var prediction1 = model.Predict(tensor).Squeeze();

            tensor = JpegToTensor(GetDataPath("datasets/towers/et2.jpg"), width, width);

            var prediction2 = model.Predict(tensor).Squeeze();

            Console.WriteLine($" Distance to picture of the same object: {TensorUtils.EuclideanDistance(prediction1, prediction2).ToString("F2")}");

            tensor = JpegToTensor(GetDataPath("datasets/towers/et3.jpg"), width, width);

            prediction2 = model.Predict(tensor).Squeeze();
            Console.WriteLine($" Distance to picture of the same object: {TensorUtils.EuclideanDistance(prediction1, prediction2).ToString("F2")}");

            tensor = JpegToTensor(GetDataPath("datasets/towers/pt1.jpg"), width, width);

            prediction2 = model.Predict(tensor).Squeeze();
            Console.WriteLine($" Ddistance to picture of a different object: {TensorUtils.EuclideanDistance(prediction1, prediction2).ToString("F2")}");
        }
Example #2
0
        static void PredictMnist(string modelPath, string xtestPath, string ytestPath = null)
        {
            var xtest = TensorUtils.Deserialize(File.OpenRead(xtestPath));

            xtest = xtest.Cast(DType.Float32);
            xtest = Ops.Div(null, xtest, 255f);

            // xtest = xtest.Narrow(0, 0, 101);

            var model = Sequential.Load(modelPath);

            var result = model.Predict(xtest, batchSize: 32);

            if (ytestPath == null)
            {
                return;
            }

            var ytest = TensorUtils.Deserialize(File.OpenRead(ytestPath));

            ytest = ytest.Cast(DType.Float32);
            // ytest = ytest.Narrow(0, 0, 101);
            ytest = Ops.Argmax(null, ytest, 1).Squeeze();

            var t = result.Narrow(0, 0, 11);

            // Console.WriteLine(t.Format());

            result = Ops.Argmax(null, result, 1).Squeeze();

            t = result.Narrow(0, 0, 11);
            // Console.WriteLine(t.Format());

            double sum = 0.0;

            for (var i = 0; i < ytest.Sizes[0]; ++i)
            {
                sum += (int)ytest.GetElementAsFloat(i) == (int)result.GetElementAsFloat(i) ? 1.0 : 0.0;
            }

            Console.WriteLine($"Accuracy: {sum / ytest.Sizes[0] * 100}%");
        }