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.

TrainSaverTest.cs 3.4 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using Tensorflow;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. [TestClass]
  10. public class TrainSaverTest : Python
  11. {
  12. public void ExportGraph()
  13. {
  14. var v = tf.Variable(0, name: "my_variable");
  15. var sess = tf.Session();
  16. tf.train.write_graph(sess.graph, "/tmp/my-model", "train1.pbtxt");
  17. }
  18. public void ImportGraph()
  19. {
  20. with<Session>(tf.Session(), sess =>
  21. {
  22. var new_saver = tf.train.import_meta_graph("C:/tmp/my-model.meta");
  23. });
  24. //tf.train.export_meta_graph(filename: "linear_regression.meta.bin");
  25. // import meta
  26. /*tf.train.import_meta_graph("linear_regression.meta.bin");
  27. var cost = graph.OperationByName("truediv").output;
  28. var pred = graph.OperationByName("Add").output;
  29. var optimizer = graph.OperationByName("GradientDescent");
  30. var X = graph.OperationByName("Placeholder").output;
  31. var Y = graph.OperationByName("Placeholder_1").output;
  32. var W = graph.OperationByName("weight").output;
  33. var b = graph.OperationByName("bias").output;*/
  34. /*var text = JsonConvert.SerializeObject(graph, new JsonSerializerSettings
  35. {
  36. Formatting = Formatting.Indented
  37. });*/
  38. }
  39. public void ImportSavedModel()
  40. {
  41. with<Session>(Session.LoadFromSavedModel("mobilenet"), sess =>
  42. {
  43. });
  44. }
  45. public void ImportGraphDefFromPbFile()
  46. {
  47. var g = new Graph();
  48. var status = g.Import("mobilenet/saved_model.pb");
  49. }
  50. public void Save1()
  51. {
  52. var w1 = tf.Variable(0, name: "save1");
  53. var init_op = tf.global_variables_initializer();
  54. // Add ops to save and restore all the variables.
  55. var saver = tf.train.Saver();
  56. with<Session>(tf.Session(), sess =>
  57. {
  58. sess.run(init_op);
  59. // Save the variables to disk.
  60. var save_path = saver.save(sess, "/tmp/model1.ckpt");
  61. Console.WriteLine($"Model saved in path: {save_path}");
  62. });
  63. }
  64. public void Save2()
  65. {
  66. var v1 = tf.get_variable("v1", shape: new TensorShape(3), initializer: tf.zeros_initializer);
  67. var v2 = tf.get_variable("v2", shape: new TensorShape(5), initializer: tf.zeros_initializer);
  68. var inc_v1 = v1.assign(v1 + 1.0f);
  69. var dec_v2 = v2.assign(v2 - 1.0f);
  70. // Add an op to initialize the variables.
  71. var init_op = tf.global_variables_initializer();
  72. // Add ops to save and restore all the variables.
  73. var saver = tf.train.Saver();
  74. with<Session>(tf.Session(), sess =>
  75. {
  76. sess.run(init_op);
  77. // o some work with the model.
  78. inc_v1.op.run();
  79. dec_v2.op.run();
  80. // Save the variables to disk.
  81. var save_path = saver.save(sess, "/tmp/model2.ckpt");
  82. Console.WriteLine($"Model saved in path: {save_path}");
  83. });
  84. }
  85. }
  86. }

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