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