Skip to content

Instantly share code, notes, and snippets.

@dealforest
Last active January 14, 2020 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dealforest/c4962a6f3aff5ff639f246bd78eb8c30 to your computer and use it in GitHub Desktop.
Save dealforest/c4962a6f3aff5ff639f246bd78eb8c30 to your computer and use it in GitHub Desktop.
the model automatically generated from mlmodel
//
// MnistImageType.swift
//
// This file was automatically generated and should not be edited.
//
import CoreML
/// Model Prediction Input Type
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
class MnistImageTypeInput : MLFeatureProvider {
/// image as grayscale (kCVPixelFormatType_OneComponent8) image buffer, 28 pixels wide by 28 pixels high
var image: CVPixelBuffer
var featureNames: Set<String> {
get {
return ["image"]
}
}
func featureValue(for featureName: String) -> MLFeatureValue? {
if (featureName == "image") {
return MLFeatureValue(pixelBuffer: image)
}
return nil
}
init(image: CVPixelBuffer) {
self.image = image
}
}
/// Model Prediction Output Type
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
class MnistImageTypeOutput : MLFeatureProvider {
/// Source provided by CoreML
private let provider : MLFeatureProvider
/// symbol as dictionary of strings to doubles
lazy var symbol: [String : Double] = {
[unowned self] in return self.provider.featureValue(for: "symbol")!.dictionaryValue as! [String : Double]
}()
/// classLabel as string value
lazy var classLabel: String = {
[unowned self] in return self.provider.featureValue(for: "classLabel")!.stringValue
}()
var featureNames: Set<String> {
return self.provider.featureNames
}
func featureValue(for featureName: String) -> MLFeatureValue? {
return self.provider.featureValue(for: featureName)
}
init(symbol: [String : Double], classLabel: String) {
self.provider = try! MLDictionaryFeatureProvider(dictionary: ["symbol" : MLFeatureValue(dictionary: symbol as [AnyHashable : NSNumber]), "classLabel" : MLFeatureValue(string: classLabel)])
}
init(features: MLFeatureProvider) {
self.provider = features
}
}
/// Class for model loading and prediction
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
class MnistImageType {
var model: MLModel
/// URL of model assuming it was installed in the same bundle as this class
class var urlOfModelInThisBundle : URL {
let bundle = Bundle(for: MnistImageType.self)
return bundle.url(forResource: "MnistImageType", withExtension:"mlmodelc")!
}
/**
Construct a model with explicit path to mlmodelc file
- parameters:
- url: the file url of the model
- throws: an NSError object that describes the problem
*/
init(contentsOf url: URL) throws {
self.model = try MLModel(contentsOf: url)
}
/// Construct a model that automatically loads the model from the app's bundle
convenience init() {
try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
}
/**
Construct a model with configuration
- parameters:
- configuration: the desired model configuration
- throws: an NSError object that describes the problem
*/
@available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 5.0, *)
convenience init(configuration: MLModelConfiguration) throws {
try self.init(contentsOf: type(of:self).urlOfModelInThisBundle, configuration: configuration)
}
/**
Construct a model with explicit path to mlmodelc file and configuration
- parameters:
- url: the file url of the model
- configuration: the desired model configuration
- throws: an NSError object that describes the problem
*/
@available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 5.0, *)
init(contentsOf url: URL, configuration: MLModelConfiguration) throws {
self.model = try MLModel(contentsOf: url, configuration: configuration)
}
/**
Make a prediction using the structured interface
- parameters:
- input: the input to the prediction as MnistImageTypeInput
- throws: an NSError object that describes the problem
- returns: the result of the prediction as MnistImageTypeOutput
*/
func prediction(input: MnistImageTypeInput) throws -> MnistImageTypeOutput {
return try self.prediction(input: input, options: MLPredictionOptions())
}
/**
Make a prediction using the structured interface
- parameters:
- input: the input to the prediction as MnistImageTypeInput
- options: prediction options
- throws: an NSError object that describes the problem
- returns: the result of the prediction as MnistImageTypeOutput
*/
func prediction(input: MnistImageTypeInput, options: MLPredictionOptions) throws -> MnistImageTypeOutput {
let outFeatures = try model.prediction(from: input, options:options)
return MnistImageTypeOutput(features: outFeatures)
}
/**
Make a prediction using the convenience interface
- parameters:
- image as grayscale (kCVPixelFormatType_OneComponent8) image buffer, 28 pixels wide by 28 pixels high
- throws: an NSError object that describes the problem
- returns: the result of the prediction as MnistImageTypeOutput
*/
func prediction(image: CVPixelBuffer) throws -> MnistImageTypeOutput {
let input_ = MnistImageTypeInput(image: image)
return try self.prediction(input: input_)
}
/**
Make a batch prediction using the structured interface
- parameters:
- inputs: the inputs to the prediction as [MnistImageTypeInput]
- options: prediction options
- throws: an NSError object that describes the problem
- returns: the result of the prediction as [MnistImageTypeOutput]
*/
@available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 5.0, *)
func predictions(inputs: [MnistImageTypeInput], options: MLPredictionOptions = MLPredictionOptions()) throws -> [MnistImageTypeOutput] {
let batchIn = MLArrayBatchProvider(array: inputs)
let batchOut = try model.predictions(from: batchIn, options: options)
var results : [MnistImageTypeOutput] = []
results.reserveCapacity(inputs.count)
for i in 0..<batchOut.count {
let outProvider = batchOut.features(at: i)
let result = MnistImageTypeOutput(features: outProvider)
results.append(result)
}
return results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment