Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using CNTK;
- namespace CNTKDemo
- {
- class MainClass
- {
- private static void PrintOutput<T>(uint sampleSize, List<List<T>> outputBuffer)
- {
- Console.WriteLine("The number of sequences in the batch: " + outputBuffer.Count);
- int seqNo = 0;
- uint outputSampleSize = sampleSize;
- foreach (var seq in outputBuffer)
- {
- Console.WriteLine(String.Format("Sequence {0} contains {1} samples.", seqNo++, seq.Count / outputSampleSize));
- uint i = 0;
- uint sampleNo = 0;
- foreach (var element in seq)
- {
- if (i++ % outputSampleSize == 0)
- {
- Console.Write(String.Format(" sample {0}: ", sampleNo));
- }
- Console.Write(element);
- if (i % outputSampleSize == 0)
- {
- Console.WriteLine(".");
- sampleNo++;
- }
- else
- {
- Console.Write(",");
- }
- }
- }
- }
- public static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- var device = DeviceDescriptor.CPUDevice;
- const string outputName = "Plus2060";
- var inputDataMap = new Dictionary<Variable, Value>();
- // Load the model.
- Function modelFunc = Function.LoadModel("z.model", device);
- // Get output variable based on name
- Variable outputVar = modelFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single();
- // Get input variable. The model has only one single input.
- // The same way described above for output variable can be used here to get input variable by name.
- Variable inputVar = modelFunc.Arguments.Single();
- var outputDataMap = new Dictionary<Variable, Value>();
- Value inputVal, outputVal;
- List<List<float>> outputBuffer;
- // Get shape data for the input variable
- NDShape inputShape = inputVar.Shape;
- uint imageWidth = inputShape[0];
- uint imageHeight = inputShape[1];
- uint imageChannels = inputShape[2];
- uint imageSize = inputShape.TotalSize;
- Console.WriteLine("Evaluate single image");
- // Image preprocessing to match input requirements of the model.
- Bitmap bmp = new Bitmap(Bitmap.FromFile("00000.png"));
- var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true);
- List<float> resizedCHW = resized.ParallelExtractCHW();
- // Create input data map
- inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device);
- inputDataMap.Add(inputVar, inputVal);
- // Create ouput data map. Using null as Value to indicate using system allocated memory.
- // Alternatively, create a Value object and add it to the data map.
- outputDataMap.Add(outputVar, null);
- // Start evaluation on the device
- modelFunc.Evaluate(inputDataMap, outputDataMap, device);
- // Get evaluate result as dense output
- outputBuffer = new List<List<float>>();
- outputVal = outputDataMap[outputVar];
- outputVal.CopyVariableValueTo(outputVar, outputBuffer);
- PrintOutput(outputVar.Shape.TotalSize, outputBuffer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement