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.3 kB

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