Code Monkey home page Code Monkey logo

mxnet.sharp's Introduction

Work In Progress version 2.0.

There are many breaking change as per RFC: apache/mxnet#16167. With this change we are introducing NumPy-compatible coding experience into MXNet

Gitter Build Status NuGet


Apache MXNet (incubating) for Deep Learning

Apache MXNet (incubating) is a deep learning framework designed for both efficiency and flexibility. It allows you to mix symbolic and imperative programming to maximize efficiency and productivity. At its core, MXNet contains a dynamic dependency scheduler that automatically parallelizes both symbolic and imperative operations on the fly. A graph optimization layer on top of that makes symbolic execution fast and memory efficient. MXNet is portable and lightweight, scaling effectively to multiple GPUs and multiple machines.

MxNet.Sharp

MxNet.Sharp is a CSharp binding coving all the Imperative, Symbolic and Gluon API's with an easy to use interface. The Gluon library in Apache MXNet provides a clear, concise, and simple API for deep learning. It makes it easy to prototype, build, and train deep learning models without sacrificing training speed.

High Level Arch

High Level Arch

A New NumPy Interface for MxNet#

The MXNet community is pleased to announce a new NumPy interface for MXNet that allows developers to retain the familiar syntax of NumPy, while leveraging performance gains from accelerated computing on GPUs and asynchronous execution on CPUs and GPUs, in addition to automatic differentiation for differentiable NumPy ops through MxNet.Autograd.

The new NumPy interface from MXNet, MxNet.Numpy, is intended to be a drop-in replacement for NumPy, as such mxnet.numpy supports many familiar numpy.ndarray operations necessary for developing machine learning or deep learning models and operations are continually being added.

Work List

  • Project prep work for v2
  • Adding numpy ndarray array object and properties
  • Implementing numpy creation function
  • Implementing numpy elementwise
  • Numpy basic indexing
  • Numpy advance indexing
  • Nummy linear algebra functions
  • Numpy manipulation functions
  • Numpy search and sorting functions
  • Numpy statistical functions
  • Gluon updates with numpy ops
  • Implement numpy extension functions for neural network
  • Gluon probability
  • Mxnet 2 Onnx and Onnx 2 Mxnet
  • More examples
  • Unit testing
  • CI Builds

MxNet.Numpy Vs NumPy Performance

Lets consider simple test to see the performance difference. I will keep adding more scenarios and with GPU test as well.

Scenario 1

using MxNet;
using MxNet.Numpy;
using System;

namespace PerfTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime start = DateTime.Now;
            var x = np.random.uniform(size: new Shape(3000, 3000));
            var y = np.random.uniform(size: new Shape(3000, 3000));
            var d = np.dot(x, y);
            npx.waitall();
            Console.WriteLine(d.shape);
            Console.WriteLine("Duration: " + (DateTime.Now - start).TotalMilliseconds / 1000);
        }
    }
}
import numpy as np
import time

start_time = time.time()
x = np.random.uniform(0, 1, (3000, 1000))
y = np.random.uniform(0, 1, (3000, 3000))
d = np.dot(x, y);
#d = 0.5 * np.sqrt(x) + np.sin(y) * np.log(x) - np.exp(y)
print(d.shape)
print("--- %s sec ---" % (time.time() - start_time))

Scenario 2

using MxNet;
using MxNet.Numpy;
using System;

namespace PerfTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime start = DateTime.Now;
            var x = np.random.uniform(size: new Shape(30000, 10000));
            var y = np.random.uniform(size: new Shape(30000, 10000));
            var d = 0.5f * np.sqrt(x) + np.sin(y) * np.log(x) - np.exp(y);
            npx.waitall();
            Console.WriteLine(d.shape);
            Console.WriteLine("Duration: " + (DateTime.Now - start).TotalMilliseconds / 1000);
        }
    }
}
import numpy as np
import time

start_time = time.time()
x = np.random.uniform(0, 1, (30000, 10000))
y = np.random.uniform(0, 1, (30000, 10000))
d = 0.5 * np.sqrt(x) + np.sin(y) * np.log(x) - np.exp(y)
print(d.shape)
print("--- %s sec ---" % (time.time() - start_time))
Scenario MxNet CPU NumPy
1 1.2247 145.4460
2 24.4994 14.3616

Nuget

Install the package: Install-Package MxNet.Sharp

https://www.nuget.org/packages/MxNet.Sharp

Add the MxNet redistributed package available as per below.

Important: Make sure your installed CUDA version matches the CUDA version in the nuget package.

Check your CUDA version with the following command:

nvcc --version

You can either upgrade your CUDA install or install the MXNet package that supports your CUDA version.

MxNet Version Build: https://github.com/apache/incubator-mxnet/releases/tag/1.5.0

Win-x64 Packages

Type Name Nuget
MxNet-CPU MxNet CPU Version Install-Package MxNet.Runtime.Redist
MxNet-MKL MxNet CPU with MKL Install-Package MxNet-MKL.Runtime.Redist
MxNet-CU101 MxNet for Cuda 10.1 and CuDnn 7 Install-Package MxNet-CU101.Runtime.Redist
MxNet-CU101MKL MxNet for Cuda 10.1 and CuDnn 7 Install-Package MxNet-CU101MKL.Runtime.Redist
MxNet-CU100 MxNet for Cuda 10 and CuDnn 7 Install-Package MxNet-CU100.Runtime.Redist
MxNet-CU100MKL MxNet with MKL for Cuda 10 and CuDnn 7 Install-Package MxNet-CU100MKL.Runtime.Redist
MxNet-CU92 MxNet for Cuda 9.2 and CuDnn 7 Install-Package MxNet-CU100.Runtime.Redist
MxNet-CU92MKL MxNet with MKL for Cuda 9.2 and CuDnn 7 Install-Package MxNet-CU92MKL.Runtime.Redist
MxNet-CU80 MxNet for Cuda 8.0 and CuDnn 7 Install-Package MxNet-CU100.Runtime.Redist
MxNet-CU80MKL MxNet with MKL for Cuda 8.0 and CuDnn 7 Install-Package MxNet-CU80MKL.Runtime.Redist

Linux-x64 Packages

Type Name Nuget
MxNet-CPU MxNet CPU Version Install-Package MxNet.Linux.Runtime.Redist
MxNet-MKL MxNet CPU with MKL Install-Package MxNet-MKL.Linux.Runtime.Redist
MxNet-CU101 MxNet for Cuda 10.1 and CuDnn 7 Yet to publish
MxNet-CU101MKL MxNet for Cuda 10.1 and CuDnn 7 Yet to publish
MxNet-CU100 MxNet for Cuda 10 and CuDnn 7 Yet to publish
MxNet-CU100MKL MxNet with MKL for Cuda 10 and CuDnn 7 Yet to publish
MxNet-CU92 MxNet for Cuda 9.2 and CuDnn 7 Yet to publish
MxNet-CU92MKL MxNet with MKL for Cuda 9.2 and CuDnn 7 Yet to publish
MxNet-CU80 MxNet for Cuda 8.0 and CuDnn 7 Yet to publish
MxNet-CU80MKL MxNet with MKL for Cuda 8.0 and CuDnn 7 Yet to publish

OSX-x64 Packages

Type Name Nuget
MxNet-CPU MxNet CPU Version Yet to publish
MxNet-MKL MxNet CPU with MKL Yet to publish
MxNet-CU101 MxNet for Cuda 10.1 and CuDnn 7 Yet to publish
MxNet-CU101MKL MxNet for Cuda 10.1 and CuDnn 7 Yet to publish
MxNet-CU100 MxNet for Cuda 10 and CuDnn 7 Yet to publish
MxNet-CU100MKL MxNet with MKL for Cuda 10 and CuDnn 7 Yet to publish
MxNet-CU92 MxNet for Cuda 9.2 and CuDnn 7 Yet to publish
MxNet-CU92MKL MxNet with MKL for Cuda 9.2 and CuDnn 7 Yet to publish
MxNet-CU80 MxNet for Cuda 8.0 and CuDnn 7 Yet to publish
MxNet-CU80MKL MxNet with MKL for Cuda 8.0 and CuDnn 7 Yet to publish

Gluon MNIST Example

Demo as per: https://mxnet.apache.org/api/python/docs/tutorials/packages/gluon/image/mnist.html

var mnist = TestUtils.GetMNIST(); //Get the MNIST dataset, it will download if not found
var batch_size = 200; //Set training batch size
var train_data = new NDArrayIter(mnist["train_data"], mnist["train_label"], batch_size, true);
var val_data = new NDArrayIter(mnist["test_data"], mnist["test_label"], batch_size);

// Define simple network with dense layers
var net = new Sequential();
net.Add(new Dense(128, ActivationType.Relu));
net.Add(new Dense(64, ActivationType.Relu));
net.Add(new Dense(10));

//Set context, multi-gpu supported
var gpus = TestUtils.ListGpus();
var ctx = gpus.Count > 0 ? gpus.Select(x => Context.Gpu(x)).ToArray() : new[] {Context.Cpu(0)};

//Initialize the weights
net.Initialize(new Xavier(magnitude: 2.24f), ctx);

//Create the trainer with all the network parameters and set the optimizer
var trainer = new Trainer(net.CollectParams(), new Adam());

var epoch = 10;
var metric = new Accuracy(); //Use Accuracy as the evaluation metric.
var softmax_cross_entropy_loss = new SoftmaxCELoss();
float lossVal = 0; //For loss calculation
for (var iter = 0; iter < epoch; iter++)
{
    var tic = DateTime.Now;
    // Reset the train data iterator.
    train_data.Reset();
    lossVal = 0;

    // Loop over the train data iterator.
    while (!train_data.End())
    {
        var batch = train_data.Next();

        // Splits train data into multiple slices along batch_axis
        // and copy each slice into a context.
        var data = Utils.SplitAndLoad(batch.Data[0], ctx, batch_axis: 0);

        // Splits train labels into multiple slices along batch_axis
        // and copy each slice into a context.
        var label = Utils.SplitAndLoad(batch.Label[0], ctx, batch_axis: 0);

        var outputs = new NDArrayList();

        // Inside training scope
        using (var ag = Autograd.Record())
        {
            outputs = Enumerable.Zip(data, label, (x, y) =>
            {
                var z = net.Call(x);

                // Computes softmax cross entropy loss.
                NDArray loss = softmax_cross_entropy_loss.Call(z, y);

                // Backpropagate the error for one iteration.
                loss.Backward();
                lossVal += loss.Mean();
                return z;
            }).ToList();
        }

        // Updates internal evaluation
        metric.Update(label, outputs.ToArray());

        // Make one step of parameter update. Trainer needs to know the
        // batch size of data to normalize the gradient by 1/batch_size.
        trainer.Step(batch.Data[0].Shape[0]);
    }

    var toc = DateTime.Now;

    // Gets the evaluation result.
    var (name, acc) = metric.Get();

    // Reset evaluation result to initial state.
    metric.Reset();
    Console.Write($"Loss: {lossVal} ");
    Console.WriteLine($"Training acc at epoch {iter}: {name}={(acc * 100).ToString("0.##")}%, Duration: {(toc - tic).TotalSeconds.ToString("0.#")}s");
}

Reached accuracy of 98% within 6th epoch.

alt text

Documentation (In Progress)

https://mxnet.tech-quantum.com/

mxnet.sharp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar

mxnet.sharp's Issues

nd.Array Yielding Unexpected Results

I am trying to create an NDArray by using nd.Array(), however if I pass in Int32 values for the array, it is giving me unexpected results.

For example, if I create:
var ndArray = nd.Array(new int[] {0, 0, 15, 15})

It creates the NDArray with DType = float32, when I am expecting DType = int32
The underlying ArrayData for the object becomes {0, 0, 2.101948E-44, 2.101948E-44}, when it should be {0, 0, 15, 15}

Additionally, calling nd.Array() and passing in the array as a NumpyDotNet NDArray also creates unexpected results. In this case, it appears to be creating the NDArray as it does in the first scenario, then casting to int32, which results in ArrayData being {0, 0, 0, 0}

I believe if nd.Array() used NativeMethods.MXNDArrayCreateEx() instead of NativeMethods.MXNDArrayCreate(), the DType could be determined from the passed in array and passed to the function to set the NDArray properly.

Slice Support For More Than Two Dimensions

When working with NDArrays that have more than two dimensions, there doesn't seem to be a way to slice the NDArray to either get or set data.

For example, when working with images using three channels, say with shape (250,250,3), I will try and do this:
img[":,:,1"]
the shape should be (250,250,1), but instead I get (250,250,3), returning all channels instead of the one that I want.

It looks like attempting to grab/setting the slice using the string format calls MxUtil.GetSliceNotation, which returns only the row and column beginning and ending indices. If MxUtil.GetSliceNotation could be updated to allow returning n-dimensions' beginning and ending indices, then it seems like more than two dimensional NDArrays could be sliced.

Publish Linux packages

Hi,

Many thanks for your great work. Iโ€™m working with Linux and would greatly appreciate if you could publish the Linux packages.

Many thanks in advance

libmxnet

Hi,
I see that you are working hard, every day the files are updated.

I have tried to compile the entire master source with the regression example and I am getting this error

Unhandled exception. System.DllNotFoundException: Unable to load DLL 'libmxnet' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at MxNet.Interop.NativeMethods.MXNDArrayCreate64(Int32[] shape, Int32 ndim, DeviceType devType, Int32 devId, Int32 delayAlloc, Int32 dtype, IntPtr& out)

Where can I get the libmxnet ?

Update nuget package

Hello!

Is it possible to release an updated version of the nuget package for MxNet.Sharp? Thank you!

Error when calling Module.Bind() when using MxNet-CU101.Runtime.Redist

In trying to utilize GPUs, I have grabbed the MxNet-CU101.Runtime.Redist package, which uses mxnet-cu101 v1.5.0.

I have written some code to try and load an existing model and bind parameters to it:

    mModel = New Module(sym, context:new [] {Context.Gpu(0)}, label_names:null, data_names:new [] {"data"})
    mModel.Bind(new [] {New IO.DataDesc("data", New Shape(1, 3, 112, 112))}, for_training:False)
    mModel.SetParams(ndaArgParams, ndaAuxParams)

However, the call to mModel.Bind() produces the following exception:
System.ArgumentException: Can not pass IntPtr.Zero
Parameter name: h
at MxNet.Executor..ctor(IntPtr h, Context context, List1 gradReqs, Dictionary2 groupToCtx)
at MxNet.Symbol.SimpleBind(Context ctx, Dictionary2 grad_req, Dictionary2 type_dict, Dictionary2 stype_dict, Dictionary2 group2ctx, String[] shared_arg_names, Executor shared_exec, NDArrayDict shared_buffer, DataDesc[] kwargs)
at MxNet.Modules.DataParallelExecutorGroup.BindiThExec(Int32 i, DataDesc[] data_shapes, DataDesc[] label_shapes, DataParallelExecutorGroup shared_group)
at MxNet.Modules.DataParallelExecutorGroup.BindExec(DataDesc[] data_shapes, DataDesc[] label_shapes, DataParallelExecutorGroup shared_group, Boolean reshape)
at MxNet.Modules.DataParallelExecutorGroup..ctor(Symbol symbol, Context[] contexts, Int32[] workload, DataDesc[] data_shapes, DataDesc[] label_shapes, String[] param_names, Boolean for_training, Boolean inputs_need_grad, DataParallelExecutorGroup shared_group, String[] fixed_param_names, OpGradReq grad_req, String[] state_names, Dictionary`2[] group2ctxs)
at MxNet.Modules.Module.Bind(DataDesc[] data_shapes, DataDesc[] label_shapes, Boolean for_training, Boolean inputs_need_grad, Boolean force_rebind, Module shared_module, OpGradReq grad_req)

In the Symbol.SimpleBind() method, the call to NativeMethods.MXExecutorSimpleBindEx() does not return anything for exe_handle which caused the error. If I switch to using the CPU version of libmxnet, this function works properly with the exact same parameters passed in.

I was able to get this working properly with mxnet-cu101 v1.6.0b20191114. I tried getting v1.6.0, but it was giving me a really small libmxnet.dll and a bunch of really large mxnet_xx.dll files. If there was a way to get v1.6.0 of mxnet-cu101, I believe this would fix the issue.

MxNet CUDA 11

Hi,
Do you plan to make a version for mxnet 1.8 ? which supports CUDA 11, because this version does not work on NVidia RTX 3060

Thanks!

Img.ImDecode() does not load NDArray properly

First, I just want to say thank you @deepakkumar1984 for all of your work on this project. This project has been excellent in allowing me to do work in computer vision using the .NET framework.

I am attempting to load an image from memory into an NDArray using Img.Decode(). However, nd.Cvimdecode() creates an Operator by passing the buffer byte array in as a parameter:

        return new Operator("_cvimdecode")
            .SetParam("buf", buf)
            .SetParam("flag", flag)
            .SetParam("to_rgb", to_rgb)
            .Invoke();

I believe the buffer should be passed in as an input NDArray, replacing .SetParam("buf", buf) with .SetInput("buf", new NDArray(buf))

Cannot convert arrays to ndarray

When I try to convert any array to ndarray I get this error:

System.ArgumentException: 'Object contains non-primitive or non-blittable data.'

and I get the error in the new NDArray

var ctx = mx.Cpu();
float[][] labels= training_labels.ToArray(); // [1000][1]
var train_y1 = new NDArray(labels, new Shape(labels.Count(), 1), ctx: ctx, DType.Float16);

Is there a way to convert List of array of floats to NDArray ?

Thanks!

Add Ability To Load Model Parameters From Memory

I need to be able to store symbol and parameter files into a database and then load them into a model after retrieving them from the database. Currently, I can store the symbol file as a JSON string and load it using Symbol.LoadJSON(), however I cannot do this with the .params file.

Could we add the ability to load the parameter file from a buffer? I was able to do this by using MXNDArrayLoadFromBuffer(). This works just like MXNDArrayLoad(), but instead of loading from a file it will load from the in-memory bytes.

Finish the C# version of Keras project which got the MxNet backend

Keras project is part of MxNet.Sharp which is a high-level neural networks API, been developed in C#. Keras-MXNet is capable of running on top of high performance, scalable Apache MXNet deep learning engine.

Use Keras-MXNet if you need a deep learning library that:

Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
Supports both convolutional networks and recurrent networks, as well as combinations of the two.
Runs seamlessly on CPU, one GPU and multi-GPU.

Project: https://github.com/tech-quantum/MxNet.Sharp/tree/master/src/MxNet.Keras

Implement line by line python code and convert to C#. Project structure is already create with all the skeleton methods. Need to implement the methods by looking at python code:

Python Version: https://github.com/awslabs/keras-apache-mxnet/tree/keras-mxnet-2.2.4

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.