2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CompletionHandlerをつかいます。
これが絶対なのかはわかりませんが、手元のDeepLabV3でCoreMLHelpersを利用した場合、30枚画像を処理して400MBメモリ使用量が下がりました。なのでいちおう共有するという感じで。

#Good
CompletionHandlerがあるとき😆

lazy var model:VNCoreMLModel = try! VNCoreMLModel(for: DeepLabV3(configuration: MLModelConfiguration()).model)
lazy var request:VNCoreMLRequest? = {
    let model = self.model
    let request = VNCoreMLRequest(model: model, completionHandler: completionHandler)
    request.imageCropAndScaleOption = .scaleFill
    return request
}()
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
do {
    try handler.perform([request!])
   } catch let error {
    print(error)
   }
// 実行したら、後の処理は完了ハンドラに任せる。(結果が返ってくると実行される)
func completionHandler(_ request:VNRequest?,_ error:Error?){
    guard let result = request!.results?.first as? VNCoreMLFeatureValueObservation else { 
 return }
// 結果処理

#Bad
ないとき😞

lazy var model:VNCoreMLModel = try! VNCoreMLModel(for: DeepLabV3(configuration: MLModelConfiguration()).model)
lazy var request:VNCoreMLRequest? = {
    let model = self.model
    let request = VNCoreMLRequest(model: model, completionHandler: nil)
    request.imageCropAndScaleOption = .scaleFill
    return request
}()
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
do {
    try handler.perform([request!])
   } catch let error {
    print(error)
   }
// ImageRequestHandler実行のすぐ下の行でObservationをとっても結果は得られるが、メモリ使用量が多い。
guard let result = request!.results?.first as? VNCoreMLFeatureValueObservation else { return }
// 結果処理

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?