/// <summary> /// Evaluates a network (without a model and without input) and obtains a single layer output /// </summary> private static void EvaluateNetworkSingleLayerNoInput() { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder string workingDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Other\Simple2d\Config"); Environment.CurrentDirectory = initialDirectory; List <float> outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator model.Init("deviceId=-1"); // Create the network string networkDescription = GetFileContents(Path.Combine(workingDirectory, @"AddOperatorConstantNoInput.cntk")); model.CreateNetwork(networkDescription); // We can call the evaluate method and get back the results (single layer)... outputs = model.Evaluate("ol", 1); } OutputResults("ol", outputs); } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } 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"); } }
/// <summary> /// Creates an instance of the <see cref="ModelEvaluator"/> class. /// </summary> /// <param name="modelFilePath">The model file path</param> /// <param name="numThreads">The number of concurrent threads for the model</param> /// <param name="id">A unique id for the model</param> /// <remarks>The id is used only for debugging purposes</remarks> private ModelEvaluator(string modelFilePath, int numThreads, int id) { m_modelInstance = id; m_model = new IEvaluateModelManagedF(); // Configure the model to run with a specific number of threads m_model.Init(string.Format("numCPUThreads={0}", numThreads)); // Load model m_model.CreateNetwork(string.Format("modelPath=\"{0}\"", modelFilePath), deviceId: -1); // Generate random input values in the appropriate structure and size var inDims = m_model.GetNodeDimensions(NodeGroup.Input); m_inKey = inDims.First().Key; m_inputs = new Dictionary <string, List <float> >() { { m_inKey, null } }; // We request the output layer names(s) and dimension, we'll use the first one. var outDims = m_model.GetNodeDimensions(NodeGroup.Output); m_outKey = outDims.First().Key; }
public async Task<string[]> EvaluateCustomDNN(string imageUrl) { string domainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory; string workingDirectory = Environment.CurrentDirectory; try { // This example requires the RestNet_18 model. // The model can be downloaded from <see cref="https://www.cntk.ai/resnet/ResNet_18.model"/> int numThreads = 1; List<float> outputs; using (var model = new IEvaluateModelManagedF()) { // initialize the evaluation engine // TODO: initializing the evaluation engine should be one only once at the beginning var initParams = string.Format("numCPUThreads={0}", numThreads); model.Init(initParams); // load the model string modelFilePath = Path.Combine(domainBaseDirectory, @"CNTK\Models\ResNet_18.model"); var modelOption = string.Format("modelPath=\"{0}\"", modelFilePath); model.CreateNetwork(modelOption, deviceId: -1); // Prepare input value in the appropriate structure and size var inDims = model.GetNodeDimensions(NodeGroup.Input); if (inDims.First().Value != 224 * 224 * 3) { throw new CNTKRuntimeException(string.Format("The input dimension for {0} is {1} which is not the expected size of {2}.", inDims.First(), inDims.First().Value, 224 * 224 * 3), string.Empty); } // Transform the image System.Net.Http.HttpClient httpClient = new HttpClient(); Stream imageStream = await httpClient.GetStreamAsync(imageUrl); Bitmap bmp = new Bitmap(Bitmap.FromStream(imageStream)); var resized = bmp.Resize(224, 224, true); var resizedCHW = resized.ParallelExtractCHW(); var inputs = new Dictionary<string, List<float>>() { { inDims.First().Key, resizedCHW } }; // We can call the evaluate method and get back the results (single layer output)... var outDims = model.GetNodeDimensions(NodeGroup.Output); outputs = model.Evaluate(inputs, outDims.First().Key); } List<string> o = new List<string>(); foreach (float f in outputs) { o.Add(f.ToString()); } return o.ToArray<string>(); } catch (Exception ex) { return new string[] { string.Format("domainBase directory {0}, workingDirectory {1}, exception details: {2}.", domainBaseDirectory, workingDirectory, ex.ToString()) }; } }
private static List <float> EvaluateGoModel(string dataDirectory, List <float> feature) { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder //Path.Combine(initialDirectory, @"..\..\Examples\Image\MNIST\Data\"); //Dictionary<string, List<float>> outputs; //using (var model = new IEvaluateModelManagedF()) //{ if (model == null) { Environment.CurrentDirectory = dataDirectory; // @"D:\Aricie\Go\cntk\Data"; model = new IEvaluateModelManagedF(); // Initialize model evaluator string config = GetFileContents(Path.Combine(Environment.CurrentDirectory, @"..\Config\02_Convolution.cntk")); model.Init(config); // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\02_Convolution"); //model.LoadModel(modelFilePath); model.CreateNetwork(string.Format("deviceId=0\nmodelPath=\"{0}\"", modelFilePath)); // Generate random input values in the appropriate structure and size //var inputs = GetDictionary("features", 28 * 28, 255); // We can preallocate the output structure and pass it in (multiple output layers) //outputs = GetDictionary("ol.z", 10, 1); } var inputs = new Dictionary <string, List <float> >(); inputs.Add("features", feature); return(model.Evaluate(inputs, "ol.act", 361)); //} //OutputResults(outputs); } //catch (CNTKException ex) //{ // Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); //} catch (Exception ex) { Console.WriteLine(@"Error: {0} CallStack: {1} Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException?.Message ?? "No Inner Exception"); } return(null); }
/// <summary> /// Program entry point /// </summary> /// <param name="args">Program arguments (ignored)</param> private static void Main(string[] args) { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder Environment.CurrentDirectory = Path.Combine(Environment.CurrentDirectory, @"..\..\Examples\Image\MNIST\Data\"); Dictionary<string, List<float>> outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator string config = GetConfig(); model.Init(config); // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); model.LoadModel(modelFilePath); // Generate random input values in the appropriate structure and size var inputs = GetDictionary("features", 28*28, 255); // We can call the evaluate method and get back the results (single layer)... // List<float> outputList = model.Evaluate(inputs, "ol.z", 10); // ... or we can preallocate the structure and pass it in (multiple output layers) outputs = GetDictionary("ol.z", 10, 1); model.Evaluate(inputs, outputs); } Console.WriteLine("--- Output results ---"); foreach (var item in outputs) { Console.WriteLine("Output layer: {0}", item.Key); foreach (var entry in item.Value) { Console.WriteLine(entry); } } } catch (Exception ex) { Console.WriteLine("Error: {0} \n {1}", ex, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } Console.WriteLine("Press <Enter> to terminate."); Console.ReadLine(); }
/// <summary> /// Program entry point /// </summary> /// <param name="args">Program arguments (ignored)</param> private static void Main(string[] args) { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder Environment.CurrentDirectory = Path.Combine(Environment.CurrentDirectory, @"..\..\Examples\Image\MNIST\Data\"); Dictionary <string, List <float> > outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator string config = GetConfig(); model.Init(config); // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); model.LoadModel(modelFilePath); // Generate random input values in the appropriate structure and size var inputs = GetDictionary("features", 28 * 28, 255); // We can call the evaluate method and get back the results (single layer)... // List<float> outputList = model.Evaluate(inputs, "ol.z", 10); // ... or we can preallocate the structure and pass it in (multiple output layers) outputs = GetDictionary("ol.z", 10, 1); model.Evaluate(inputs, outputs); } Console.WriteLine("--- Output results ---"); foreach (var item in outputs) { Console.WriteLine("Output layer: {0}", item.Key); foreach (var entry in item.Value) { Console.WriteLine(entry); } } } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } Console.WriteLine("Press <Enter> to terminate."); Console.ReadLine(); }
private void Evaluate(string imagePath) { this.LoadImage(imagePath); var configFilePath = this.ConfigFilePath; var modelFilePath = this.ModelFilePath; var labelMappingFilePath = this.LabelMappingFilePath; // Important Environment.CurrentDirectory = Path.GetDirectoryName(this.ConfigFilePath); using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator var lines = File.ReadAllLines(configFilePath); var config = string.Join("\n", lines); model.Init(config); // Load model model.CreateNetwork(string.Format("deviceId=-1\nmodelPath=\"{0}\"", modelFilePath)); // Generate random input values in the appropriate structure and size var feature = this.GetFeature().ToList(); var inputs = new Dictionary <string, List <float> >(); inputs.Add("features", feature); var labels = System.IO.File.ReadAllLines(labelMappingFilePath); var labelCount = labels.Length; var outputNodeName = string.Format("{0}.z", this.OutputNodeName); var outputs = model.Evaluate(inputs, outputNodeName, labelCount); // convert to probability var results = Softmax(outputs); var output = labels.Select((label, index) => new CntkResult { Label = label, Probability = results[index], ProbabilityText = results[index].ToString("F32") }).ToList(); this.Output = new ListCollectionView(output); } }
public static List <float> Evaluate(string modelFilePath, string configContent, Dictionary <string, List <float> > inputs, string outputKey, int outputSize) { //Dictionary<string, List<float>> outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator model.Init(configContent); // Load model // model.LoadModel(modelFilePath); model.CreateNetwork($"deviceId=0\nmodelPath=\"{modelFilePath}\""); // We can call the evaluate method and get back the results (single layer)... // List<float> outputList = model.Evaluate(inputs, "ol.z", 10); return(model.Evaluate(inputs, outputKey, outputSize)); // ... or we can preallocate the structure and pass it in (multiple output layers) //var toReturn = new List<float>(outputSize); //for (int i = 0; i < outputSize; i++) //{ // toReturn.Add(1); //} //outputs = new Dictionary<string, List<float>>(); //outputs.Add("ol.z", toReturn); //model.Evaluate(inputs, outputs); //Console.WriteLine("--- Output results ---"); //foreach (var item in outputs) //{ // Console.WriteLine("Output layer: {0}", item.Key); // foreach (var entry in item.Value) // { // Console.WriteLine(entry); // } //} } }
/// <summary> /// Creates an instance of the <see cref="ModelEvaluator"/> class. /// </summary> /// <param name="modelFilePath">The model file path</param> /// <param name="numThreads">The number of concurrent threads for the model</param> /// <param name="id">A unique id for the model</param> /// <remarks>The id is used only for debugging purposes</remarks> private ModelEvaluator(string modelFilePath, int numThreads, int id) { m_modelInstance = id; m_model = new IEvaluateModelManagedF(); // Configure the model to run with a specific number of threads m_model.Init(string.Format("numCPUThreads={0}", numThreads)); // Load model m_model.CreateNetwork(string.Format("modelPath=\"{0}\"", modelFilePath), deviceId: -1); // Generate random input values in the appropriate structure and size var inDims = m_model.GetNodeDimensions(NodeGroup.Input); m_inKey = inDims.First().Key; m_inputs = new Dictionary<string, List<float>>() { { m_inKey, null } }; // We request the output layer names(s) and dimension, we'll use the first one. var outDims = m_model.GetNodeDimensions(NodeGroup.Output); m_outKey = outDims.First().Key; }
/// <summary> /// Evaluates a trained model and obtains multiple layers output /// </summary> private static void EvaluateModelMultipleLayers() { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder Environment.CurrentDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Image\MNIST\Data\"); Dictionary <string, List <float> > outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator string config = GetFileContents(Path.Combine(Environment.CurrentDirectory, @"..\Config\01_OneHidden.cntk")); model.Init(config); // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); model.CreateNetwork(string.Format("deviceId=-1\nmodelPath=\"{0}\"", modelFilePath)); // Generate random input values in the appropriate structure and size var inputs = GetDictionary("features", 28 * 28, 255); // We can preallocate the output structure and pass it in (multiple output layers) outputs = GetDictionary("ol.z", 10, 1); model.Evaluate(inputs, outputs); } OutputResults(outputs); } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } 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"); } }
/// <summary> /// Evaluates a trained model and obtains a single layer output /// </summary> private static void EvaluateModelSingleLayer() { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder Environment.CurrentDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Image\MNIST\Data\"); List<float> outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator string config = GetFileContents(Path.Combine(Environment.CurrentDirectory, @"..\Config\01_OneHidden.cntk")); model.Init(config); // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); model.CreateNetwork(string.Format("deviceId=-1\nmodelPath=\"{0}\"", modelFilePath)); // Generate random input values in the appropriate structure and size var inputs = GetDictionary("features", 28*28, 255); // We can call the evaluate method and get back the results (single layer)... outputs = model.Evaluate(inputs, "ol.z", 10); } OutputResults("ol.z", outputs); } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } 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"); } }
public async Task <string[]> EvaluateCustomDNN(string imageUrl) { string domainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory; string workingDirectory = Environment.CurrentDirectory; try { // This example requires the RestNet_18 model. // The model can be downloaded from <see cref="https://www.cntk.ai/resnet/ResNet_18.model"/> int numThreads = 1; List <float> outputs; using (var model = new IEvaluateModelManagedF()) { // initialize the evaluation engine // TODO: initializing the evaluation engine should be one only once at the beginning var initParams = string.Format("numCPUThreads={0}", numThreads); model.Init(initParams); // load the model string modelFilePath = Path.Combine(domainBaseDirectory, @"CNTK\Models\ResNet_18.model"); var modelOption = string.Format("modelPath=\"{0}\"", modelFilePath); model.CreateNetwork(modelOption, deviceId: -1); // Prepare input value in the appropriate structure and size var inDims = model.GetNodeDimensions(NodeGroup.Input); if (inDims.First().Value != 224 * 224 * 3) { throw new CNTKRuntimeException(string.Format("The input dimension for {0} is {1} which is not the expected size of {2}.", inDims.First(), inDims.First().Value, 224 * 224 * 3), string.Empty); } // Transform the image System.Net.Http.HttpClient httpClient = new HttpClient(); Stream imageStream = await httpClient.GetStreamAsync(imageUrl); Bitmap bmp = new Bitmap(Bitmap.FromStream(imageStream)); var resized = bmp.Resize(224, 224, true); var resizedCHW = resized.ParallelExtractCHW(); var inputs = new Dictionary <string, List <float> >() { { inDims.First().Key, resizedCHW } }; // We can call the evaluate method and get back the results (single layer output)... var outDims = model.GetNodeDimensions(NodeGroup.Output); outputs = model.Evaluate(inputs, outDims.First().Key); } List <string> o = new List <string>(); foreach (float f in outputs) { o.Add(f.ToString()); } return(o.ToArray <string>()); } catch (Exception ex) { return(new string[] { string.Format("domainBase directory {0}, workingDirectory {1}, exception details: {2}.", domainBaseDirectory, workingDirectory, ex.ToString()) }); } }
/// <summary> /// Evaluates a network (without a model and without input) and obtains a single layer output /// </summary> private static void EvaluateNetworkSingleLayerNoInput() { try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder string workingDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Other\Simple2d\Config"); Environment.CurrentDirectory = initialDirectory; List<float> outputs; using (var model = new IEvaluateModelManagedF()) { // Initialize model evaluator model.Init("deviceId=-1"); // Create the network string networkDescription = GetFileContents(Path.Combine(workingDirectory, @"AddOperatorConstantNoInput.cntk")); model.CreateNetwork(networkDescription); // We can call the evaluate method and get back the results (single layer)... outputs = model.Evaluate("ol", 1); } OutputResults("ol", outputs); } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } 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"); } }