Skip to content

Commit 69cc717

Browse files
kevmalprathyusha12345
authored andcommitted
F# update data and migrate TensorFlowEstimator sample (dotnet#367)
1 parent 0e7da1d commit 69cc717

File tree

7 files changed

+25
-31
lines changed

7 files changed

+25
-31
lines changed

samples/fsharp/getting-started/DeepLearning_TensorFlowEstimator/Directory.Build.props

Lines changed: 0 additions & 7 deletions
This file was deleted.

samples/fsharp/getting-started/DeepLearning_TensorFlowEstimator/ImageClassification.Predict/ImageClassification.Predict.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
1414
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="$(MicrosoftMLVersion)" />
15-
<PackageReference Include="Microsoft.ML.TensorFlow" Version="$(MicrosoftMLVersion)" />
15+
<PackageReference Include="Microsoft.ML.TensorFlow" Version="0.12.0-preview" />
1616
</ItemGroup>
1717

1818
</Project>

samples/fsharp/getting-started/DeepLearning_TensorFlowEstimator/ImageClassification.Predict/Program.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ let classifyImages dataLocation imagesFolder modelLocation =
110110
printHeader ["Loading model"]
111111

112112
let mlContext = MLContext(seed = Nullable 1)
113-
let loadedModel =
113+
let loadedModel, inputSchema =
114114
use f = File.OpenRead(modelLocation)
115115
mlContext.Model.Load(f)
116116
printfn "Model loaded: %s" modelLocation
117-
let predictor = loadedModel.CreatePredictionEngine<ImageNetData,ImageNetPrediction>(mlContext)
117+
let predictor = mlContext.Model.CreatePredictionEngine<ImageNetData,ImageNetPrediction>(loadedModel)
118118

119119
printHeader ["Making classifications"]
120120
File.ReadAllLines(dataLocation)

samples/fsharp/getting-started/DeepLearning_TensorFlowEstimator/ImageClassification.Train/ImageClassification.Train.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
1414
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="$(MicrosoftMLVersion)" />
15-
<PackageReference Include="Microsoft.ML.TensorFlow" Version="$(MicrosoftMLVersion)" />
15+
<PackageReference Include="Microsoft.ML.TensorFlow" Version="0.12.0-preview" />
1616
</ItemGroup>
1717

1818
</Project>

samples/fsharp/getting-started/DeepLearning_TensorFlowEstimator/ImageClassification.Train/Program.fs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
open System.IO
33
open Microsoft.ML
44
open Microsoft.ML.Data
5-
open Microsoft.ML.ImageAnalytics
65

76
let dataRoot = FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location)
87

@@ -74,25 +73,27 @@ let buildAndTrainModel dataLocation imagesFolder inputModelLocation imageClassif
7473
printfn "Training file: %s" dataLocation
7574
printfn "Default parameters: image size =(%d,%d), image mean: %d" imageHeight imageWidth mean
7675
let mlContext = MLContext(seed = Nullable 1)
77-
let data = mlContext.Data.ReadFromTextFile<ImageNetData>(dataLocation, hasHeader = false)
76+
let data = mlContext.Data.LoadFromTextFile<ImageNetData>(dataLocation, hasHeader = false)
7877
let pipeline =
7978
EstimatorChain()
8079
.Append(mlContext.Transforms.Conversion.MapValueToKey("LabelTokey", "Label"))
81-
.Append(mlContext.Transforms.LoadImages(imagesFolder, struct ("ImageReal", "ImagePath")))
82-
.Append(mlContext.Transforms.Resize("ImageReal", imageWidth, imageHeight, inputColumnName = "ImageReal"))
83-
.Append(mlContext.Transforms.ExtractPixels(ImagePixelExtractorTransformer.ColumnInfo("input", "ImageReal", interleave = channelsLast, offset = float32 mean)))
84-
.Append(mlContext.Transforms.ScoreTensorFlowModel(inputModelLocation, [| "softmax2_pre_activation" |], [| "input" |]))
85-
.Append(mlContext.MulticlassClassification.Trainers.LogisticRegression("LabelTokey", "softmax2_pre_activation"))
86-
.Append(mlContext.Transforms.Conversion.MapKeyToValue(struct("PredictedLabelValue","PredictedLabel")))
87-
80+
.Append(mlContext.Transforms.LoadImages("ImageReal", imagesFolder, "ImagePath"))
81+
.Append(mlContext.Transforms.ResizeImages("ImageReal", imageWidth, imageHeight, inputColumnName = "ImageReal"))
82+
.Append(mlContext.Transforms.ExtractPixels("input", "ImageReal", interleavePixelColors = channelsLast, offsetImage = float32 mean))
83+
.Append(mlContext.Model.LoadTensorFlowModel(inputModelLocation).
84+
ScoreTensorFlowModel(outputColumnNames = [|"softmax2_pre_activation"|], inputColumnNames = [|"input"|], addBatchDimensionInput = true))
85+
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy("LabelTokey", "softmax2_pre_activation"))
86+
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabelValue","PredictedLabel"))
87+
.AppendCacheCheckpoint(mlContext)
88+
8889
printHeader ["Training classification model"]
8990
let model = pipeline.Fit(data)
9091
let trainData = model.Transform(data)
91-
mlContext.CreateEnumerable<_>(trainData, false, true)
92+
mlContext.Data.CreateEnumerable<_>(trainData, false, true)
9293
|> Seq.iter printImagePrediction
9394

9495
printHeader ["Classification metrics"]
95-
let metrics = mlContext.MulticlassClassification.Evaluate(trainData, label = "LabelTokey", predictedLabel = "PredictedLabel")
96+
let metrics = mlContext.MulticlassClassification.Evaluate(trainData, labelColumnName = "LabelTokey", predictedLabelColumnName = "PredictedLabel")
9697
printfn "LogLoss is: %.15f" metrics.LogLoss
9798
metrics.PerClassLogLoss
9899
|> Seq.map string
@@ -105,7 +106,7 @@ let buildAndTrainModel dataLocation imagesFolder inputModelLocation imageClassif
105106
File.Delete(outFile)
106107
do
107108
use f = File.OpenWrite(outFile)
108-
mlContext.Model.Save(model, f)
109+
mlContext.Model.Save(model, trainData.Schema, f)
109110
printfn "Model saved: %s" outFile
110111

111112
[<EntryPoint>]
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
broccoli.jpg broccoli
2-
pizza.jpg pizza
3-
pizza2.jpg pizza
4-
teddy2.jpg teddy
5-
teddy3.jpg teddy
6-
teddy4.jpg teddy
7-
toaster.jpg toaster
8-
toaster2.png toaster
1+
broccoli.jpg food
2+
pizza.jpg food
3+
pizza2.jpg food
4+
teddy2.jpg toy
5+
teddy3.jpg toy
6+
teddy4.jpg toy
7+
toaster.jpg appliance
8+
toaster2.png appliance

0 commit comments

Comments
 (0)