|
| 1 | +open System |
| 2 | +open System.IO |
| 3 | +open Microsoft.ML |
| 4 | +open Microsoft.ML.Data |
| 5 | +open Microsoft.ML.ImageAnalytics |
| 6 | +open Microsoft.ML.Core.Data |
| 7 | + |
| 8 | +let dataRoot = FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location) |
| 9 | + |
| 10 | +let imageHeight = 224 |
| 11 | +let imageWidth = 224 |
| 12 | +let mean = 117 |
| 13 | +let scale = 1 |
| 14 | +let channelsLast = true |
| 15 | +[<Literal>] |
| 16 | +let OutputTensorName = "softmax2" |
| 17 | + |
| 18 | +[<CLIMutable>] |
| 19 | +type ImageNetData = |
| 20 | + { |
| 21 | + [<LoadColumn(0)>] |
| 22 | + ImagePath : string |
| 23 | + [<LoadColumn(1)>] |
| 24 | + Label : string |
| 25 | + } |
| 26 | + |
| 27 | +[<CLIMutable>] |
| 28 | +type ImageNetDataProbability = |
| 29 | + { |
| 30 | + ImagePath : string |
| 31 | + Label : string |
| 32 | + PredictedLabel : string |
| 33 | + Probability : float32 |
| 34 | + } |
| 35 | + |
| 36 | +[<CLIMutable>] |
| 37 | +type ImageNetPipeline = |
| 38 | + { |
| 39 | + ImagePath : string |
| 40 | + Label : string |
| 41 | + PredictedLabelValue : string |
| 42 | + Score : float32 [] |
| 43 | + softmax2_pre_activation : float32 [] |
| 44 | + } |
| 45 | +[<CLIMutable>] |
| 46 | +type ImageNetPrediction = |
| 47 | + { |
| 48 | + [<ColumnName(OutputTensorName)>] |
| 49 | + PredictedLabels : float32 [] |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +let printImagePrediction (x : ImageNetPipeline) = |
| 54 | + let defaultForeground = Console.ForegroundColor |
| 55 | + let labelColor = ConsoleColor.Magenta |
| 56 | + let probColor = ConsoleColor.Blue |
| 57 | + printf "ImagePath: " |
| 58 | + Console.ForegroundColor <- labelColor |
| 59 | + printf "%s" (Path.GetFileName(x.ImagePath)) |
| 60 | + Console.ForegroundColor <- defaultForeground |
| 61 | + printf " predicted as " |
| 62 | + Console.ForegroundColor <- labelColor |
| 63 | + printf "%s" x.PredictedLabelValue |
| 64 | + Console.ForegroundColor <- defaultForeground |
| 65 | + Console.Write(" with score ") |
| 66 | + Console.ForegroundColor <- probColor |
| 67 | + printf "%f" (x.Score |> Seq.max) |
| 68 | + Console.ForegroundColor <- defaultForeground; |
| 69 | + printfn "" |
| 70 | + |
| 71 | +let printHeader lines = |
| 72 | + let defaultColor = Console.ForegroundColor |
| 73 | + Console.ForegroundColor <- ConsoleColor.Yellow |
| 74 | + printfn " " |
| 75 | + lines |> Seq.iter (printfn "%s") |
| 76 | + let maxLength = lines |> Seq.map (fun x -> x.Length) |> Seq.max |
| 77 | + printfn "%s" (String('#', maxLength)) |
| 78 | + Console.ForegroundColor <- defaultColor |
| 79 | + |
| 80 | +let printExn lines = |
| 81 | + let defaultColor = Console.ForegroundColor |
| 82 | + Console.ForegroundColor <- ConsoleColor.Red |
| 83 | + printfn " " |
| 84 | + printfn "EXCEPTION" |
| 85 | + printfn "#########" |
| 86 | + Console.ForegroundColor <- defaultColor |
| 87 | + lines |> Seq.iter (printfn "%s") |
| 88 | + |
| 89 | +let printImageNetProb (x : ImageNetDataProbability) = |
| 90 | + |
| 91 | + let defaultForeground = Console.ForegroundColor |
| 92 | + let labelColor = ConsoleColor.Magenta |
| 93 | + let probColor = ConsoleColor.Blue |
| 94 | + let exactLabel = ConsoleColor.Green |
| 95 | + let failLabel = ConsoleColor.Red |
| 96 | + |
| 97 | + printf "ImagePath: " |
| 98 | + Console.ForegroundColor <- labelColor |
| 99 | + printf "%s" (Path.GetFileName(x.ImagePath)) |
| 100 | + Console.ForegroundColor <- defaultForeground |
| 101 | + printf " labeled as " |
| 102 | + Console.ForegroundColor <- labelColor |
| 103 | + printf "%s" x.Label |
| 104 | + Console.ForegroundColor <- defaultForeground |
| 105 | + printf " predicted as " |
| 106 | + if x.Label = x.PredictedLabel then |
| 107 | + Console.ForegroundColor <- exactLabel |
| 108 | + printf "%s" x.PredictedLabel |
| 109 | + else |
| 110 | + Console.ForegroundColor <- failLabel |
| 111 | + printf "%s" x.PredictedLabel |
| 112 | + Console.ForegroundColor <- defaultForeground |
| 113 | + printf " with probability " |
| 114 | + Console.ForegroundColor <- probColor |
| 115 | + printf "%f" x.Probability |
| 116 | + Console.ForegroundColor <- defaultForeground |
| 117 | + printfn "" |
| 118 | + |
| 119 | +let score dataLocation imagesFolder inputModelLocation labelsTxt = |
| 120 | + printfn "Read model" |
| 121 | + printfn "Model location: %s" inputModelLocation |
| 122 | + printfn "Images folder: %s" imagesFolder |
| 123 | + printfn "Training file: %s" dataLocation |
| 124 | + printfn "Default parameters: image size =(%d,%d), image mean: %d" imageHeight imageWidth mean |
| 125 | + let mlContext = MLContext(seed = Nullable 1) |
| 126 | + let data = mlContext.Data.ReadFromTextFile<ImageNetData>(dataLocation, hasHeader = false) |
| 127 | + let pipeline = |
| 128 | + EstimatorChain() |
| 129 | + .Append(mlContext.Transforms.LoadImages(imageFolder = imagesFolder, columns = [|struct("ImagePath", "ImageReal")|])) |
| 130 | + .Append(mlContext.Transforms.Resize("ImageReal", "ImageReal", imageHeight, imageWidth)) |
| 131 | + .Append(mlContext.Transforms.ExtractPixels([| ImagePixelExtractorTransform.ColumnInfo("ImageReal", "input", interleave = channelsLast, offset = float32 mean) |])) |
| 132 | + .Append(mlContext.Transforms.ScoreTensorFlowModel(inputModelLocation, [| "input" |], [| "softmax2" |])) |
| 133 | + let model = pipeline.Fit(data) |
| 134 | + let predictionEngine = model.CreatePredictionEngine<ImageNetData, ImageNetPrediction>(mlContext) |
| 135 | + |
| 136 | + printHeader ["Classificate images"] |
| 137 | + printfn "Images folder: %s" imagesFolder |
| 138 | + printfn "Training file: %s" dataLocation |
| 139 | + printfn "Labels file: %s" labelsTxt |
| 140 | + |
| 141 | + let labels = File.ReadAllLines(labelsTxt) |
| 142 | + |
| 143 | + File.ReadAllLines(dataLocation) |
| 144 | + |> Seq.map (fun x -> let fields = x.Split '\t' in {ImagePath = Path.Combine(imagesFolder, fields.[0]); Label = fields.[1]}) |
| 145 | + |> Seq.map |
| 146 | + (fun sample -> |
| 147 | + let preds = predictionEngine.Predict(sample).PredictedLabels |
| 148 | + let bestLabelIndex = |
| 149 | + preds |
| 150 | + |> Seq.mapi (fun i x -> i, x) |
| 151 | + |> Seq.maxBy snd |
| 152 | + |> fst |
| 153 | + { |
| 154 | + PredictedLabel = labels.[bestLabelIndex] |
| 155 | + Probability = preds.[bestLabelIndex] |
| 156 | + ImagePath = sample.ImagePath |
| 157 | + Label = sample.Label |
| 158 | + } |
| 159 | + ) |
| 160 | + |> Seq.iter printImageNetProb |
| 161 | + |
| 162 | +[<EntryPoint>] |
| 163 | +let main _argv = |
| 164 | + let assetsPath = Path.Combine(dataRoot.Directory.FullName, @"..\..\..\assets") |
| 165 | + let tagsTsv = Path.Combine(assetsPath, "inputs", "images", "tags.tsv") |
| 166 | + let imagesFolder = Path.Combine(assetsPath, "inputs", "images") |
| 167 | + let inceptionPb = Path.Combine(assetsPath, "inputs", "inception", "tensorflow_inception_graph.pb") |
| 168 | + let labelsTxt = Path.Combine(assetsPath, "inputs", "inception", "imagenet_comp_graph_label_strings.txt") |
| 169 | + |
| 170 | + try |
| 171 | + score tagsTsv imagesFolder inceptionPb labelsTxt |
| 172 | + with |
| 173 | + | e -> printExn [e.Message] |
| 174 | + |
| 175 | + let defaultColor = Console.ForegroundColor |
| 176 | + Console.ForegroundColor <- ConsoleColor.Green |
| 177 | + printfn " " |
| 178 | + printfn "Press any key to finish." |
| 179 | + Console.ForegroundColor <- defaultColor |
| 180 | + Console.ReadKey() |> ignore |
| 181 | + 0 |
0 commit comments