static MLearner() { if (!File.Exists(annFileName)) { return; } network = new ANN_MLP(); network.Load(annFileName); }
// Loading the Trained Model public void LoadTrainedModel(String ModelFileName) { #if !NETFX_CORE // Loading the Trained Model from File try { nnet.Load(ModelFileName); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } #endif }
private void Btn_reg_Click(object sender, EventArgs e) { using (Matrix <int> layerSize = new Matrix <int>(new int[] { 2, 5, 1 })) using (Mat layerSizeMat = layerSize.Mat) using (ANN_MLP network = new ANN_MLP()) { network.Load(annFileName); //network.SetLayerSizes(layerSizeMat); //network.SetActivationFunction(ANN_MLP.AnnMlpActivationFunction.SigmoidSym, 0, 0); //network.TermCriteria = new MCvTermCriteria(10, 1.0e-8); //network.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop, 0.1, 0.1); float[,] testData = new float[1, 2] { { float.Parse(this.txb_percent.Text), float.Parse(this.txb_avg.Text) } }; Matrix <float> sample = new Matrix <float>(testData); Matrix <float> prediction = new Matrix <float>(1, 1); network.Predict(sample, prediction); float response = prediction.Data[0, 0]; MessageBox.Show($"判断结果:{response}"); } }
private void btn_ANNReg_Click(object sender, EventArgs e) { var regPath = txbregPath.Text; if (string.IsNullOrEmpty(regPath)) { MessageBox.Show("待识别文件夹不能空"); return; } var isAct = ckbIsAct.Checked; var files = Directory.GetFiles(regPath); var testData = new List <List <float> >(); for (int i = 0; i < files.Length; i++) { var path = files[i]; Image <Gray, byte> img = new Image <Gray, byte>(path); testData.Add(getImgData(img)); } using (ANN_MLP network = new ANN_MLP()) { network.Load(annFileName); int colCount = width * height; Matrix <float> sample = new Matrix <float>(1, colCount); Matrix <float> prediction = new Matrix <float>(1, 1); //1测试数据 var testCount = testData.Count; var rightCount = 0;//正确act识别数量 for (int i = 0; i < testCount; i++) { var testColData = testData[i]; for (int j = 0; j < testColData.Count; j++) { sample[0, j] = testColData[j]; } network.Predict(sample, prediction); float response = prediction.Data[0, 0]; if (isAct && response > 0.5) { rightCount++; Console.WriteLine($"该数据是涂答的,正确识别{response}"); } else if (isAct && response <= 0.5) { Console.WriteLine($"该数据是涂答的,错误识别{response}"); File.Copy(files[i], Path.Combine(actRegErrorDir, Path.GetFileName(files[i])), true); } else if (!isAct && response <= 0.5) { rightCount++; Console.WriteLine($"该数据是未涂答的,正确识别{response}"); } else if (!isAct && response > 0.5) { Console.WriteLine($"该数据是未涂答的,错误识别{response}"); File.Copy(files[i], Path.Combine(negRegErrorDir, Path.GetFileName(files[i])), true); } else { Console.WriteLine("未知识别结果"); } } var result = $"测试数量:{testCount},正确数量:{rightCount},正确率:{rightCount * 1.0 / testCount}"; Console.WriteLine(result); MessageBox.Show(result); } }