Skip to content

Commit ee14339

Browse files
authored
Fix runtime exception in ImageClassification. (dotnet#3249)
* Fix runtime exception in ImageClassification. * Cleanup. * revert changes. * 3P license notice.
1 parent a57ad10 commit ee14339

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

Diff for: THIRD-PARTY-NOTICES.TXT

+24-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,27 @@ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
4242
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
4343
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
4444
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46+
47+
License notice for SharpZipLib
48+
------------------------------
49+
50+
https://github.com/icsharpcode/SharpZipLib
51+
52+
Copyright © 2000-2018 SharpZipLib Contributors
53+
54+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
55+
software and associated documentation files (the "Software"), to deal in the Software
56+
without restriction, including without limitation the rights to use, copy, modify, merge,
57+
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
58+
to whom the Software is furnished to do so, subject to the following conditions:
59+
60+
The above copyright notice and this permission notice shall be included in all copies or
61+
substantial portions of the Software.
62+
63+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
64+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
65+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
66+
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
67+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
68+
DEALINGS IN THE SOFTWARE.

Diff for: docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
4+
using System.Net;
5+
using ICSharpCode.SharpZipLib.GZip;
6+
using ICSharpCode.SharpZipLib.Tar;
37
using Microsoft.ML.Data;
48

59
namespace Microsoft.ML.Samples.Dynamic
@@ -13,7 +17,14 @@ public static void Example()
1317
{
1418
// Download the ResNet 101 model from the location below.
1519
// https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz
16-
var modelLocation = @"resnet_v2_101/resnet_v2_101_299_frozen.pb";
20+
21+
string modelLocation = "resnet_v2_101_299_frozen.pb";
22+
if (!File.Exists(modelLocation))
23+
{
24+
modelLocation = Download(@"https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz", @"resnet_v2_101_299_frozen.tgz");
25+
Unzip(Path.Join(Directory.GetCurrentDirectory(), modelLocation), Directory.GetCurrentDirectory());
26+
modelLocation = "resnet_v2_101_299_frozen.pb";
27+
}
1728

1829
var mlContext = new MLContext();
1930
var data = GetTensorData();
@@ -22,7 +33,7 @@ public static void Example()
2233
// Create a ML pipeline.
2334
var pipeline = mlContext.Model.LoadTensorFlowModel(modelLocation).ScoreTensorFlowModel(
2435
new[] { nameof(OutputScores.output) },
25-
new[] { nameof(TensorData.input) });
36+
new[] { nameof(TensorData.input) }, addBatchDimensionInput: true);
2637

2738
// Run the pipeline and get the transformed values.
2839
var estimator = pipeline.Fit(idv);
@@ -86,5 +97,31 @@ class OutputScores
8697
{
8798
public float[] output { get; set; }
8899
}
100+
101+
private static string Download(string baseGitPath, string dataFile)
102+
{
103+
using (WebClient client = new WebClient())
104+
{
105+
client.DownloadFile(new Uri($"{baseGitPath}"), dataFile);
106+
}
107+
108+
return dataFile;
109+
}
110+
111+
/// <summary>
112+
/// Taken from https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples.
113+
/// </summary>
114+
private static void Unzip(string path, string targetDir)
115+
{
116+
Stream inStream = File.OpenRead(path);
117+
Stream gzipStream = new GZipInputStream(inStream);
118+
119+
TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
120+
tarArchive.ExtractContents(targetDir);
121+
tarArchive.Close();
122+
123+
gzipStream.Close();
124+
inStream.Close();
125+
}
89126
}
90127
}

Diff for: src/Microsoft.ML.SamplesUtils/Microsoft.ML.SamplesUtils.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<IncludeInPackage>Microsoft.ML.SampleUtils</IncludeInPackage>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="SharpZipLib.NETStandard" Version="1.0.7" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
1014
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />

0 commit comments

Comments
 (0)