Skip to content

Commit e2e301c

Browse files
eerhardtCESARDELATORRE
authored andcommitted
Refactor TensorFlowImageClassification to no longer write the model file to disk. (#560)
Implement a sample "in memory" file loader to use with the PredictionEnginePool.
1 parent 286c3fb commit e2e301c

6 files changed

Lines changed: 35 additions & 27 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.Extensions.ML;
2+
using Microsoft.Extensions.Primitives;
3+
using Microsoft.ML;
4+
using System.Threading;
5+
6+
namespace TensorFlowImageClassification.ML
7+
{
8+
public class InMemoryModelLoader : ModelLoader
9+
{
10+
private readonly ITransformer _model;
11+
12+
public InMemoryModelLoader(ITransformer model)
13+
{
14+
_model = model;
15+
}
16+
17+
public override ITransformer GetModel() => _model;
18+
19+
public override IChangeToken GetReloadToken() =>
20+
// This IChangeToken will never notify a change.
21+
new CancellationChangeToken(CancellationToken.None);
22+
}
23+
}

samples/csharp/end-to-end-apps/DeepLearning_ImageClassification_TensorFlow/TensorFlowImageClassification/ML/TensorFlowModelConfigurator.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
using Microsoft.ML;
2-
using System;
32
using System.Collections.Generic;
4-
using System.IO;
53
using System.Linq;
6-
using System.Threading.Tasks;
74
using TensorFlowImageClassification.ML.DataModels;
85

96
namespace TensorFlowImageClassification.ML
107
{
118
public class TensorFlowModelConfigurator
129
{
1310
private readonly MLContext _mlContext;
14-
private readonly ITransformer _mlModel;
11+
12+
public ITransformer Model { get; }
1513

1614
public TensorFlowModelConfigurator(string tensorFlowModelFilePath)
1715
{
1816
_mlContext = new MLContext();
1917

2018
// Model creation and pipeline definition for images needs to run just once, so calling it from the constructor:
21-
_mlModel = SetupMlnetModel(tensorFlowModelFilePath);
19+
Model = SetupMlnetModel(tensorFlowModelFilePath);
2220
}
2321

2422
public struct ImageSettings
@@ -61,11 +59,5 @@ private IDataView CreateEmptyDataView()
6159
var dv = _mlContext.Data.LoadFromEnumerable<ImageInputData>(list);
6260
return dv;
6361
}
64-
65-
public void SaveMLNetModel(string mlnetModelFilePath)
66-
{
67-
// Save/persist the model to a .ZIP file to be loaded by the PredictionEnginePool
68-
_mlContext.Model.Save(_mlModel, null, mlnetModelFilePath);
69-
}
7062
}
7163
}

samples/csharp/end-to-end-apps/DeepLearning_ImageClassification_TensorFlow/TensorFlowImageClassification/Startup.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
62
using Microsoft.AspNetCore.Builder;
73
using Microsoft.AspNetCore.Hosting;
84
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.HttpsPolicy;
105
using Microsoft.AspNetCore.Mvc;
116
using Microsoft.Extensions.Configuration;
127
using Microsoft.Extensions.DependencyInjection;
138
using Microsoft.Extensions.ML;
9+
using Microsoft.ML;
1410
using TensorFlowImageClassification.ML;
1511
using TensorFlowImageClassification.ML.DataModels;
1612

@@ -19,21 +15,18 @@ namespace TensorFlowImageClassification
1915
public class Startup
2016
{
2117
private readonly string _tensorFlowModelFilePath;
22-
private readonly string _mlnetModelFilePath;
18+
private readonly ITransformer _mlnetModel;
2319

2420
public Startup(IConfiguration configuration)
2521
{
2622
Configuration = configuration;
2723

2824
_tensorFlowModelFilePath = GetAbsolutePath(Configuration["MLModel:TensorFlowModelFilePath"]);
29-
_mlnetModelFilePath = GetAbsolutePath(Configuration["MLModel:MLNETModelFilePath"]);
3025

3126
/////////////////////////////////////////////////////////////////
3227
//Configure the ML.NET model for the pre-trained TensorFlow model
3328
TensorFlowModelConfigurator tensorFlowModelConfigurator = new TensorFlowModelConfigurator(_tensorFlowModelFilePath);
34-
35-
// Save the ML.NET model .zip file based on the TensorFlow model and related configuration
36-
tensorFlowModelConfigurator.SaveMLNetModel(_mlnetModelFilePath);
29+
_mlnetModel = tensorFlowModelConfigurator.Model;
3730
}
3831

3932
public IConfiguration Configuration { get; }
@@ -53,8 +46,12 @@ public void ConfigureServices(IServiceCollection services)
5346
/////////////////////////////////////////////////////////////////////////////
5447
// Register the PredictionEnginePool as a service in the IoC container for DI
5548
//
56-
services.AddPredictionEnginePool<ImageInputData, ImageLabelPredictions>()
57-
.FromFile(_mlnetModelFilePath);
49+
services.AddPredictionEnginePool<ImageInputData, ImageLabelPredictions>();
50+
services.AddOptions<PredictionEnginePoolOptions<ImageInputData, ImageLabelPredictions>>()
51+
.Configure(options =>
52+
{
53+
options.ModelLoader = new InMemoryModelLoader(_mlnetModel);
54+
});
5855
}
5956

6057
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

samples/csharp/end-to-end-apps/DeepLearning_ImageClassification_TensorFlow/TensorFlowImageClassification/TensorFlowImageClassification.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424

2525

2626
<ItemGroup>
27-
<None Update="ML\MLNETModel\MLModel.zip">
28-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29-
</None>
3027
<None Update="ML\TensorFlowModel\labels.txt">
3128
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3229
</None>

samples/csharp/end-to-end-apps/DeepLearning_ImageClassification_TensorFlow/TensorFlowImageClassification/appsettings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"MLModel": {
33
"TensorFlowModelFilePath": "ML/TensorFlowModel/model.pb",
4-
"MLNETModelFilePath": "ML/MLNETModel/MLModel.zip",
54
"LabelsFilePath": "ML/TensorFlowModel/labels.txt"
65
},
76
"Logging": {

0 commit comments

Comments
 (0)