Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using ICSharpCode.SharpZipLib.GZip;
Comment thread
codemzs marked this conversation as resolved.
using ICSharpCode.SharpZipLib.Tar;
using Microsoft.ML.Data;

namespace Microsoft.ML.Samples.Dynamic
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Taken from https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples.
Comment thread
codemzs marked this conversation as resolved.
/// </summary>
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<IncludeInPackage>Microsoft.ML.SampleUtils</IncludeInPackage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpZipLib.NETStandard" Version="1.0.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
Expand Down