Apache Mxnet 简明教程

Apache MXNet - Toolkits and Ecosystem

为了支持跨多个领域的深度学习应用程序的研发,Apache MXNet 为我们提供了一个丰富的工具包、库等生态系统。让我们探索它们--

To support the research and development of Deep Learning applications across many fields, Apache MXNet provides us a rich ecosystem of toolkits, libraries and many more. Let us explore them −

ToolKits

以下是 MXNet 提供的部分最常用且最重要的工具包--

Following are some of the most used and important toolkits provided by MXNet −

GluonCV

顾名思义,GluonCV 是一个由 MXNet 驱动的 Gluon 工具包,用于计算机视觉。它提供了计算机视觉 (CV) 中最先进的 DL(深度学习)算法的实现。在 GluonCV 工具包的帮助下,工程师、研究人员和学生们可以验证新想法并轻松学习 CV。

As name implies GluonCV is a Gluon toolkit for computer vision powered by MXNet. It provides implementation of state-of-the-art DL (Deep Learning) algorithms in computer vision (CV). With the help of GluonCV toolkit engineers, researchers, and students can validate new ideas and learn CV easily.

下面给出了一些 features of GluonCV — 

Given below are some of the features of GluonCV

  1. It trains scripts for reproducing state-of-the-art results reported in latest research.

  2. More than 170+ high quality pretrained models.

  3. Embrace flexible development pattern.

  4. GluonCV is easy to optimize. We can deploy it without retaining heavy weight DL framework.

  5. It provides carefully designed APIs that greatly lessen the implementation intricacy.

  6. Community support.

  7. Easy to understand implementations.

以下 supported applications 基于 GluonCV 工具包:

Following are the supported applications by GluonCV toolkit:

  1. Image Classification

  2. Object Detection

  3. Semantic Segmentation

  4. Instance Segmentation

  5. Pose Estimation

  6. Video Action Recognition

我们可以使用 pip 安装 GluonCV,如下所示 -

We can install GluonCV by using pip as follows −

pip install --upgrade mxnet gluoncv

GluonNLP

顾名思义,GluonNLP 是由 MXNet 驱动的用于自然语言处理 (NLP) 的 Gluon 工具包。它提供了 NLP 领域中先进的 DL(深度学习)模型实现。

As name implies GluonNLP is a Gluon toolkit for Natural Language Processing (NLP) powered by MXNet. It provides implementation of state-of-the-art DL (Deep Learning) models in NLP.

借助 GluonNLP 工具包,工程师、研究人员和学生可以构建文本数据管道和模型。基于这些模型,他们可以快速构建研究理念和产品原型。

With the help of GluonNLP toolkit engineers, researchers, and students can build blocks for text data pipelines and models. Based on these models, they can quickly prototype the research ideas and product.

以下是 GluonNLP 的一些特性:

Given below are some of the features of GluonNLP:

  1. It trains scripts for reproducing state-of-the-art results reported in latest research.

  2. Set of pretrained models for common NLP tasks.

  3. It provides carefully designed APIs that greatly lessen the implementation intricacy.

  4. Community support.

  5. It also provides tutorials to help you get started on new NLP tasks.

以下是我们使用 GluonNLP 工具包可以实现的 NLP 任务 -

Following are the NLP tasks we can implement with GluonNLP toolkit −

  1. Word Embedding

  2. Language Model

  3. Machine Translation

  4. Text Classification

  5. Sentiment Analysis

  6. Natural Language Inference

  7. Text Generation

  8. Dependency Parsing

  9. Named Entity Recognition

  10. Intent Classification and Slot Labeling

我们可以使用 pip 安装 GluonNLP,如下所示 -

We can install GluonNLP by using pip as follows −

pip install --upgrade mxnet gluonnlp

GluonTS

顾名思义,GluonTS 是由 MXNet 驱动的用于概率时间序列建模的 Gluon 工具包。

As name implies GluonTS is a Gluon toolkit for Probabilistic Time Series Modeling powered by MXNet.

它提供以下特性 -

It provides the following features −

  1. State-of-the-art (SOTA) deep learning models ready to be trained.

  2. The utilities for loading as well as iterating over time-series datasets.

  3. Building blocks to define your own model.

借助 GluonTS 工具包,工程师、研究人员和学生可以在自己的数据上训练和评估任何内置模型,快速尝试不同的解决方案,并针对其时间序列任务提出解决方案。

With the help of GluonTS toolkit engineers, researchers, and students can train and evaluate any of the built-in models on their own data, quickly experiment with different solutions, and come up with a solution for their time series tasks.

他们还可以使用所提供的抽象和构建模块创建自定义时间序列模型,并针对基线算法快速对其进行基准测试。

They can also use the provided abstractions and building blocks to create custom time series models, and rapidly benchmark them against baseline algorithms.

我们可以使用 pip 安装 GluonTS,如下所示 -

We can install GluonTS by using pip as follows −

pip install gluonts

GluonFR

顾名思义,它是一个用于 FR(人脸识别)的 Apache MXNet Gluon 工具包。它提供了以下特性 -

As name implies, it is an Apache MXNet Gluon toolkit for FR (Face Recognition). It provides the following features −

  1. State-of-the-art (SOTA) deep learning models in face recognition.

  2. The implementation of SoftmaxCrossEntropyLoss, ArcLoss, TripletLoss, RingLoss, CosLoss/AMsoftmax, L2-Softmax, A-Softmax, CenterLoss, ContrastiveLoss, and LGM Loss, etc.

要安装 gluon face,则需要 Python 3.5 或更高版本。我们还需要先安装 GluonCV 和 MXNet,如下所示:

In order to install Gluon Face, we need Python 3.5 or later. We also first need to install GluonCV and MXNet first as follows −

pip install gluoncv --pre
pip install mxnet-mkl --pre --upgrade
pip install mxnet-cuXXmkl --pre –upgrade # if cuda XX is installed

安装依赖项后,可以使用以下命令安装 GluonFR:

Once you installed the dependencies, you can use the following command to install GluonFR −

From Source

From Source

pip install git+https://github.com/THUFutureLab/gluon-face.git@master

Pip

Pip

pip install gluonfr

Ecosystem

现在,让我们探索 MXNet 丰富的库、包和框架:

Now let us explore MXNet’s rich libraries, packages, and frameworks −

Coach RL

Intel AI 实验室创建的 Python 强化学习 (RL) 框架 Coach。它支持轻松试验 State-of-the-art RL 算法。Coach RL 支持 Apache MXNet 作为后端,并允许轻松集成新的环境进行求解。

Coach, a Python Reinforcement Learning (RL) framework created by Intel AI lab. It enables easy experimentation with State-of-the-art RL algorithms. Coach RL supports Apache MXNet as a back end and allows simple integration of new environment to solve.

为了轻松扩展和重复利用现有组件,Coach RL 很好的解耦了基本的强化学习组件,如算法、环境、神经网络架构、探索策略。

In order to extend and reuse existing components easily, Coach RL very well decoupled the basic reinforcement learning components such as algorithms, environments, NN architectures, exploration policies.

以下面向 Coach RL 框架的智能体和支持算法:

Following are the agents and supported algorithms for Coach RL framework −

Value Optimization Agents

  1. Deep Q Network (DQN)

  2. Double Deep Q Network (DDQN)

  3. Dueling Q Network

  4. Mixed Monte Carlo (MMC)

  5. Persistent Advantage Learning (PAL)

  6. Categorical Deep Q Network (C51)

  7. Quantile Regression Deep Q Network (QR-DQN)

  8. N-Step Q Learning

  9. Neural Episodic Control (NEC)

  10. Normalized Advantage Functions (NAF)

  11. Rainbow

Policy Optimization Agents

  1. Policy Gradients (PG)

  2. Asynchronous Advantage Actor-Critic (A3C)

  3. Deep Deterministic Policy Gradients (DDPG)

  4. Proximal Policy Optimization (PPO)

  5. Clipped Proximal Policy Optimization (CPPO)

  6. Generalized Advantage Estimation (GAE)

  7. Sample Efficient Actor-Critic with Experience Replay (ACER)

  8. Soft Actor-Critic (SAC)

  9. Twin Delayed Deep Deterministic Policy Gradient (TD3)

General Agents

  1. Direct Future Prediction (DFP)

Imitation Learning Agents

  1. Behavioral Cloning (BC)

  2. Conditional Imitation Learning

Hierarchical Reinforcement Learning Agents

  1. Hierarchical Actor Critic (HAC)

Deep Graph Library

Deep Graph Library (DGL) 由纽约大学和亚马逊网络服务团队(上海)开发,这是一个可在 MXNet 之上提供图神经网络 (GNN) 轻松实现的 Python 包。它还可在 PyTorch、Gluon 等其他现有主流深度学习库之上提供 GNN 的轻松实现。

Deep Graph Library (DGL), developed by NYU and AWS teams, Shanghai, is a Python package that provides easy implementations of Graph Neural Networks (GNNs) on top of MXNet. It also provides easy implementation of GNNs on top of other existing major deep learning libraries like PyTorch, Gluon, etc.

Deep Graph Library 是一款免费软件。它在 Ubuntu 16.04、macOS X 以及 Windows 7 或更高版本的系统上均可用。它还需要 Python 3.5 或更高版本。

Deep Graph Library is a free software. It is available on all Linux distributions later than Ubuntu 16.04, macOS X, and Windows 7 or later. It also requires the Python 3.5 version or later.

以下是 DGL 的功能 −

Following are the features of DGL −

No Migration cost − DGL 建立在流行的现有 DL 框架之上,因此使用它没有迁移成本。

No Migration cost − There is no migration cost for using DGL as it is built on top of popular exiting DL frameworks.

Message Passing − DGL 提供消息传递并对其具有多功能控制。消息传递的范围从低级操作(例如沿着选定的边发送)到高级控制(例如图级特征更新)。

Message Passing − DGL provides message passing and it has versatile control over it. The message passing ranges from low-level operations such as sending along selected edges to high-level control such as graph-wide feature updates.

Smooth Learning Curve − 由于强大的用户自定义函数既灵活又易于使用,因此学习和使用 DGL 非常容易。

Smooth Learning Curve − It is quite easy to learn and use DGL as the powerful user-defined functions are flexible as well as easy to use.

Transparent Speed Optimization − DGL 通过自动批处理计算和稀疏矩阵乘法提供透明的速度优化。

Transparent Speed Optimization − DGL provides transparent speed optimization by doing automatic batching of computations and sparse matrix multiplication.

High performance − 为了达到最大的效率,DGL 自动将 DNN(深度神经网络)训练批处理在一起或多个图。

High performance − In order to achieve maximum efficiency, DGL automatically batches DNN (deep neural networks) training on one or many graphs together.

Easy & friendly interface − DGL 为我们提供了用于边缘特征访问以及图结构操作的简单友好的界面。

Easy & friendly interface − DGL provides us easy & friendly interfaces for edge feature access as well as graph structure manipulation.

InsightFace

InsightFace,一个用于面部分析的深度学习工具包,它提供了由 MXNet 提供支持的计算机视觉中 SOTA(最先进)面部分析算法的实现。它提供 −

InsightFace, a Deep Learning Toolkit for Face Analysis that provides implementation of SOTA (state-of-the-art) face analysis algorithm in computer vision powered by MXNet. It provides −

  1. High-quality large set of pre-trained models.

  2. State-of-the-art (SOTA) training scripts.

  3. InsightFace is easy to optimize. We can deploy it without retaining heavy weight DL framework.

  4. It provides carefully designed APIs that greatly lessen the implementation intricacy.

  5. Building blocks to define your own model.

我们可以使用 pip 安装 InsightFace 如下 −

We can install InsightFace by using pip as follows −

pip install --upgrade insightface

请注意,在安装 InsightFace 之前,请根据您的系统配置安装正确的 MXNet 软件包。

Please note that before installing InsightFace, please install the correct MXNet package according to your system configuration.

Keras-MXNet

众所周知,Keras 是用 Python 编写的用于神经网络(NN)的高级 API,Keras-MXNet 为 Keras 提供后端支持。它可以在高性能和可扩展的 Apache MXNet DL 框架之上运行。

As we know that Keras is a high-level Neural Network (NN) API written in Python, Keras-MXNet provides us a backend support for the Keras. It can run on top of high performance and scalable Apache MXNet DL framework.

Keras-MXNet 的功能如下所述 −

The features of Keras-MXNet are mentioned below −

  1. Allows users for easy, smooth, and fast prototyping. It all happens through user friendliness, modularity, and extensibility.

  2. Supports both CNN (Convolutional Neural Networks) and RNN (Recurrent Neural Networks) as well as the combination of both also.

  3. Runs flawlessly on both Central Processing Unit (CPU) and Graphical Processing Unit (GPU).

  4. Can run on one or multi GPU.

为了使用此后端,您首先需要按如下方式安装 keras-mxnet −

In order to work with this backend, you first need to install keras-mxnet as follows −

pip install keras-mxnet

现在,如果您使用 GPU,则按以下方式安装并支持 CUDA 9 的 MXNet。

Now, if you are using GPUs then install MXNet with CUDA 9 support as follows −

pip install mxnet-cu90

但如果您只使用 CPU,则按以下方式安装基本 MXNet。

But if you are using CPU-only then install basic MXNet as follows −

pip install mxnet

MXBoard

MXBoard 是一个用 Python 编写的记录工具,用于记录 MXNet 数据帧并在 TensorBoard 中显示。换句话说,MXBoard 意在遵循 tensorboard-pytorch API。它支持 TensorBoard 中的大多数数据类型。

MXBoard is logging tool, written in Python, that is used to record MXNet data frames and display in TensorBoard. In other words, the MXBoard is meant to follow the tensorboard-pytorch API. It supports most of the data types in TensorBoard.

下面提到了一些数据类型:

Some of them are mentioned below −

  1. Graph

  2. Scalar

  3. Histogram

  4. Embedding

  5. Image

  6. Text

  7. Audio

  8. Precision-Recall Curve

MXFusion

MXFusion 是一个模块化概率编程库,具有深度学习功能。MXFusion 允许我们充分利用模块化,这是概率编程库的一个关键特性。它易于使用,并为用户提供了一个便捷的界面,用于设计概率模型并将其应用于实际问题。

MXFusion is a modular probabilistic programming library with deep learning. MXFusion allows us to fully exploited modularity, which is a key feature of deep learning libraries, for probabilistic programming. It is simple to use and provides the users a convenient interface for designing probabilistic models and applying them to the real-world problems.

MXFusion 已在 Python 版本 3.4 及更高版本、MacOS 和 Linux 操作系统上验证。为了安装 MXFusion,我们首先需要安装以下依赖项:

MXFusion is verified on Python version 3.4 and more on MacOS and Linux OS. In order to install MXFusion, we need to first install the following dependencies −

  1. MXNet >= 1.3

  2. Networkx >= 2.1

借助以下 pip 命令,您可以安装 MXFusion:

With the help of following pip command, you can install MXFusion −

pip install mxfusion

TVM

Apache TVM,一个面向 CPU、GPU 和专用加速器等硬件后端的开源端到端深度学习编译堆栈,旨在填补以生产力为中心的深度学习框架与以性能为导向的硬件后端之间的差距。随着最新的 MXNet 1.6.0 版本,用户可以利用 Apache(孵化中)TVM 用 Python 编程语言实现高性能算子内核。

Apache TVM, an open-source end-to-end deep learning compiler stack for hardware-backends such as CPUs, GPUs, and specialized accelerators, aims to fill the gap between the productivity-focused deep-learning frameworks and performance-oriented hardware backends. With the latest release MXNet 1.6.0, users can leverage Apache(incubating) TVM to implement high-performance operator kernels in Python programming language.

Apache TVM 最初实际上是华盛顿大学 Paul G. Allen 计算机科学与工程学院 SAMPL 小组的一个研究项目,现在它正在 Apache 软件基金会(ASF)孵化,由一个 OSC(开源社区)推动,该社区涉及多个行业以及 Apache 方式下的学术机构。

Apache TVM actually started as a research project at the SAMPL group of Paul G. Allen School of Computer Science & Engineering, University of Washington and now it is an effort undergoing incubation at The Apache Software Foundation (ASF) which is driven by an OSC (open source community) that involves multiple industry as well as academic institutions under the Apache way.

以下是 Apache(孵化中)TVM 的主要特性:

Following are the main features of Apache(incubating) TVM −

  1. Simplifies the former C++ based development process.

  2. Enables sharing the same implementation across multiple hardware backends such as CPUs, GPUs, etc.

  3. TVM provides compilation of DL models in various frameworks such as Kears, MXNet, PyTorch, Tensorflow, CoreML, DarkNet into minimum deployable modules on diverse hardware backends.

  4. It also provides us the infrastructure to automatically generate and optimize tensor operators with better performance.

XFer

Xfer,一个迁移学习框架,是用 Python 编写的。它基本上采用 MXNet 模型,并且也训练元模型或修改模型以适应新的目标数据集。

Xfer, a transfer learning framework, is written in Python. It basically takes an MXNet model and train a meta-model or modifies the model for a new target dataset as well.

简单来说,Xfer 是一个 Python 库,允许用户快速轻松地传输存储在 DNN(深度神经网络)中的知识。

In simple words, Xfer is a Python library that allows users to quick and easy transfer of knowledge stored in DNN (deep neural networks).

Xfer 可用于:

Xfer can be used −

  1. For the classification of data of arbitrary numeric format.

  2. To the common cases of images or text data.

  3. As a pipeline that spams from extracting features to training a repurposer (an object that performs classification in the target task).

下面是 Xfer 的功能:

Following are the features of Xfer:

  1. Resource efficiency

  2. Data efficiency

  3. Easy access to neural networks

  4. Uncertainty modeling

  5. Rapid prototyping

  6. Utilities for feature extraction from NN