From ebfb3275cfc818c7a525dfe28326cf476c2d8526 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 25 Dec 2021 09:42:19 -0600 Subject: [PATCH] clean code. --- src/TensorFlowNET.Core/Binding.Util.cs | 42 ++---------- .../Framework/random_seed.cs | 13 ++-- .../Graphs/NullContextmanager.cs | 32 ---------- src/TensorFlowNET.Core/Sessions/Session.cs | 2 +- .../Tensorflow.Binding.csproj | 2 +- src/TensorFlowNET.Core/ops._DefaultStack.cs | 64 ------------------- src/TensorFlowNET.Core/ops.name_scope.cs | 9 +-- src/TensorFlowNET.Keras/Engine/Layer.Apply.cs | 22 ++++--- .../Layer.FunctionalConstructionCall.cs | 37 +++++------ .../Tensorflow.Keras.csproj | 2 +- .../PythonTest.cs | 25 -------- .../Basics/TrainSaverTest.cs | 5 +- test/TensorFlowNET.UnitTest/PythonTest.cs | 25 -------- 13 files changed, 54 insertions(+), 226 deletions(-) delete mode 100644 src/TensorFlowNET.Core/Graphs/NullContextmanager.cs delete mode 100644 src/TensorFlowNET.Core/ops._DefaultStack.cs diff --git a/src/TensorFlowNET.Core/Binding.Util.cs b/src/TensorFlowNET.Core/Binding.Util.cs index 9ff0c06d..31902f14 100644 --- a/src/TensorFlowNET.Core/Binding.Util.cs +++ b/src/TensorFlowNET.Core/Binding.Util.cs @@ -203,49 +203,21 @@ namespace Tensorflow yield return values[i]; } - [DebuggerStepThrough] - public static void tf_with(ITensorFlowObject py, Action action) - { - try - { - py.__enter__(); - action(py); - } - finally - { - py.__exit__(); - py.Dispose(); - } - } - [DebuggerStepThrough] public static void tf_with(T py, Action action) where T : ITensorFlowObject { - try - { - py.__enter__(); - action(py); - } - finally - { - py.__exit__(); - py.Dispose(); - } + py.__enter__(); + action(py); + py.__exit__(); } [DebuggerStepThrough] public static TOut tf_with(TIn py, Func action) where TIn : ITensorFlowObject { - try - { - py.__enter__(); - return action(py); - } - finally - { - py.__exit__(); - py.Dispose(); - } + py.__enter__(); + var result = action(py); + py.__exit__(); + return result; } public static float time() diff --git a/src/TensorFlowNET.Core/Framework/random_seed.cs b/src/TensorFlowNET.Core/Framework/random_seed.cs index bd962c74..ccc09fb2 100644 --- a/src/TensorFlowNET.Core/Framework/random_seed.cs +++ b/src/TensorFlowNET.Core/Framework/random_seed.cs @@ -60,21 +60,22 @@ namespace Tensorflow var (seed, seed2) = get_seed(op_seed); Tensor _seed, _seed2; if (seed is null) - _seed = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed"); + _seed = constant_op.constant(0L, name: "seed"); else - _seed = constant_op.constant(seed.Value, dtype: TF_DataType.TF_INT64, name: "seed"); + _seed = constant_op.constant((long)seed.Value, name: "seed"); if (seed2 is null) - _seed2 = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed2"); + _seed2 = constant_op.constant(0L, name: "seed2"); else { _seed2 = tf_with(ops.name_scope("seed2"), scope => { - _seed2 = constant_op.constant(seed2.Value, dtype: TF_DataType.TF_INT64); + _seed2 = constant_op.constant((long)seed2.Value); return array_ops.where_v2( math_ops.logical_and( - math_ops.equal(_seed, 0l), math_ops.equal(_seed2, 0l)), - constant_op.constant(2^31 - 1, dtype: dtypes.int64), + math_ops.equal(_seed, 0L), + math_ops.equal(_seed2, 0L)), + constant_op.constant(2^31L - 1), _seed2, name: scope); }); diff --git a/src/TensorFlowNET.Core/Graphs/NullContextmanager.cs b/src/TensorFlowNET.Core/Graphs/NullContextmanager.cs deleted file mode 100644 index 7a2e2dc3..00000000 --- a/src/TensorFlowNET.Core/Graphs/NullContextmanager.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; - -namespace Tensorflow -{ - public class NullContextmanager : ITensorFlowObject - { - public void __init__() - { - throw new NotImplementedException(); - } - - public void __enter__() - { - throw new NotImplementedException(); - } - - public void __del__() - { - throw new NotImplementedException(); - } - - public void __exit__() - { - throw new NotImplementedException(); - } - - public void Dispose() - { - throw new NotImplementedException(); - } - } -} diff --git a/src/TensorFlowNET.Core/Sessions/Session.cs b/src/TensorFlowNET.Core/Sessions/Session.cs index 5b842408..05178fb8 100644 --- a/src/TensorFlowNET.Core/Sessions/Session.cs +++ b/src/TensorFlowNET.Core/Sessions/Session.cs @@ -21,7 +21,7 @@ using Tensorflow.Util; namespace Tensorflow { - public class Session : BaseSession, ITensorFlowObject + public class Session : BaseSession { public Session(string target = "", Graph g = null) : base(target, g, null) { } diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index dccdd98f..10fa0662 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -2,7 +2,7 @@ netstandard2.0 - TensorFlow.NET + Tensorflow.Binding Tensorflow 2.2.0 0.70.0 diff --git a/src/TensorFlowNET.Core/ops._DefaultStack.cs b/src/TensorFlowNET.Core/ops._DefaultStack.cs deleted file mode 100644 index 394b90d2..00000000 --- a/src/TensorFlowNET.Core/ops._DefaultStack.cs +++ /dev/null @@ -1,64 +0,0 @@ -/***************************************************************************** - Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -******************************************************************************/ - -using System; -using System.Collections.Generic; - -namespace Tensorflow -{ - public partial class ops - { - _DefaultStack _default_session_stack = new _DefaultStack(); - - public class _DefaultStack : ITensorFlowObject - { - Stack stack; -#pragma warning disable CS0414 // The field 'ops._DefaultStack._enforce_nesting' is assigned but its value is never used - bool _enforce_nesting = true; -#pragma warning restore CS0414 // The field 'ops._DefaultStack._enforce_nesting' is assigned but its value is never used - - public _DefaultStack() - { - stack = new Stack(); - } - - public void __enter__() - { - - } - - public void __exit__() - { - - } - - public void Dispose() - { - throw new NotImplementedException(); - } - - public void __init__() - { - - } - - public void __del__() - { - - } - } - } -} diff --git a/src/TensorFlowNET.Core/ops.name_scope.cs b/src/TensorFlowNET.Core/ops.name_scope.cs index da42c404..336afa2a 100644 --- a/src/TensorFlowNET.Core/ops.name_scope.cs +++ b/src/TensorFlowNET.Core/ops.name_scope.cs @@ -99,15 +99,16 @@ namespace Tensorflow [DebuggerStepThrough] public void Dispose() { - if (tf.Context.executing_eagerly()) - tf.Context.ScopeName = old_scope_name; - else - get_default_graph()._name_stack = old_scope_name; + } [DebuggerStepThrough] public void __exit__() { + if (tf.Context.executing_eagerly()) + tf.Context.ScopeName = old_scope_name; + else + get_default_graph()._name_stack = old_scope_name; } [DebuggerNonUserCode] diff --git a/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs b/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs index cabd56cd..fb37a89c 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs @@ -33,18 +33,20 @@ namespace Tensorflow.Keras.Engine else nameScope = _name_scope(); - tf_with(ops.name_scope(nameScope), scope => - { - if (!built) - MaybeBuild(inputs); + var scope = ops.name_scope(nameScope); + scope.__enter__(); + + if (!built) + MaybeBuild(inputs); + + outputs = Call(inputs, state: state, training: training); - outputs = Call(inputs, state: state, training: training); + // memory leak + // _set_connectivity_metadata_(inputs, outputs); + _handle_activity_regularization(inputs, outputs); + _set_mask_metadata(inputs, outputs, null); - // memory leak - // _set_connectivity_metadata_(inputs, outputs); - _handle_activity_regularization(inputs, outputs); - _set_mask_metadata(inputs, outputs, null); - }); + scope.__exit__(); return outputs; } diff --git a/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs b/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs index 2e00275d..1d96e581 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs @@ -26,26 +26,27 @@ namespace Tensorflow.Keras.Engine var graph = keras.backend.get_graph(); graph.as_default(); - tf_with(ops.name_scope(_name_scope()), scope => - { - MaybeBuild(inputs); - - // Wrapping `call` function in autograph to allow for dynamic control - // flow and control dependencies in call. We are limiting this to - // subclassed layers as autograph is strictly needed only for - // subclassed layers and models. - // tf_convert will respect the value of autograph setting in the - // enclosing tf.function, if any. - if (!dynamic) - throw new NotImplementedException(""); - - outputs = Call(inputs); + var scope = ops.name_scope(_name_scope()); + scope.__enter__(); + + MaybeBuild(inputs); + + // Wrapping `call` function in autograph to allow for dynamic control + // flow and control dependencies in call. We are limiting this to + // subclassed layers as autograph is strictly needed only for + // subclassed layers and models. + // tf_convert will respect the value of autograph setting in the + // enclosing tf.function, if any. + if (!dynamic) + throw new NotImplementedException(""); + + outputs = Call(inputs); - _set_connectivity_metadata_(inputs, outputs); - _handle_activity_regularization(inputs, outputs); - _set_mask_metadata(inputs, outputs, null); - }); + _set_connectivity_metadata_(inputs, outputs); + _handle_activity_regularization(inputs, outputs); + _set_mask_metadata(inputs, outputs, null); + scope.__exit__(); graph.Exit(); return outputs; diff --git a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj index 8e9f062d..3d448454 100644 --- a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj +++ b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj @@ -60,7 +60,7 @@ Keras is an API designed for human beings, not machines. Keras follows best prac - + diff --git a/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs b/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs index 329b3393..e49103a1 100644 --- a/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs @@ -252,35 +252,10 @@ namespace TensorFlowNET.UnitTest //else //{ s = self._create_session(graph, config, force_gpu); - self._constrain_devices_and_set_default(s, use_gpu, force_gpu); //} return s.as_default(); } - private ITensorFlowObject _constrain_devices_and_set_default(Session sess, bool useGpu, bool forceGpu) - { - //def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): - //"""Set the session and its graph to global default and constrain devices.""" - //if context.executing_eagerly(): - // yield None - //else: - // with sess.graph.as_default(), sess.as_default(): - // if force_gpu: - // # Use the name of an actual device if one is detected, or - // # '/device:GPU:0' otherwise - // gpu_name = gpu_device_name() - // if not gpu_name: - // gpu_name = "/device:GPU:0" - // with sess.graph.device(gpu_name): - // yield sess - // elif use_gpu: - // yield sess - // else: - // with sess.graph.device("/device:CPU:0"): - // yield sess - return sess; - } - // See session() for details. private Session _create_session(Graph graph, object cfg, bool forceGpu) { diff --git a/test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs b/test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs index 60020744..2a4a79dc 100644 --- a/test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs +++ b/test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs @@ -42,10 +42,7 @@ namespace TensorFlowNET.UnitTest.Basics public void ImportSavedModel() { - tf_with(Session.LoadFromSavedModel("mobilenet"), sess => - { - - }); + Session.LoadFromSavedModel("mobilenet"); } public void ImportGraphDefFromPbFile() diff --git a/test/TensorFlowNET.UnitTest/PythonTest.cs b/test/TensorFlowNET.UnitTest/PythonTest.cs index f0246337..0ee8762c 100644 --- a/test/TensorFlowNET.UnitTest/PythonTest.cs +++ b/test/TensorFlowNET.UnitTest/PythonTest.cs @@ -266,35 +266,10 @@ namespace TensorFlowNET.UnitTest //else //{ s = self._create_session(graph, config, force_gpu); - self._constrain_devices_and_set_default(s, use_gpu, force_gpu); //} return s.as_default(); } - private ITensorFlowObject _constrain_devices_and_set_default(Session sess, bool useGpu, bool forceGpu) - { - //def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): - //"""Set the session and its graph to global default and constrain devices.""" - //if context.executing_eagerly(): - // yield None - //else: - // with sess.graph.as_default(), sess.as_default(): - // if force_gpu: - // # Use the name of an actual device if one is detected, or - // # '/device:GPU:0' otherwise - // gpu_name = gpu_device_name() - // if not gpu_name: - // gpu_name = "/device:GPU:0" - // with sess.graph.device(gpu_name): - // yield sess - // elif use_gpu: - // yield sess - // else: - // with sess.graph.device("/device:CPU:0"): - // yield sess - return sess; - } - // See session() for details. private Session _create_session(Graph graph, object cfg, bool forceGpu) {