/Not-Nikola

Primary LanguageSwift

Not Nikola

An image classifier in the style of Not Hotdog from the Silicon Valley TV show. Built with Core ML and trained with Create ML.

Training

The classifier model was trained with images of Nikola Brežnjak from the TelTech Bahamas cruise. Training for a new model can be setup with Create ML and Swift Playgrounds in a few lines of code.

import CreateMLUI

let builder = MLImageClassifierBuilder()
builder.showInLiveView()
Labeled folders of images can then be dragged into the playground to train and test the classifier.

Usage

A Core ML image analysis request will return classification results based on image input. As a result of the Nikola model being trained with a relatively low number of images, this example app only identifies an image as Nikola if it has the highest possible confidence value.

let model = try VNCoreMLModel(for: NikolaClassifier().model)

let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in
    DispatchQueue.main.async {
        if let results = request.results {
            let classifications = results as! [VNClassificationObservation]
            self?.classificationView.isHidden = false
            
            if classifications.isEmpty {
                self?.classificationLabel.text = "Nothing there"
            } else {
                let classification = classifications.first
                if classification?.identifier == "Nikola" && CGFloat(classification!.confidence) == 1.0 {
                    self?.classificationLabel.text = "Nikola"
                } else {
                    self?.classificationLabel.text = "Not Nikola"
                }
            }
        }
    }
})

A more detailed tutorial on training with Create ML can be found at https://www.raywenderlich.com/5653-create-ml-tutorial-getting-started.