Beispiel #1
0
 public async Task TrainNeuralNetwork()
 {
     BloodDonatonNeuralNet net = new BloodDonatonNeuralNet();
     await Task.Run(() =>
     {
         try
         {
             Init();
         }
         catch (Exception ex)
         {
         }
     });
 }
Beispiel #2
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.MainMenu);
            // Create your application here

            textViewLastBloodPicture = FindViewById <TextView>(Resource.Id.textViewLastBloodPicture);
            textViewAnalyzeName      = FindViewById <TextView>(Resource.Id.textViewAnalyzeName);
            textViewAnalyzeResults   = FindViewById <TextView>(Resource.Id.textViewAnalyzeResults);
            textViewShouldDo         = FindViewById <TextView>(Resource.Id.textViewShouldDo);

            textViewShouldDo1 = FindViewById <TextView>(Resource.Id.textViewShouldDo1);
            textViewShouldDo2 = FindViewById <TextView>(Resource.Id.textViewShouldDo2);

            imageViewShuldDo1 = FindViewById <ImageView>(Resource.Id.imageViewShuldDo1);
            imageViewShuldDo2 = FindViewById <ImageView>(Resource.Id.imageViewShuldDo2);

            buttonGetLastBP        = FindViewById <Button>(Resource.Id.buttonGetLastBP);
            buttonGetLastBP.Click += ButtonGetLastBP_Click;
            buttonAnalyzeBP        = FindViewById <Button>(Resource.Id.buttonAnalyzeBP);
            buttonAnalyzeBP.Click += ButtonAnalyzeBP_Click;
            buttonGetPlan          = FindViewById <Button>(Resource.Id.buttonGetPlan);
            buttonGetPlan.Click   += ButtonGetPlan_Click;

            BloodPictureGraph = FindViewById <PlotView>(Resource.Id.BloodPictureGraph);

            buttonGetPlan.Visibility          = ViewStates.Invisible;
            textViewAnalyzeName.Visibility    = ViewStates.Invisible;
            textViewAnalyzeResults.Visibility = ViewStates.Invisible;
            textViewShouldDo.Visibility       = ViewStates.Invisible;

            progressDialog = new ProgressDialog(this);
            progressDialog.Indeterminate = true;
            progressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
            progressDialog.SetMessage("Calculating...");
            progressDialog.SetCancelable(false);
            progressDialog.Create();

            progressDialog.Show();

            //await TrainNeuralNetwork();
            neuralNetwork = await LoadNeuralNetwork();

            bloodPictureData = DatabaseAdapter.GetLastBloodData().Result;

            BloodPictureGraph.Model = CalculateBP();
            progressDialog.Hide();
        }
Beispiel #3
0
        public async Task <BloodDonatonNeuralNet> LoadNeuralNetwork()
        {
            BloodDonatonNeuralNet net = new BloodDonatonNeuralNet();
            await Task.Run(() =>
            {
                try
                {
                    var FullFilePath   = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), filename);
                    BinaryFormatter bf = new BinaryFormatter();
                    using (Stream fsout = new FileStream(FullFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        bf.Binder = new PreMergeToMergedDeserializationBinder();
                        net       = (BloodDonatonNeuralNet)bf.Deserialize(fsout);
                    }
                }
                catch (Exception ex)
                {
                }
            });

            return(net);
        }
Beispiel #4
0
        public void Init()
        {
            var dataset = CreateDataSet();
            int counter = 0;

            needToStop = false;
            // initialize input and output values
            double[][] input  = new double[dataSetArrayCount][];
            double[][] output = new double[dataSetArrayCount][];

            for (int i = 0; i < dataset.Count; i++)
            {
                input[i]    = new double[6];
                input[i][0] = dataset[i].Erytrocyt;
                input[i][1] = dataset[i].Fibrinogen;
                input[i][2] = dataset[i].Hemocyt;
                input[i][3] = dataset[i].Leukocyt;
                input[i][4] = dataset[i].Protrombin;
                input[i][5] = dataset[i].Trombocyt;

                output[i]    = new double[2];
                output[i][0] = dataset[i].Result1;
                output[i][1] = dataset[i].Result2;
            }



            SigmoidFunction sigmoidFunction = new SigmoidFunction(5);
            // create neural network
            ActivationNetwork network = new ActivationNetwork(
                sigmoidFunction,
                6,  // two inputs in the network
                5,  // two neurons in the first layer
                2); // one neuron in the second layer

            // create teacher

            network.Randomize();
            BackPropagationLearning teacher =
                new BackPropagationLearning(network);

            teacher.LearningRate = 5;
            // loop
            while (!needToStop)
            {
                // run epoch of learning procedure
                double error = teacher.RunEpoch(input, output);
                // check error value to see if we need to stop
                // ...

                counter++;
                if (counter == 2000)
                {
                    Console.WriteLine(error);
                    counter = 0;
                }
                //if (error < 200)
                if (error < 0.05)
                {
                    needToStop = true;
                }
            }

            BloodDonatonNeuralNet bloodDonatonNeuralNet = new BloodDonatonNeuralNet();

            bloodDonatonNeuralNet.NeuralNetwork = network;

            AssetManager assets = this.Assets;
            IFormatter   formatter;

            using (MemoryStream stream = new MemoryStream())
            {
                formatter = new BinaryFormatter();
                formatter.Serialize(stream, bloodDonatonNeuralNet);

                var FullFilePath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), filename);

                stream.Seek(0, SeekOrigin.Begin);

                using (FileStream fs = new FileStream(FullFilePath, FileMode.OpenOrCreate))
                {
                    stream.CopyTo(fs);
                    fs.Flush();
                }
            }
        }