Update Image Classification Sample to 1.4 (#740)

* Update to 1.4

* Working sample

* Updated README

* Updated csproj
This commit is contained in:
Luis Quintanilla 2019-11-07 21:42:37 -05:00 коммит произвёл Cesar De la Torre
Родитель 188f0cc43f
Коммит 346a8933b7
5 изменённых файлов: 48 добавлений и 41 удалений

Просмотреть файл

@ -1,2 +1,4 @@
**/assets/UD
**/assets/CD
**/assets/CD
**/workspace/*
!**/workspace/README.md

Просмотреть файл

@ -6,9 +6,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ML" Version="1.4.0-preview2" />
<PackageReference Include="Microsoft.ML.Dnn" Version="0.16.0-preview2" />
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.4.0-preview2" />
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="$(MicrosoftMLVersion)" />
<PackageReference Include="Microsoft.ML.Vision" Version="$(MicrosoftMLVersion)" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.15.0" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -4,7 +4,7 @@ using System.Linq;
using System.IO;
using Microsoft.ML;
using static Microsoft.ML.DataOperationsCatalog;
using Microsoft.ML.Transforms;
using Microsoft.ML.Vision;
namespace DeepLearning_ImageClassification_Binary
{
@ -13,6 +13,7 @@ namespace DeepLearning_ImageClassification_Binary
static void Main(string[] args)
{
var projectDirectory = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../"));
var workspaceRelativePath = Path.Combine(projectDirectory, "workspace");
var assetsRelativePath = Path.Combine(projectDirectory, "assets");
MLContext mlContext = new MLContext();
@ -26,10 +27,9 @@ namespace DeepLearning_ImageClassification_Binary
var preprocessingPipeline = mlContext.Transforms.Conversion.MapValueToKey(
inputColumnName: "Label",
outputColumnName: "LabelAsKey")
.Append(mlContext.Transforms.LoadImages(
.Append(mlContext.Transforms.LoadRawImageBytes(
outputColumnName: "Image",
imageFolder: assetsRelativePath,
useImageType: false,
inputColumnName: "ImagePath"));
IDataView preProcessedData = preprocessingPipeline
@ -43,18 +43,20 @@ namespace DeepLearning_ImageClassification_Binary
IDataView validationSet = validationTestSplit.TrainSet;
IDataView testSet = validationTestSplit.TestSet;
var trainingPipeline = mlContext.Model.ImageClassification(
featuresColumnName: "Image",
labelColumnName: "LabelAsKey",
arch: ImageClassificationEstimator.Architecture.ResnetV2101,
epoch: 100,
batchSize: 20,
testOnTrainSet: false,
metricsCallback: (metrics) => Console.WriteLine(metrics),
validationSet: validationSet,
reuseTrainSetBottleneckCachedValues: true,
reuseValidationSetBottleneckCachedValues: true,
disableEarlyStopping: false)
var classifierOptions = new ImageClassificationTrainer.Options()
{
FeatureColumnName = "Image",
LabelColumnName = "LabelAsKey",
ValidationSet = validationSet,
Arch = ImageClassificationTrainer.Architecture.ResnetV2101,
MetricsCallback = (metrics) => Console.WriteLine(metrics),
TestOnTrainSet = false,
ReuseTrainSetBottleneckCachedValues = true,
ReuseValidationSetBottleneckCachedValues = true,
WorkspacePath=workspaceRelativePath
};
var trainingPipeline = mlContext.MulticlassClassification.Trainers.ImageClassification(classifierOptions)
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
ITransformer trainedModel = trainingPipeline.Fit(trainSet);

Просмотреть файл

@ -0,0 +1 @@
Computed bottleneck values and trained TensorFlow model are stored in this directory.

Просмотреть файл

@ -133,13 +133,12 @@ IDataView shuffledData = mlContext.Data.ShuffleRows(imageData);
```csharp
var preprocessingPipeline = mlContext.Transforms.Conversion.MapValueToKey(
inputColumnName:"Label",
outputColumnName:"LabelAsKey")
.Append(mlContext.Transforms.LoadImages(
outputColumnName:"Image",
inputColumnName: "Label",
outputColumnName: "LabelAsKey")
.Append(mlContext.Transforms.LoadRawImageBytes(
outputColumnName: "Image",
imageFolder: assetsRelativePath,
useImageType: false,
inputColumnName:"ImagePath"));
inputColumnName: "ImagePath"));
```
1. Fit the data to the preprocessing pipeline.
@ -164,18 +163,20 @@ IDataView testSet = validationTestSplit.TestSet;
## Define the training pipeline
```csharp
var trainingPipeline = mlContext.Model.ImageClassification(
featuresColumnName: "Image",
labelColumnName: "LabelAsKey",
arch: ImageClassificationEstimator.Architecture.ResnetV2101,
epoch: 100,
batchSize: 20,
testOnTrainSet: false,
metricsCallback: (metrics) => Console.WriteLine(metrics),
validationSet: validationSet,
reuseTrainSetBottleneckCachedValues: true,
reuseValidationSetBottleneckCachedValues: true,
disableEarlyStopping:false)
var classifierOptions = new ImageClassificationTrainer.Options()
{
FeatureColumnName = "Image",
LabelColumnName = "LabelAsKey",
ValidationSet = validationSet,
Arch = ImageClassificationTrainer.Architecture.ResnetV2101,
MetricsCallback = (metrics) => Console.WriteLine(metrics),
TestOnTrainSet = false,
ReuseTrainSetBottleneckCachedValues = true,
ReuseValidationSetBottleneckCachedValues = true,
WorkspacePath=workspaceRelativePath
};
var trainingPipeline = mlContext.MulticlassClassification.Trainers.ImageClassification(classifierOptions)
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
```
@ -254,10 +255,10 @@ Run your console app. The output should be similar to that below. You may see wa
### Bottleneck phase
```text
Phase: Bottleneck Computation, Dataset used: Train, Image Index: 279, Image Name:
Phase: Bottleneck Computation, Dataset used: Train, Image Index: 280, Image Name:
Phase: Bottleneck Computation, Dataset used: Validation, Image Index: 1, Image Name:
Phase: Bottleneck Computation, Dataset used: Validation, Image Index: 2, Image Name:
Phase: Bottleneck Computation, Dataset used: Train, Image Index: 279
Phase: Bottleneck Computation, Dataset used: Train, Image Index: 280
Phase: Bottleneck Computation, Dataset used: Validation, Image Index: 1
Phase: Bottleneck Computation, Dataset used: Validation, Image Index: 2
```
### Training phase