コード例 #1
0
        private void btnAccept_Click(object sender, EventArgs e)
        {
            NeuralPrototype proto = null;

            try
            {
                proto = BuildPrototype();

                using (var nn = proto.Builder.Compile())
                {
                    var outShape = nn.OutputShape;
                    if (outShape.Hyperdimension != 2u)
                    {
                        throw new ApplicationException("Output shape hyperdimension not equal to 2.");
                    }
                    if (outShape[0] != 1)
                    {
                        throw new ApplicationException($"Output shape 1rd dimension not equal to 1.");
                    }
                    if (outShape[1] != Categories.Count)
                    {
                        throw new ApplicationException($"Output shape 2nd dimension not equal to {Categories.Count}.");
                    }
                }

                prototype = proto;
                MessageBox.Show("Prototype created.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                proto?.Dispose();
                var message = string.Empty;
                if (ex is ApplicationException)
                {
                    message = ex.Message;
                }
                else
                {
                    message = "Prototype network cannot be compiled.";
                }
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #2
0
        private async void btnClassifierLoading_Click(object sender, EventArgs e)
        {
            if (loaded == null)
            {
                ofd.Title      = "Open network";
                ofd.Filter     = "Artificial Neural Network | *.ann";
                ofd.DefaultExt = ".ann";

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                lblTestStatus.ForeColor = Color.DarkOrange;
                lblTestStatus.Text      = "Loading . . .";

                btnClassifierLoading.Enabled = false;

                var prototype = new NeuralPrototype();

                try
                {
                    loaded = await prototype.Load(ofd.FileName);
                }
                catch (Exception ex)
                {
                    if (ex is FormatException fex)
                    {
                        MessageBox.Show(fex.Message, "Invalid file format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    prototype.Dispose();
                    btnClassifierLoading.Enabled = true;

                    lblTestStatus.ForeColor = Color.DarkRed;
                    lblTestStatus.Text      = "Loading failed!";
                    return;
                }

                var choice = DialogResult.No;

                if (loaded != null)
                {
                    if (!training && classifier == null)
                    {
                        choice = MessageBox.Show("Loading successful. Would you also like to load the neural prototype as well?", "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    }

                    btnClassifierLoading.Text    = "Unload";
                    btnClassifierLoading.Enabled = true;
                    cbUseLoaded.Enabled          = true;
                }
                else
                {
                    if (!training && classifier == null)
                    {
                        choice = MessageBox.Show($"Neural network not found in the file, but the prototype has been found.{Environment.NewLine}Would you like to load the neural prototype?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show("Neural network not found in the file. Prototype will be discarded since training is in progress or a trained classifier is present.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                    btnClassifierLoading.Enabled = true;
                }

                if (choice == DialogResult.Yes)
                {
                    AI.BrainPrototype = prototype;
                }
                else
                {
                    prototype.Dispose();
                }

                lblTestStatus.ForeColor = Color.DarkGreen;
                lblTestStatus.Text      = "Done!";
            }
            else
            {
                loaded.Dispose();
                loaded = null;

                btnClassifierLoading.Text = "Load";
                cbUseLoaded.Enabled       = false;
            }
        }