From 43febca3ea501a1a1a21e2892ce451d199413ed3 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 10 Sep 2019 23:21:37 -0500 Subject: [PATCH] added many not implemented interfaces for Object Detection API. #386 --- src/TensorFlowNET.Core/APIs/tf.estimator.cs | 21 +++++++++- src/TensorFlowNET.Core/Binding.cs | 2 + .../Estimators/Estimator.cs | 32 +++++++++++++++- src/TensorFlowNET.Core/Estimators/EvalSpec.cs | 14 +++++++ src/TensorFlowNET.Core/Estimators/Exporter.cs | 11 ++++++ .../Estimators/FinalExporter.cs | 14 +++++++ .../Estimators/RunConfig.cs | 5 ++- .../Estimators/TrainSpec.cs | 16 ++++++++ src/TensorFlowNET.Core/Estimators/Training.cs | 4 +- .../Estimators/_Evaluator.cs | 14 +++++++ .../_NewCheckpointListenerForEvaluate.cs | 10 +++++ .../Estimators/_SavedModelExporter.cs | 14 +++++++ .../Estimators/_TrainingExecutor.cs | 12 ++++-- .../Entities/TrainAndEvalDict.cs | 18 +++++++++ .../MetaArchitectures/FasterRCNNMetaArch.cs | 10 +++++ .../ObjectDetection/ModelLib.cs | 38 ++++++++++++++++++- .../ImageProcessing/ObjectDetection/Main.cs | 29 +++++++++----- 17 files changed, 242 insertions(+), 22 deletions(-) create mode 100644 src/TensorFlowNET.Core/Estimators/EvalSpec.cs create mode 100644 src/TensorFlowNET.Core/Estimators/Exporter.cs create mode 100644 src/TensorFlowNET.Core/Estimators/FinalExporter.cs create mode 100644 src/TensorFlowNET.Core/Estimators/TrainSpec.cs create mode 100644 src/TensorFlowNET.Core/Estimators/_Evaluator.cs create mode 100644 src/TensorFlowNET.Core/Estimators/_NewCheckpointListenerForEvaluate.cs create mode 100644 src/TensorFlowNET.Core/Estimators/_SavedModelExporter.cs create mode 100644 src/TensorFlowNET.Models/ObjectDetection/Entities/TrainAndEvalDict.cs create mode 100644 src/TensorFlowNET.Models/ObjectDetection/MetaArchitectures/FasterRCNNMetaArch.cs diff --git a/src/TensorFlowNET.Core/APIs/tf.estimator.cs b/src/TensorFlowNET.Core/APIs/tf.estimator.cs index d97943b1..8314f202 100644 --- a/src/TensorFlowNET.Core/APIs/tf.estimator.cs +++ b/src/TensorFlowNET.Core/APIs/tf.estimator.cs @@ -32,8 +32,25 @@ namespace Tensorflow public RunConfig RunConfig(string model_dir) => new RunConfig(model_dir: model_dir); - public void train_and_evaluate() - => Training.train_and_evaluate(); + public void train_and_evaluate(Estimator estimator, TrainSpec train_spec, EvalSpec eval_spec) + => Training.train_and_evaluate(estimator: estimator, train_spec: train_spec, eval_spec: eval_spec); + + public TrainSpec TrainSpec(Action input_fn, int max_steps) + => new TrainSpec(input_fn: input_fn, max_steps: max_steps); + + /// + /// Create an `Exporter` to use with `tf.estimator.EvalSpec`. + /// + /// + /// + /// + /// + public FinalExporter FinalExporter(string name, Action serving_input_receiver_fn, bool as_text = false) + => new FinalExporter(name: name, serving_input_receiver_fn: serving_input_receiver_fn, + as_text: as_text); + + public EvalSpec EvalSpec(string name, Action input_fn, FinalExporter exporters) + => new EvalSpec(name: name, input_fn: input_fn, exporters: exporters); } } } diff --git a/src/TensorFlowNET.Core/Binding.cs b/src/TensorFlowNET.Core/Binding.cs index e3136f83..34dfbbdb 100644 --- a/src/TensorFlowNET.Core/Binding.cs +++ b/src/TensorFlowNET.Core/Binding.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Dynamic; using System.Text; namespace Tensorflow @@ -17,6 +18,7 @@ namespace Tensorflow /// /// Used for TensorShape None /// + /// public static readonly int Unknown = -1; } } diff --git a/src/TensorFlowNET.Core/Estimators/Estimator.cs b/src/TensorFlowNET.Core/Estimators/Estimator.cs index ae6c3670..aa0ca9f8 100644 --- a/src/TensorFlowNET.Core/Estimators/Estimator.cs +++ b/src/TensorFlowNET.Core/Estimators/Estimator.cs @@ -1,16 +1,44 @@ using System; using System.Collections.Generic; using System.Text; +using static Tensorflow.Binding; namespace Tensorflow.Estimators { + /// + /// Estimator class to train and evaluate TensorFlow models. + /// public class Estimator : IObjectLife { - public RunConfig config; + RunConfig _config; + ConfigProto _session_config; + string _model_dir; public Estimator(RunConfig config) { - this.config = config; + _config = config; + _model_dir = _config.model_dir; + _session_config = _config.session_config; + } + + public Estimator train(Action input_fn, int max_steps = 1, + _NewCheckpointListenerForEvaluate[] saving_listeners = null) + { + _train_model(); + throw new NotImplementedException(""); + } + + private void _train_model() + { + _train_model_default(); + } + + private void _train_model_default() + { + using (var g = tf.Graph().as_default()) + { + + } } public void __init__() diff --git a/src/TensorFlowNET.Core/Estimators/EvalSpec.cs b/src/TensorFlowNET.Core/Estimators/EvalSpec.cs new file mode 100644 index 00000000..ca9ff94f --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/EvalSpec.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class EvalSpec + { + public EvalSpec(string name, Action input_fn, FinalExporter exporters) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Estimators/Exporter.cs b/src/TensorFlowNET.Core/Estimators/Exporter.cs new file mode 100644 index 00000000..2610458e --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/Exporter.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public abstract class Exporter + { + public abstract void export(Estimator estimator, string export_path, string checkpoint_path); + } +} diff --git a/src/TensorFlowNET.Core/Estimators/FinalExporter.cs b/src/TensorFlowNET.Core/Estimators/FinalExporter.cs new file mode 100644 index 00000000..af78c2bf --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/FinalExporter.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class FinalExporter + { + public FinalExporter(string name, Action serving_input_receiver_fn, bool as_text = false) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Estimators/RunConfig.cs b/src/TensorFlowNET.Core/Estimators/RunConfig.cs index a5ec5807..0b15390a 100644 --- a/src/TensorFlowNET.Core/Estimators/RunConfig.cs +++ b/src/TensorFlowNET.Core/Estimators/RunConfig.cs @@ -4,9 +4,12 @@ namespace Tensorflow.Estimators { public class RunConfig { + public string model_dir { get; set; } + public ConfigProto session_config { get; set; } + public RunConfig(string model_dir) { - + this.model_dir = model_dir; } } } diff --git a/src/TensorFlowNET.Core/Estimators/TrainSpec.cs b/src/TensorFlowNET.Core/Estimators/TrainSpec.cs new file mode 100644 index 00000000..2252b53c --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/TrainSpec.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class TrainSpec + { + public int max_steps { get; set; } + + public TrainSpec(Action input_fn, int max_steps) + { + this.max_steps = max_steps; + } + } +} diff --git a/src/TensorFlowNET.Core/Estimators/Training.cs b/src/TensorFlowNET.Core/Estimators/Training.cs index 2d43fa4e..a4604181 100644 --- a/src/TensorFlowNET.Core/Estimators/Training.cs +++ b/src/TensorFlowNET.Core/Estimators/Training.cs @@ -6,9 +6,9 @@ namespace Tensorflow.Estimators { public class Training { - public static void train_and_evaluate() + public static void train_and_evaluate(Estimator estimator, TrainSpec train_spec, EvalSpec eval_spec) { - var executor = new _TrainingExecutor(); + var executor = new _TrainingExecutor(estimator: estimator, train_spec: train_spec, eval_spec: eval_spec); executor.run(); } } diff --git a/src/TensorFlowNET.Core/Estimators/_Evaluator.cs b/src/TensorFlowNET.Core/Estimators/_Evaluator.cs new file mode 100644 index 00000000..bd0aa4be --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/_Evaluator.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class _Evaluator + { + public _Evaluator(Estimator estimator, EvalSpec eval_spec, int max_training_steps) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Estimators/_NewCheckpointListenerForEvaluate.cs b/src/TensorFlowNET.Core/Estimators/_NewCheckpointListenerForEvaluate.cs new file mode 100644 index 00000000..2850255f --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/_NewCheckpointListenerForEvaluate.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class _NewCheckpointListenerForEvaluate + { + } +} diff --git a/src/TensorFlowNET.Core/Estimators/_SavedModelExporter.cs b/src/TensorFlowNET.Core/Estimators/_SavedModelExporter.cs new file mode 100644 index 00000000..9beb35ea --- /dev/null +++ b/src/TensorFlowNET.Core/Estimators/_SavedModelExporter.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Estimators +{ + public class _SavedModelExporter : Exporter + { + public override void export(Estimator estimator, string export_path, string checkpoint_path) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Estimators/_TrainingExecutor.cs b/src/TensorFlowNET.Core/Estimators/_TrainingExecutor.cs index c3787e30..c5759183 100644 --- a/src/TensorFlowNET.Core/Estimators/_TrainingExecutor.cs +++ b/src/TensorFlowNET.Core/Estimators/_TrainingExecutor.cs @@ -7,10 +7,12 @@ namespace Tensorflow.Estimators public class _TrainingExecutor { Estimator _estimator; + EvalSpec _eval_spec; + TrainSpec _train_spec; - public _TrainingExecutor(Estimator estimator) + public _TrainingExecutor(Estimator estimator, TrainSpec train_spec, EvalSpec eval_spec) { - + _estimator = estimator; } public void run() @@ -20,7 +22,11 @@ namespace Tensorflow.Estimators private void run_local() { - + var evaluator = new _Evaluator(_estimator, _eval_spec, _train_spec.max_steps); + /*_estimator.train(input_fn: _train_spec.input_fn, + max_steps: _train_spec.max_steps, + hooks: train_hooks, + saving_listeners: saving_listeners);*/ } } } diff --git a/src/TensorFlowNET.Models/ObjectDetection/Entities/TrainAndEvalDict.cs b/src/TensorFlowNET.Models/ObjectDetection/Entities/TrainAndEvalDict.cs new file mode 100644 index 00000000..2f519c0c --- /dev/null +++ b/src/TensorFlowNET.Models/ObjectDetection/Entities/TrainAndEvalDict.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Estimators; + +namespace Tensorflow.Models.ObjectDetection +{ + public class TrainAndEvalDict + { + public Estimator estimator { get; set; } + public Action train_input_fn { get; set; } + public Action[] eval_input_fns { get; set; } + public string[] eval_input_names { get; set; } + public Action eval_on_train_input_fn { get; set; } + public Action predict_input_fn { get; set; } + public int train_steps { get; set; } + } +} diff --git a/src/TensorFlowNET.Models/ObjectDetection/MetaArchitectures/FasterRCNNMetaArch.cs b/src/TensorFlowNET.Models/ObjectDetection/MetaArchitectures/FasterRCNNMetaArch.cs new file mode 100644 index 00000000..2b501a46 --- /dev/null +++ b/src/TensorFlowNET.Models/ObjectDetection/MetaArchitectures/FasterRCNNMetaArch.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Models.ObjectDetection.MetaArchitectures +{ + public class FasterRCNNMetaArch + { + } +} diff --git a/src/TensorFlowNET.Models/ObjectDetection/ModelLib.cs b/src/TensorFlowNET.Models/ObjectDetection/ModelLib.cs index 3a9a851b..d8543970 100644 --- a/src/TensorFlowNET.Models/ObjectDetection/ModelLib.cs +++ b/src/TensorFlowNET.Models/ObjectDetection/ModelLib.cs @@ -3,19 +3,53 @@ using System.Collections.Generic; using System.Text; using static Tensorflow.Binding; using Tensorflow.Estimators; +using System.Linq; namespace Tensorflow.Models.ObjectDetection { public class ModelLib { - public void create_estimator_and_inputs(RunConfig run_config) + public TrainAndEvalDict create_estimator_and_inputs(RunConfig run_config, + int train_steps = 1) { var estimator = tf.estimator.Estimator(config: run_config); + + return new TrainAndEvalDict + { + estimator = estimator, + train_steps = train_steps, + eval_input_fns = new Action[0] + }; } - public void create_train_and_eval_specs() + public (TrainSpec, EvalSpec[]) create_train_and_eval_specs(Action train_input_fn, Action[] eval_input_fns, Action eval_on_train_input_fn, + Action predict_input_fn, int train_steps, bool eval_on_train_data = false, + string final_exporter_name = "Servo", string[] eval_spec_names = null) { + var train_spec = tf.estimator.TrainSpec(input_fn: train_input_fn, max_steps: train_steps); + + if (eval_spec_names == null) + eval_spec_names = range(len(eval_input_fns)) + .Select(x => x.ToString()) + .ToArray(); + + var eval_specs = new List() + { + new EvalSpec("", null, null) // for test. + }; + foreach (var (index, (eval_spec_name, eval_input_fn)) in enumerate(zip(eval_spec_names, eval_input_fns).ToList())) + { + var exporter_name = index == 0 ? final_exporter_name : $"{final_exporter_name}_{eval_spec_name}"; + var exporter = tf.estimator.FinalExporter(name: exporter_name, serving_input_receiver_fn: predict_input_fn); + eval_specs.Add(tf.estimator.EvalSpec(name: eval_spec_name, + input_fn: eval_input_fn, + exporters: exporter)); + } + + if (eval_on_train_data) + throw new NotImplementedException(""); + return (train_spec, eval_specs.ToArray()); } } } diff --git a/test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection/Main.cs b/test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection/Main.cs index 44667996..9e25616c 100644 --- a/test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection/Main.cs +++ b/test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection/Main.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using Tensorflow; +using Tensorflow.Estimators; using Tensorflow.Models.ObjectDetection; using static Tensorflow.Binding; @@ -15,20 +16,30 @@ namespace TensorFlowNET.Examples.ImageProcessing.ObjectDetection public string Name => "Object Detection API"; - dynamic FLAGS = new - { - model_dir = "D:/Projects/PythonLab/tf-models/research/object_detection/models/model" - }; - ModelLib model_lib = new ModelLib(); public bool Run() { - var config = tf.estimator.RunConfig(model_dir: FLAGS.model_dir); - model_lib.create_estimator_and_inputs(run_config: config); + string model_dir = "D:/Projects/PythonLab/tf-models/research/object_detection/models/model"; + + var config = tf.estimator.RunConfig(model_dir: model_dir); + var train_and_eval_dict = model_lib.create_estimator_and_inputs(run_config: config); + var estimator = train_and_eval_dict.estimator; + var train_input_fn = train_and_eval_dict.train_input_fn; + var eval_input_fns = train_and_eval_dict.eval_input_fns; + var eval_on_train_input_fn = train_and_eval_dict.eval_on_train_input_fn; + var predict_input_fn = train_and_eval_dict.predict_input_fn; + var train_steps = train_and_eval_dict.train_steps; + + var (train_spec, eval_specs) = model_lib.create_train_and_eval_specs(train_input_fn, + eval_input_fns, + eval_on_train_input_fn, + predict_input_fn, + train_steps, + eval_on_train_data: false); // Currently only a single Eval Spec is allowed. - tf.estimator.train_and_evaluate(); + tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0]); return true; } @@ -53,8 +64,6 @@ namespace TensorFlowNET.Examples.ImageProcessing.ObjectDetection throw new NotImplementedException(); } - - public void Train(Session sess) { throw new NotImplementedException();