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.

Session.cs 3.6 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.IO;
  15. using System.Runtime.CompilerServices;
  16. using Tensorflow.Util;
  17. using static Tensorflow.Binding;
  18. namespace Tensorflow
  19. {
  20. public class Session : BaseSession, IObjectLife
  21. {
  22. public Session(string target = "", Graph g = null) : base(target, g, null)
  23. { }
  24. public Session(IntPtr handle, Graph g = null) : base("", g, null)
  25. {
  26. _handle = handle;
  27. }
  28. public Session(Graph g, SessionOptions opts = null, Status s = null) : base("", g, opts, s)
  29. { }
  30. public Session as_default()
  31. {
  32. tf._defaultSessionFactory.Value = this;
  33. return this;
  34. }
  35. [MethodImpl(MethodImplOptions.NoOptimization)]
  36. public static Session LoadFromSavedModel(string path)
  37. {
  38. lock (Locks.ProcessWide)
  39. {
  40. var graph = c_api.TF_NewGraph();
  41. var status = new Status();
  42. var opt = new SessionOptions();
  43. var tags = new string[] {"serve"};
  44. var buffer = new TF_Buffer();
  45. IntPtr sess;
  46. try
  47. {
  48. sess = c_api.TF_LoadSessionFromSavedModel(opt,
  49. IntPtr.Zero,
  50. path,
  51. tags,
  52. tags.Length,
  53. graph,
  54. ref buffer,
  55. status);
  56. status.Check(true);
  57. } catch (TensorflowException ex) when (ex.Message.Contains("Could not find SavedModel"))
  58. {
  59. status = new Status();
  60. sess = c_api.TF_LoadSessionFromSavedModel(opt,
  61. IntPtr.Zero,
  62. Path.GetFullPath(path),
  63. tags,
  64. tags.Length,
  65. graph,
  66. ref buffer,
  67. status);
  68. status.Check(true);
  69. }
  70. // load graph bytes
  71. // var data = new byte[buffer.length];
  72. // Marshal.Copy(buffer.data, data, 0, (int)buffer.length);
  73. // var meta_graph = MetaGraphDef.Parser.ParseFrom(data);*/
  74. return new Session(sess, g: new Graph(graph)).as_default();
  75. }
  76. }
  77. public static implicit operator IntPtr(Session session) => session._handle;
  78. public static implicit operator Session(IntPtr handle) => new Session(handle);
  79. public void __enter__()
  80. {
  81. }
  82. public void __exit__()
  83. {
  84. }
  85. public void __init__()
  86. {
  87. }
  88. public void __del__()
  89. {
  90. }
  91. }
  92. }