From 5e43f4dcabf18f62709beec049365cdcfbd417a4 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Wed, 21 Aug 2019 07:10:02 -0500 Subject: [PATCH] init commit for YOLOv3. --- .../tf.optimizers.cs => APIs/tf.train.cs} | 3 + .../TensorFlowNET.Core.csproj | 4 +- .../Train/ExponentialMovingAverage.cs | 36 ++++++ .../ImageProcessing/YOLO/Main.cs | 103 ++++++++++++++++++ .../ImageProcessing/YOLO/YOLOv3.cs | 10 ++ .../ImageProcessing/YOLO/config.cs | 19 ++++ .../TensorFlowNET.Examples.csproj | 4 + 7 files changed, 177 insertions(+), 2 deletions(-) rename src/TensorFlowNET.Core/{Train/tf.optimizers.cs => APIs/tf.train.cs} (95%) create mode 100644 src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs create mode 100644 test/TensorFlowNET.Examples/ImageProcessing/YOLO/Main.cs create mode 100644 test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs create mode 100644 test/TensorFlowNET.Examples/ImageProcessing/YOLO/config.cs diff --git a/src/TensorFlowNET.Core/Train/tf.optimizers.cs b/src/TensorFlowNET.Core/APIs/tf.train.cs similarity index 95% rename from src/TensorFlowNET.Core/Train/tf.optimizers.cs rename to src/TensorFlowNET.Core/APIs/tf.train.cs index 0c801f90..6d8a9935 100644 --- a/src/TensorFlowNET.Core/Train/tf.optimizers.cs +++ b/src/TensorFlowNET.Core/APIs/tf.train.cs @@ -31,6 +31,9 @@ namespace Tensorflow public Optimizer AdamOptimizer(float learning_rate, string name = "Adam") => new AdamOptimizer(learning_rate, name: name); + public object ExponentialMovingAverage(float decay) + => new ExponentialMovingAverage(decay); + public Saver Saver(VariableV1[] var_list = null) => new Saver(var_list: var_list); public string write_graph(Graph graph, string logdir, string name, bool as_text = true) diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index abd6e1bf..be1ba3ac 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -17,10 +17,10 @@ TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C# Google's TensorFlow full binding in .NET Standard. Docs: https://tensorflownet.readthedocs.io - 0.11.10.0 + 0.11.0.0 Changes since v0.10.0: 7.3 - 0.11.10.0 + 0.11.0.0 LICENSE true true diff --git a/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs b/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs new file mode 100644 index 00000000..81e491ac --- /dev/null +++ b/src/TensorFlowNET.Core/Train/ExponentialMovingAverage.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Train +{ + public class ExponentialMovingAverage + { + float _decay; + int? _num_updates; + bool _zero_debias; + string _name; + public string name => _name; + + public ExponentialMovingAverage(float decay, int? num_updates = null, bool zero_debias = false, + string name = "ExponentialMovingAverage") + { + _decay = decay; + _num_updates = num_updates; + _zero_debias = zero_debias; + _name = name; + } + + /// + /// Maintains moving averages of variables. + /// + /// + /// + public Operation apply(VariableV1[] var_list = null) + { + throw new NotImplementedException(""); + } + + + } +} diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/Main.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/Main.cs new file mode 100644 index 00000000..8a88225a --- /dev/null +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/Main.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Tensorflow; +using static Tensorflow.Binding; + +namespace TensorFlowNET.Examples.ImageProcessing.YOLO +{ + /// + /// Implementation of YOLO v3 object detector in Tensorflow + /// https://github.com/YunYang1994/tensorflow-yolov3 + /// + public class Main : IExample + { + public bool Enabled { get; set; } = true; + public bool IsImportingGraph { get; set; } = false; + + public string Name => "YOLOv3"; + + Dictionary classes; + Config config; + + Tensor input_data; + Tensor label_sbbox; + Tensor label_mbbox; + Tensor label_lbbox; + Tensor true_sbboxes; + Tensor true_mbboxes; + Tensor true_lbboxes; + Tensor trainable; + + public bool Run() + { + PrepareData(); + + var graph = IsImportingGraph ? ImportGraph() : BuildGraph(); + + using (var sess = tf.Session(graph)) + { + Train(sess); + } + + return true; + } + + public void Train(Session sess) + { + + } + + public void Test(Session sess) + { + throw new NotImplementedException(); + } + + public Graph BuildGraph() + { + var graph = new Graph().as_default(); + + tf_with(tf.name_scope("define_input"), scope => + { + input_data = tf.placeholder(dtype: tf.float32, name: "input_data"); + label_sbbox = tf.placeholder(dtype: tf.float32, name: "label_sbbox"); + label_mbbox = tf.placeholder(dtype: tf.float32, name: "label_mbbox"); + label_lbbox = tf.placeholder(dtype: tf.float32, name: "label_lbbox"); + true_sbboxes = tf.placeholder(dtype: tf.float32, name: "sbboxes"); + true_mbboxes = tf.placeholder(dtype: tf.float32, name: "mbboxes"); + true_lbboxes = tf.placeholder(dtype: tf.float32, name: "lbboxes"); + trainable = tf.placeholder(dtype: tf.@bool, name: "training"); + }); + + tf_with(tf.name_scope("define_loss"), scope => + { + //model = new YOLOv3(input_data, trainable); + }); + + return graph; + } + + public Graph ImportGraph() + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void PrepareData() + { + config = new Config(Name); + + string dataDir = Path.Combine(Name, "data"); + Directory.CreateDirectory(dataDir); + + classes = new Dictionary(); + foreach (var line in File.ReadAllLines(config.CLASSES)) + classes[classes.Count] = line; + } + } +} diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs new file mode 100644 index 00000000..3036cc0d --- /dev/null +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TensorFlowNET.Examples.ImageProcessing.YOLO +{ + public class YOLOv3 + { + } +} diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/config.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/config.cs new file mode 100644 index 00000000..3d9105ed --- /dev/null +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/config.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace TensorFlowNET.Examples.ImageProcessing.YOLO +{ + public class Config + { + string _root; + public string CLASSES; + + public Config(string root) + { + _root = root; + CLASSES = Path.Combine(_root, "data", "classes", "coco.names"); + } + } +} diff --git a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj index f4e2340a..c675bedc 100644 --- a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj +++ b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj @@ -6,6 +6,10 @@ false + + DEBUG;TRACE + +