protected override void EndProcessing() { foreach (var device in DeviceDescriptor.AllDevices()) { WriteObject(device); } }
private void Awake() { contentWindow = new InputImageWindow(1, contentWindowDefaultRect, "Content Image"); contentWindow.showImageMat = showImageMat; styleWindow = new InputImageWindow(2, styleWindowDefaultRect, "Style Image"); styleWindow.showImageMat = showImageMat; resultWindow = new OutputImageWindow(3, resultWindowDefaultRect, "Result Image"); resultWindow.showImageMat = showImageMat; contentSize = new Vector2Int(512, 512); styleSize = new Vector2Int(512, 512); //add the parameter sets styleTransferParams = new List <UniversalStyleTransferModel.ParameterSet>(); for (int i = 0; i < 5; ++i) { styleTransferParams.Insert(0, new UniversalStyleTransferModel.ParameterSet((UniversalStyleTransferModel.PassIndex)i)); } //get available device to run the style transfer on foreach (var d in DeviceDescriptor.AllDevices()) { if (d.Type == DeviceKind.CPU && cpuDevice == null) { cpuDevice = d; } else if (d.Type == DeviceKind.GPU && gpuDevice == null) { gpuDevice = d; } } }
static void Main(string[] args) { try { var devices = DeviceDescriptor.AllDevices().Where(x => (x.Type == DeviceKind.GPU)).ToList(); if (devices.Count == 0) { throw new Exception("No GPU Device found. Please run the CPU examples instead!"); } //Logging.OnWriteLog += Logging_OnWriteLog; //Setting global device GlobalParameters.Device = devices[0]; //XOR Example XORExample.LoadData(); XORExample.BuildModel(); XORExample.Train(); //Housing regression example HousingRegression.LoadData(); HousingRegression.BuildModel(); HousingRegression.Train(); //MNIST Classification example MNISTClassifier.LoadData(); MNISTClassifier.BuildModel(); MNISTClassifier.Train(); //In-progress TimeSeriesPrediction.LoadData(); TimeSeriesPrediction.BuildModel(); TimeSeriesPrediction.Train(); //Cifar-10 Classification example //Cifar10Classification.LoadData(); //Cifar10Classification.BuildModel(); //Cifar10Classification.Train(); //Image classification example Console.WriteLine("ResNet50 Prediction: " + ImageClassification.ImagenetTest(Common.ImageNetModel.ResNet50)[0].Name); Console.WriteLine("Cifar 10 Prediction: " + ImageClassification.Cifar10Test(Common.Cifar10Model.ResNet110)[0].Name); //Object Detection //ObjectDetection.PascalDetection(); //ObjectDetection.GroceryDetection(); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } }
private DeviceDescriptor GetDevice(NeuralNetworkSettingsEntity nnSettings) { if (!nnSettings.Device.HasText()) { return(DeviceDescriptor.UseDefaultDevice()); } var dev = DeviceDescriptor.AllDevices().FirstOrDefault(a => a.AsString() == nnSettings.Device); if (dev == null) { return(DeviceDescriptor.UseDefaultDevice()); } return(dev); }
public void SetupUsingResetModel(DeviceDescriptor device) { try { Console.WriteLine("\n===== Setup memory tests using Resnet Model ====="); var deviceList = DeviceDescriptor.AllDevices(); MemoryTests.Device0 = deviceList[0]; // Load the model. string modelFilePath = "resnet20.dnn"; CNTKLibraryManagedExamples.ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath)); Function modelFunc = Function.LoadModel(modelFilePath, device); Variable inputVar = modelFunc.Arguments.Single(); MemoryTests.ArgumentVar0 = inputVar; MemoryTests.InputVar0 = modelFunc.Inputs.First(); MemoryTests.OutputVar0 = modelFunc.Outputs[0]; MemoryTests.OutputVar = modelFunc.Output; Variable outputVar = MemoryTests.OutputVar; MemoryTests.Axis0 = outputVar.DynamicAxes.FirstOrDefault(); // Get shape data for the input variable NDShape inputShape = inputVar.Shape; int imageWidth = inputShape[0]; int imageHeight = inputShape[1]; int imageChannels = inputShape[2]; int imageSize = inputShape.TotalSize; var inputDataMap = new Dictionary <Variable, Value>(); var outputDataMap = new Dictionary <Variable, Value>(); var imageList = new List <string>() { "00000.png", "00001.png", "00002.png" }; foreach (var image in imageList) { CNTKLibraryManagedExamples.ThrowIfFileNotExist(image, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", image)); } Bitmap bmp, resized; List <float> resizedCHW; var seqData = new List <float>(); for (int sampleIndex = 0; sampleIndex < imageList.Count; sampleIndex++) { bmp = new Bitmap(Bitmap.FromFile(imageList[sampleIndex])); resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); resizedCHW = resized.ParallelExtractCHW(); seqData.AddRange(resizedCHW); } var inputVal = Value.CreateBatch(inputVar.Shape, seqData, device); inputDataMap.Add(inputVar, inputVal); outputDataMap.Add(outputVar, null); modelFunc.Evaluate(inputDataMap, outputDataMap, device); var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); MemoryTests.OutputVal = outputVal; Console.WriteLine("\nTest object reference inside SetupUsingResetModel.\n"); MemoryTests.WriteOutputs(); } catch (Exception ex) { Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); throw ex; } }
public void SetupUsingResetModel(DeviceDescriptor device) { try { Console.WriteLine("\n===== Setup memory tests using Resnet Model ====="); var deviceList = DeviceDescriptor.AllDevices(); MemoryTests.Device0 = deviceList[0]; // Load the model. string modelFilePath = "resnet20.dnn"; CNTKLibraryManagedExamples.ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath)); Function modelFunc = Function.Load(modelFilePath, device); Variable inputVar = modelFunc.Arguments.Single(); MemoryTests.ArgumentVar0 = inputVar; MemoryTests.InputVar0 = modelFunc.Inputs.First(); MemoryTests.OutputVar0 = modelFunc.Outputs[0]; MemoryTests.OutputVar = modelFunc.Output; Variable outputVar = MemoryTests.OutputVar; MemoryTests.Axis0 = outputVar.DynamicAxes.FirstOrDefault(); // Get shape data for the input variable NDShape inputShape = inputVar.Shape; int imageWidth = inputShape[0]; int imageHeight = inputShape[1]; int imageChannels = inputShape[2]; int imageSize = inputShape.TotalSize; var imageList = new List <string>() { "00000.png", "00001.png", "00002.png" }; foreach (var image in imageList) { CNTKLibraryManagedExamples.ThrowIfFileNotExist(image, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", image)); } Bitmap bmp, resized; List <float> resizedCHW; var seqData1 = new List <float>(); var seqData2 = new List <float>(); for (int sampleIndex = 0; sampleIndex < imageList.Count; sampleIndex++) { bmp = new Bitmap(Bitmap.FromFile(imageList[sampleIndex])); resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); resizedCHW = resized.ParallelExtractCHW(); if (sampleIndex < imageList.Count - 1) { seqData1.AddRange(resizedCHW); } seqData2.AddRange(resizedCHW); } var inputDataMap1 = new Dictionary <Variable, Value>(); var outputDataMap1 = new Dictionary <Variable, Value>(); var inputVal1 = Value.CreateBatch(inputVar.Shape, seqData1, device); inputDataMap1.Add(inputVar, inputVal1); outputDataMap1.Add(outputVar, null); // Using temprary Value object returned by Evaluate(). modelFunc.Evaluate(inputDataMap1, outputDataMap1, device); var outputVal1 = outputDataMap1[outputVar]; var outputData1 = outputVal1.GetDenseData <float>(outputVar); // Using cloned persistent Value object returned by Evaluate(). var outputDataMap1WithClone = new Dictionary <Variable, Value>(); outputDataMap1WithClone.Add(outputVar, null); modelFunc.Evaluate(inputDataMap1, outputDataMap1WithClone, true, device); // Using temprary Value object which overwrites the one returned by the previous Evaluate(). var inputDataMap2 = new Dictionary <Variable, Value>(); var outputDataMap2 = new Dictionary <Variable, Value>(); var inputVal2 = Value.CreateBatch(inputVar.Shape, seqData2, device); inputDataMap2.Add(inputVar, inputVal2); outputDataMap2.Add(outputVar, null); modelFunc.Evaluate(inputDataMap2, outputDataMap2, device); // Test access to the persistent Value object, which should be still valid. var outputVal1WithClone = outputDataMap1WithClone[outputVar]; var outputData1WithClone = outputVal1WithClone.GetDenseData <float>(outputVar); // Test access to the temprary Value object returned by the latest Evaluate(). var outputVal2 = outputDataMap2[outputVar]; var outputData2 = outputVal2.GetDenseData <float>(outputVar); // Test access to the temprary Value object returned by the previous Evaluate(), which is not valid any more. bool exceptionCaught = false; try { var data = outputVal1.GetDenseData <float>(outputVar); } catch (Exception ex) { if (ex is ApplicationException && ex.Message.StartsWith("This Value object is invalid and can no longer be accessed.")) { exceptionCaught = true; } } if (exceptionCaught == false) { throw new ApplicationException("The expected exception has not been caught."); } MemoryTests.OutputVal = outputVal1WithClone; Console.WriteLine("\nTest object reference inside SetupUsingResetModel.\n"); MemoryTests.WriteOutputs(); } catch (Exception ex) { Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); throw ex; } }
public string[] GetAvailableDevices() { InitialSetup(); return(DeviceDescriptor.AllDevices().Select(a => a.AsString()).ToArray()); }