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:
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.
# 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)
# Initialize its weights with the default initialization method
# Do a forward pass with random data
x = nd.random.uniform(-1,1,(3,4))
# Access the weight after the first forward pass:
# 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.Conv2D(channels=16, kernel_size=3, activation='relu'),
# The dense layer will automatically reshape the 4-D output of last
# max pooling layer into the 2-D shape: (x.shape, x.size/x.shape)
# The following codes show how to initialize the weights and run the forward pass:
# Input shape is (batch_size, color_channels, height, width)
x = nd.random.uniform(shape=(4,1,28,28))
y = net(x)
# Example of accessing the 1st layer’s weight and 6th layer’s bias:
# Create a neural network flexibly
def __init__(self, **kwargs):
# Run `nn.Block`'s init method
self.blk = nn.Sequential()
self.dense = nn.Dense(5)
def forward(self, x):
y = nd.relu(self.blk(x))
net = MixMLP()
# The usage of net is similar as before.
x = nd.random.uniform(shape=(2,2))
# Finally, let’s access a particular layer’s weight
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 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:
10 Ultimate Python AI Libraries: