private static Task <Tuple <long, TrainedNework_Simple> > TrainAsync(Tuple <string, EncogOCR_SketchData[]>[] sketches, long token, bool shouldGenerateGarbage, CancellationTokenSource cancel) { CancellationToken cancelToken = cancel.Token; return(Task.Run(() => { string[] outputMap = sketches. Select(o => o.Item1). ToArray(); int inputSize = sketches[0].Item2[0].NNInput.Length; #region Training Data List <double[]> inputs = new List <double[]>(); List <double[]> outputs = new List <double[]>(); int groupIndex = 0; foreach (var group in sketches) { double[] output = Enumerable.Range(0, outputMap.Length). Select((o, i) => i == groupIndex ? 1d : 0d). ToArray(); foreach (var input in group.Item2) { inputs.Add(input.NNInput); outputs.Add(output); } groupIndex++; } if (shouldGenerateGarbage) { GenerateGarbageData(sketches.SelectMany(o => o.Item2).ToArray(), outputMap.Length, inputs, outputs); } #endregion //NOTE: If there is an exception, the network couldn't be trained BasicNetwork network = null; try { network = UtilityEncog.GetTrainedNetwork2(inputs.ToArray(), outputs.ToArray(), 20, 300, cancelToken).NetworkOrNull; } catch (Exception) { } var returnProps = new TrainedNework_Simple() { InputSize = inputSize, Outputs = outputMap, Network = network, }; return Tuple.Create(token, returnProps); }, cancelToken)); }
public void NetworkChanged(TrainedNework_Simple network) { _network = new Network(network); // Detect input size change if (_networkInputs == null || _networkInputs.Size != network.InputSize) { _networkInputs = new NetworkInputs(network.InputSize); } // Detect output size change if (_networkOutputs == null || !_networkOutputs.IsSame(network.Outputs)) { _networkOutputs = new NetworkOutputs(network.Outputs); BuildLegend(_networkOutputs.Names, _networkOutputs.Hues); } #region Dots if (_dots == null) { EnsureDotsCreated(); } else { _dots.Distances_Input.Clear(); _dots.Distances_Output.Clear(); // Clear foreach (Dot dot in _dots.Dots) { dot.NNInput = null; dot.NNOutput = null; } // Rebuild foreach (Dot dot in _dots.Dots) { ReconstructDot(dot); } } #endregion }
private static SketchDots TestSamples(TrainedNework_Simple network, EncogOCR_SketchData[] inputSketches, double[] hues) { // Run a bunch of test images through the NN //NOTE: Only .NNInput and .NNOutput props are populated SketchSample[] sketches = TestSamples_InputOutput(network, inputSketches); // Build a visual with each sketch as a dot Visual3D visual = TestSamples_Draw(sketches, hues); // Calculate the forces between each sketch Tuple <int, int, double>[] distance_Input, distance_Output; TestSamples_Distance(out distance_Input, out distance_Output, sketches); return(new SketchDots() { Sketches = sketches, Visual = visual, Distances_Input = distance_Input, Distances_Output = distance_Output, }); }
private static SketchSample[] TestSamples_InputOutput(TrainedNework_Simple network, EncogOCR_SketchData[] inputSketches) { List <SketchSample> retVal = new List <SketchSample>(); int inputSize = network.Network.InputCount; int outputSize = network.Network.OutputCount; // Inputs foreach (EncogOCR_SketchData input in inputSketches) { double[] output = new double[outputSize]; network.Network.Compute(input.NNInput, output); retVal.Add(new SketchSample() { NNInput = input.NNInput, NNOutput = output }); } // Solid Colors for (int cntr = 0; cntr <= 16; cntr++) { double val = cntr / 16d; double[] input = Enumerable.Range(0, inputSize).Select(o => val).ToArray(); double[] output = new double[outputSize]; network.Network.Compute(input, output); retVal.Add(new SketchSample() { NNInput = input, NNOutput = output }); } //TODO: Generate random brush stroke images //TODO: Generate random pixelated images return(retVal.ToArray()); }
//TODO: If they are only adding a new training sketch, then add to existing instead of repositioning everything (but do reavaluate all outputs, because the network is different) public void VisualizeThis(TrainedNework_Simple network, IEnumerable <EncogOCR_SketchData> sketches) { panelLegend.Children.Clear(); //_viewport.Children.RemoveAll() //_viewport.Children.Remove(_colorWheelVisual); if (_sketches != null) { _viewport.Children.Remove(_sketches.Visual); } _network = network; if (sketches == null) { _inputSketches = null; } else { _inputSketches = sketches.ToArray(); } if (_network == null || _inputSketches == null || _inputSketches.Length == 0) { return; } _hues = GetHues(_network.Outputs.Length); // Make a 2D legend that shows color of each name BuildLegend(_network.Outputs, _hues); //_colorWheelVisual = GetColorWheel(_hues.Item1, _hues.Item2); //_viewport.Children.Add(_colorWheelVisual); _sketches = TestSamples(_network, _inputSketches, _hues); _viewport.Children.Add(_sketches.Visual); }
/// <summary> /// This clears everything except: /// session names out of the combo box /// pixels /// pen size /// </summary> private void ClearEverything() { if (_visualizer != null) { _visualizer.Clear(); } _currentSketch = null; grdGuessDetails.Children.Clear(); grdGuessDetails.RowDefinitions.Clear(); pnlPreviousDrawings.Items.Clear(); _prevSketches.Clear(); cboDrawingLabel.Items.Clear(); canvasInk.Strokes.Clear(); _network = null; RedrawSmallImage(); RedrawPreviousImages(true); }
public Network(TrainedNework_Simple network) { this.Net = network; }
private static Task<Tuple<long, TrainedNework_Simple>> TrainAsync(Tuple<string, EncogOCR_SketchData[]>[] sketches, long token, bool shouldGenerateGarbage, CancellationTokenSource cancel) { CancellationToken cancelToken = cancel.Token; return Task.Run(() => { string[] outputMap = sketches. Select(o => o.Item1). ToArray(); int inputSize = sketches[0].Item2[0].NNInput.Length; #region Training Data List<double[]> inputs = new List<double[]>(); List<double[]> outputs = new List<double[]>(); int groupIndex = 0; foreach (var group in sketches) { double[] output = Enumerable.Range(0, outputMap.Length). Select((o, i) => i == groupIndex ? 1d : 0d). ToArray(); foreach (var input in group.Item2) { inputs.Add(input.NNInput); outputs.Add(output); } groupIndex++; } if (shouldGenerateGarbage) { GenerateGarbageData(sketches.SelectMany(o => o.Item2).ToArray(), outputMap.Length, inputs, outputs); } #endregion //NOTE: If there is an exception, the network couldn't be trained BasicNetwork network = null; try { network = UtilityEncog.GetTrainedNetwork2(inputs.ToArray(), outputs.ToArray(), 20, 300, cancelToken).NetworkOrNull; } catch (Exception) { } var returnProps = new TrainedNework_Simple() { InputSize = inputSize, Outputs = outputMap, Network = network, }; return Tuple.Create(token, returnProps); }, cancelToken); }
private static SketchSample[] TestSamples_InputOutput(TrainedNework_Simple network, EncogOCR_SketchData[] inputSketches) { List<SketchSample> retVal = new List<SketchSample>(); int inputSize = network.Network.InputCount; int outputSize = network.Network.OutputCount; // Inputs foreach (EncogOCR_SketchData input in inputSketches) { double[] output = new double[outputSize]; network.Network.Compute(input.NNInput, output); retVal.Add(new SketchSample() { NNInput = input.NNInput, NNOutput = output }); } // Solid Colors for (int cntr = 0; cntr <= 16; cntr++) { double val = cntr / 16d; double[] input = Enumerable.Range(0, inputSize).Select(o => val).ToArray(); double[] output = new double[outputSize]; network.Network.Compute(input, output); retVal.Add(new SketchSample() { NNInput = input, NNOutput = output }); } //TODO: Generate random brush stroke images //TODO: Generate random pixelated images return retVal.ToArray(); }
private static SketchDots TestSamples(TrainedNework_Simple network, EncogOCR_SketchData[] inputSketches, double[] hues) { // Run a bunch of test images through the NN //NOTE: Only .NNInput and .NNOutput props are populated SketchSample[] sketches = TestSamples_InputOutput(network, inputSketches); // Build a visual with each sketch as a dot Visual3D visual = TestSamples_Draw(sketches, hues); // Calculate the forces between each sketch Tuple<int, int, double>[] distance_Input, distance_Output; TestSamples_Distance(out distance_Input, out distance_Output, sketches); return new SketchDots() { Sketches = sketches, Visual = visual, Distances_Input = distance_Input, Distances_Output = distance_Output, }; }
//TODO: If they are only adding a new training sketch, then add to existing instead of repositioning everything (but do reavaluate all outputs, because the network is different) public void VisualizeThis(TrainedNework_Simple network, IEnumerable<EncogOCR_SketchData> sketches) { panelLegend.Children.Clear(); //_viewport.Children.RemoveAll() //_viewport.Children.Remove(_colorWheelVisual); if (_sketches != null) { _viewport.Children.Remove(_sketches.Visual); } _network = network; if (sketches == null) { _inputSketches = null; } else { _inputSketches = sketches.ToArray(); } if (_network == null || _inputSketches == null || _inputSketches.Length == 0) { return; } _hues = GetHues(_network.Outputs.Length); // Make a 2D legend that shows color of each name BuildLegend(_network.Outputs, _hues); //_colorWheelVisual = GetColorWheel(_hues.Item1, _hues.Item2); //_viewport.Children.Add(_colorWheelVisual); _sketches = TestSamples(_network, _inputSketches, _hues); _viewport.Children.Add(_sketches.Visual); }