This file contains hidden or 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
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] | |
) |
This file contains hidden or 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 | |
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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
// 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); |
This file contains hidden or 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 hidden or 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
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 } |
This file contains hidden or 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() |
NewerOlder