I can suggest another code for this, that translates UIImage (HWC shape (64,64,3)) to MLMultiArray (CHW shape (3,64,64)) and scale it to 0.0 - 1.0 range:
public func preprocess(image: UIImage, width: Int, height: Int) -> MLMultiArray? {
let size = CGSize(width: width, height: height)
guard let pixels = image.resize(to: size).pixelData()?.map({ (Double($0) / 255.0) }) else {
return nil
}
guard let array = try? MLMultiArray(shape: [3, height, width] as [NSNumber], 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
}