@@ -32,8 +32,25 @@ namespace Tensorflow | |||||
public RunConfig RunConfig(string model_dir) | public RunConfig RunConfig(string model_dir) | ||||
=> new RunConfig(model_dir: 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); | |||||
/// <summary> | |||||
/// Create an `Exporter` to use with `tf.estimator.EvalSpec`. | |||||
/// </summary> | |||||
/// <param name="name"></param> | |||||
/// <param name="serving_input_receiver_fn"></param> | |||||
/// <param name="as_text"></param> | |||||
/// <returns></returns> | |||||
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); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -1,5 +1,6 @@ | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Dynamic; | |||||
using System.Text; | using System.Text; | ||||
namespace Tensorflow | namespace Tensorflow | ||||
@@ -17,6 +18,7 @@ namespace Tensorflow | |||||
/// <summary> | /// <summary> | ||||
/// Used for TensorShape None | /// Used for TensorShape None | ||||
/// </summary> | /// </summary> | ||||
/// | |||||
public static readonly int Unknown = -1; | public static readonly int Unknown = -1; | ||||
} | } | ||||
} | } |
@@ -1,16 +1,44 @@ | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Text; | using System.Text; | ||||
using static Tensorflow.Binding; | |||||
namespace Tensorflow.Estimators | namespace Tensorflow.Estimators | ||||
{ | { | ||||
/// <summary> | |||||
/// Estimator class to train and evaluate TensorFlow models. | |||||
/// </summary> | |||||
public class Estimator : IObjectLife | public class Estimator : IObjectLife | ||||
{ | { | ||||
public RunConfig config; | |||||
RunConfig _config; | |||||
ConfigProto _session_config; | |||||
string _model_dir; | |||||
public Estimator(RunConfig config) | 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__() | public void __init__() | ||||
@@ -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) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -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); | |||||
} | |||||
} |
@@ -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) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -4,9 +4,12 @@ namespace Tensorflow.Estimators | |||||
{ | { | ||||
public class RunConfig | public class RunConfig | ||||
{ | { | ||||
public string model_dir { get; set; } | |||||
public ConfigProto session_config { get; set; } | |||||
public RunConfig(string model_dir) | public RunConfig(string model_dir) | ||||
{ | { | ||||
this.model_dir = model_dir; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -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; | |||||
} | |||||
} | |||||
} |
@@ -6,9 +6,9 @@ namespace Tensorflow.Estimators | |||||
{ | { | ||||
public class Training | 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(); | executor.run(); | ||||
} | } | ||||
} | } | ||||
@@ -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) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,10 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace Tensorflow.Estimators | |||||
{ | |||||
public class _NewCheckpointListenerForEvaluate | |||||
{ | |||||
} | |||||
} |
@@ -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) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -7,10 +7,12 @@ namespace Tensorflow.Estimators | |||||
public class _TrainingExecutor | public class _TrainingExecutor | ||||
{ | { | ||||
Estimator _estimator; | 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() | public void run() | ||||
@@ -20,7 +22,11 @@ namespace Tensorflow.Estimators | |||||
private void run_local() | 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);*/ | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -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; } | |||||
} | |||||
} |
@@ -0,0 +1,10 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace Tensorflow.Models.ObjectDetection.MetaArchitectures | |||||
{ | |||||
public class FasterRCNNMetaArch | |||||
{ | |||||
} | |||||
} |
@@ -3,19 +3,53 @@ using System.Collections.Generic; | |||||
using System.Text; | using System.Text; | ||||
using static Tensorflow.Binding; | using static Tensorflow.Binding; | ||||
using Tensorflow.Estimators; | using Tensorflow.Estimators; | ||||
using System.Linq; | |||||
namespace Tensorflow.Models.ObjectDetection | namespace Tensorflow.Models.ObjectDetection | ||||
{ | { | ||||
public class ModelLib | 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); | 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<EvalSpec>() | |||||
{ | |||||
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()); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -3,6 +3,7 @@ using System.Collections.Generic; | |||||
using System.IO; | using System.IO; | ||||
using System.Text; | using System.Text; | ||||
using Tensorflow; | using Tensorflow; | ||||
using Tensorflow.Estimators; | |||||
using Tensorflow.Models.ObjectDetection; | using Tensorflow.Models.ObjectDetection; | ||||
using static Tensorflow.Binding; | using static Tensorflow.Binding; | ||||
@@ -15,20 +16,30 @@ namespace TensorFlowNET.Examples.ImageProcessing.ObjectDetection | |||||
public string Name => "Object Detection API"; | 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(); | ModelLib model_lib = new ModelLib(); | ||||
public bool Run() | 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. | // 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; | return true; | ||||
} | } | ||||
@@ -53,8 +64,6 @@ namespace TensorFlowNET.Examples.ImageProcessing.ObjectDetection | |||||
throw new NotImplementedException(); | throw new NotImplementedException(); | ||||
} | } | ||||
public void Train(Session sess) | public void Train(Session sess) | ||||
{ | { | ||||
throw new NotImplementedException(); | throw new NotImplementedException(); | ||||