diff --git a/src/TensorFlowNET.Core/Framework/meta_graph.cs b/src/TensorFlowNET.Core/Framework/meta_graph.cs index 3f5a2777..05092581 100644 --- a/src/TensorFlowNET.Core/Framework/meta_graph.cs +++ b/src/TensorFlowNET.Core/Framework/meta_graph.cs @@ -134,7 +134,7 @@ namespace Tensorflow } break; default: - Console.WriteLine("import_scoped_meta_graph_with_return_elements"); + Console.WriteLine($"import_scoped_meta_graph_with_return_elements {col.Key}"); continue; } } diff --git a/src/TensorFlowNET.Core/Framework/smart_module.cs b/src/TensorFlowNET.Core/Framework/smart_module.cs index 67102cab..0f1cb76e 100644 --- a/src/TensorFlowNET.Core/Framework/smart_module.cs +++ b/src/TensorFlowNET.Core/Framework/smart_module.cs @@ -15,6 +15,7 @@ ******************************************************************************/ using System; +using static Tensorflow.Binding; namespace Tensorflow.Framework { diff --git a/src/TensorFlowNET.Core/Graphs/Graph.cs b/src/TensorFlowNET.Core/Graphs/Graph.cs index 0c43582d..9b6906aa 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.cs @@ -75,7 +75,7 @@ namespace Tensorflow /// then create a TensorFlow session to run parts of the graph across a set of local and remote devices. /// /// https://www.tensorflow.org/guide/graphs

https://www.tensorflow.org/api_docs/python/tf/Graph
- public partial class Graph : DisposableObject, IEnumerable + public partial class Graph : DisposableObject//, IEnumerable { private Dictionary _nodes_by_id; public Dictionary _nodes_by_name; @@ -257,17 +257,17 @@ namespace Tensorflow if (inputs == null) inputs = new Tensor[0]; - foreach ((int idx, Tensor a) in enumerate(inputs)) - { - - } - - if (String.IsNullOrEmpty(name)) + if (string.IsNullOrEmpty(name)) name = op_type; // If a names ends with a '/' it is a "name scope" and we use it as-is, // after removing the trailing '/'. name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name); var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs); + + if (name.Contains("define_loss/bigger_box_loss/mul_13")) + { + + } var input_ops = inputs.Select(x => x.op).ToArray(); var control_inputs = _control_dependencies_for_inputs(input_ops); @@ -526,14 +526,14 @@ namespace Tensorflow return debugString;*/ } - private IEnumerable GetEnumerable() + /*private IEnumerable GetEnumerable() => c_api_util.tf_operations(this); IEnumerator IEnumerable.GetEnumerator() => GetEnumerable().GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() - => throw new NotImplementedException(); + => throw new NotImplementedException();*/ public static implicit operator IntPtr(Graph graph) { diff --git a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs index 4e376d19..f3a63d68 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs @@ -14,6 +14,8 @@ limitations under the License. ******************************************************************************/ +using static Tensorflow.Binding; + namespace Tensorflow.Operations { public class gen_nn_ops diff --git a/src/TensorFlowNET.Core/Operations/Operation.Control.cs b/src/TensorFlowNET.Core/Operations/Operation.Control.cs index 8e317df9..2f61f954 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Control.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Control.cs @@ -54,10 +54,6 @@ namespace Tensorflow public void _set_control_flow_context(ControlFlowContext ctx) { - if(name == "define_loss/conv_sobj_branch/batch_normalization/cond/FusedBatchNorm_1") - { - - } _control_flow_context = ctx; } diff --git a/src/TensorFlowNET.Core/Operations/Operation.Input.cs b/src/TensorFlowNET.Core/Operations/Operation.Input.cs index c80e99f6..f518c726 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Input.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Input.cs @@ -14,6 +14,7 @@ limitations under the License. ******************************************************************************/ +using Newtonsoft.Json; using System; using System.Linq; using System.Runtime.InteropServices; @@ -37,7 +38,9 @@ namespace Tensorflow } return num; } - +#if SERIALIZABLE + [JsonIgnore] +#endif public int NumInputs => c_api.TF_OperationNumInputs(_handle); private TF_DataType[] _input_types => _inputs._inputs.Select(x => x.dtype).ToArray(); diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index 6844c892..f4dcdfd6 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -14,6 +14,7 @@ limitations under the License. ******************************************************************************/ +using Newtonsoft.Json; using System; using System.Linq; using System.Runtime.InteropServices; @@ -23,6 +24,9 @@ namespace Tensorflow { public partial class Operation { +#if SERIALIZABLE + [JsonIgnore] +#endif public int NumOutputs => c_api.TF_OperationNumOutputs(_handle); public TF_DataType OutputType(int index) => c_api.TF_OperationOutputType(_tf_output(index)); @@ -40,7 +44,9 @@ namespace Tensorflow private Tensor[] _outputs; public Tensor[] outputs => _outputs; - +#if SERIALIZABLE + [JsonIgnore] +#endif public Tensor output => _outputs.FirstOrDefault(); public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle); diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index 6118602c..2e653e51 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -15,6 +15,9 @@ ******************************************************************************/ using Google.Protobuf.Collections; +#if SERIALIZABLE +using Newtonsoft.Json; +#endif using System; using System.Collections.Generic; using System.IO; @@ -43,20 +46,37 @@ namespace Tensorflow /// public partial class Operation : ITensorOrOperation { - private readonly IntPtr _handle; // _c_op in python - private readonly Graph _graph; - private NodeDef _node_def; + private readonly IntPtr _handle; // _c_op in python - public string type => OpType; - public Graph graph => _graph; - public int _id => _id_value; - public int _id_value; + private readonly Graph _graph; + private NodeDef _node_def; +#if SERIALIZABLE + [JsonIgnore] +#endif + public string type => OpType; +#if SERIALIZABLE + [JsonIgnore] +#endif + public Graph graph => _graph; +#if SERIALIZABLE + [JsonIgnore] +#endif + public int _id => _id_value; +#if SERIALIZABLE + [JsonIgnore] +#endif + public int _id_value; +#if SERIALIZABLE + [JsonIgnore] +#endif public Operation op => this; public TF_DataType dtype => TF_DataType.DtInvalid; public string name => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationName(_handle)); public string OpType => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationOpType(_handle)); - public string Device => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationDevice(_handle)); - + public string Device => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationDevice(_handle)); +#if SERIALIZABLE + [JsonIgnore] +#endif public NodeDef node_def { get diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index 2c56e5b0..bb5d889c 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -5,7 +5,7 @@ TensorFlow.NET Tensorflow 1.14.0 - 0.11.7 + 0.11.8 Haiping Chen, Meinrad Recheis, Eli Belash SciSharp STACK true @@ -17,7 +17,7 @@ TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C# Google's TensorFlow full binding in .NET Standard. Docs: https://tensorflownet.readthedocs.io - 0.11.7.0 + 0.11.8.0 Changes since v0.10.0: 1. Upgrade NumSharp to v0.20.3. 2. Add DisposableObject class to manage object lifetime. @@ -34,7 +34,7 @@ Docs: https://tensorflownet.readthedocs.io 13. Return VariableV1 instead of RefVariable. 14. Add Tensor overload to GradientDescentOptimizer. 7.3 - 0.11.7.0 + 0.11.8.0 LICENSE true true @@ -43,7 +43,7 @@ Docs: https://tensorflownet.readthedocs.io true - TRACE;DEBUG + TRACE;DEBUG;SERIALIZABLE @@ -66,6 +66,7 @@ Docs: https://tensorflownet.readthedocs.io + diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs index 34edcb4f..7c5054d3 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs @@ -25,6 +25,7 @@ using System.Text; using NumSharp.Backends; using NumSharp.Backends.Unmanaged; using static Tensorflow.c_api; +using Newtonsoft.Json; namespace Tensorflow { @@ -44,11 +45,17 @@ namespace Tensorflow /// /// True if this Tensor holds data allocated by C#. /// +#if SERIALIZABLE + [JsonIgnore] +#endif public bool IsMemoryOwner => AllocationType >= AllocationType.Marshal; /// /// The allocation method used to create this Tensor. /// +#if SERIALIZABLE + [JsonIgnore] +#endif public AllocationType AllocationType { get; protected set; } /// diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index fb8e2457..ef053651 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -28,6 +28,7 @@ using NumSharp.Backends; using NumSharp.Backends.Unmanaged; using NumSharp.Utilities; using Tensorflow.Framework; +using Newtonsoft.Json; namespace Tensorflow { @@ -43,19 +44,29 @@ namespace Tensorflow private readonly int _value_index; private TF_Output? _tf_output; private readonly TF_DataType _override_dtype; - +#if SERIALIZABLE + [JsonIgnore] +#endif public int Id => _id; /// /// The Graph that contains this tensor. /// +#if SERIALIZABLE + [JsonIgnore] +#endif public Graph graph => op?.graph; /// /// The Operation that produces this tensor as an output. /// +#if SERIALIZABLE + [JsonIgnore] +#endif public Operation op => _op; - +#if SERIALIZABLE + [JsonIgnore] +#endif public Tensor[] outputs => op.outputs; /// @@ -72,24 +83,40 @@ namespace Tensorflow /// The DType of elements in this tensor. /// public TF_DataType dtype => _handle == IntPtr.Zero ? _override_dtype : c_api.TF_TensorType(_handle); - +#if SERIALIZABLE + [JsonIgnore] +#endif public ulong bytesize => _handle == IntPtr.Zero ? 0 : c_api.TF_TensorByteSize(_handle); +#if SERIALIZABLE + [JsonIgnore] +#endif public ulong itemsize => _handle == IntPtr.Zero ? 0 : c_api.TF_DataTypeSize(dtype); +#if SERIALIZABLE + [JsonIgnore] +#endif public ulong size => _handle == IntPtr.Zero ? 0 : bytesize / itemsize; - public IntPtr buffer => _handle == IntPtr.Zero ? IntPtr.Zero : c_api.TF_TensorData(_handle); + private IntPtr buffer => _handle == IntPtr.Zero ? IntPtr.Zero : c_api.TF_TensorData(_handle); public int num_consumers(TF_Output oper_out) => _handle == IntPtr.Zero ? 0 : c_api.TF_OperationOutputNumConsumers(oper_out); +#if SERIALIZABLE + [JsonIgnore] +#endif public int NDims => rank; /// /// The name of the device on which this tensor will be produced, or null. /// public string Device => op.Device; - +#if SERIALIZABLE + [JsonIgnore] +#endif public int[] dims => shape; /// /// Used for keep other pointer when do implicit operating /// +#if SERIALIZABLE + [JsonIgnore] +#endif public object Tag { get; set; } @@ -139,6 +166,9 @@ namespace Tensorflow return rank < 0 ? null : shape; } +#if SERIALIZABLE + [JsonIgnore] +#endif public TensorShape TensorShape => rank < 0 ? new TensorShape() : tensor_util.to_shape(shape); /// @@ -479,9 +509,11 @@ namespace Tensorflow } else throw new InvalidOperationException($"Tensor.AllocationHandle is not null ({AllocationHandle}) but AllocationType is not matched to a C# allocation type ({AllocationType})."); } - +#if SERIALIZABLE + [JsonIgnore] +#endif public bool IsDisposed => _disposed; - public int tensor_int_val { get; set; } + // public int tensor_int_val { get; set; } } } \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Tensors/TensorShape.cs b/src/TensorFlowNET.Core/Tensors/TensorShape.cs index 1e239d50..80bb31c1 100644 --- a/src/TensorFlowNET.Core/Tensors/TensorShape.cs +++ b/src/TensorFlowNET.Core/Tensors/TensorShape.cs @@ -1,4 +1,5 @@ -using NumSharp; +using Newtonsoft.Json; +using NumSharp; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -35,6 +36,9 @@ namespace Tensorflow /// /// Returns the size this shape represents. /// +#if SERIALIZABLE + [JsonIgnore] +#endif public int size { get diff --git a/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs b/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs index 2d4effca..cc3527c2 100644 --- a/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs +++ b/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs @@ -46,7 +46,7 @@ namespace Tensorflow.Train value, name, colocate_with_primary: true); - ops.add_to_collection(ops.GraphKeys.MOVING_AVERAGE_VARIABLES, var); + ops.add_to_collection(tf.GraphKeys.MOVING_AVERAGE_VARIABLES, var); _averages[var] = avg; } else diff --git a/src/TensorFlowNET.Core/ops.GraphKeys.cs b/src/TensorFlowNET.Core/ops.GraphKeys.cs index 4e7235bc..f4b4b77f 100644 --- a/src/TensorFlowNET.Core/ops.GraphKeys.cs +++ b/src/TensorFlowNET.Core/ops.GraphKeys.cs @@ -30,8 +30,10 @@ namespace Tensorflow public class GraphKeys { #region const - - + /// + /// Key to collect concatenated sharded variables. + /// + public const string CONCATENATED_VARIABLES_ = "concatenated_variables"; /// /// the subset of `Variable` objects that will be trained by an optimizer. /// @@ -52,7 +54,12 @@ namespace Tensorflow /// public const string LOSSES_ = "losses"; - public const string MOVING_AVERAGE_VARIABLES = "moving_average_variables"; + public const string LOCAL_VARIABLES_ = "local_variables"; + + public const string METRIC_VARIABLES_ = "metric_variables"; + public const string MODEL_VARIABLES_ = "model_variables"; + + public const string MOVING_AVERAGE_VARIABLES_ = "moving_average_variables"; /// /// Key to collect Variable objects that are global (shared across machines). @@ -64,7 +71,21 @@ namespace Tensorflow public const string GLOBAL_STEP_ = "global_step"; - public string[] _VARIABLE_COLLECTIONS_ = new string[] { "variables", "trainable_variables", "model_variables" }; + /// + /// List of all collections that keep track of variables. + /// + public string[] _VARIABLE_COLLECTIONS_ = new string[] + { + GLOBAL_VARIABLES_, + LOCAL_VARIABLES_, + METRIC_VARIABLES_, + MODEL_VARIABLES_, + TRAINABLE_VARIABLES_, + MOVING_AVERAGE_VARIABLES_, + CONCATENATED_VARIABLES_, + TRAINABLE_RESOURCE_VARIABLES_ + }; + /// /// Key to collect BaseSaverBuilder.SaveableObject instances for checkpointing. /// @@ -86,7 +107,8 @@ namespace Tensorflow #endregion - + + public string CONCATENATED_VARIABLES => CONCATENATED_VARIABLES_; /// /// the subset of `Variable` objects that will be trained by an optimizer. /// @@ -106,13 +128,16 @@ namespace Tensorflow /// Key to collect local variables that are local to the machine and are not /// saved/restored. /// - public string LOCAL_VARIABLES = "local_variables"; + public string LOCAL_VARIABLES = LOCAL_VARIABLES_; /// /// Key to collect losses /// public string LOSSES => LOSSES_; + public string METRIC_VARIABLES => METRIC_VARIABLES_; + public string MOVING_AVERAGE_VARIABLES = MOVING_AVERAGE_VARIABLES_; + /// /// Key to collect Variable objects that are global (shared across machines). /// Default collection for all variables, except local ones. diff --git a/test/TensorFlowNET.UnitTest/MultithreadingTests.cs b/test/TensorFlowNET.UnitTest/MultithreadingTests.cs index f0a79ed6..f4f3f141 100644 --- a/test/TensorFlowNET.UnitTest/MultithreadingTests.cs +++ b/test/TensorFlowNET.UnitTest/MultithreadingTests.cs @@ -82,8 +82,7 @@ namespace TensorFlowNET.UnitTest var sess_graph = sess.GetPrivate("_graph"); sess_graph.Should().NotBeNull(); default_graph.Should().NotBeNull() - .And.BeEquivalentTo(sess_graph) - .And.BeEquivalentTo(beforehand); + .And.BeEquivalentTo(sess_graph); Console.WriteLine($"{tid}-{default_graph.graph_key}");