public CNNVisualisation(CNN nn) : this(rysyPINVOKE.new_CNNVisualisation(CNN.getCPtr(nn)), true) { if (rysyPINVOKE.SWIGPendingException.Pending) { throw rysyPINVOKE.SWIGPendingException.Retrieve(); } }
public static void Kaggle_Test() { //DigitImage[] training = CSV_Helper.Read(DataSet.Training); DigitImage[] testing = CSV_Helper.Read(DataSet.Testing); int dialogResult; CNN cnn = CreateCNN(out dialogResult); DigitImage[] digitImagesDatas = testing; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int testing_count = digitImagesDatas.Length; int correct_count = 0; double timeLimit = 0, error = 0; Matrix[] input, targets; Console.WriteLine("System is getting tested. You will see the results when it is done...\n"); if (cursorTopTesting == 0) { cursorTopTesting = Console.CursorTop; } InitializeInputAndTarget(out input, out targets); timeLimit = stopwatch.ElapsedMilliseconds; string submissionCSV = "ImageId,Label" + Environment.NewLine; for (int i = 0; i < testing_count; i++) { for (int j = 0; j < 28; j++) { for (int k = 0; k < 28; k++) { input[0][j, k] = digitImagesDatas[i].pixels[j][k]; } } input[0].Normalize(0f, 255f, 0f, 1f); Matrix ans = cnn.Predict(input); submissionCSV += (i + 1).ToString() + "," + ans.GetMaxRowIndex() + Environment.NewLine; if (stopwatch.ElapsedMilliseconds > timeLimit) { // every 0.5 sec update error timeLimit += 500; error = cnn.GetError(); } int val = Map(0, testing_count, 0, 100, i); ProgressBar(val, i, testing_count, error, stopwatch.ElapsedMilliseconds / 1000.0, cursorTopTesting); } CSV_Helper.Write(submissionCSV); }
public void CLOSE_CONNECTION() { if (CNN.State == ConnectionState.Open) { CNN.Close(); } }
public CNN(CNN other) : this(rysyPINVOKE.new_CNN__SWIG_1(CNN.getCPtr(other)), true) { if (rysyPINVOKE.SWIGPendingException.Pending) { throw rysyPINVOKE.SWIGPendingException.Retrieve(); } }
public JobCNNClassify(GRasterLayer featureRasterLayer, GRasterLayer labelRasterLayer, int epochs, int model, int width, int height, int channel) { _t = new Thread(() => { ImageClassifyEnv env = new ImageClassifyEnv(featureRasterLayer, labelRasterLayer); CNN cnn = new CNN(new int[] { channel, width, height }, env.ActionNum); //training Summary = "模型训练中"; for (int i = 0; i < epochs; i++) { int batchSize = cnn.BatchSize; var(states, labels) = env.RandomEval(batchSize); double[][] inputX = new double[batchSize][]; for (int j = 0; j < batchSize; j++) { inputX[j] = states[j]; } double loss = cnn.Train(inputX, labels); Process = (double)i / epochs; } //classify Summary = "分类应用中"; IRasterLayerCursorTool pRasterLayerCursorTool = new GRasterLayerCursorTool(); pRasterLayerCursorTool.Visit(featureRasterLayer); //GDI graph Bitmap classificationBitmap = new Bitmap(featureRasterLayer.XSize, featureRasterLayer.YSize); Graphics g = Graphics.FromImage(classificationBitmap); // int seed = 0; int totalPixels = featureRasterLayer.XSize * featureRasterLayer.YSize; //应用dqn对图像分类 for (int i = 0; i < featureRasterLayer.XSize; i++) { for (int j = 0; j < featureRasterLayer.YSize; j++) { //get normalized input raw value double[] normal = pRasterLayerCursorTool.PickNormalValue(i, j); //}{debug double[] action = cnn.Predict(normal); //convert action to raw byte value int gray = env.RandomSeedKeys[NP.Argmax(action)]; //后台绘制,报告进度 Color c = Color.FromArgb(gray, gray, gray); Pen p = new Pen(c); SolidBrush brush = new SolidBrush(c); g.FillRectangle(brush, new Rectangle(i, j, 1, 1)); //report progress Process = (double)(seed++) / totalPixels; } } //保存结果至tmp string fullFileName = Directory.GetCurrentDirectory() + @"\tmp\" + DateTime.Now.ToFileTimeUtc() + ".png"; classificationBitmap.Save(fullFileName); //complete Summary = "CNN训练分类完成"; Complete = true; OnTaskComplete?.Invoke(Name, fullFileName); }); }
private Guid SaveNetworkToDB(CNN network, string name) { using (var context = new NetworkContext()) { if (context.NetworkModels.FirstOrDefault(item => item.Name == name) != null) { return(ProcessUpdate(network, name, context)); } else { return(ProcessSave(network, name, context)); } } }
private void Compare(CNN first, CNN second) { for (int i = 0; i < first.LayersCount; ++i) { if (first.Layers[i] is ConvolutionalLayer) { CompareLayers(first.Layers[i] as ConvolutionalLayer, second.Layers[i] as ConvolutionalLayer); } } for (int i = 0; i < first.LayersCount; ++i) { ComparePerceptronLayers(first.Perceptron.Layers[i], second.Perceptron.Layers[i]); } }
public static void CNN_OverfittingTest() { CNN cnn = new CNN(); DigitImage[] digitImages = MNIST_Parser.ReadFromFile(DataSet.Testing, 100); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int iteration_count = 10; for (int i = 0; i < iteration_count; i++) { for (int j = 0; j < digitImages.Length; ++j) { Matrix[] input = new Matrix[1]; input[0] = new Matrix(digitImages[j].pixels); Matrix target = new Matrix(10, 1); target[(int)digitImages[j].label, 0] = 1f; cnn.Train(input, target); double error = cnn.GetError(); int val = (int)((i - 0) / (double)(iteration_count - 1 - 0) * (100 - 0) + 0); ProgressBar(val, i, iteration_count, error, stopwatch.ElapsedMilliseconds / 1000.0); } } for (int j = 0; j < digitImages.Length; ++j) { Matrix[] input = new Matrix[1]; input[0] = new Matrix(digitImages[j].pixels); Matrix output = cnn.Predict(input); Console.WriteLine(output.ToString()); Console.WriteLine(digitImages[j].ToString()); Console.ReadLine(); } }
public void ClassificationByCNN() { //loss double _loss = 1.0; //training epochs int epochs = 100; // GRasterLayer featureLayer = new GRasterLayer(featureFullFilename); GRasterLayer labelLayer = new GRasterLayer(trainFullFilename); //create environment for agent exploring IEnv env = new ImageClassifyEnv(featureLayer, labelLayer); //assume 18dim equals 3x6 (image) CNN cnn = new CNN(new int[] { 1, 3, 6 }, env.ActionNum); //training for (int i = 0; i < epochs; i++) { int batchSize = cnn.BatchSize; var(states, labels) = env.RandomEval(batchSize); double[][] inputX = new double[batchSize][]; for (int j = 0; j < batchSize; j++) { inputX[j] = states[j]; } _loss = cnn.Train(inputX, labels); } //in general, loss is less than 5 Assert.IsTrue(_loss < 5.0); //apply cnn to classify featureLayer IRasterLayerCursorTool pRasterLayerCursorTool = new GRasterLayerCursorTool(); pRasterLayerCursorTool.Visit(featureLayer); //get normalized input raw value double[] normal = pRasterLayerCursorTool.PickNormalValue(50, 50); double[] action = cnn.Predict(normal); int landCoverType = env.RandomSeedKeys[NP.Argmax(action)]; //do something as you need. i.e. draw landCoverType to bitmap at position ( i , j ) //the classification results are not stable because of the training epochs are too few. Assert.IsTrue(landCoverType >= 0); }
private void ReadNetworkFromDB(CNN network, Guid netwrokId) { using (var context = new NetworkContext()) { var networkModel = context.NetworkModels.First(item => item.NetworkModelId == netwrokId); var cnnModelId = context.CnnModels .First(model => model.CnnModelId == networkModel.CnnId).CnnModelId; var dbCnnLayers = context.CnnLayers .Where(layer => layer.ModelId == cnnModelId && layer.LayerType != 5) .OrderBy(layer => layer.PositionIn).ToList(); network.Layers = new List <IConvLayer>(); foreach (var item in dbCnnLayers) { if (item.LayerType != (byte)LayerType.FlattenLayer) { item.Weights = context.CnnWeightsSet.FirstOrDefault(i => i.LayerId == item.CnnLayerId); network.Layers.Add(item.ToConvLayer()); } } network.FlattenLayer = new FlattenLayer(); var percId = context.PerceptronModels .FirstOrDefault(item => item.NetworkModelId == netwrokId).PerceptronModelId; var dbPerceptronLayers = context.PerceptronLayers .Where(item => item.PerceptronModelId == percId) .OrderBy(item => item.PositionIn).ToList(); network.Perceptron = new MultilayerPerceptron(); foreach (var item in dbPerceptronLayers) { item.Weights = context.PerceptronWeights .FirstOrDefault(i => i.PerceptronWeightsId == item.PerceptronLayerId); network.Perceptron.Layers.Add(item.ToPercLayer()); } } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); opf.Filter = "CeNiN file|*.cenin"; if (opf.ShowDialog() != DialogResult.OK) { return; } textBox1.Clear(); prependLine("Loading CeNiN file..."); Application.DoEvents(); tic(); cnn = new CNN(opf.FileName); prependLine(cnn.layerCount + "+2 layers, " + cnn.totalWeightCount + " weights and " + cnn.totalBiasCount + " biases were loaded in " + toc() + " seconds."); button2.Enabled = true; }
public static void CNN_Testing(DigitImage[] digitImagesDatas, bool predictionIsOn, CNN cnn, int iterationCount) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int testing_count = digitImagesDatas.Length; int correct_count = 0; double timeLimit = 0, error = 0; Matrix[] input, targets; Console.WriteLine("System is getting tested. You will see the results when it is done...\n"); if (cursorTopTesting == 0) { cursorTopTesting = Console.CursorTop; } InitializeInputAndTarget(out input, out targets); timeLimit = stopwatch.ElapsedMilliseconds; for (int i = 0; i < testing_count; i++) { for (int j = 0; j < 28; j++) { for (int k = 0; k < 28; k++) { input[0][j, k] = digitImagesDatas[i].pixels[j][k]; } } input[0].Normalize(0f, 255f, 0f, 1f); Matrix ans = null; if (predictionIsOn) { ans = cnn.Predict(input); } else { cnn.Train(input, targets[digitImagesDatas[i].label]); ans = cnn.Layers[cnn.Layers.Length - 1].Output[0]; } if (ans.GetMaxRowIndex() == digitImagesDatas[i].label) { correct_count++; } if (stopwatch.ElapsedMilliseconds > timeLimit) { // every 0.5 sec update error timeLimit += 500; error = cnn.GetError(); } int val = Map(0, testing_count, 0, 100, i); ProgressBar(val, i, testing_count, error, stopwatch.ElapsedMilliseconds / 1000.0, cursorTopTesting); } double accuracy = (correct_count * 1f / testing_count) * 100.0; Console.WriteLine("\nIteration Count:" + iterationCount); Console.WriteLine("\nTime :" + (stopwatch.ElapsedMilliseconds / 1000.0).ToString("F4")); Console.WriteLine("\nAccuracy: %{0:F2}\n", accuracy); Console.WriteLine("Correct/All: {0}/{1}", correct_count, testing_count); cursorTopTesting = Console.CursorTop; if (accuracy >= 95 && iterationCount != -1) { string name = accuracy.ToString("F2") + "__" + iterationCount + "__"; Random random = new Random(); int length = 6; for (int i = 0; i < length; i++) { char c = (char)random.Next('A', 'F' + 1); name += c; } cnn.SaveData(name + ".json"); } }
private Guid ProcessSave(CNN network, string name, NetworkContext context) { var cnnModel = new CnnModel { CnnModelId = Guid.NewGuid() }; var layers = new List <CnnLayer>(); var cnnWeights = new List <CnnWeights>(); for (int i = 0; i < network.LayersCount; i++) { if (network.Layers[i] is ConvolutionalLayer) { var cnnLayer = network.Layers[i] as ConvolutionalLayer; var layer = new CnnLayer { CnnLayerId = Guid.NewGuid(), PositionIn = i, KernelHeight = cnnLayer.KernelSize, KernelWidth = cnnLayer.KernelSize, KernelsCount = cnnLayer.KernelsCount, FeatureMapsCountIn = cnnLayer.KernelDepth, LayerType = (byte)LayerType.CovolutionalLayer, Model = cnnModel }; var weights = new CnnWeights { CnnWeightsId = Guid.NewGuid(), Layer = layer, LayerId = layer.CnnLayerId }; var w = string.Empty; var builder = new StringBuilder(String.Empty); for (int j = 0; j < cnnLayer.KernelsCount; ++j) { for (int k = 0; k < cnnLayer.KernelDepth; ++k) { for (int a = 0; a < cnnLayer.Kernels[j][k].Length; ++a) { for (int b = 0; b < cnnLayer.Kernels[j][k][a].Length; ++b) { builder.Append(cnnLayer.Kernels[j][k][a][b] + ";"); } } } } w = builder.ToString(); weights.Weights = w; cnnWeights.Add(weights); layer.Weights = weights; layers.Add(layer); } else if (network.Layers[i] is PollingLayer) { var cnnLayer = network.Layers[i] as PollingLayer; var layer = new CnnLayer { CnnLayerId = Guid.NewGuid(), PositionIn = i, KernelHeight = cnnLayer.KernelSize, KernelWidth = cnnLayer.KernelSize, LayerType = (byte)LayerType.PoolingLayer, Model = cnnModel }; layers.Add(layer); } else { var cnnLayer = network.Layers[i] as ReLuLayer; var layer = new CnnLayer { CnnLayerId = Guid.NewGuid(), PositionIn = i, KernelHeight = cnnLayer.KernelSize, KernelWidth = cnnLayer.KernelSize, LayerType = (byte)LayerType.ReluLayer, Model = cnnModel }; layers.Add(layer); } } var l = new CnnLayer { CnnLayerId = Guid.NewGuid(), KernelHeight = network.FlattenLayer.KernelSize, KernelWidth = network.FlattenLayer.KernelSize, LayerType = (byte)LayerType.FlattenLayer, Model = cnnModel }; layers.Add(l); cnnModel.Layers = layers; var perceptronModel = new PerceptronModel { PerceptronModelId = Guid.NewGuid() }; var percLayers = new List <PerceptronLayer>(); var percWeights = new List <PerceptronWeights>(); for (var i = 0; i < network.Perceptron.LayersCount; ++i) { var layer = network.Perceptron.Layers[i]; var perLayer = new PerceptronLayer { PerceptronLayerId = Guid.NewGuid(), NeuronsCount = layer.NeuronsCount, PositionIn = i, Perceptron = perceptronModel }; var weights = new PerceptronWeights { PerceptronWeightsId = perLayer.PerceptronLayerId, Height = layer.WeightRowsCount, Width = layer.WeightColumnsCount }; string w = String.Empty; var builder = new StringBuilder(String.Empty); for (int a = 0; a < layer.Weights.Length; ++a) { for (int b = 0; b < layer.Weights[a].Length; ++b) { builder.Append(layer.Weights[a][b] + ";"); } } w = builder.ToString(); weights.Weights = w; percWeights.Add(weights); percLayers.Add(perLayer); } perceptronModel.Layers = percLayers; //save var networkModel = new NetworkModel { NetworkModelId = Guid.NewGuid(), Perceptron = perceptronModel, PerceptronId = perceptronModel.PerceptronModelId, Cnn = cnnModel, CnnId = cnnModel.CnnModelId, Name = name }; cnnModel.NetworkModel = networkModel; cnnModel.NetworkModelId = networkModel.NetworkModelId; perceptronModel.NetworkModel = networkModel; perceptronModel.NetworkModelId = networkModel.NetworkModelId; context.NetworkModels.Add(networkModel); context.CnnLayers.AddRange(layers); context.CnnWeightsSet.AddRange(cnnWeights); context.CnnModels.Add(cnnModel); context.PerceptronLayers.AddRange(percLayers); context.PerceptronWeights.AddRange(percWeights); context.PerceptronModels.Add(perceptronModel); context.SaveChanges(); return(networkModel.NetworkModelId); }
private Guid ProcessUpdate(CNN network, string name, NetworkContext context) { var cnnModel = context.NetworkModels.FirstOrDefault(item => item.Name == name); var layers = context.CnnLayers .Where(layer => layer.ModelId == cnnModel.CnnId && layer.LayerType != 5) .OrderBy(layer => layer.PositionIn).ToList(); var cnnWeights = new List <CnnWeights>(); for (int i = 0; i < network.LayersCount; i++) { if (network.Layers[i] is ConvolutionalLayer) { var layerId = layers[i].CnnLayerId; var weights = context.CnnWeightsSet.FirstOrDefault(item => item.LayerId == layerId); var currentCnnLayer = network.Layers[i] as ConvolutionalLayer; var w = string.Empty; var builder = new StringBuilder(String.Empty); for (int j = 0; j < currentCnnLayer.KernelsCount; ++j) { for (int k = 0; k < currentCnnLayer.KernelDepth; ++k) { for (int a = 0; a < currentCnnLayer.Kernels[j][k].Length; ++a) { for (int b = 0; b < currentCnnLayer.Kernels[j][k][a].Length; ++b) { builder.Append(currentCnnLayer.Kernels[j][k][a][b] + ";"); } } } } weights.Weights = builder.ToString(); cnnWeights.Add(weights); } } var perceptronModel = context.PerceptronModels .FirstOrDefault(item => item.NetworkModelId == cnnModel.NetworkModelId); var dbPerceptronLayers = context.PerceptronLayers .Where(item => item.PerceptronModelId == perceptronModel.PerceptronModelId) .OrderBy(l => l.PositionIn).ToList(); var percWeights = new List <PerceptronWeights>(); for (int i = 0; i < network.Perceptron.Layers.Count; ++i) { var percLayerId = dbPerceptronLayers[i].PerceptronLayerId; var weights = context.PerceptronWeights .FirstOrDefault(l => l.PerceptronWeightsId == percLayerId); var currentLayer = network.Perceptron.Layers[i]; string w = String.Empty; var builder = new StringBuilder(String.Empty); for (int a = 0; a < currentLayer.Weights.Length; ++a) { for (int b = 0; b < currentLayer.Weights[a].Length; ++b) { builder.Append(currentLayer.Weights[a][b] + ";"); } } weights.Weights = builder.ToString(); percWeights.Add(weights); } context.SaveChanges(); return(Guid.Empty); }
/// <summary> Creates a NDL.</summary> /// <param name="message">the Message to which this Type belongs /// </param> public NDL(Message message):base(message) { data = new Type[11]; data[0] = new CNN(message); data[1] = new TS(message); data[2] = new TS(message); data[3] = new IS(message, 302); data[4] = new IS(message, 303); data[5] = new IS(message, 304); data[6] = new HD(message); data[7] = new IS(message, 306); data[8] = new IS(message, 305); data[9] = new IS(message, 307); data[10] = new IS(message, 308); }
public Network CreateStandart(params int[] param) { var network = new CNN(); network.Layers = new List <IConvLayer>(); var lr = 0.001; #region Coco //for cnn (neurons count) = filter_size^2 + input channels count //network.Layers.Add(ConvLayerFactory.Create(350, 3, 5, (byte)LayerType.CovolutionalLayer, lr, 400)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); //network.Layers.Add(ConvLayerFactory.Create(174, 5, 5, (byte)LayerType.CovolutionalLayer, lr, 350)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); //network.Layers.Add(ConvLayerFactory.Create(86, 5, 5, (byte)LayerType.CovolutionalLayer, lr, 174)); //network.Layers.Add(ConvLayerFactory.Create(84, 5, 3, (byte)LayerType.CovolutionalLayer, lr, 86)); //network.Layers.Add(ConvLayerFactory.Create(82, 3, 3, (byte)LayerType.CovolutionalLayer, lr, 84)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); //network.Layers.Add(ConvLayerFactory.Create(40, 3, 5, (byte)LayerType.CovolutionalLayer, lr, 82)); //network.Layers.Add(ConvLayerFactory.Create(38, 5, 3, (byte)LayerType.CovolutionalLayer, lr, 40)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); //network.Layers.Add(ConvLayerFactory.Create(18, 3, 3, (byte)LayerType.CovolutionalLayer, lr, 36)); //network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); //network.FlattenLayer = ConvLayerFactory.CreateFlattenLayer(); #endregion #region Cifar10 network.Layers.Add(ConvLayerFactory.Create(32, 3, 16, (byte)LayerType.CovolutionalLayer, lr, 40)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); network.Layers.Add(ConvLayerFactory.Create(30, 16, 16, (byte)LayerType.CovolutionalLayer, lr, 32)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); network.Layers.Add(ConvLayerFactory.Create(14, 16, 40, (byte)LayerType.CovolutionalLayer, lr, 30)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); network.Layers.Add(ConvLayerFactory.Create(12, 40, 40, (byte)LayerType.CovolutionalLayer, lr, 14)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); network.Layers.Add(ConvLayerFactory.Create(10, 40, 80, (byte)LayerType.CovolutionalLayer, lr, 14)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.PoolingLayer)); network.Layers.Add(ConvLayerFactory.Create(layerType: (byte)LayerType.ReluLayer)); network.FlattenLayer = ConvLayerFactory.CreateFlattenLayer(); network.PerLayers = new List <Components.BaseLayer>(); Normal normal = new Normal(0, Math.Pow(param[0], -0.5)); double[][] hiddenLayerWeights = new double[param[0]][]; for (int i = 0; i < param[0]; ++i) { hiddenLayerWeights[i] = new double[param[1]]; for (int j = 0; j < param[1]; ++j) { hiddenLayerWeights[i][j] = normal.Sample(); } } //normal distribution normal = new Normal(0, Math.Pow(param[1], -0.5)); double[][] outputLayerWeights = new double[param[1]][]; for (int i = 0; i < param[1]; ++i) { outputLayerWeights[i] = new double[param[2]]; for (int j = 0; j < param[2]; ++j) { outputLayerWeights[i][j] = normal.Sample(); } } network.PerLayers.Add(new SigmoidLayer { Weights = hiddenLayerWeights }); network.PerLayers.Add(new SoftMaxLayer { Weights = outputLayerWeights }); #endregion //network.Perceptron = (MultilayerPerceptron)(new MultilayerPerceptronFactory().CreateStandart(param)); return(network); }
public MainForm() { InitializeComponent(); if ((Properties.Settings.Default.last_path_to_videos != "") && (Directory.Exists(Properties.Settings.Default.last_path_to_videos))) { vidFolder = new VideoFolder(); vidFolder.Load(Properties.Settings.Default.last_path_to_videos); lVideos_count.Text = "Found " + vidFolder.getCount(); tb_videos_path.Text = vidFolder.getPath(); } else { vidFolder = new VideoFolder(); } if (Properties.Settings.Default.last_path_for_images_to_save != "") { var path = Properties.Settings.Default.last_path_for_images_to_save; tb_images_to_save_path.Text = path; } if ((Properties.Settings.Default.last_path_for_images_to_detect != "") && (Directory.Exists(Properties.Settings.Default.last_path_for_images_to_detect))) { path_to_detect_images = Properties.Settings.Default.last_path_for_images_to_detect; tb_images_to_detect_path.Text = path_to_detect_images; folders_to_detect.Add(path_to_detect_images); foreach (string s in Directory.GetDirectories(path_to_detect_images)) { folders_to_detect.Add(s); } lFoldersToDetectCount.Text = folders_to_detect.Count.ToString(); images_count_to_detect = 0; foreach (string p in folders_to_detect) { string[] dirs = Directory.GetFiles(p, "*.jpg"); images_count_to_detect += dirs.Length; } lImagesToDetectCount.Text = images_count_to_detect.ToString(); } if (Properties.Settings.Default.last_path_to_cascades != "") { path_to_cascades = Properties.Settings.Default.last_path_to_cascades; if (path_to_cascades.Length != 0) { xmlFolder = new XmlFolder(); xmlFolder.Load(path_to_cascades); tb_cascade_path.Text = path_to_cascades; if (xmlFolder.GetCount() > 0) { lCascadeLoaded.Text = "Loaded " + xmlFolder.GetCount().ToString(); } else { lCascadeLoaded.Text = "Not Exists"; } tb_cascade_path.Text = Properties.Settings.Default.last_path_to_cascades; } else { lCascadeLoaded.Text = "Not loaded"; } } else { xmlFolder = new XmlFolder(); } if (Properties.Settings.Default.last_path_for_detected_images_to_save != "") { var path = Properties.Settings.Default.last_path_for_detected_images_to_save; tb_detected_images_to_save_path.Text = path; } if ((Properties.Settings.Default.last_path_to_learn_pictures != "") && (Directory.Exists(Properties.Settings.Default.last_path_to_learn_pictures))) { trainFolder = new ImageFolder(); trainFolder.Load(Properties.Settings.Default.last_path_to_learn_pictures); lLearn_count.Text = "Found " + trainFolder.GetCount(); tb_train_imgs_path.Text = trainFolder.GetPath(); } if ((Properties.Settings.Default.last_path_to_test_pictures != "") && (Directory.Exists(Properties.Settings.Default.last_path_to_test_pictures))) { testFolder = new ImageFolder(); testFolder.Load(Properties.Settings.Default.last_path_to_test_pictures); lTest_count.Text = "Found " + testFolder.GetCount(); tb_test_imgs_path.Text = testFolder.GetPath(); } if ((Properties.Settings.Default.last_path_to_pictures != "") && (Directory.Exists(Properties.Settings.Default.last_path_to_pictures))) { recognizeImageFolder = new ImageFolder(); recognizeImageFolder.Load(Properties.Settings.Default.last_path_to_pictures); lImages.Text = "Found " + recognizeImageFolder.GetCount(); tb_imgs_path.Text = recognizeImageFolder.GetPath(); } if (Properties.Settings.Default.last_path_to_results_save != "") { path_to_results_save = Properties.Settings.Default.last_path_to_results_save; if (Directory.Exists(path_to_results_save)) { tb_result_save_path.Text = path_to_results_save; } } if (Properties.Settings.Default.last_path_to_network != "") { lNetwork.Text = "exists"; var path = Properties.Settings.Default.last_path_to_network; tb_network_path.Text = path; network = new CNN(path); } else { network = new CNN(""); } //Properties.Settings.Default.is_opened_first_time = true; /*if (Properties.Settings.Default.is_opened_first_time) * { * MessageBox.Show("Hello new User"); * Properties.Settings.Default.is_opened_first_time = false; * Properties.Settings.Default.Save(); * }*/ }
public static void CNN_Training() { int trCount = 60000, tsCount = 10000; double error = 0f, timeLimit = 0f; int totalEpoch = 10; bool predictionIsOn = true; Random random = new Random(); DigitImage[] trainigDigitImagesDatas = MNIST_Parser.ReadFromFile(DataSet.Training, trCount); DigitImage[] testingDigitImagesDatas = MNIST_Parser.ReadFromFile(DataSet.Testing, tsCount); int training_count = trainigDigitImagesDatas.Length; int dialogResult; CNN cnn = CreateCNN(out dialogResult); Matrix[] input, targets; InitializeInputAndTarget(out input, out targets); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); if (dialogResult == 0) { Console.WriteLine("System is getting trained..."); //if we never assigned this assign only once if (cursorTopTraining == -1) { cursorTopTraining = Console.CursorTop; } for (int epoch = 0; epoch < totalEpoch; epoch++) { double lossSum = 0.0; DigitImage[] digitImages = trainigDigitImagesDatas.OrderBy(image => random.Next(training_count)).ToArray(); for (int i = 0; i < training_count; i++) { for (int j = 0; j < 28; j++) { for (int k = 0; k < 28; k++) { input[0][j, k] = digitImages[i].pixels[j][k]; } } input[0].Normalize(0f, 255f, 0f, 1f); cnn.Train(input, targets[digitImages[i].label]); //if (stopwatch.ElapsedMilliseconds > timeLimit) // { // every 0.5 sec update error //timeLimit += 500; error = cnn.GetError(); lossSum += error; //} int val = Map(0, training_count * totalEpoch, 0, 100, training_count * epoch + i); ProgressBar(val, training_count * epoch + i, training_count * totalEpoch, lossSum / (i + 1), stopwatch.ElapsedMilliseconds / 1000.0, cursorTopTraining); } CNN_Testing(testingDigitImagesDatas, predictionIsOn, cnn, epoch + 1); } Console.WriteLine("\nSystem has been trained."); } else { CNN_Testing(testingDigitImagesDatas, predictionIsOn, cnn, -1); } }
static void Main(string[] args) { string path = "D:/2/Train/"; Dataset ds_test = new Dataset(10, ColorModel.Grayscale, 32, 32); Dataset ds_train = new Dataset(10, ColorModel.Grayscale, 32, 32); for (int i = 0; i < 10; i++) { for (int j = 0; j < 120; j++)//each of 10 directories contains 120 images { if (j % 6 != 0) { ds_train.AddImage(path + i + "/" + j + ".jpg", i, false, false, false); } else { ds_test.AddImage(path + i + "/" + j + ".jpg", i, false, false, false); } } } ds_test.PrepareData(); ds_train.PrepareData(); CNN cnn = new CNN(CNNType.CNN); cnn.LoadData(ds_train); cnn.AddLayer(new Layer(15, 5, 5, 1, 1, false, ActivationFcn.ReLU)); //15 kernels 5x5 cnn.AddLayer(new Layer(2, 2, PoolType.Max, 15)); //max pooling 2x2 cnn.AddLayer(new Layer(20, 5, 5, 1, 1, false, ActivationFcn.ReLU)); //20 kernels 5x5 cnn.AddLayer(new Layer(2, 2, PoolType.Max, 20)); //max pooling 2x2 cnn.AddLayer(new Layer(50, LayerType.FullConnected, ActivationFcn.ReLU)); cnn.AddLayer(new Layer(10, LayerType.Hidden, ActivationFcn.Tanh)); cnn.CreateNewCNN(); int Epochs = 10; int TrainCount = ds_train.Data.Count(); Queue <Out> res = new Queue <Out>(); Out Res; double cost = 0; int T = 0; int F = 0; cnn.Epohs = 0; cnn.Iterations = 0; cnn.LearnRate = 0.01; for (int i = 0; i < Epochs; i++)//training { cnn.MixDataset(); for (int j = 0; j < TrainCount; j++) { Res = cnn.BackPropagate(j); res.Enqueue(Res); cost += Res.Cost; if (Res.RecognRes) { T++; } else { F++; } cnn.Iterations++; if (cnn.Epohs > 0) { Res = res.Dequeue(); cost -= Res.Cost; if (Res.RecognRes) { T--; } else { F--; } } if (cnn.Iterations % 100 == 0) { if (cnn.Epohs > 0) { Console.WriteLine("Epoch: " + cnn.Epohs + " Iteration: " + cnn.Iterations); Console.WriteLine("Loss: " + String.Format("{0:f4}", cost / TrainCount) + " Accuracy: " + String.Format("{0:f2}", 100.0 * T / (T + F)) + "%"); Console.WriteLine(); } else { Console.WriteLine("Epoch: " + cnn.Epohs + " Iteration: " + cnn.Iterations); Console.WriteLine("Loss: " + String.Format("{0:f4}", cost / cnn.Iterations) + " Accuracy: " + String.Format("{0:f2}", 100.0 * T / (T + F)) + "%"); Console.WriteLine(); } } } cnn.Epohs++; cnn.LearnRate *= cnn.LearnRateDecrease; } T = 0; F = 0; int TestCount = ds_test.Data.Count(); double[] result; for (int i = 0; i < TestCount; i++)//testing { result = cnn.Run(ds_test.Data[i]); if (result.Max() == result[ds_test.Answers[i]]) { T++; } else { F++; } Console.WriteLine(" Accuracy: " + String.Format("{0:f2}", 100.0 * T / (T + F)) + "%"); } Console.ReadKey(); }
public static void Main(string[] args) { var dataset_path = "/home/michal/dataset/mnist/"; //load dataset var dataset = new DatasetMnist(dataset_path + "train-images.idx3-ubyte", dataset_path + "train-labels.idx1-ubyte", dataset_path + "t10k-images.idx3-ubyte", dataset_path + "t10k-labels.idx1-ubyte"); /* * create example network * 3 convolutional layers, dropout and full connected layer as output * C3x3x16 - P2x2 - C3x3x32 - P2x2 - C3x3x32 - FC10 */ var cnn = new CNN(dataset.get_input_shape(), dataset.get_output_shape(), (float)0.0025); cnn.add_layer("convolution", new Shape(3, 3, 16)); cnn.add_layer("elu"); cnn.add_layer("max_pooling", new Shape(2, 2)); cnn.add_layer("convolution", new Shape(3, 3, 32)); cnn.add_layer("elu"); cnn.add_layer("max_pooling", new Shape(2, 2)); cnn.add_layer("convolution", new Shape(3, 3, 32)); cnn.add_layer("elu"); cnn.add_layer("dropout"); cnn.add_layer("output"); cnn.print(); //train network - set epoch count uint epoch_count = 1; cnn.train(dataset.get_training_output_all(), dataset.get_training_input_all(), epoch_count); // test network response on whole testing dataset items var compare = new ClassificationCompare(dataset.get_classes_count()); //neural network output - vector of floats var nn_output = rysy.VectorFloatCreate(dataset.get_classes_count()); //for all testing items for (uint item_idx = 0; item_idx < dataset.get_testing_count(); item_idx++) { //get network response cnn.forward(nn_output, dataset.get_testing_input(item_idx)); //compare with testing dataset compare.add(dataset.get_testing_output(item_idx), nn_output); if (compare.is_nan_error()) { Console.WriteLine("NaN error"); } } //process computing and print results compare.compute(); Console.WriteLine(compare.asString()); Console.WriteLine("program done"); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(CNN obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }