Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using CNTK;
  7.  
  8. namespace CNTKDemo
  9. {
  10. class MainClass
  11. {
  12. private static void PrintOutput<T>(uint sampleSize, List<List<T>> outputBuffer)
  13. {
  14. Console.WriteLine("The number of sequences in the batch: " + outputBuffer.Count);
  15. int seqNo = 0;
  16. uint outputSampleSize = sampleSize;
  17. foreach (var seq in outputBuffer)
  18. {
  19. Console.WriteLine(String.Format("Sequence {0} contains {1} samples.", seqNo++, seq.Count / outputSampleSize));
  20. uint i = 0;
  21. uint sampleNo = 0;
  22. foreach (var element in seq)
  23. {
  24. if (i++ % outputSampleSize == 0)
  25. {
  26. Console.Write(String.Format(" sample {0}: ", sampleNo));
  27. }
  28. Console.Write(element);
  29. if (i % outputSampleSize == 0)
  30. {
  31. Console.WriteLine(".");
  32. sampleNo++;
  33. }
  34. else
  35. {
  36. Console.Write(",");
  37. }
  38. }
  39. }
  40. }
  41.  
  42. public static void Main(string[] args)
  43. {
  44. Console.WriteLine("Hello World!");
  45.  
  46. var device = DeviceDescriptor.CPUDevice;
  47.  
  48. const string outputName = "Plus2060";
  49. var inputDataMap = new Dictionary<Variable, Value>();
  50.  
  51. // Load the model.
  52. Function modelFunc = Function.LoadModel("z.model", device);
  53.  
  54. // Get output variable based on name
  55. Variable outputVar = modelFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single();
  56.  
  57. // Get input variable. The model has only one single input.
  58. // The same way described above for output variable can be used here to get input variable by name.
  59. Variable inputVar = modelFunc.Arguments.Single();
  60. var outputDataMap = new Dictionary<Variable, Value>();
  61. Value inputVal, outputVal;
  62. List<List<float>> outputBuffer;
  63.  
  64. // Get shape data for the input variable
  65. NDShape inputShape = inputVar.Shape;
  66. uint imageWidth = inputShape[0];
  67. uint imageHeight = inputShape[1];
  68. uint imageChannels = inputShape[2];
  69. uint imageSize = inputShape.TotalSize;
  70.  
  71. Console.WriteLine("Evaluate single image");
  72.  
  73. // Image preprocessing to match input requirements of the model.
  74. Bitmap bmp = new Bitmap(Bitmap.FromFile("00000.png"));
  75. var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true);
  76. List<float> resizedCHW = resized.ParallelExtractCHW();
  77.  
  78. // Create input data map
  79. inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device);
  80. inputDataMap.Add(inputVar, inputVal);
  81.  
  82. // Create ouput data map. Using null as Value to indicate using system allocated memory.
  83. // Alternatively, create a Value object and add it to the data map.
  84. outputDataMap.Add(outputVar, null);
  85.  
  86. // Start evaluation on the device
  87. modelFunc.Evaluate(inputDataMap, outputDataMap, device);
  88.  
  89. // Get evaluate result as dense output
  90. outputBuffer = new List<List<float>>();
  91. outputVal = outputDataMap[outputVar];
  92. outputVal.CopyVariableValueTo(outputVar, outputBuffer);
  93.  
  94. PrintOutput(outputVar.Shape.TotalSize, outputBuffer);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement