forked from dotnet/machinelearning-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
130 lines (117 loc) · 5.01 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
open System
open System.IO
open Microsoft.ML
open Microsoft.ML.Data
let dataRoot = FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location)
let imageHeight = 224
let imageWidth = 224
let mean = 117
let scale = 1
let channelsLast = true
[<CLIMutable>]
type ImageNetData =
{
[<LoadColumn(0)>]
ImagePath : string
[<LoadColumn(1)>]
Label : string
}
[<CLIMutable>]
type ImageNetPipeline =
{
ImagePath : string
Label : string
PredictedLabelValue : string
Score : float32 []
softmax2_pre_activation : float32 []
}
let printImagePrediction (x : ImageNetPipeline) =
let defaultForeground = Console.ForegroundColor
let labelColor = ConsoleColor.Magenta
let probColor = ConsoleColor.Blue
printf "ImagePath: "
Console.ForegroundColor <- labelColor
printf "%s" (Path.GetFileName(x.ImagePath))
Console.ForegroundColor <- defaultForeground
printf " predicted as "
Console.ForegroundColor <- labelColor
printf "%s" x.PredictedLabelValue
Console.ForegroundColor <- defaultForeground
Console.Write(" with score ")
Console.ForegroundColor <- probColor
printf "%f" (x.Score |> Seq.max)
Console.ForegroundColor <- defaultForeground;
printfn ""
let printHeader lines =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Yellow
printfn " "
lines |> Seq.iter (printfn "%s")
let maxLength = lines |> Seq.map (fun x -> x.Length) |> Seq.max
printfn "%s" (String('#', maxLength))
Console.ForegroundColor <- defaultColor
let printExn lines =
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Red
printfn " "
printfn "EXCEPTION"
printfn "#########"
Console.ForegroundColor <- defaultColor
lines |> Seq.iter (printfn "%s")
let buildAndTrainModel dataLocation imagesFolder inputModelLocation imageClassifierZip =
printfn "Read model"
printfn "Model location: %s" inputModelLocation
printfn "Images folder: %s" imagesFolder
printfn "Training file: %s" dataLocation
printfn "Default parameters: image size =(%d,%d), image mean: %d" imageHeight imageWidth mean
let mlContext = MLContext(seed = Nullable 1)
let data = mlContext.Data.LoadFromTextFile<ImageNetData>(dataLocation, hasHeader = false)
let pipeline =
EstimatorChain()
.Append(mlContext.Transforms.Conversion.MapValueToKey("LabelTokey", "Label"))
.Append(mlContext.Transforms.LoadImages("ImageReal", imagesFolder, "ImagePath"))
.Append(mlContext.Transforms.ResizeImages("ImageReal", imageWidth, imageHeight, inputColumnName = "ImageReal"))
.Append(mlContext.Transforms.ExtractPixels("input", "ImageReal", interleavePixelColors = channelsLast, offsetImage = float32 mean))
.Append(mlContext.Model.LoadTensorFlowModel(inputModelLocation).
ScoreTensorFlowModel(outputColumnNames = [|"softmax2_pre_activation"|], inputColumnNames = [|"input"|], addBatchDimensionInput = true))
.Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy("LabelTokey", "softmax2_pre_activation"))
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabelValue","PredictedLabel"))
.AppendCacheCheckpoint(mlContext)
printHeader ["Training classification model"]
let model = pipeline.Fit(data)
let trainData = model.Transform(data)
mlContext.Data.CreateEnumerable<_>(trainData, false, true)
|> Seq.iter printImagePrediction
printHeader ["Classification metrics"]
let metrics = mlContext.MulticlassClassification.Evaluate(trainData, labelColumnName = "LabelTokey", predictedLabelColumnName = "PredictedLabel")
printfn "LogLoss is: %.15f" metrics.LogLoss
metrics.PerClassLogLoss
|> Seq.map string
|> String.concat " , "
|> printfn "PerClassLogLoss is: %s"
printHeader ["Save model to local file"]
let outFile = Path.Combine(dataRoot.Directory.FullName, imageClassifierZip)
if File.Exists outFile then
File.Delete(outFile)
do
use f = File.OpenWrite(outFile)
mlContext.Model.Save(model, trainData.Schema, f)
printfn "Model saved: %s" outFile
[<EntryPoint>]
let main _argv =
let assetsPath = Path.Combine(dataRoot.Directory.FullName, @"..\..\..\assets")
let tagsTsv = Path.Combine(assetsPath, "inputs", "data", "tags.tsv")
let imagesFolder = Path.Combine(assetsPath, "inputs", "data")
let inceptionPb = Path.Combine(assetsPath, "inputs", "inception", "tensorflow_inception_graph.pb")
let imageClassifierZip = Path.Combine(assetsPath, "outputs", "imageClassifier.zip")
try
buildAndTrainModel tagsTsv imagesFolder inceptionPb imageClassifierZip
with
| e -> printExn [e.ToString()]
let defaultColor = Console.ForegroundColor
Console.ForegroundColor <- ConsoleColor.Green
printfn " "
printfn "Press any key to finish."
Console.ForegroundColor <- defaultColor
Console.ReadKey() |> ignore
0