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

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