public async Task Test(string dataset, string filename) { ModelSerializer modelSerializer = new ModelSerializer(); model = modelSerializer.DeserializeCNN(filename); fDataSet testdataset = new fDataSet(); testdataset.Deserializer(dataset); int index = random.Next(0, testdataset.fData.Count); AI.Core.fDataSet dataSet = new AI.Core.fDataSet(); dataSet.fData = testdataset.fData; pixel = testdataset.fData[index].Pixel; label = testdataset.fData[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); AI.ML.CNN.Trainers.ADAM adam = new AI.ML.CNN.Trainers.ADAM(); softmaxOutput = adam.Softmax(probs); double maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; await Clients.All.SendAsync("ReceiveMessage", pixel); await Clients.All.SendAsync("ReceiveLabel", result); }
public async Task ConfigureModel(string configstring, string algorithm, string dataset, string filename) { await Clients.All.SendAsync("ReceiveMessage", "Configuring model..."); model = new AI.ML.CNN.Model(); model.Configure(configstring); await Clients.All.SendAsync("ReceiveMessage", "Initializing dataset..."); switch (dataset) { case "MNIST": case "mnist": dataFileName = @"C:\files\data\mnist\train-images.idx3-ubyte"; labelFileName = @"C:\files\data\mnist\train-labels.idx1-ubyte"; dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadGrayImage(dataFileName, 1, 0.0, 1.0, labelFileName, 0.0, 1.0); dataSet = new AI.Core.fDataSet(); dataSet.fData = dataList; await Clients.All.SendAsync("SetCookie", dataset, "dataset"); break; case "CIFAR": case "cifar": dataFileName = @"C:\files\data\cifar\cifar-10-batches-bin\data_batch_1.bin"; dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadColorImage(dataFileName, 1, 0.0, 1.0, 0.0, 1.0); dataSet = new AI.Core.fDataSet(); dataSet.fData = dataList; await Clients.All.SendAsync("SetCookie", dataset, "dataset"); break; default: dataSet = new AI.Core.fDataSet(); dataSet.Deserializer(dataset); await Clients.All.SendAsync("SetCookie", dataset, "dataset"); break; } await Clients.All.SendAsync("ReceiveMessage", "Initializing training..."); //parse trainer string AI.ML.CNN.Trainers.DeltaRule deltaRule = new AI.ML.CNN.Trainers.DeltaRule(); deltaRule.Configure <AI.ML.CNN.Lossfunc.CategoricalCrossEntropy>(model, epochs, dataSet, learningRate, momentum); await Clients.All.SendAsync("ReceiveMessage", deltaRule.NextVerbose()); filename = @"C:\files\model\" + filename; AI.Core.ModelSerializer modelSerializer = new AI.Core.ModelSerializer(); path = modelSerializer.Serialize(configstring, model, filename); await Clients.All.SendAsync("SetCookie", path, "model"); await Clients.All.SendAsync("ReceiveMessage", "done"); }
public async Task Test(string dataset, string filename) { switch (dataset) { case "MNIST": case "mnist": dataFileName = @"C:\files\data\mnist\train-images.idx3-ubyte"; labelFileName = @"C:\files\data\mnist\train-labels.idx1-ubyte"; model = modelSerializer.DeserializeCNN(filename); List <AI.Core.fData> dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadGrayImage(dataFileName, 1, 0.0, 1.0, labelFileName, 0.0, 1.0); index = random.Next(0, dataList.Count); dataSet.fData = dataList; pixel = dataList[index].Pixel; label = dataList[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); softmaxOutput = adam.Softmax(probs); maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; await Clients.All.SendAsync("ReceiveMessage", pixel); await Clients.All.SendAsync("ReceiveLabel", result); break; default: dataSet = new AI.Core.fDataSet(); dataSet.Deserializer(dataset); model = modelSerializer.DeserializeCNN(filename); index = random.Next(0, dataSet.fData.Count); pixel = dataSet.fData[index].Pixel; label = dataSet.fData[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); softmaxOutput = adam.Softmax(probs); maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; await Clients.All.SendAsync("ReceiveMessage", pixel); await Clients.All.SendAsync("ReceiveLabel", result); break; } }
public string Serialize(string config, object model, string saveas) { string modeltype = model.GetType().ToString(); switch (modeltype) { case "AI.ML.CNN.Model": AI.ML.CNN.Model cnnmodel = (AI.ML.CNN.Model)model; return(SerializeCNN(config, cnnmodel, saveas)); case "AI.ML.ANN.Model": throw new NotImplementedException(); default: throw new Exception("Model Serilizer not found"); } throw new Exception(); }
public async Task Test(string dataset, string filename) { switch (dataset) { case "MNIST": case "mnist": dataFileName = @"C:\files\data\mnist\train-images.idx3-ubyte"; labelFileName = @"C:\files\data\mnist\train-labels.idx1-ubyte"; model = modelSerializer.DeserializeCNN(filename); dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadGrayImage(dataFileName, 1, 0.0, 1.0, labelFileName, 0.0, 1.0); index = random.Next(0, dataList.Count); dataSet.fData = dataList; pixel = dataList[index].Pixel; label = dataList[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); softmaxOutput = adam.Softmax(probs); maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); size = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length); matImg = new Mat(size, size, DepthType.Cv8U, 3); Gimg = matImg.ToImage <Gray, Byte>(); Gimage = setGrayPixels(Gimg, size, pixel); input_size = new int[] { 256, 256 }; resizedPixel = Resize_Gray(Gimage, input_size); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 1); await Clients.All.SendAsync("ReceiveLabel", result); break; case "CIFAR": case "cifar": dataFileName = @"C:\files\data\cifar\cifar-10-batches-bin\data_batch_1.bin"; model = modelSerializer.DeserializeCNN(filename); dataList = (List <AI.Core.fData>)AI.Core.UByteLoader.ReadColorImage(dataFileName, 1, 0.0, 1.0, 0.0, 1.0); index = random.Next(0, dataList.Count); dataSet.fData = dataList; pixel = dataSet.fData[index].Pixel; label = dataSet.fData[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); softmaxOutput = adam.Softmax(probs); maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); size = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length / 3); matImg = new Mat(size, size, DepthType.Cv8U, 3); Cimg = matImg.ToImage <Bgr, Byte>(); Cimage = setColorPixels(Cimg, size, pixel); input_size = new int[] { 256, 256 }; resizedPixel = Resize_Color(Cimage, input_size); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; cifarResult[0] = classes[label]; cifarResult[1] = classes[predict]; await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 3); await Clients.All.SendAsync("ReceiveLabel", cifarResult); break; default: dataSet = new AI.Core.fDataSet(); dataSet.Deserializer(dataset); pixel = dataSet.fData[index].Pixel; model = modelSerializer.DeserializeCNN(filename); index = random.Next(0, dataSet.fData.Count); label = dataSet.fData[index].DecodeLabel; //model.predict probs = model.Predict(dataSet.fData[index]); softmaxOutput = adam.Softmax(probs); maxValue = softmaxOutput.Max(); predict = softmaxOutput.ToList().IndexOf(maxValue); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; if (model.Input.Output.Count == 3) { size = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length / 3); matImg = new Mat(size, size, DepthType.Cv8U, 3); Cimg = matImg.ToImage <Bgr, Byte>(); Cimage = setColorPixels(Cimg, size, pixel); input_size = new int[] { 256, 256 }; resizedPixel = Resize_Color(Cimage, input_size); cifarResult[0] = classes[label]; cifarResult[1] = classes[predict]; await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 3); await Clients.All.SendAsync("ReceiveLabel", cifarResult); } else { size = (int)System.Math.Sqrt(dataSet.fData[0].Pixel.Length); matImg = new Mat(size, size, DepthType.Cv8U, 3); Gimg = matImg.ToImage <Gray, Byte>(); Gimage = setGrayPixels(Gimg, size, pixel); input_size = new int[] { 256, 256 }; resizedPixel = Resize_Gray(Gimage, input_size); //int lindex = random.Next(0, 5); //predict = lindex == 2 ? random.Next(0, 9) : label; result[0] = label; result[1] = predict; await Clients.All.SendAsync("ReceiveMessage", resizedPixel, 1); await Clients.All.SendAsync("ReceiveLabel", result); } break; } }
public ML.CNN.Model DeserializeCNN(string filename) { CNNModel cnnmodel = JsonConvert.DeserializeObject <CNNModel>(File.ReadAllText(filename)); ML.CNN.Model Model = new AI.ML.CNN.Model(); Model.Configure(cnnmodel.config); int count = 0; int anncount = 0; for (int i = 1; i < Model.Layers.Length; i++) { ML.CNN.Model.Unit lyr; lyr = Model.Layers[i]; bool convtype = lyr.GetType().ToString() == "AI.ML.CNN.Layers.Convolution" ? true : false; bool conctype = lyr.GetType().ToString() == "AI.ML.CNN.Layers.Connected" ? true : false; switch (convtype) { case true: ML.CNN.Layers.Convolution convLyr = (ML.CNN.Layers.Convolution)lyr; AI.ML.CNN.Layers.Convolution.Kernel krn; for (int j = 0; j < convLyr.Filters.Count; j++) { // j: indexing filters for (int k = 0; k < ((AI.ML.CNN.Layers.Convolution.Filter)convLyr.Filters[j]).Kernels.Length; k++) { //k: indexing kernels krn = (AI.ML.CNN.Layers.Convolution.Kernel)((AI.ML.CNN.Layers.Convolution.Filter)convLyr.Filters[j]).Kernels[k]; krn.Weights = cnnmodel.cnnWeights[count++].Weights; } } break; case false: switch (conctype) { case true: ML.CNN.Layers.Connected connLyr = (ML.CNN.Layers.Connected)lyr; ML.ANN.Neuron n; ML.ANN.Synapse syn; double?[] weights; for (int j = 0; j < connLyr.Neurons.Length; j++) { n = connLyr.Neurons[j]; weights = cnnmodel.annWeights[anncount++].Weights; for (int k = 0; k < n.Synapse.Count; k++) { syn = n.Synapse[k]; syn.W = weights[k]; } } break; case false: break; } break; default: break; } } return(Model); }