You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PbtxtParser.cs 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace TensorFlowNET.Examples.Utility
  6. {
  7. public class PbtxtItem
  8. {
  9. public string name { get; set; }
  10. public int id { get; set; }
  11. public string display_name { get; set; }
  12. }
  13. public class PbtxtItems
  14. {
  15. public List<PbtxtItem> items { get; set; }
  16. }
  17. public class PbtxtParser
  18. {
  19. public static PbtxtItems ParsePbtxtFile(string filePath)
  20. {
  21. string line;
  22. string newText = "{\"items\":[";
  23. try
  24. {
  25. using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
  26. {
  27. while ((line = reader.ReadLine()) != null)
  28. {
  29. string newline = string.Empty;
  30. if (line.Contains("{"))
  31. {
  32. newline = line.Replace("item", "").Trim();
  33. //newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";
  34. newText += newline;
  35. }
  36. else if (line.Contains("}"))
  37. {
  38. newText = newText.Remove(newText.Length - 1);
  39. newText += line;
  40. newText += ",";
  41. }
  42. else
  43. {
  44. newline = line.Replace(":", "\":").Trim();
  45. newline = "\"" + newline;// newline.Insert(0, "\"");
  46. newline += ",";
  47. newText += newline;
  48. }
  49. }
  50. newText = newText.Remove(newText.Length - 1);
  51. newText += "]}";
  52. reader.Close();
  53. }
  54. PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText);
  55. return items;
  56. }
  57. catch (Exception ex)
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。