public NeuralPrototype Build(NeuralPrototype current) { prototype = current; DisplayPrototype(current); ShowDialog(); return(prototype); }
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); } }
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; } }
private NeuralPrototype BuildPrototype() { var proto = new NeuralPrototype(); for (var i = 0; i < pnlLayers.Controls.Count; ++i) { var control = pnlLayers.Controls[i]; if (!(control is UserControl)) { continue; } else if (control is ConvLayer conv) { proto.Add ( new ConvPrototype { Size = conv.LayerSize, Filter = conv.LayerFilter, Stride = conv.LayerStride, Padding = conv.LayerPadding, Activation = conv.LayerActivation } ); } else if (control is PoolLayer pool) { proto.Add ( new PoolPrototype { Filter = pool.LayerFilter, Stride = pool.LayerStride, Type = pool.LayerPoolingType } ); } else if (control is AdaptLayer adapt) { proto.Add ( new AdaptPrototype { Normalize = adapt.LayerNormalize, Activation = adapt.LayerActivation, Reshape = adapt.LayerReshape, Shape = adapt.LayerReshape ? adapt.LayerShape : Shape.Scalar() } ); } else if (control is FCLayer fc) { proto.Add ( new FCPrototype { Size = fc.LayerSize, Activation = fc.LayerActivation } ); } } return(proto); }
private void DisplayPrototype(NeuralPrototype proto) { if (proto == null) { throw new ArgumentNullException(nameof(proto)); } Clear(); for (var i = 0; i < proto.LayerCount; ++i) { var layer = proto[i]; if (!(layer is LayerPrototype)) { continue; } else if (layer is ConvPrototype conv) { Add ( new ConvLayer { LayerSize = conv.Size, LayerFilter = conv.Filter, LayerStride = conv.Stride, LayerPadding = conv.Padding, LayerActivation = conv.Activation } ); } else if (layer is PoolPrototype pool) { Add ( new PoolLayer { LayerFilter = pool.Filter, LayerStride = pool.Stride, LayerPoolingType = pool.Type, } ); } else if (layer is AdaptPrototype adapt) { Add ( new AdaptLayer { LayerNormalize = adapt.Normalize, LayerActivation = adapt.Activation, LayerReshape = adapt.Reshape, LayerShape = adapt.Reshape ? adapt.Shape : Shape.Scalar() } ); } else if (layer is FCPrototype fc) { Add ( new FCLayer { LayerSize = fc.Size, LayerActivation = fc.Activation } ); } } }