From 893333e0fdbc481ff8a085882ffec9f95a28141c Mon Sep 17 00:00:00 2001 From: Zeeshan Siddiqui Date: Mon, 8 Apr 2019 15:24:30 -0700 Subject: [PATCH 1/4] Fix runtime exception in ImageClassification. --- .../Dynamic/TensorFlow/ImageClassification.cs | 41 ++++++++++++++++++- .../Microsoft.ML.Samples.csproj | 1 + .../Microsoft.ML.SamplesUtils.csproj | 4 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs index 6b22c5816c..9f64e6e2a8 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs @@ -1,6 +1,10 @@ using System; using System.Linq; +using System.Net; using Microsoft.ML.Data; +using System.IO; +using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.GZip; namespace Microsoft.ML.Samples.Dynamic { @@ -13,7 +17,14 @@ public static void Example() { // Download the ResNet 101 model from the location below. // https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz - var modelLocation = @"resnet_v2_101/resnet_v2_101_299_frozen.pb"; + + string modelLocation = "resnet_v2_101_299_frozen.pb"; + if (!File.Exists(modelLocation)) + { + modelLocation = Download(@"https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz", @"resnet_v2_101_299_frozen.tgz"); + Unzip(Path.Join(Directory.GetCurrentDirectory(), modelLocation), Directory.GetCurrentDirectory()); + modelLocation = "resnet_v2_101_299_frozen.pb"; + } var mlContext = new MLContext(); var data = GetTensorData(); @@ -22,7 +33,7 @@ public static void Example() // Create a ML pipeline. var pipeline = mlContext.Model.LoadTensorFlowModel(modelLocation).ScoreTensorFlowModel( new[] { nameof(OutputScores.output) }, - new[] { nameof(TensorData.input) }); + new[] { nameof(TensorData.input) }, addBatchDimensionInput: true); // Run the pipeline and get the transformed values. var estimator = pipeline.Fit(idv); @@ -86,5 +97,31 @@ class OutputScores { public float[] output { get; set; } } + + private static string Download(string baseGitPath, string dataFile) + { + using (WebClient client = new WebClient()) + { + client.DownloadFile(new Uri($"{baseGitPath}"), dataFile); + } + + return dataFile; + } + + /// + /// Taken from https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples. + /// + private static void Unzip(string path, string targetDir) + { + Stream inStream = File.OpenRead(path); + Stream gzipStream = new GZipInputStream(inStream); + + TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream); + tarArchive.ExtractContents(targetDir); + tarArchive.Close(); + + gzipStream.Close(); + inStream.Close(); + } } } \ No newline at end of file diff --git a/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj b/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj index 01106e7ee5..887b79bf8b 100644 --- a/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj +++ b/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj @@ -185,6 +185,7 @@ + diff --git a/src/Microsoft.ML.SamplesUtils/Microsoft.ML.SamplesUtils.csproj b/src/Microsoft.ML.SamplesUtils/Microsoft.ML.SamplesUtils.csproj index b7c0a83577..3fb71a6442 100644 --- a/src/Microsoft.ML.SamplesUtils/Microsoft.ML.SamplesUtils.csproj +++ b/src/Microsoft.ML.SamplesUtils/Microsoft.ML.SamplesUtils.csproj @@ -5,6 +5,10 @@ Microsoft.ML.SampleUtils + + + + From b69216aab0938b80e2973bae8b54046ec44d6931 Mon Sep 17 00:00:00 2001 From: Zeeshan Siddiqui Date: Mon, 8 Apr 2019 15:28:09 -0700 Subject: [PATCH 2/4] Cleanup. --- .../Dynamic/TensorFlow/ImageClassification.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs index 9f64e6e2a8..d97d7391a6 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs @@ -1,10 +1,10 @@ using System; +using System.IO; using System.Linq; using System.Net; -using Microsoft.ML.Data; -using System.IO; -using ICSharpCode.SharpZipLib.Tar; using ICSharpCode.SharpZipLib.GZip; +using ICSharpCode.SharpZipLib.Tar; +using Microsoft.ML.Data; namespace Microsoft.ML.Samples.Dynamic { From 5d35b06a696f6cd9ea5034c005a53cf0f1c2d11f Mon Sep 17 00:00:00 2001 From: Zeeshan Siddiqui Date: Mon, 8 Apr 2019 15:33:38 -0700 Subject: [PATCH 3/4] revert changes. --- docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj b/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj index 887b79bf8b..01106e7ee5 100644 --- a/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj +++ b/docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj @@ -185,7 +185,6 @@ - From a3201ea3ea195c5739e3977374608eaab2c22bac Mon Sep 17 00:00:00 2001 From: Zeeshan Siddiqui Date: Tue, 9 Apr 2019 11:10:54 -0700 Subject: [PATCH 4/4] 3P license notice. --- THIRD-PARTY-NOTICES.TXT | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/THIRD-PARTY-NOTICES.TXT b/THIRD-PARTY-NOTICES.TXT index 778dd888df..ae6f0a7ec3 100644 --- a/THIRD-PARTY-NOTICES.TXT +++ b/THIRD-PARTY-NOTICES.TXT @@ -42,4 +42,27 @@ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for SharpZipLib +------------------------------ + +https://github.com/icsharpcode/SharpZipLib + +Copyright © 2000-2018 SharpZipLib Contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file