Skip to content

Instantly share code, notes, and snippets.

@jamesonthecrow
jamesonthecrow / fritz_cli_tutorial.py
Last active July 6, 2019 13:50
Mobile machine learning with the Fritz CLI. https://fritz.ai
# 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']
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)
@jamesonthecrow
jamesonthecrow / ViewController.swift
Last active June 11, 2019 22:30
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
import UIKit
import AVFoundation
import Fritz
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
var maskView: UIImageView!
var backgroundView: UIImageView!
@jamesonthecrow
jamesonthecrow / PetSeg-RunPrediction.java
Last active June 10, 2019 21:22
Pet Segmentation on Android with Fritz (www.fritz.ai)
// Run the image through the model to identify pixels belonging to a pet.
FritzVisionSegmentResult segmentResult = predictor.predict(visionImage);
@jamesonthecrow
jamesonthecrow / PetSeg-createImage.java
Last active June 10, 2019 21:22
Pet Segmentation on Android with Fritz (www.fritz.ai)
// 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);
@jamesonthecrow
jamesonthecrow / ViewController.swift
Created May 15, 2019 04:20
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
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(
@jamesonthecrow
jamesonthecrow / ViewController.swift
Created May 15, 2019 04:17
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
// ...
import Fritz
class ViewController: UIViewController {
var cameraView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
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()
# 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)
// 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)