apple / apple/coremltools

2023-06-14 12:36:57.129025+0530 FaceRecognitionPOC[70558:1381417] [coreml] Failed to get the home directory when checking model path. MLUpdateProgressEvent(rawValue: 1)

Open
#1,886 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## ❓Question

- If this is a question about the Core ML Frame work or Xcode, please ask your question in the Apple Developer Forum: https://developer.apple.com/forums/
I am getting issue while try to train an array of images with name i have followed ur code and my code is

//
// MyRecognitionClass.swift
// FaceRecognitionPOC
//
// Created by Dhuba Lakshmana Prasad on 14/06/23.
//

import Foundation
import UIKit
import CoreML
import Vision

class MyRecognitionClass{

var classifierURL = UpdatableMNISTDigitClassifier.urlOfModelInThisBundle
var modelName = "v2.mlmodelc"

lazy var classifier: MLModel = {
return UpdatableMNISTDigitClassifier(model: .init()).model
}()

lazy var modelConfig: MLModelConfiguration = {
let modelConfig = MLModelConfiguration()
modelConfig.computeUnits = .all
return modelConfig
}()

init(){}

/// Predict from URL of ml model
/// - Parameter url:
func predict(url:URL, image: UIImage) throws {
let classifier = try UpdatableMNISTDigitClassifier.init(contentsOf: url, configuration: modelConfig)
// let image = UIImage.init(named: imageName)!
// let input = UpdatableMNISTDigitClassifierInput.init(image:convert(image: image.cgImage!) )

// let inputImageSize: CGFloat = 224.0
// let minLen = min(image.size.width, image.size.height)
let resizedImage = image.resize(to: CGSize(width: 224, height: 224))

// let input = UpdatableMNISTDigitClassifierInput.init(image: UIImage().preprocessImage(from: image)!)
let input = UpdatableMNISTDigitClassifierInput.init(image: UIImage().preprocessImage(from: resizedImage)!)
let results = try! classifier.prediction(input: input)
print(results.digit)
}
/// Predict with ml model
/// - Parameter model:
func predict(model:MLModel, image: UIImage) throws {
// let image = UIImage.init(named: "seven")!
// let input = UpdatableMNISTDigitClassifierInput.init(image:convert(image: image.cgImage!) )
let input = UpdatableMNISTDigitClassifierInput.init(image: UIImage().preprocessImage(from: image)!)
let testData = MLArrayBatchProvider.init(array: [input])
let results = try model.predictions(fromBatch: testData)
print(results.features(at: 0).featureValue(for: "digit"))
}

func startTraining(images:[UIImage], name: String){

let trainingValues = getTrainingData(myImages: images, name: name)

do {
let updateTask = try MLUpdateTask(forModelAt: classifierURL, trainingData: trainingValues, configuration: modelConfig,
progressHandlers: MLUpdateProgressHandlers(forEvents: [.trainingBegin,.epochEnd],
progressHandler: { (contextProgress) in
print(contextProgress.event)

}) { (finalContext) in
if (finalContext.task.error == nil) {
do {
//Check if its trained on seven
try self.predict(model: finalContext.model, image: images[0])

// Save it
self.saveMode(model: finalContext.model)

// Reload and again test if it gives any correct predictions
let urlToSave = try self.getModelDirURL(modelName:self.modelName)
try self.predict(url: urlToSave, image: images[0])

}
catch {
print(error)
}
}
})
updateTask.resume()

} catch {
print("Error while upgrading \(error.localizedDescription)")
}
}
}
extension MyRecognitionClass{

func getModelDirURL(modelName:String) throws -> URL{

let fileManager = FileManager.default
let appSupportDirectory = try fileManager.url(for: .applicationSupportDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
let permanentUrl = appSupportDirectory.appendingPathComponent(modelName)
print(permanentUrl)
return permanentUrl
}

func saveMode(model:MLWritable){

do {
let urlToSave = try getModelDirURL(modelName: modelName)
try model.write(to: urlToSave)
}
catch {
print(error)
}
}


func convert(image: CGImage) -> CVPixelBuffer {
let imageInputDescription = classifier.modelDescription.inputDescriptionsByName["image"]!
let imageConstraint = imageInputDescription.imageConstraint!
return try! MLFeatureValue(cgImage: image, constraint: imageConstraint).imageBufferValue!
}

func getTrainingData(myImages: [UIImage], name: String) -> MLArrayBatchProvider {

// let image = UIImage.init(named:"seven")!
// let images = [image,image,image]
let images = myImages
var dataArr = [CVPixelBuffer]()

for image in images{
// dataArr.append(convert(image: image.cgImage!))
dataArr.append(UIImage().preprocessImage(from: image)!)
}

// let trainingModels = dataArr.map { UpdatableMNISTDigitClassifierTrainingInput(image: $0 , digit: "7")}
let trainingModels = dataArr.map { UpdatableMNISTDigitClassifierTrainingInput(image: $0 , digit: name)}

let modelsBatch = MLArrayBatchProvider(array:trainingModels)


// let mlarr = try! MLMultiArray.init(shape: [2], dataType: .double)
// mlarr[0] = 2
// mlarr[1] = 2
// let mlarr1 = try! MLMultiArray.init(shape: [1], dataType: .double)
// mlarr1[0] = 4
// let trainingData = getTrainingData()
//
// let ip = addupdatableTrainingInput.init(number: mlarr, output_true: mlarr1)
// // let modelsBatch = MLArrayBatchProvider(array:[ip])

return modelsBatch
}

}
I did not use the python file how to use it.

Contributor guide

Open the contributing guide

Research direction

The report is a question about Swift code using Core ML and how to use the Python side of coremltools; it does not identify a repository file, test, or requested project change. Start with the linked Apple Developer Forum guidance and determine whether the reported model-path error belongs to coremltools; there is no project-specific definition of done in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, swift
Domain
machine-learning, mobile-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
10/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.