Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Mac64bit Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/CoreML/Update Model
This example is the version from Tue, 16th Dec 2019.
Project "Update Model.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()
// this example is to update a specific model.
// you need to change code if you have your own model
dim InputFile as FolderItem = SpecialFolder.Desktop.Child("UpdatableDrawingClassifier.mlmodelc")
dim OutputFile as FolderItem = SpecialFolder.Desktop.Child("UpdatableDrawingClassifier 2.mlmodelc")
// read it
dim e1 as NSErrorMBS
Dim m As MLModelMBS = MLModelMBS.modelWithContentsOfFile(InputFIle, e1)
if m = nil then
list.AddRow "Failed to load model."
list.AddRow e1.LocalizedDescription
return
end if
// get description
dim md as MLModelDescriptionMBS = m.modelDescription
dim trainingInputDescriptionsByName as Dictionary = md.trainingInputDescriptionsByName
// we need to update image and so look for the constrain, so the image can be scaled/converted to match
Dim imageConstraint As MLImageConstraintMBS
For Each key As Variant In trainingInputDescriptionsByName.keys
Dim f As MLFeatureDescriptionMBS = trainingInputDescriptionsByName.Value(key)
if f.Type = f.TypeImage then
imageConstraint = f.imageConstraint
end if
next
if imageConstraint = nil then
list.AddRow "Failed to find image constraint."
return
end if
dim ImageFile as FolderItem = SpecialFolder.Desktop.Child("mbslogo.png")
dim e2 as NSErrorMBS
Dim imageFeature As MLFeatureValueMBS = MLFeatureValueMBS.featureValueWithImageFile(ImageFile, imageConstraint, e2)
if e2 <> nil then
list.AddRow "Failed to read image."
list.AddRow e2.LocalizedDescription
return
end if
dim interestedEvents as integer = _
MLUpdateProgressHandlersMBS.ProgressEventTrainingBegin+_
MLUpdateProgressHandlersMBS.ProgressEventMiniBatchEnd+_
MLUpdateProgressHandlersMBS.ProgressEventTrainingBegin
ProgressHandler = new MLUpdateProgressHandlers(interestedEvents)
ProgressHandler.OutputFile = OutputFile
ProgressHandler.list = list
dim dic as new Dictionary
dic.Value("label") = "car"
dic.Value("drawing") = imageFeature
dim e3 as NSErrorMBS
Dim feature As New MLDictionaryFeatureProviderMBS(dic, e3)
if e3 <> nil then
list.AddRow "Failed to create feature."
list.AddRow e3.LocalizedDescription
return
end if
dim features() as MLFeatureProviderMBS
features.Append feature
Dim trainingData As New MLArrayBatchProviderMBS(features)
Dim configuration As New MLModelConfigurationMBS
configuration.computeUnits = configuration.ComputeUnitsAll
dim error as NSErrorMBS
Dim task As New MLUpdateTaskMBS(InputFile, trainingData, configuration, ProgressHandler, error)
if error <> nil then
list.AddRow error.LocalizedDescription
return
end if
dim parameters as new Dictionary
task.resumeWithParameters parameters
End EventHandler
Property ProgressHandler As MLUpdateProgressHandlers
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
Class MLUpdateProgressHandlers Inherits MLUpdateProgressHandlersMBS
EventHandler Sub completionHandler(context as MLUpdateContextMBS)
list.AddRow "Complete"
dim r as Boolean
dim error as NSErrorMBS
r = context.writeToFile(OutputFile, error)
if r then
list.AddRow "Model saved."
else
list.AddRow "Failed to save model."
list.AddRow error.LocalizedDescription
end if
End EventHandler
EventHandler Sub progressHandler(context as MLUpdateContextMBS)
dim s as string
select case context.ProgressEvent
case context.ProgressEventTrainingBegin
s = "TrainingBegin"
case context.ProgressEventEpochEnd
s = "EpochEnd"
case context.ProgressEventMiniBatchEnd
s = "MiniBatchEnd"
else
s = str(context.ProgressEvent)
end Select
list.AddRow "Progress "+s
End EventHandler
Property OutputFile As FolderItem
Property list As listbox
End Class
End Project
The items on this page are in the following plugins: MBS Mac64bit Plugin.