Skip to content

Instantly share code, notes, and snippets.

@jamesonthecrow
jamesonthecrow / fritz_cli_tutorial.py
Created July 6, 2019 14:02
Mobile machine learning with Fritz. https://fritz.ai
keras.backend.clear_session()
# Retrain the model with our new configuration and callback
model = build_model()
model.compile(
keras.optimizers.Adam(lr=metadata['learning_rate']),
loss=keras.losses.sparse_categorical_crossentropy,
metrics=[keras.metrics.sparse_categorical_accuracy]
)
@jamesonthecrow
jamesonthecrow / fritz_cli_tutorial.py
Created July 6, 2019 14:01
Mobile machine learning made easy with the Fritz CLI. https://fritz.ai
import fritz
import fritz.train
# Fritz needs to be configured first. Calling the fritz.Configure() method will
# read the credentials we setup for the CLI earlier.
fritz.configure()
# Create the callback
# Start by defining a training configuration and storing it as metadata
@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 / 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 / PetSeg-CreatePredictor.java
Last active July 26, 2019 14:42
Pet Segmentation on Android with Fritz (www.fritz.ai)
// Initialize the model included with the app
PetSegmentationOnDeviceModel onDeviceModel = new PetSegmentationOnDeviceModel();
FritzVisionSegmentPredictorOptions options = new FritzVisionSegmentPredictorOptions.Builder()
.targetConfidenceThreshold(.4f)
.build();
// Create the predictor with the Pet Segmentation model.
predictor = FritzVision.ImageSegmentation.getPredictor(onDeviceModel, options);
@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
Last active July 26, 2019 14:47
Pet Segmentation iOS View Controller with Fritz (www.fritz.ai)
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
/// The rest of the view controller...
/// Scores output from model greater than this value will be set as 1.
/// Lowering this value will make the mask more intense for lower confidence values.
var clippingScoresAbove: Double { return 0.6 }
/// Values lower than this value will not appear in the mask.
var zeroingScoresBelow: Double { return 0.4 }
@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()