public ((NDarray, NDarray), (NDarray, NDarray)) BuildDataSets() { List <string> Labels = imageDatas.GetLabels(); if (Labels == null || Labels.Count == 0) { throw new Exception("Invalid Dataset Labels!"); } List <byte[]> train = new List <byte[]>(); List <int> train_labels = new List <int>(); List <byte[]> test = new List <byte[]>(); List <int> test_labels = new List <int>(); if (BuildSets(train, train_labels, test, test_labels, Labels) != imageDatas.Count) { throw new Exception(string.Format("Error creating data sets: Expected:{0}", imageDatas.Count)); } (NDarray, NDarray)tuple1 = GetDataSets(train, train_labels); (NDarray, NDarray)tuple2 = GetDataSets(test, test_labels); return(tuple1, tuple2); }
// Performs predictions on a supplied dataset and loaded neural model: // Calculates prediction accuracies and errors relative to the expected labeling public ImageDatas Predict(bool isCNN) { if (model_loaded == null) { throw new Exception("Invalid Model!"); } if (dataSets == null || !dataSets.isImageDatas()) { throw new Exception("Invalid Dataset!"); } ImageDatas ids = dataSets.GetImageDatas(); ImageDatas idf = new ImageDatas(); NDarray x_data = dataSets.BuildDataSet(); List <string> labels = ids.GetLabels(); if (isCNN) { x_data = (K.ImageDataFormat() == "channels_first") ? x_data.reshape(x_data.shape[0], 1, height, width) : x_data.reshape(x_data.shape[0], height, width, 1); } Console.WriteLine("Predicting {0} Images", ids.Count); NDarray y = model_loaded.Predict(x_data, verbose: 2); int index; NDarray result; for (int i = 0; i < y.len; i++) { result = y[i]; result = result.argmax(); index = result.asscalar <int>(); if (ids[i].Label != labels[index]) { ids[i].Index = labels.IndexOf(ids[i].Label) + 1; idf.Add(ids[i]); } } double accuracy = Math.Round(((y.len - idf.Count) * 100) / (double)y.len, 2); idf.SetResults(string.Format("Predicted:{0} Correct: {1} Incorrect:{2} Accuracy:{3}", y.len, y.len - idf.Count, idf.Count, accuracy)); return(idf); }