Platforms to show: All Mac Windows Linux Cross-Platform
/WinFrameworks/Windows ML Test
Required plugins for this example: MBS WinFrameworks Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /WinFrameworks/Windows ML Test
This example is the version from Tue, 28th Sep 2020.
Project "Windows ML Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open()
// Learn about Windows AI
// https://docs.microsoft.com/en-us/windows/ai/
// this example is based on Desktop version of SqueezeNetObjectDetection sample:
// https://github.com/Microsoft/Windows-Machine-Learning
// get model here: https://github.com/microsoft/Windows-Machine-Learning/tree/master/SharedContent/models
Dim ModelFile As FolderItem =SpecialFolder.Desktop.Child("SqueezeNet.onnx")
if not ModelFile.Exists then
MsgBox "Please download model first!"
quit
end if
dim model as WinLearningModelMBS = WinLearningModelMBS.LoadFromFile(ModelFile)
if model = nil then
msgbox "Failed to load model!"
// Check path and you need Windows 10
quit
End If
List.AddRow "Author", model.Author
List.AddRow "Description", model.Description
List.AddRow "Domain", model.Domain
List.AddRow "Name", model.Name
List.AddRow "Version", Str(model.Version)
list.AddRow ""
dim metadata as Dictionary = model.Metadata
if metadata <> Nil then
for each key as Variant in metadata.keys
list.AddRow key.StringValue, metadata.Value(key).StringValue
next
list.AddRow ""
end if
Dim inputFeatures() As WinLearningModelFeatureDescriptorMBS = model.InputFeatures
for each feature as WinLearningModelFeatureDescriptorMBS in inputFeatures
list.AddRow "Input "+feature.Name, feature.KindString + " " + feature.Extra + " " + feature.Description
next
dim outputFeatures() as WinLearningModelFeatureDescriptorMBS = model.OutputFeatures
for each feature as WinLearningModelFeatureDescriptorMBS in outputFeatures
list.AddRow "Output "+feature.Name, feature.KindString + " " + feature.Extra + " " + feature.Description
next
// try it
// get image file here: https://github.com/microsoft/Windows-Machine-Learning/tree/master/SharedContent/media
dim ImageFile as FolderItem = SpecialFolder.Desktop.Child("kitten_224.png")
if not ImageFile.Exists then return
// first output should be tensor feature for this model and has the shape
dim outputFeature as WinLearningModelTensorFeatureDescriptorMBS = WinLearningModelTensorFeatureDescriptorMBS(outputFeatures(0))
dim shape() as integer = outputFeature.Shape // { 1, 1000, 1, 1 }
dim session as new WinLearningModelSessionMBS(model)
dim binding as new WinLearningModelBindingMBS(session)
binding.BindWithImage("data_0", ImageFile) // fill value with image
binding.BindWithFloat("softmaxout_1", shape, nil) // create output array with right shape
dim result as WinLearningModelEvaluationResultMBS = session.Evaluate(binding)
// check result
list.AddRow ""
dim ErrorStatus as integer = result.ErrorStatus
dim OutputNames() as string = result.OutputNames
if result.Succeeded then
dim values() as single = result.GetTensorFloat("softmaxout_1")
// Labels.txt from https://github.com/microsoft/Windows-Machine-Learning/tree/master/Samples/SqueezeNetObjectDetection/Desktop/cpp
dim LabelFile as FolderItem = SpecialFolder.Desktop.Child("Labels.txt")
dim LabelDic as new Dictionary
if LabelFile.Exists then
dim t as TextInputStream = TextInputStream.Open(LabelFile)
while not t.eof
dim line as string = t.ReadLine
if line.len > 0 then
dim p as integer = line.instr(",")
if p > 0 then
Dim keyStr As String = line.Left(p-1)
Dim keyInt As Integer = Val(keyStr)
Dim ValueStr As String = line.Mid(p+1)
LabelDic.Value(keyInt) = ValueStr
end if
end if
wend
end if
dim Indexes() as integer
for i as integer = 0 to values.Ubound
indexes.Append i
next
values.SortWith(indexes)
// show last 5 entries with highest priority
dim u as integer = values.Ubound
for i as integer = u downto u - 4
list.AddRow labeldic.Lookup(indexes(i), "no label"), Str(values(i))
Next
end if
End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Module UtilModule
Function Extra(extends feature as WinLearningModelFeatureDescriptorMBS) As string
if feature isa WinLearningModelTensorFeatureDescriptorMBS then
dim f as WinLearningModelTensorFeatureDescriptorMBS = WinLearningModelTensorFeatureDescriptorMBS(feature)
dim shape() as integer = f.Shape
dim shapes() as string
for each s as integer in shape
shapes.Append Str(s)
next
return f.TensorKindName + " (" + join(shapes, ", ") + ")"
end if
if feature isa WinLearningModelImageFeatureDescriptorMBS then
dim f as WinLearningModelImageFeatureDescriptorMBS = WinLearningModelImageFeatureDescriptorMBS(feature)
Return Str(f.Width) + "x" + Str(f.Height)
end if
if feature isa WinLearningModelSequenceFeatureDescriptorMBS then
dim f as WinLearningModelSequenceFeatureDescriptorMBS = WinLearningModelSequenceFeatureDescriptorMBS(feature)
Return f.ElementDescriptor.Name+" "+f.ElementDescriptor.KindString
end if
if feature isa WinLearningModelMapFeatureDescriptorMBS then
dim f as WinLearningModelMapFeatureDescriptorMBS = WinLearningModelMapFeatureDescriptorMBS(feature)
Return f.ValueDescriptor.Name+" "+f.ValueDescriptor.KindString
End If
End Function
Function KindString(extends m as WinLearningModelFeatureDescriptorMBS) As string
Select case m.Kind
case m.KindImage
return "Image"
case m.KindMap
return "Map"
case m.KindSequence
return "Sequence"
case m.KindTensor
return "Tensor"
Else
Return str(m.Kind)
end Select
End Function
Function TensorKindName(extends m as WinLearningModelTensorFeatureDescriptorMBS) As string
Select case m.TensorKind
case m.TensorKindBoolean
return "Boolean"
case m.TensorKindComplex128
return "Complex128"
case m.TensorKindComplex64
return "Complex64"
case m.TensorKindDouble
return "Double"
case m.TensorKindFloat
return "Float"
case m.TensorKindFloat16
return "Float16"
case m.TensorKindInt16
return "Int16"
case m.TensorKindInt32
return "Int32"
case m.TensorKindInt64
return "Int64"
case m.TensorKindInt8
return "Int8"
case m.TensorKindString
return "String"
case m.TensorKindUInt16
return "UInt16"
case m.TensorKindUInt32
return "UInt32"
case m.TensorKindUInt64
return "UInt64"
case m.TensorKindUInt8
return "UInt8"
else
Return str(m.TensorKind)
end Select
End Function
End Module
End Project
See also:
The items on this page are in the following plugins: MBS WinFrameworks Plugin.