Skip to content

Instantly share code, notes, and snippets.

@alwc
Forked from otmb/README.md
Created April 7, 2019 17:23
Show Gist options
  • Save alwc/1b05fa7006c4ea339d19a904270fc391 to your computer and use it in GitHub Desktop.
Save alwc/1b05fa7006c4ea339d19a904270fc391 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