diff --git a/data/shasta-daisy.jpg b/data/shasta-daisy.jpg new file mode 100644 index 00000000..9a0a46eb Binary files /dev/null and b/data/shasta-daisy.jpg differ diff --git a/docs/source/LogisticRegression.md b/docs/source/LogisticRegression.md index 8c524ea9..3820eca5 100644 --- a/docs/source/LogisticRegression.md +++ b/docs/source/LogisticRegression.md @@ -6,31 +6,11 @@ Logistic regression is a statistical analysis method used to predict a data valu 逻辑回归是一种统计分析方法,用于根据已有得观察数据来预测未知数据。逻辑回归模型通过分析一个或多个现有自变量之间的关系来预测从属数据变量。 -The dependent variable of logistics regression can be two-category or multi-category, but the two-category is more common and easier to explain. So the most common use in practice is the logistics of the two classifications. +The dependent variable of logistics regression can be two-category or multi-category, but the two-category is more common and easier to explain. So the most common use in practice is the logistics of the two classifications. An example used by TensorFlow.NET is a hand-written digit recognition, which is a multi-category. -逻辑回归的因变量可以是二分类的,也可以是多分类的,但是二分类的更为常用,也更加容易解释。 +逻辑回归的因变量可以是二分类的,也可以是多分类的,但是二分类的更为常用,也更加容易解释。 TensorFlow.NET用的例子是一个手写数字识别,它是一个多分类的问题。 - -Logistics regression corresponds to a hidden status p through the function trumpetp = S(ax+b), then determine the value of the dependent -variable according to the size of p and 1-p.The function S here is the Sigmoid function: - S(t)=1/(1+e^(-t) -By changing t to ax+b, you can get the parameter form of the logistic regression model: - P(x;a,b) = 1 / (1 + e^(-ax+b)) - -logistic回归通过函数S将ax+b对应到一个隐状态p,p = S(ax+b),然后根据p与1-p的大小决定因变量的值。这里的函数S就是Sigmoid函数: - S(t)=1/(1+e^(-t) - -将t换成ax+b,可以得到逻辑回归模型的参数形式: - P(x;a,b) = 1 / (1 + e^(-ax+b)) - -![image](https://github.com/SciEvan/TensorFlow.NET/tree/master/docs/source/sigmoid.png) - -sigmoid函数的图像 - -By the function of the function S, we can limit the output value to the interval [0, 1], -p(x) can then be used to represent the probability p(y=1|x), the probability that y is divided into 1 group when an x occurs. - -通过函数S的作用,我们可以将输出的值限制在区间[0, 1]上,p(x)则可以用来表示概率p(y=1|x),即当一个x发生时,y被分到1那一组的概率 +Softmax regression allows us to handle ![1557035393445](_static\logistic-regression\1557035393445.png) where K is the number of classes. The full example is [here](https://github.com/SciSharp/TensorFlow.NET/blob/master/test/TensorFlowNET.Examples/LogisticRegression.cs). diff --git a/docs/source/_static/logistic-regression/1557035393445.png b/docs/source/_static/logistic-regression/1557035393445.png new file mode 100644 index 00000000..a9ca67a8 Binary files /dev/null and b/docs/source/_static/logistic-regression/1557035393445.png differ diff --git a/docs/source/sigmoid.png b/docs/source/_static/sigmoid.png similarity index 100% rename from docs/source/sigmoid.png rename to docs/source/_static/sigmoid.png diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index 204b2d01..3aa0192b 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.7.0 + 0.8.0 Haiping Chen SciSharp STACK false @@ -17,15 +17,15 @@ TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C# Google's TensorFlow full binding in .NET Standard. Docs: https://tensorflownet.readthedocs.io - 0.7.0.0 - Changes since v0.6: + 0.8.0.0 + Changes since v0.7: Add XOR example. Add KMeans example. Add Object Detection example. Add Word2Vec example. 7.2 - 0.7.0.0 + 0.8.0.0 diff --git a/test/TensorFlowNET.Examples/ImageRecognition.cs b/test/TensorFlowNET.Examples/ImageRecognitionInception.cs similarity index 57% rename from test/TensorFlowNET.Examples/ImageRecognition.cs rename to test/TensorFlowNET.Examples/ImageRecognitionInception.cs index 6ba8d898..05fe1928 100644 --- a/test/TensorFlowNET.Examples/ImageRecognition.cs +++ b/test/TensorFlowNET.Examples/ImageRecognitionInception.cs @@ -1,27 +1,33 @@ using NumSharp; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using System.IO.Compression; +using Console = Colorful.Console; using System.Linq; using System.Net; using System.Text; using Tensorflow; +using System.Drawing; namespace TensorFlowNET.Examples { - public class ImageRecognition : Python, IExample + /// + /// Inception v3 is a widely-used image recognition model + /// that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset. + /// The model is the culmination of many ideas developed by multiple researchers over the years. + /// + public class ImageRecognitionInception : Python, IExample { public int Priority => 7; public bool Enabled { get; set; } = true; - public string Name => "Image Recognition"; + public string Name => "Image Recognition Inception"; public bool ImportGraph { get; set; } = false; - string dir = "ImageRecognition"; + string dir = "ImageRecognitionInception"; string pbFile = "tensorflow_inception_graph.pb"; string labelFile = "imagenet_comp_graph_label_strings.txt"; - string picFile = "grace_hopper.jpg"; public bool Run() { @@ -29,41 +35,39 @@ namespace TensorFlowNET.Examples var labels = File.ReadAllLines(Path.Join(dir, labelFile)); var files = Directory.GetFiles(Path.Join(dir, "img")); - foreach (var file in files) - { - var tensor = ReadTensorFromImageFile(file); - var graph = new Graph().as_default(); - //import GraphDef from pb file - graph.Import(Path.Join(dir, pbFile)); + var graph = new Graph().as_default(); + //import GraphDef from pb file + graph.Import(Path.Join(dir, pbFile)); + + var input_name = "input"; + var output_name = "output"; - var input_name = "input"; - var output_name = "output"; + var input_operation = graph.OperationByName(input_name); + var output_operation = graph.OperationByName(output_name); - var input_operation = graph.OperationByName(input_name); - var output_operation = graph.OperationByName(output_name); + var result_labels = new List(); + var sw = new Stopwatch(); - var idx = 0; - float propability = 0; - with(tf.Session(graph), sess => + with(tf.Session(graph), sess => + { + foreach (var file in files) { + sw.Restart(); + + // load image file + var tensor = ReadTensorFromImageFile(file); var results = sess.run(output_operation.outputs[0], new FeedItem(input_operation.outputs[0], tensor)); - var probabilities = results.Data(); - for (int i = 0; i < probabilities.Length; i++) - { - if (probabilities[i] > propability) - { - idx = i; - propability = probabilities[i]; - } - } - }); - - Console.WriteLine($"{picFile}: {labels[idx]} {propability}"); - return labels[idx].Equals("military uniform"); - } - - return false; + results = np.squeeze(results); + int idx = np.argmax(results); + + Console.WriteLine($"{file.Split(Path.DirectorySeparatorChar).Last()}: {labels[idx]} {results[idx]} in {sw.ElapsedMilliseconds}ms", Color.Tan); + + result_labels.Add(labels[idx]); + } + }); + + return result_labels.Contains("military uniform"); } private NDArray ReadTensorFromImageFile(string file_name, @@ -72,7 +76,7 @@ namespace TensorFlowNET.Examples int input_mean = 117, int input_std = 1) { - return with(tf.Graph().as_default(), graph => + return with(tf.Graph(), graph => { var file_reader = tf.read_file(file_name, "file_reader"); var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg"); @@ -102,6 +106,9 @@ namespace TensorFlowNET.Examples Directory.CreateDirectory(Path.Join(dir, "img")); url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg"; Utility.Web.Download(url, Path.Join(dir, "img"), "grace_hopper.jpg"); + + url = $"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/shasta-daisy.jpg"; + Utility.Web.Download(url, Path.Join(dir, "img"), "shasta-daisy.jpg"); } } } diff --git a/test/TensorFlowNET.Examples/Program.cs b/test/TensorFlowNET.Examples/Program.cs index 23c50b76..01928b88 100644 --- a/test/TensorFlowNET.Examples/Program.cs +++ b/test/TensorFlowNET.Examples/Program.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Drawing; using System.Linq; using System.Reflection; +using Tensorflow; using Console = Colorful.Console; namespace TensorFlowNET.Examples @@ -12,16 +13,18 @@ namespace TensorFlowNET.Examples { static void Main(string[] args) { - var assembly = Assembly.GetEntryAssembly(); var errors = new List(); var success = new List(); var disabled = new List(); - var examples = assembly.GetTypes() + var examples = Assembly.GetEntryAssembly().GetTypes() .Where(x => x.GetInterfaces().Contains(typeof(IExample))) .Select(x => (IExample)Activator.CreateInstance(x)) .OrderBy(x => x.Priority) .ToArray(); + Console.WriteLine($"TensorFlow v{tf.VERSION}", Color.Yellow); + Console.WriteLine($"TensorFlow.NET v{Assembly.GetAssembly(typeof(TF_DataType)).GetName().Version}", Color.Yellow); + var sw = new Stopwatch(); foreach (IExample example in examples) { @@ -30,7 +33,6 @@ namespace TensorFlowNET.Examples Console.WriteLine($"{DateTime.UtcNow} Starting {example.Name}", Color.White); - try { if (example.Enabled || args.Length > 0) // if a specific example was specified run it, regardless of enabled value @@ -55,7 +57,6 @@ namespace TensorFlowNET.Examples Console.WriteLine(ex); } - Console.WriteLine($"{DateTime.UtcNow} Completed {example.Name}", Color.White); } diff --git a/test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs b/test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs index 43e4aa88..6a7cf55b 100644 --- a/test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs +++ b/test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs @@ -1,10 +1,15 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using Tensorflow; namespace TensorFlowNET.Examples { + /// + /// Bidirectional LSTM-CRF Models for Sequence Tagging + /// https://github.com/guillaumegenthial/tf_ner/tree/master/models/lstm_crf + /// public class BiLstmCrfNer : Python, IExample { public int Priority => 13; @@ -13,15 +18,53 @@ namespace TensorFlowNET.Examples public bool ImportGraph { get; set; } = false; public string Name => "bi-LSTM + CRF NER"; + HyperParams @params = new HyperParams(); + + public bool Run() + { + PrepareData(); + return true; + } public void PrepareData() { - + if (!Directory.Exists(HyperParams.DATADIR)) + Directory.CreateDirectory(HyperParams.DATADIR); + + if (!Directory.Exists(@params.RESULTDIR)) + Directory.CreateDirectory(@params.RESULTDIR); + + if (!Directory.Exists(@params.MODELDIR)) + Directory.CreateDirectory(@params.MODELDIR); + + if (!Directory.Exists(@params.EVALDIR)) + Directory.CreateDirectory(@params.EVALDIR); } - public bool Run() + private class HyperParams { - return true; + public const string DATADIR = "BiLstmCrfNer"; + public string RESULTDIR = Path.Combine(DATADIR, "results"); + public string MODELDIR; + public string EVALDIR; + + public int dim = 300; + public float dropout = 0.5f; + public int num_oov_buckets = 1; + public int epochs = 25; + public int batch_size = 20; + public int buffer = 15000; + public int lstm_size = 100; + public string words = Path.Combine(DATADIR, "vocab.words.txt"); + public string chars = Path.Combine(DATADIR, "vocab.chars.txt"); + public string tags = Path.Combine(DATADIR, "vocab.tags.txt"); + public string glove = Path.Combine(DATADIR, "glove.npz"); + + public HyperParams() + { + MODELDIR = Path.Combine(RESULTDIR, "model"); + EVALDIR = Path.Combine(MODELDIR, "eval"); + } } } }