-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Image classification using DNNs and Transfer Learning. #4057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1a219d1
Image classification using DNNs and Transfer Learning.
codemzs eb6fef9
Undo sample changes.
codemzs dce734a
Disable ONNX conversion test because of protobuf version conflict.
codemzs a6168fe
Disable ONNX conversion test because of protobuf version conflict.
codemzs 13f3f58
Upgrade to latest TF.NEt and enable ONNX test.
codemzs 71d0968
stop buffer resue. comment out buggy tests.
codemzs 825fbdf
enable buggy tests.
codemzs 735e354
enable buggy tests.
codemzs 30e0713
pass string directly as tensor instead of converting to utf8.
codemzs 0b84992
pass string directly as tensor instead of converting to utf8.
codemzs 42f97da
fix session dispose call in DNNTransfomer.
codemzs 1231587
fix session dispose call in DNNTransfomer.
codemzs 8fe76a0
fix crash issue when tesing tf.net.
Oceania2018 3c3613b
Merge pull request #4 from Oceania2018/imageclassificationapi
codemzs a48da72
fix for premature deallocation by TF.NET.
codemzs 9669cbc
fix for destructor.
codemzs 28f5c72
fix for destructor.
codemzs 6cf21f4
Upgrade to latest TF.NEt that contains fix for default graph.
codemzs 67170a1
TDV-TF mapping.
codemzs 0bcc228
Add test for transfer learning.
codemzs e28cd18
Add Inception V3 model.
codemzs 0723aa4
Add Inception V3 model and unit-test
codemzs a1a6972
Add Inception V3 model and unit-test
codemzs d38ee21
Add Inception V3 model and unit-test
codemzs e0e326f
Add Inception V3 model and unit-test
codemzs e618eff
Add Inception V3 model and unit-test
codemzs 981ef13
Update transfer learning test.
codemzs eb354a0
Remove hard coded paths from test.
codemzs 72260cf
tsts
codemzs b614621
Add more samples and minor refactoring.
codemzs d7296ba
Take dependency of scisharp tensorflow redist.
codemzs bd6e2cb
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs 84c08e7
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs 11cc810
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs 95f8453
PR feedback.
codemzs 6cb29c4
PR feedback.
codemzs d36e3db
Cleanup.
codemzs 5e2eed7
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
codemzs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Image classification using DNNs and Transfer Learning.
- Loading branch information
commit 1a219d180442f3c0c662881a22b4a6da14b0fe1e
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TransferLearning.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using ICSharpCode.SharpZipLib.GZip; | ||
using ICSharpCode.SharpZipLib.Tar; | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Samples.Dynamic | ||
{ | ||
public static class TransferLearning | ||
{ | ||
/// <summary> | ||
/// Example use of the TensorFlow image model in a ML.NET pipeline. | ||
/// </summary> | ||
public static void Example() | ||
{ | ||
var sw = new Stopwatch(); | ||
|
||
var mlContext = new MLContext(); | ||
var data = GetTensorData(); | ||
var idv = mlContext.Data.LoadFromEnumerable(data); | ||
|
||
// Create a ML pipeline. | ||
var pipeline = | ||
mlContext.Transforms.Conversion.MapValueToKey( | ||
nameof(TensorData.Label)) | ||
.Append(mlContext.Model.ImageClassification( | ||
nameof(TensorData.input), | ||
nameof(TensorData.Label), batchSize: 2, | ||
addBatchDimensionInput: true)); | ||
|
||
// Run the pipeline and get the transformed values. | ||
var estimator = pipeline.Fit(idv); | ||
var transformedValues = estimator.Transform(idv); | ||
|
||
// Retrieve model scores. | ||
var outScores = mlContext.Data.CreateEnumerable<OutputScores>( | ||
transformedValues, reuseRowObject: false); | ||
|
||
// Display scores. (for the sake of brevity we display scores of the | ||
// first 3 classes) | ||
foreach (var prediction in outScores) | ||
{ | ||
int numClasses = 0; | ||
foreach (var classScore in prediction.Scores.Take(2)) | ||
{ | ||
Console.WriteLine( | ||
$"Class #{numClasses++} score = {classScore}"); | ||
} | ||
Console.WriteLine( | ||
$"Predicted Label: {prediction.PredictedLabel}"); | ||
|
||
|
||
Console.WriteLine(new string('-', 10)); | ||
} | ||
|
||
Console.WriteLine(sw.Elapsed); | ||
// Class #0 score = 0.03416626 | ||
// Class #1 score = 0.9658337 | ||
// Predicted Label: 1 | ||
// ---------- | ||
// Class #0 score = 0.02959618 | ||
// Class #1 score = 0.9704038 | ||
// Predicted Label: 1 | ||
// ---------- | ||
} | ||
|
||
private const int imageHeight = 224; | ||
private const int imageWidth = 224; | ||
private const int numChannels = 3; | ||
private const int inputSize = imageHeight * imageWidth * numChannels; | ||
|
||
/// <summary> | ||
/// A class to hold sample tensor data. | ||
/// Member name should match the inputs that the model expects (in this | ||
/// case, input). | ||
/// </summary> | ||
public class TensorData | ||
{ | ||
[VectorType(imageHeight, imageWidth, numChannels)] | ||
public float[] input { get; set; } | ||
|
||
public Int64 Label { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Method to generate sample test data. Returns 2 sample rows. | ||
/// </summary> | ||
public static TensorData[] GetTensorData() | ||
codemzs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// This can be any numerical data. Assume image pixel values. | ||
var image1 = Enumerable.Range(0, inputSize).Select( | ||
x => (float)x / inputSize).ToArray(); | ||
|
||
var image2 = Enumerable.Range(0, inputSize).Select( | ||
x => (float)(x + 10000) / inputSize).ToArray(); | ||
return new TensorData[] { new TensorData() { input = image1, Label = 0 }, | ||
new TensorData() { input = image2, Label = 1 } }; | ||
} | ||
|
||
/// <summary> | ||
/// Class to contain the output values from the transformation. | ||
/// </summary> | ||
class OutputScores | ||
{ | ||
public float[] Scores { get; set; } | ||
public Int64 PredictedLabel { 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. | ||
/// </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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Pack"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<PackageDescription>Microsoft.ML.Dnn contains APIs to do high level DNN training such as image classification.</PackageDescription> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../Microsoft.ML/Microsoft.ML.nupkgproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project DefaultTargets="Pack"> | ||
|
||
<Import Project="Microsoft.ML.Dnn.nupkgproj" /> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Content Include="$(MSBuildThisFileDirectory)..\..\tools\DnnImageModels\Resnet101V2Tensorflow\*.*"> | ||
<Link>DnnImageModels\%(RecursiveDir)%(Filename)%(Extension)</Link> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
using Microsoft.ML; | ||
|
||
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Tests" + PublicKey.TestValue)] | ||
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Tensorflow" + PublicKey.Value)] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.