代码之家  ›  专栏  ›  技术社区  ›  Roi Mulia

DeepLabV3 MLMultiArray输出到图像的转换速度非常慢

  •  0
  • Roi Mulia  · 技术社区  · 3 年前

    我正在使用DeepLabV3 MLModel 苹果通过以下链接提供: https://developer.apple.com/machine-learning/models/

    看起来实际的预测很快。我正在运行 CIImage s我转换为 CVPixelBuffer s(快速)。
    在执行预测之后,我需要得到实际的分割掩码 CIImage
    即使在我的iPhone 13 Pro上,这个操作也运行得非常慢。

    代码:

    let prediction = try! deepLab.prediction(image: pixelBuffer)
    let semanticPredictions = prediction.semanticPredictions
    

    我试图找回的两种方法 CIImage 分割掩模的速度都非常慢:

    • 使用 UIGraphicsImageRenderer

      func fetchMasKUsingGraphicRenderer(mlMultiArray: MLMultiArray) -> CIImage {
      
        let aWidth = CGFloat(mlMultiArray.shape[0].intValue)
        let aHeight = CGFloat(mlMultiArray.shape[1].intValue)
      
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: aWidth, height: aHeight))
      
        let img = renderer.image(actions: { context in
            let ctx = context.cgContext
            ctx.clear(CGRect(x: 0.0, y: 0.0, width: Double(aWidth), height: Double(aHeight)));
            for j in 0..<Int(aHeight) {
                for i in 0..<Int(aWidth) {
      
                    let aValue =
                        (mlMultiArray[j * Int(aHeight) + i].floatValue > 0.0) ?
                            1.0 : 0.0
                    let aRect = CGRect(
                        x: CGFloat(i),
                        y: CGFloat(j),
                        width: 1.0,
                        height: 1.0)
      
                    let aColor: UIColor = UIColor(
                        displayP3Red: 0.0,
                        green: 0.0,
                        blue: 0.0,
                        alpha: CGFloat(aValue))
      
                    aColor.setFill()
                    UIRectFill(aRect)
                }
            }
        })
      
        return CIImage(image: img)!
      }
      
    • 使用Hollance的CoreMLHelpers: https://github.com/hollance/CoreMLHelpers :

      let maskedCiImage = CIImage(cgImage: semanticPredictions.cgImage()!)
      

    做这件事的正确方法是什么?谢谢

    0 回复  |  直到 3 年前