BrainCore is a simple but fast neural network framework written in Swift.

Overview

BrainCore

CocoaPods Compatible Carthage Compatible

BrainCore is a simple but fast neural network framework written in Swift. It uses Metal which makes it screamin' fast. If you want to see it in action check out InfiniteMonkeys—an app that uses a recursive neural network to generate poems.

Features

  • Inner product layers
  • Linear rectifier (ReLU) layers
  • Sigmoid layers
  • LSTM layers
  • L2 Loss layers

Requirements

  • iOS 8.0+ / Mac OS X 10.11+
  • Xcode 7.2+
  • A device that supports Metal (doesn't work on the iOS simulator)

Usage

Network Definition

Before you build your network, start by building all the layers. This is as simple as calling each constructor:

let dataLayer = MyDataLayer()
let lstmLayer = LSTMLayer(weights: lstmWeights, biases: lstmBiases)
let ipLayer = InnerProductLayer(weights: ipWeights, biases: ipBiases)
let reluLayer = ReLULayer(size: ipBiases.count)
let sinkLayer = MySinkLayer()

BrainCore uses overloaded operators to make network definitions more concise. To connect layers together simply use the => operator inside a Net.build {} closure:

let net = Net.build {
    dataLayer => lstmLayer => ipLayer => reluLayer => sinkLayer
}

If you need to concatenate the output of two layers put them inside square brackets:

let net = Net.build {
    [dataLayer1, dataLayer2] => lstmLayer => ipLayer => reluLayer => sinkLayer
}

Similarly, if you need to split the output of one layer put its target layers in square brackets:

let net = Net.build {
    dataLayer => lstmLayer => ipLayer => reluLayer => [sinkLayer1, sinkLayer2]
}

When splitting, the inputSize of the target layers will determine where to split. If the sum of the target layers' inputSizes doesn't match the source layer's outputSize and error will be thrown.

If you want to continue on separate branches after a split you have to split the definition into separate lines:

let net = Net.build {
    dataLayer => lstmLayer => [ipLayer1, ipLayer2]
    ipLayer1 => reluLayer1 => sinkLayer1
    ipLayer2 => reluLayer2 => sinkLayer2
}

Finally if you want send multiple copies of the output of a layer to different layers use the =>> operator:

let net = Net.build {
    dataLayer => lstmLayer
    lstmLayer =>> ipLayer1 => reluLayer1 => sinkLayer1
    lstmLayer =>> ipLayer2 => reluLayer2 => sinkLayer2
}

Evaluating

Currently BrainCore only supports executing pre-trained networks. Ideally you would train your network on a server using one of the well-established neural network frameworks and import the trained weights into BrainCore. We are working on implementing solvers so that you can do everything inside BrainCore, stay posted.

Let's start by creating the layers.

// Load weights and biases from a pre-trained network
let lstmWeights = ...
let lstmBiases = ...
let ipWeights = ...
let ipBiases = ...

// Create layers
let dataLayer = MyDataLayer()
let lstmLayer = LSTMLayer(weights: lstmWeights, biases: lstmBiases)
let ipLayer = InnerProductLayer(weights: ipWeights, biases: ipBiases)
let reluLayer = ReLULayer(size: ipBiases.count)
let sinkLayer = MySinkLayer()

Next we'll build the net.

let net = Net.build {
    dataLayer => lstmLayer => ipLayer => reluLayer => sinkLayer
}

And finally execute! You need to provide a Metal device to the runner which is usually just the default device.

guard let device = MTLCreateSystemDefaultDevice() else {
    fatalError("Failed to create a Metal device.")
}

let evaluator: Evaluator
do {
    evaluator = try Evaluator(net: net, device: device)
} catch let e {
    fatalError("Failed to create an Evaluator: \(e)")
}

evaluator.evaluate { snapshot in
    print("Feed-forward pass complete!")
}

The evaluator may fail to build if there is any problem creating the buffers or initializing all the Metal code, that's why there is a try.

Calling evaluate() will execute a single forward pass, but you can call this as often as you want. In fact you will want to call evaluate() multiple times before you get any results back so that you maximise the GPU bandwidth. You can also increase the batch size to execute multiple passes in parallel.

Your data layer will most likely want to provide new data every time you call evaluate(). So your code may look something like

while !shouldStop {
    dataLayer.gather()
    evaluator.evaluate(completion)
}

Note: both the sink layer's consume() function and the completion closure will be called from a background thread. Make sure you synchronize access to the data as needed and try not to block on either of those calls for too long.


License

Upsurge is available under the MIT license. See the LICENSE file for more info.

Comments
  • Fix Carthage build

    Fix Carthage build

    Fixes https://github.com/Carthage/Carthage/issues/1428.

    Related issues: https://github.com/Carthage/Carthage/issues/517, http://www.openradar.me/20490378.

    Since you are setting up the project by using submodules and a workspace (as seen in bin/setup), there is no need to reference the dependency in Carthage/Build/$platform directories.

    opened by ikesyo 1
  • Add LSTM Backward

    Add LSTM Backward

    This is INCREDIBLY dense so we need to be really careful about checking this over like 30 times for errors.

    For reference here is the paper I based my implementation off of: http://arxiv.org/abs/1503.04069 (look at Supplementary Material section)

    and here is another reference that confirms (minus some of his own typos) the correctness: http://arunmallya.github.io/writeups/nn/lstm/#/6

    s3: awaiting validation t3: enhancement 
    opened by aidangomez 1
  • Separate evaluation from training

    Separate evaluation from training

    The evaluator is different from the trainer is some fundamental ways. For instance it makes sense to run the evaluator with a batch size of 1 multiple times in the case of streaming data. While the trainer always has a batch size and it rarely makes sense to run more than one. Therefore what used to be Runner was separated into Evaluator and Trainer.

    opened by alejandro-isaza 1
  • Improve net construction

    Improve net construction

    • [x] Concatenation

      [L1, L2] => L3

    • [x] Splitting

      L1.fromOffset(0) => L2 L1.fromOffset(10) => L3

    • [x] Linear connection

      L1 => L2 => L3

    opened by aidangomez 1
  • Add Backwards Pass To Runner

    Add Backwards Pass To Runner

    Pretty heavy PR, but comes out quite nicely in the end. You'll have to have a look at parallelism. I only know that the Backwards/ForwardsRunnerInstances have (at least) functioning parallelism, I can't really say if it's optimal.

    opened by aidangomez 1
  • Add solver and loss

    Add solver and loss

    • Added L2 Loss Layer and LossLayer protocol
    • Added SGDSolver and Solver protocol

    Solver contains a Runner contains a ForwardRunnerInstance and BackwardRunnerInstance.

    • ForwardRunnerInstance is responsible for input/output data
    • BackwardRunnerInstance is responsible for input/output diff
    • Solver is responsible for updating parameters
    opened by aidangomez 1
  • Implement Training

    Implement Training

    @aleph7 Made some updates to the ImplementTraining branch. Take a look and let me know what you think about the direction I'm taking (considering how you felt about the last one 😬)?

    t3: enhancement 
    opened by aidangomez 1
  • Refactor Net

    Refactor Net

    Net's should:

    • [x] have layers
    • [x] have blobs of data tied to layers
    • [x] implement forward, backward passes
    • [x] be able to request an update of a Layer's parameters
    • [x] report on the performance of the last forward pass
    opened by aidangomez 1
  • Compile in Xcode 9 problems

    Compile in Xcode 9 problems

    I've run the bin/setup, but still I get a No such module 'Upsurge' warning, even if I find an upsurge.framework and add it to the targets. I can't seem to get the Upsurge project from GitHub to compile as it won't upgrade to Swift 4. I'm using a framework from MLKit-Master, which is not the right way! Sorry if this is vague, as I can't get past this early stage.

    opened by luckybulldozer 0
  • Failing unit tests for SGDSolver

    Failing unit tests for SGDSolver

    I realize that your SGDSolver class in version 0.4.0 is probably a work in progress since it is not documented, but I decided to try it anyway. In my own code using that class, I got hundreds of identical console errors during training:

    error 10:02:43.602113 -0500 xctest Execution of the command buffer was aborted due to an error during execution. Internal Error (IOAF code 1)

    I decided to run the BrainCore unit tests, and discovered that they are getting the same console errors. The unit test failures are:

    file:///Users/samalone/Projects/Gorillias/Carthage/Checkouts/BrainCore/Tests/SGDSolverTests.swift: test failure: -[SGDSolverTests testTrainXOR()] failed: Asynchronous wait failed: Exceeded timeout of 20 seconds, with unfulfilled expectations: "Net train".

    file:///Users/samalone/Projects/Gorillias/Carthage/Checkouts/BrainCore/Tests/SGDSolverTests.swift: test failure: -[SGDSolverTests testTrainXOR()] failed: XCTAssertLessThan failed: ("0.0292995") is not less than ("-0.000584897") -

    file:///Users/samalone/Projects/Gorillias/Carthage/Checkouts/BrainCore/Tests/SGDSolverTests.swift: test failure: -[SGDSolverTests testTrainXOR()] failed: XCTAssertGreaterThan failed: ("-0.553643") is not greater than ("0.0646476") -

    file:///Users/samalone/Projects/Gorillias/Carthage/Checkouts/BrainCore/Tests/SGDSolverTests.swift: test failure: -[SGDSolverTests testTrainXOR()] failed: XCTAssertGreaterThan failed: ("-0.697093") is not greater than ("0.603576") -

    I'm running on a retina MacBook Pro Late 2013. The graphics card which Metal is presumably using is a NVIDIA GeForce GT 750M 2048 MB. Let me know if I can do any debugging or provide more information.

    opened by samalone 9
  • Add ability to import Caffe networks

    Add ability to import Caffe networks

    I don't think we should add all of Protobuf but maybe have a script that converts a caffe prototxt into simpler format. Then have that simpler format be the standard BrainCore network definition format.

    help wanted t3: enhancement hacktoberfest 
    opened by alejandro-isaza 13
Releases(0.4.0)
Owner
Alejandro Isaza
Alejandro Isaza
DeepInfant® is a Neural network system designed to predict whether and why your baby is crying.

DeepInfant DeepInfant® is a Neural network system designed to predict whether and why your baby is crying. DeepInfant uses artificial intelligence and

Skytells AI Research 14 Oct 19, 2022
A toolbox of AI modules written in Swift: Graphs/Trees, Support Vector Machines, Neural Networks, PCA, K-Means, Genetic Algorithms

AIToolbox A toolbox of AI modules written in Swift: Graphs/Trees, Linear Regression, Support Vector Machines, Neural Networks, PCA, KMeans, Genetic Al

Kevin Coble 776 Dec 18, 2022
Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

Palle 87 Dec 29, 2022
Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

Vishal 331 Oct 14, 2022
DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning.

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

DL4S Team 2 Dec 5, 2021
Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Neural Networks

mtcnn-caffe Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Neural Networks. This project provide you a method to update mu

Weilin Cong 500 Oct 30, 2022
Automatic colorization using deep neural networks. Colorful Image Colorization. In ECCV, 2016.

Colorful Image Colorization [Project Page] Richard Zhang, Phillip Isola, Alexei A. Efros. In ECCV, 2016. + automatic colorization functionality for Re

Richard Zhang 3k Dec 27, 2022
A framework for building fast genetic algorithms in Swift.

Revolver is a framework for building fast genetic algorithms in Swift 3.0. Features Chromosomes: strings, trees Genetic operators: reproduction, mutat

Petr Mánek 27 Nov 17, 2022
Tiny YOLO for iOS implemented using CoreML but also using the new MPS graph API.

YOLO with Core ML and MPSNNGraph This is the source code for my blog post YOLO: Core ML versus MPSNNGraph. YOLO is an object detection network. It can

Matthijs Hollemans 900 Dec 31, 2022
Detecting Text in Natural Image with Connectionist Text Proposal Network (ECCV'16)

Detecting Text in Natural Image with Connectionist Text Proposal Network The codes are used for implementing CTPN for scene text detection, described

Tian Zhi 1.3k Dec 22, 2022
MLKit is a simple machine learning framework written in Swift.

MLKit (a.k.a Machine Learning Kit) ?? MLKit is a simple machine learning framework written in Swift. Currently MLKit features machine learning algorit

Guled 152 Nov 17, 2022
This is an open-source project for the aesthetic evaluation of images based on the deep learning-caffe framework, which we completed in the Victory team of Besti.

This is an open-source project for the aesthetic evaluation of images based on the deep learning-caffe framework, which we completed in the Victory team of Besti.

The Victory Group of Besti 102 Dec 15, 2022
Text-cli - Command line tool for extracting text from images using Apple's Vision framework

text-cli Command line tool for extracting text from images using Apple's Vision

San Francisco International Airport Museum 9 Aug 29, 2022
A simple Swift package which acts as an OpenAI client for the GPT-3 API.

SwiftyGPT3 A simple Swift package which acts as an OpenAI client for GPT-3 brought to you by the Airgift Crew. Supports GPT-3 Codex! Requirements iOS

Airgift 23 Dec 25, 2022
A simple deep learning library for estimating a set of tags and extracting semantic feature vectors from given illustrations.

Illustration2Vec illustration2vec (i2v) is a simple library for estimating a set of tags and extracting semantic feature vectors from given illustrati

Masaki Saito 661 Dec 12, 2022
Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

Jitsi Meet is a set of Open Source projects which empower users to use and deploy video conferencing platforms with state-of-the-art video quality and features.

Jitsi 19.1k Jan 5, 2023
A Swift library for creating and exporting CoreML Models in Swift

SwiftCoreMLTools A Swift Library for creating CoreML models in Swift. Work in progress This library expose a (function builder based) DSL as well as a

Jacopo Mangiavacchi 140 Dec 5, 2022
The Swift machine learning library.

Swift AI is a high-performance deep learning library written entirely in Swift. We currently offer support for all Apple platforms, with Linux support

Swift AI 5.9k Jan 2, 2023
Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy.

Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

null 80 Dec 21, 2022