At
Athelas, we use Convolutional Neural Networks(CNNs) for a lot more than
just classification! In this post, we’ll see how CNNs can be used, with
great results, in image instance segmentation.
n iOS 10,
API to implement and run Convolutional Neural Network (CNN) on iOS
devices have been added to the MetalPerformanceShaders (MPS) framework.
We can now take advantage of GPU on iOS devices to run fast CNN computation
. In other word, we can use outcome of cutting edge deep learning technologies on your device even while offline.
Apple provides samples for the MPSCNN (MPSCNNHelloWorld / MetalImageRecognition), and I integrated it into my sample code collection repo:
https://github.com/shu223/iOS-10-Sampler
Realtime Image Recognition using MPSCNN
In these sample apps,
pre-trained model parameters
(weight / bias) are used in the app, and those files are named
xxxx.dat
.
What kind of file is this?
Is this something special by Apple? Can MPS CNN use only this format? What format is the content?
In this article, I write about the model format of MPS CNN.
File format
The `xxxx.dat` files which are included in samples such as Apple’s MPSCNNHelloWorld or MetalImageRecognition are
ordinary binary files
. Thus those are
NOT
MPSCNN or Apple proprietary / exclusive.
This file can be loaded to memory
without any dependencies on Metal or MetalPerformanceShaders
as follows:
let fd = open( filePath, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
assert(fd != -1, “Error: failed to open output file at \””+filePath+”\” errno = \(errno)\n”)
guard let hdr = mmap(nil, size, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0) else {
close(fd)
return nil
}
Then, to use this with MPSCNN, Casting UnsafeMutableRawPointer to UnsafePointer
,
let p = UnsafePointer(hdr.assumingMemoryBound(to: Float.self))
assert(p != UnsafePointer(bitPattern: -1), “mmap failed with errno = \(errno)”)
and pass this pointer to the initializer of MPSCNNConvolution or MPSCNNFullyConnected.
public init(device: MTLDevice, convolutionDescriptor: MPSCNNConvolutionDescriptor, kernelWeights: UnsafePointer, biasTerms: UnsafePointer?, flags: MPSCNNConvolutionFlags)
MPSCNNConvolution or MPSCNNFullyConnected copies the parameters when it’s initialized, so you can release the data loaded on memory and close the file.
munmap(hdr, Int(size))
close(fd)
These are also independent of Metal or MetalPerformanceShaders, very generic methods for files or memory.
In conclusion,
any file format can be used
, as far as it can be read by the iOS app.
链接:
https://medium.com/@shu223/model-format-of-mpscnn-on-ios-metal-performance-shaders-94b653a3dcc8
github链接:
https://github.com/shu223/iOS-10-Sampler
原文链接:
http://m.weibo.cn/1402400261/4099626824419715