Skip to content

Instantly share code, notes, and snippets.

@s1ddok
Created July 3, 2017 12:32
Show Gist options
  • Save s1ddok/b9443f73ffd839b212526ba85a343b73 to your computer and use it in GitHub Desktop.
Save s1ddok/b9443f73ffd839b212526ba85a343b73 to your computer and use it in GitHub Desktop.
// Retrieve results
guard let outputFeatures = featureProvider.featureValue(for: selectedModel.outputLayerName)?.multiArrayValue else {
fatalError("Couldn't retrieve features")
}
// Calculate total buffer size by multiplying shape tensor's dimensions
let bufferSize = outputFeatures.shape.lazy.map { $0.intValue }.reduce(1, { $0 * $1 })
// Get data pointer to the buffer
let dataPointer = UnsafeMutableBufferPointer(start: outputFeatures.dataPointer.assumingMemoryBound(to: Double.self),
count: bufferSize)
// Prepare buffer for single-channel image result
var imgData = [UInt8](repeating: 0, count: bufferSize)
// Normalize result features by applying sigmoid to every pixel and convert to UInt8
for i in 0..<inputW {
for j in 0..<inputH {
let idx = i * inputW + j
let value = dataPointer[idx]
let sigmoid = { (input: Double) -> Double in
return 1 / (1 + exp(-input))
}
let result = sigmoid(value)
imgData[idx] = UInt8(result * 255)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment