This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert to mobile formats | |
import coremltools | |
import tensorflow as tf | |
import tempfile | |
def convert_to_coreml(model): | |
return coremltools.converters.keras.convert( | |
model, | |
input_names=['input'], | |
output_names=['digit'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.datasets import mnist | |
keras.backend.clear_session() | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
def build_model(): | |
input = keras.layers.Input((28, 28, 1)) | |
out = keras.layers.Conv2D(16, 3, strides=2, activation='relu')(input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AVFoundation | |
import Fritz | |
class ViewController: UIViewController, UIImagePickerControllerDelegate, | |
UINavigationControllerDelegate { | |
@IBOutlet var imageView: UIImageView! | |
var maskView: UIImageView! | |
var backgroundView: UIImageView! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run the image through the model to identify pixels belonging to a pet. | |
FritzVisionSegmentResult segmentResult = predictor.predict(visionImage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Determine how to rotate the image from the camera used. | |
int imgRotation = FritzVisionOrientation.getImageRotationFromCamera(this, cameraId); | |
// Create a FritzVisionImage object from android.media.Image | |
FritzVisionImage visionImage = FritzVisionImage.fromMediaImage(image, imgRotation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate { | |
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { | |
let image = FritzVisionImage(buffer: sampleBuffer) | |
image.metadata = FritzVisionImageMetadata() | |
image.metadata?.orientation = FritzImageOrientation(from: connection) | |
guard let result = try? visionModel.predict(image) else { return } | |
let mask = result.buildSingleClassMask( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import Fritz | |
class ViewController: UIViewController { | |
var cameraView: UIImageView! | |
override func viewDidLoad() { | |
super.viewDidLoad() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from coremltools.models.neural_network import flexible_shape_utils | |
def make_mlmodel_flexible(spec, size_range=(100, 1920): | |
"""Make input and output sizes of a Core ML model flexible. | |
Args: | |
spec (NeuralNetwork_pb2): a Core ML neural network spec | |
size_range ([Int]): a tuple containing the min and max input sizes. | |
""" | |
size_range_spec = flexible_shape_utils.NeuralNetworkImageSizeRange() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a model with a normal convolution. | |
inpt = keras.layers.Input(shape=(500, 500, 3)) | |
out = keras.layers.Conv2D(10, 10)(inpt) | |
model = keras.models.Model(inpt, out) | |
mlmodel = coremltools.converters.keras.convert(model) | |
mlmodel.save('convolution.mlmodel') | |
# Create a model with a dialted (atrous) convolution. | |
inpt = keras.layers.Input(shape=(500, 500, 3)) | |
out = keras.layers.Conv2D(10, 10, dilation_rate=4)(inpt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PUT A BREAKPOINT HERE | |
// Compile the model. | |
let compiledModelURL = try! MLModel.compileModel(at: assetPath!) | |
// Initialize the model for use on a specific set of hardware | |
let config = MLModelConfiguration() | |
config.computeUnits = .all // can be .all, .cpuAndGPU, or .cpuOnly | |
let mlmodel = try! MLModel(contentsOf: compiledModelURL, configuration: config) |