Converting UIImage to MLMultiArray for Keras Model

前端 未结 3 1457
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 09:41

In Python, I trained an image classification model with keras to receive input as a [224, 224, 3] array and output a prediction (1 or 0). When I load the save the model and

3条回答
  •  抹茶落季
    2021-01-14 10:18

    Did not tried this, but here is how its done for the FOOD101 sample

    func preprocess(image: UIImage) -> MLMultiArray? {
            let size = CGSize(width: 299, height: 299)
    
    
            guard let pixels = image.resize(to: size).pixelData()?.map({ (Double($0) / 255.0 - 0.5) * 2 }) else {
                return nil
            }
    
            guard let array = try? MLMultiArray(shape: [3, 299, 299], dataType: .double) else {
                return nil
            }
    
            let r = pixels.enumerated().filter { $0.offset % 4 == 0 }.map { $0.element }
            let g = pixels.enumerated().filter { $0.offset % 4 == 1 }.map { $0.element }
            let b = pixels.enumerated().filter { $0.offset % 4 == 2 }.map { $0.element }
    
            let combination = r + g + b
            for (index, element) in combination.enumerated() {
                array[index] = NSNumber(value: element)
            }
    
            return array
        }
    

    https://github.com/ph1ps/Food101-CoreML

提交回复
热议问题