Git Product home page Git Product logo

nativekeras's Introduction

NativeKeras is an open source library for deep learning. NativeKeras is a high-level library, vaguely based on Keras and Gluon. At the same time, NativeKeras is build in native code (C++). Thus, it's language agnostic in the sense that it could be easily integrated into any language that speaks JSON and Protocol Buffers! Bottom line is: no need of Python unless that's the language of your choice!

NativeKeras is implemented directly on top of Microsoft's Cognitive Toolkit.

As a sample implementatoin, NativeKeras provides a C# library.

Status

The current code base implements a good number of basic Keras functionality like initializers and activations. The basic Keras layers (Dense, Dropout, etc) are also implemented. Last, a good number of the image related layers (Conv2D, MaxPooling2D, etc) is implemented as well.

Limitations

NativeKeras is a proof of concept at this stage.

  • Windows is the only supported platform
  • No GPU use yet
  • Recurrent neural networks are not supported, however, there is a plan to enable them

Installation

To give NativeKeras a try from C#, install NativeKeras.CpuOnly nuget.

Otherwise, follow the build setup instructions below.

Goals and Roadmap

NativeKeras C++ layers are similar to the ongoing work to integrate ONNX into CNTK. To avoid duplication, we are planning to add new layers as they are added to CNTK. This is why NativeKeras still lacks, among others, LSTM and Embedding layers.

Our current priorities, in no specific order, are:

  • Streamline the build process.
  • Provide nuggets.
  • Streamline CPU/GPU training/scoring process using an approach similar to Keras' global options.
  • Add more optimizers. Currently SGD is pretty much the only one supported and tested.
  • Move to .NET Core and support Linux.
  • Extend the JSON model to support arbitrary (non-sequential) models.

Setup to build NativeKeras

The project depends on a few C++ libraries. One way to add most dependencies is via vcpkg:

git clone https://github.com/Microsoft/vcpkg.git
.\bootstrap-vcpkg.bat
.\vcpkg integrate install
.\vcpkg install protobuf:x64-windows
.\vcpkg install nlohmann-json:x64-windows
.\vcpkg install fmt:x64-windows
.\vcpkg install boost:x64-windows
.\vcpkg install gtest:x64-windows
.\vcpkg install torch-th:x64-windows

There are two dependencies which need to be installed by hand: CNTK and Torch's Tensor library.

Both are relatively easy to build (for C++ libraries that is):

  • CNTK: Either follow the development instructions. The Release_CpuOnly target is integrated in the project - see the instructions below. Alternatively, get the CNTK.CPUOnly nuget version 2.3.1. It contains the headers and the libraries - there are instrucitons below where to copy them.
  • Torch's Tensor library: it is installed via vcpkg, but the dll is needed at runtime.

To hook them up, without changing any project settings:

  • Torch's Tensor library: Copy the result folder of MAKE_INSTALL under the nativekeras solution, renaming it to th7. Alternatively, the content can be copied from the various subfolders of vcpkg/installed. The target directory contains three folders:
    • bin - the dll
    • include - the header files (contains a single directory called TH7)
    • lib - TH.lib
  • CNTK: Create a folder called cntk under the solution. From the CNTK repository, copy CNTK/Source/CNTKv2LibraryDll/API folder to the newly created cntk folder under the solution. Now you have cntk/API. Similary, from the CNTK repository copy CNTK/x64/Release_CpuOnly, to cntk/Release_CpuOnly. Alternatively, if using the nuget, copy the headers to the API directory, etc.

The CNTK version changes rapidly, I usually have to update the CNTK library name in the Visual Studio linker input section.

After these two steps, I would expect the project to build.

Training MNIST, Keras-style

Keras' MNIST example coded in NativeKeras:

// Define the DNN
var model = new Sequential();
model.Add(new Conv2D(32, kernelSize: new int[] { 3, 3 }, inputShape: new int[] { 28, 28, 1 }, activation: "relu"));
model.Add(new Conv2D(64, kernelSize: new int[] { 3, 3 }, activation: "relu"));

model.Add(new MaxPooling2D(poolSize: new int[] { 2, 2 }));
model.Add(new Dropout(0.25));
model.Add(new Flatten());
model.Add(new Dense(128, activation: "relu"));
model.Add(new Dropout(0.5));
model.Add(new Dense(10, activation: "softmax"));

var optimizer = new SGD(lr: 0.01);
model.Compile("categorical_crossentropy", optimizer, new string[] { "accuracy" });

// Load the training data. The code uses TensorSharp.
var xtrain = TensorUtils.Deserialize(new FileStream("Datasets/nda_mnist/mnist_xtrain.nda", FileMode.Open));
var ytrain = TensorUtils.Deserialize(new FileStream("Datasets/nda_mnist/mnist_ytrain.nda", FileMode.Open));

// The data is in row-major byte format. Convert to float and normalize.
xtrain = xtrain.Cast(DType.Float32);
xtrain = Ops.Div(null, xtrain, 255f);

ytrain = ytrain.Cast(DType.Float32);

// Kick off the training
model.Fit(xtrain, ytrain, batchSize: 128, epochs: 12);

Reference Keras Documentation directly for the meaning of each function and parameter used in the deep network configuratoin. Yes, it is that easy!

Then to predict:

// Load the test data
var xtest = TensorUtils.Deserialize(File.OpenRead("Datasets/nda_mnist/mnist_xtest.nda"));
xtest = xtest.Cast(DType.Float32);
xtest = Ops.Div(null, xtest, 255f);

var model = Sequential.Load("Models/mnist.model");

// Predict
var result = model.Predict(xtest, batchSize: 32);

The examples are from this repository's Example folder.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

nativekeras's People

Contributors

ivannp avatar microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nativekeras's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.