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 2.6 kB

7 years ago
6 years ago
6 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace Tensorflow
  18. {
  19. public class Session : BaseSession, ITensorFlowObject
  20. {
  21. public Session(string target = "", Graph g = null) : base(target, g, null)
  22. { }
  23. public Session(IntPtr handle, Graph g = null) : base(handle, g)
  24. { }
  25. public Session(Graph g, ConfigProto config = null, Status s = null) : base("", g, config, s)
  26. { }
  27. public Session as_default()
  28. {
  29. return ops.set_default_session(this);
  30. }
  31. public static Session LoadFromSavedModel(string path)
  32. {
  33. var graph = new Graph();
  34. using var status = new Status();
  35. using var opt = c_api.TF_NewSessionOptions();
  36. var tags = new string[] { "serve" };
  37. var sess = c_api.TF_LoadSessionFromSavedModel(opt,
  38. IntPtr.Zero,
  39. path,
  40. tags,
  41. tags.Length,
  42. graph,
  43. IntPtr.Zero,
  44. status.Handle);
  45. status.Check(true);
  46. // load graph bytes
  47. // var data = new byte[buffer.length];
  48. // Marshal.Copy(buffer.data, data, 0, (int)buffer.length);
  49. // var meta_graph = MetaGraphDef.Parser.ParseFrom(data);*/
  50. return new Session(sess, g: graph);
  51. }
  52. public static implicit operator IntPtr(Session session) => session._handle;
  53. public static implicit operator Session(IntPtr handle) => new Session(handle);
  54. public void __enter__()
  55. {
  56. }
  57. public void __exit__()
  58. {
  59. }
  60. public void __init__()
  61. {
  62. }
  63. public void __del__()
  64. {
  65. }
  66. }
  67. }