Exemplo n.º 1
0
        public async Task Test(string dataset, string filename)
        {
            switch (dataset)
            {
            case "MNIST":
            case "mnist":
                dataFileName  = @"C:\files\data\mnist\train-images.idx3-ubyte";
                labelFileName = @"C:\files\data\mnist\train-labels.idx1-ubyte";
                model         = modelSerializer.DeserializeCNN(filename);
                List <AI.Core.fData> dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadGrayImage(dataFileName, 1, 0.0, 1.0, labelFileName, 0.0, 1.0);
                index         = random.Next(0, dataList.Count);
                dataSet.fData = dataList;
                pixel         = dataList[index].Pixel;
                label         = dataList[index].DecodeLabel;
                //model.predict
                probs         = model.Predict(dataSet.fData[index]);
                softmaxOutput = adam.Softmax(probs);
                maxValue      = softmaxOutput.Max();
                predict       = softmaxOutput.ToList().IndexOf(maxValue);
                //int lindex = random.Next(0, 5);
                //predict = lindex == 2 ? random.Next(0, 9) : label;
                result[0] = label;
                result[1] = predict;
                await Clients.All.SendAsync("ReceiveMessage", pixel);

                await Clients.All.SendAsync("ReceiveLabel", result);

                break;

            default:
                dataSet = new AI.Core.fDataSet();
                dataSet.Deserializer(dataset);
                model = modelSerializer.DeserializeCNN(filename);
                index = random.Next(0, dataSet.fData.Count);
                pixel = dataSet.fData[index].Pixel;
                label = dataSet.fData[index].DecodeLabel;
                //model.predict
                probs         = model.Predict(dataSet.fData[index]);
                softmaxOutput = adam.Softmax(probs);
                maxValue      = softmaxOutput.Max();
                predict       = softmaxOutput.ToList().IndexOf(maxValue);
                //int lindex = random.Next(0, 5);
                //predict = lindex == 2 ? random.Next(0, 9) : label;
                result[0] = label;
                result[1] = predict;
                await Clients.All.SendAsync("ReceiveMessage", pixel);

                await Clients.All.SendAsync("ReceiveLabel", result);

                break;
            }
        }
Exemplo n.º 2
0
        public async Task Test(string dataset, string filename)
        {
            ModelSerializer modelSerializer = new ModelSerializer();

            model = modelSerializer.DeserializeCNN(filename);

            fDataSet testdataset = new fDataSet();

            testdataset.Deserializer(dataset);

            int index = random.Next(0, testdataset.fData.Count);

            AI.Core.fDataSet dataSet = new AI.Core.fDataSet();
            dataSet.fData = testdataset.fData;
            pixel         = testdataset.fData[index].Pixel;
            label         = testdataset.fData[index].DecodeLabel;

            //model.predict
            probs = model.Predict(dataSet.fData[index]);
            AI.ML.CNN.Trainers.ADAM adam = new AI.ML.CNN.Trainers.ADAM();
            softmaxOutput = adam.Softmax(probs);
            double maxValue = softmaxOutput.Max();

            predict = softmaxOutput.ToList().IndexOf(maxValue);

            //int lindex = random.Next(0, 5);
            //predict = lindex == 2 ? random.Next(0, 9) : label;
            result[0] = label;
            result[1] = predict;
            await Clients.All.SendAsync("ReceiveMessage", pixel);

            await Clients.All.SendAsync("ReceiveLabel", result);
        }
Exemplo n.º 3
0
        public async Task Test(string dataset, string filename)
        {
            switch (dataset)
            {
            case "MNIST":
            case "mnist":
                dataFileName  = @"C:\files\data\mnist\train-images.idx3-ubyte";
                labelFileName = @"C:\files\data\mnist\train-labels.idx1-ubyte";
                model         = modelSerializer.DeserializeCNN(filename);
                dataList      = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadGrayImage(dataFileName, 1, 0.0, 1.0, labelFileName, 0.0, 1.0);
                index         = random.Next(0, dataList.Count);
                dataSet.fData = dataList;
                pixel         = dataList[index].Pixel;
                label         = dataList[index].DecodeLabel;
                //model.predict
                probs         = model.Predict(dataSet.fData[index]);
                softmaxOutput = adam.Softmax(probs);
                maxValue      = softmaxOutput.Max();
                predict       = softmaxOutput.ToList().IndexOf(maxValue);
                size          = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length);
                matImg        = new Mat(size, size, DepthType.Cv8U, 3);
                Gimg          = matImg.ToImage <Gray, Byte>();
                Gimage        = setGrayPixels(Gimg, size, pixel);
                input_size    = new int[] { 256, 256 };
                resizedPixel  = Resize_Gray(Gimage, input_size);
                //int lindex = random.Next(0, 5);
                //predict = lindex == 2 ? random.Next(0, 9) : label;
                result[0] = label;
                result[1] = predict;
                await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 1);

                await Clients.All.SendAsync("ReceiveLabel", result);

                break;

            case "CIFAR":
            case "cifar":
                dataFileName  = @"C:\files\data\cifar\cifar-10-batches-bin\data_batch_1.bin";
                model         = modelSerializer.DeserializeCNN(filename);
                dataList      = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadColorImage(dataFileName, 1, 0.0, 1.0, 0.0, 1.0);
                index         = random.Next(0, dataList.Count);
                dataSet.fData = dataList;
                pixel         = dataSet.fData[index].Pixel;
                label         = dataSet.fData[index].DecodeLabel;
                //model.predict
                probs         = model.Predict(dataSet.fData[index]);
                softmaxOutput = adam.Softmax(probs);
                maxValue      = softmaxOutput.Max();
                predict       = softmaxOutput.ToList().IndexOf(maxValue);
                size          = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length / 3);
                matImg        = new Mat(size, size, DepthType.Cv8U, 3);
                Cimg          = matImg.ToImage <Bgr, Byte>();
                Cimage        = setColorPixels(Cimg, size, pixel);
                input_size    = new int[] { 256, 256 };
                resizedPixel  = Resize_Color(Cimage, input_size);
                //int lindex = random.Next(0, 5);
                //predict = lindex == 2 ? random.Next(0, 9) : label;
                cifarResult[0] = classes[label];
                cifarResult[1] = classes[predict];
                await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 3);

                await Clients.All.SendAsync("ReceiveLabel", cifarResult);

                break;

            default:
                dataSet = new AI.Core.fDataSet();
                dataSet.Deserializer(dataset);
                pixel = dataSet.fData[index].Pixel;
                model = modelSerializer.DeserializeCNN(filename);
                index = random.Next(0, dataSet.fData.Count);
                label = dataSet.fData[index].DecodeLabel;
                //model.predict
                probs         = model.Predict(dataSet.fData[index]);
                softmaxOutput = adam.Softmax(probs);
                maxValue      = softmaxOutput.Max();
                predict       = softmaxOutput.ToList().IndexOf(maxValue);
                //int lindex = random.Next(0, 5);
                //predict = lindex == 2 ? random.Next(0, 9) : label;
                result[0] = label;
                result[1] = predict;
                if (model.Input.Output.Count == 3)
                {
                    size           = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length / 3);
                    matImg         = new Mat(size, size, DepthType.Cv8U, 3);
                    Cimg           = matImg.ToImage <Bgr, Byte>();
                    Cimage         = setColorPixels(Cimg, size, pixel);
                    input_size     = new int[] { 256, 256 };
                    resizedPixel   = Resize_Color(Cimage, input_size);
                    cifarResult[0] = classes[label];
                    cifarResult[1] = classes[predict];
                    await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 3);

                    await Clients.All.SendAsync("ReceiveLabel", cifarResult);
                }
                else
                {
                    size         = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length);
                    matImg       = new Mat(size, size, DepthType.Cv8U, 3);
                    Gimg         = matImg.ToImage <Gray, Byte>();
                    Gimage       = setGrayPixels(Gimg, size, pixel);
                    input_size   = new int[] { 256, 256 };
                    resizedPixel = Resize_Gray(Gimage, input_size);
                    //int lindex = random.Next(0, 5);
                    //predict = lindex == 2 ? random.Next(0, 9) : label;
                    result[0] = label;
                    result[1] = predict;
                    await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 1);

                    await Clients.All.SendAsync("ReceiveLabel", result);
                }
                break;
            }
        }