PROGrand/yolo-coreml

<question>model change

Closed this issue · 2 comments

Dear PROGrand

I has implementing the YOLOv4 model in my app. I want to switch the models for detecting other objects(trained separately). To do this, I will modify the YOLO4My load function using UserDefaults.standard.string as following. It seems to be functional. But I am not sure when change the model, previous models memory area will be unloaded or not automatically. If I need to unload previous loaded model, how should I do?
I am very appreciated to get your advice.


class YOLO4My : YOLOWrapper {
override func load(width: Int, height: Int, confidence: Float, nms: Float, maxBoundingBoxes: Int) async throws {
let TargetModelName = UserDefaults.standard.string(forKey: "TargetMLModelName")
guard let modelURL = Bundle.main.url(forResource: TargetModelName, withExtension: "mlmodelc") else {
return
}
let model = try await my.load(contentsOf: modelURL).model
try yolo = YOLO(width: width, height: height,
channels: 3, model: model, confidenceThreshold: confidence, nmsThreshold: nms, maxBoundingBoxes: maxBoundingBoxes)
}
}


Previous model should be unloaded by ARC. Reference to model is stored in yolo object, so make sure of completion of all model.prediction calls and reset yolo to nil before my.load (i changed it in example).

Dear PROGrand

ARC seems not timely(when I want) unload the previous model, then "yolo to nil" worked fine. Thanks very much.

After that, I has been trying the two model switch. During the test, I has curious behavior.
while one model(A) loading. following error come out.


[espresso] [Espresso::handle_ex_plan] exception=ANECF error: ANECCompile(/var/mobile/Library/Caches/com.apple.aned/**) FAILED: err=(CompilationFailure)
[coreml] Error plan build: -1.
[client] doUnloadModel:options:qos:error:: nil _ANEModel
[espresso] ANECF error:


In this case memory was occupied with around 590MB

Another model(B) loading, no failure come out, and memory was occupied with around 1.4GB.

For model A, I can capture the photo and prediction several time. it seems work fine.
But for model B, only one time capture photo and predation works. at the second capturing(using AVfoundation photo output) dropped suddenly due to memory issue message.
I used the following stop and start function.


func stopSession() {
if captureSession.isRunning {
DispatchQueue.global().async {
self.captureSession.stopRunning()
}
}
}

func startSession() {
if !captureSession.isRunning {
DispatchQueue.global().async {
self.captureSession.startRunning()
}
}
}


Why two different model loading condition happened even if I use the same your converter and swift code?
and Can you advice how to prevent sudden drop?
Memory meter shows still green area(less than 1.9GB)