Skip to content

Instantly share code, notes, and snippets.

@otmb
Last active August 14, 2024 02:38
Show Gist options
  • Save otmb/7adf88882d2995ca63ad0ee5a0d3f91a to your computer and use it in GitHub Desktop.
Save otmb/7adf88882d2995ca63ad0ee5a0d3f91a to your computer and use it in GitHub Desktop.
MLMultiArray to Double Array

Sample Data

Get cat.bin

Xcode Resources Add cat.bin

resources.png

Playground

import CoreML

let url = Bundle.main.url(forResource: "cat", withExtension: "bin")
let data = try! Data(contentsOf: url!)
let ptr = UnsafeMutableRawPointer(mutating: (data as NSData).bytes)
let m = try? MLMultiArray(dataPointer: ptr,
                         shape: [3, 360, 480],
                         dataType: .double,
                         strides: [NSNumber(value: 360*480), 480, 1])

let mm = m!
let length = mm.count
let doublePtr =  mm.dataPointer.bindMemory(to: Double.self, capacity: length)
let doubleBuffer = UnsafeBufferPointer(start: doublePtr, count: length)
let output = Array(doubleBuffer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment