Are you looking for a truly open-source deep learning framework suited for flexible research prototyping and production, and building a nice GUI for them? You can deliver ultra-scalable deep learning solutions easily by combining MXNet and Python4Delphi library, inside Delphi and C++Builder.
This post will guide you on how to run the MXNet library and use Python for Delphi to display it in the Delphi Windows GUI app.
Table of Contents
What is the MXNet library?
Apache MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to combine different flavors of deep learning programs to increase efficiency and productivity. In this article, we’ll review the MXNet version that uses OpenBLAS (optimized BLAS (Basic Linear Algebra Subprograms)) and MKL-DNN (Math Kernel Library for Deep Neural Networks).
At its core, MXNet contains a dynamic dependency scheduler that automatically parallelized 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, scalable to many GPUs and machines. MXNet is more than a deep learning project. It is a community on a mission of democratizing AI. It is a collection of blueprints and guidelines for building deep learning systems and interesting insights into DL systems for hackers.
What makes MXNet different?
Here is a quick overview of MXNet powerful features:
- Hybrid Front-End: Provide seamless transitions between Gluon eager imperative mode and symbolic mode to provide both flexibility and speed.
- Distributed Training: Scalable distributed training and performance optimization in research and production is enabled by the dual Parameter Server and Horovod support.
- 8 Language Bindings: Deep integration into Python and support for Scala, Julia, Clojure, Java, C++, R, and Perl.
- A thriving ecosystem of tools and libraries extends MXNet and enables use-cases in computer vision, NLP, time series, and more.
How do I enable MXNet inside Python4Delphi in Windows?
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
The next step is installing MXNet in your system. Here is how you can get MXNet using the pip command:
1 |
pip install mxnet |
How do I create a neural network inside Python4Delphi in Windows using MXNet?
What does the following code do?
- Create neural networks in Gluon by importing the neural network nn package from gluon.
- Create your neural network’s first layer, starting with a dense layer with 2 output units.
- Then we do a forward pass with random data.
- Chain layers into a neural network.
- Implements a famous network called LeNet through nn.Sequential.
- Initialize the weights and run the forward pass.
- Example of accessing any given layer’s weight and bias.
- Create a neural network flexibly.
- Accessing a particular layer’s weight.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# Create neural networks in Gluon # Import the neural network nn package from gluon from mxnet import nd from mxnet.gluon import nn # Create your neural network’s first layer layer = nn.Dense(2) print(layer) # Initialize its weights with the default initialization method layer.initialize() # Do a forward pass with random data x = nd.random.uniform(-1,1,(3,4)) print(layer(x)) # Access the weight after the first forward pass: layer.weight.data() # Chain layers into a neural network # The following code implements a famous network called LeNet through nn.Sequential: net = nn.Sequential() # Add a sequence of layers. net.add(# Similar to Dense, it is not necessary to specify the input channels # by the argument `in_channels`, which will be automatically inferred # in the first forward pass. Also, we apply a relu activation on the # output. In addition, we can use a tuple to specify a non-square # kernel size, such as `kernel_size=(2,4)` nn.Conv2D(channels=6, kernel_size=5, activation='relu'), # One can also use a tuple to specify non-symmetric pool and stride sizes nn.MaxPool2D(pool_size=2, strides=2), nn.Conv2D(channels=16, kernel_size=3, activation='relu'), nn.MaxPool2D(pool_size=2, strides=2), # The dense layer will automatically reshape the 4-D output of last # max pooling layer into the 2-D shape: (x.shape[0], x.size/x.shape[0]) nn.Dense(120, activation="relu"), nn.Dense(84, activation="relu"), nn.Dense(10)) print(net) # The following codes show how to initialize the weights and run the forward pass: net.initialize() # Input shape is (batch_size, color_channels, height, width) x = nd.random.uniform(shape=(4,1,28,28)) y = net(x) print(y.shape) # Example of accessing the 1st layer’s weight and 6th layer’s bias: print(net[0].weight.data().shape, net[5].bias.data().shape) # Create a neural network flexibly class MixMLP(nn.Block): def __init__(self, **kwargs): # Run `nn.Block`'s init method super(MixMLP, self).__init__(**kwargs) self.blk = nn.Sequential() self.blk.add(nn.Dense(3, activation='relu'), nn.Dense(4, activation='relu')) self.dense = nn.Dense(5) def forward(self, x): y = nd.relu(self.blk(x)) print(y) return self.dense(y) net = MixMLP() print(net) # The usage of net is similar as before. net.initialize() x = nd.random.uniform(shape=(2,2)) print(net(x)) # Finally, let’s access a particular layer’s weight print(net.blk[1].weight.data()) |
Let’s see the result:
Download the source code here!
Congratulations, now you have learned how to run the MXNet library using Python for Delphi to display it in the Delphi Windows GUI app! Now you can solve various real-world problems using the ultra-scalable deep learning framework created by the MXNet library and Python4Delphi.
Where can I find more examples of building GUI for AI libraries?
Check out the MXNet deep learning framework for Python and use it in your projects:
https://pypi.org/project/mxnet/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi
Or read our collections of AI-related articles:
Keras:
TensorFlow:
PyTorch:
scikit-learn:
Theano:
10 Ultimate Python AI Libraries: