Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of /~https://github.com/apache/incubator-mxnet in…
Browse files Browse the repository at this point in the history
…to mkldnn-tutorials
  • Loading branch information
TaoLv committed Feb 22, 2019
2 parents 5238f4a + 87441c3 commit e7640da
Show file tree
Hide file tree
Showing 56 changed files with 2,107 additions and 314 deletions.
1 change: 1 addition & 0 deletions docs/api/python/symbol/symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Composite multiple symbols into a new one by an operator.
:nosignatures:
Symbol.infer_type
Symbol.infer_type_partial
Symbol.infer_shape
Symbol.infer_shape_partial
```
Expand Down
50 changes: 45 additions & 5 deletions docs/install/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1188,14 +1188,17 @@ MXNet should work on any cloud provider's CPU-only instances. Follow the Python
<div class="devices">
<div class="raspberry-pi">

MXNet supports the Debian based Raspbian ARM based operating system so you can run MXNet on Raspberry Pi Devices.
MXNet supports the Debian based Raspbian ARM based operating system so you can run MXNet on Raspberry Pi 3B devices.

These instructions will walk through how to build MXNet for the Raspberry Pi and install the Python bindings for the library.

You can do a dockerized cross compilation build on your local machine or a native build on-device.

The complete MXNet library and its requirements can take almost 200MB of RAM, and loading large models with the library can take over 1GB of RAM. Because of this, we recommend running MXNet on the Raspberry Pi 3 or an equivalent device that has more than 1 GB of RAM and a Secure Digital (SD) card that has at least 4 GB of free memory.

## Quick installation
You can use this [pre-built Python wheel](wget https://mxnet-public.s3.amazonaws.com/install/raspbian/mxnet-1.5.0-py2.py3-none-any.whl) on a Raspberry Pi 3B with Stretch. You will likely need to install several dependencies to get MXNet to work. Refer to the following **Build** section for details.

**Cross compilation build (Experimental)**

## Docker installation
Expand All @@ -1222,11 +1225,48 @@ ci/build.py -p armv7

## Install

Create a virtualenv and install the package we created previously.
Your Pi will need several dependencies.

Install MXNet dependencies with the following:
```
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
build-essential \
ca-certificates \
cmake \
curl \
git \
libatlas-base-dev \
libcurl4-openssl-dev \
libjemalloc-dev \
liblapack-dev \
libopenblas-dev \
libopencv-dev \
libzmq3-dev \
ninja-build \
python-dev \
software-properties-common \
sudo \
unzip \
virtualenv \
wget
```
Install virtualenv with:
```
sudo pip install virtualenv
```
Create a Python 2.7 environment for MXNet with:
```
virtualenv -p `which python` mxnet_py27
```
You may use Python 3, however the [wine bottle detection example](https://mxnet.incubator.apache.org/versions/master/tutorials/embedded/wine_detector.html) for the Pi with camera requires Python 2.7.

Create a virtualenv and install the wheel we created previously, or the wheel that you downloaded.

```
virtualenv -p `which python3` mxnet_py3
source mxnet_py3/bin/activate
virtualenv -p `which python3` mxnet_py27
source mxnet_py27/bin/activate
pip install mxnet-x.x.x-py2.py3-none-any.whl
```

Expand Down Expand Up @@ -1257,7 +1297,7 @@ Install these dependencies using the following commands in any directory:

```
sudo apt-get update
sudo apt-get -y install git cmake ninja-build build-essential g++-4.9 c++-4.9 liblapack* libblas* libopencv* libopenblas* python3-dev virtualenv
sudo apt-get -y install git cmake ninja-build build-essential g++-4.9 c++-4.9 liblapack* libblas* libopencv* libopenblas* python3-dev python-dev virtualenv
```

Clone the MXNet source code repository using the following `git` command in your home directory:
Expand Down
4 changes: 2 additions & 2 deletions docs/install/raspbian_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

<!-- This page should be deleted after sometime (Allowing search engines
to update links) -->
<meta http-equiv="refresh" content="3; url=http://mxnet.io/install/index.html" />
<meta http-equiv="refresh" content="3; url=https://mxnet.incubator.apache.org/versions/master/install/index.html?platform=Devices&language=Python&processor=CPU" />
<!-- Just in case redirection does not work -->
<p>
<a href="http://mxnet.io/install/index.html">
<a href="https://mxnet.incubator.apache.org/versions/master/install/index.html?platform=Devices&language=Python&processor=CPU">
This content is moved to a new MXNet install page. Redirecting... </a>
</p>
28 changes: 28 additions & 0 deletions docs/install/scala_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
The following instructions are provided for macOS and Ubuntu. Windows is not yet available.

**Note:** If you use IntelliJ or a similar IDE, you may want to follow the [MXNet-Scala on IntelliJ tutorial](../tutorials/scala/mxnet_scala_on_intellij.html) instead of these instructions.
**Note:** Currently, we only support scala 2.11

<hr>

Expand Down Expand Up @@ -114,6 +115,33 @@ mvn install

<hr>

## Interpreter

To run the scala interpreter, first download and install scala 2.11.x (run `scala -version` to make sure you have the right version installed.**

### Installing the Interpreter

**Ubuntu***

```
sudo apt-get install scala
```

**macOS***

```
brew install scala@2.11
```

Then, add scala to your path by following the instructions output by homebrew.

### Running the Interpreter

To run the interpreter, download the appropriate mxnet jar from [the maven repository](https://search.maven.org/search?q=g:org.apache.mxnet) or build from source following the instructions above.

Then, run `scala -cp {path/to/mxnet-full_2.11-os-version.jar}` to start it.
If you receive a "NumberFormatException" when running the interpreter, run `export TERM=xterm-color` before starting the interpreter.

## Documentation

Scaladocs are generated as part of the docs build pipeline. You can find them published in the [Scala API](http://mxnet.incubator.apache.org/api/scala/index.html) section of the website or by going to the [scaladocs output](https://mxnet.incubator.apache.org/api/scala/docs/index.html#org.apache.mxnet.package) directly.
Expand Down
6 changes: 1 addition & 5 deletions docs/tutorials/scala/char_lstm.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ In this tutorial, you will accomplish the following:

## Prerequisites

To complete this tutorial, you need:

- MXNet. See the instructions for your operating system in [Setup and Installation](http://mxnet.io/install/index.html)
- [Scala 2.11.8](https://www.scala-lang.org/download/2.11.8.html)
- [Maven 3](https://maven.apache.org/install.html)
To complete this tutorial, setup and run the scala interpreter by following the [instructions](https://mxnet.incubator.apache.org/install/scala_setup.html#interpreter).

## Download the Data

Expand Down
4 changes: 2 additions & 2 deletions example/captcha/mxnet_captcha.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ label <- mx.symbol.Reshape(data = label, target_shape = c(0))
captcha_net <- mx.symbol.SoftmaxOutput(data = fc2, label = label, name = "softmax")

mx.metric.acc2 <- mx.metric.custom("accuracy", function(label, pred) {
ypred <- max.col(t(pred)) - 1
ypred <- max.col(t(data.matrix(pred))) - 1
ypred <- matrix(ypred, nrow = nrow(label), ncol = ncol(label), byrow = TRUE)
return(sum(colSums(label == ypred) == 4) / ncol(label))
return(sum(colSums(data.matrix(label) == ypred) == 4) / ncol(label))
})

data.shape <- c(80, 30, 3)
Expand Down
168 changes: 168 additions & 0 deletions example/gan/CGAN_mnist_R/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# Conditional Generative Adversarial Network with MXNet R package

This tutorial shows how to build and train a Conditional Generative Adversarial Network (CGAN) on MNIST images.

## How GAN works
A Generative Adversarial Model simultaneously trains two models: a generator that learns to output fake samples from an unknown distribution and a discriminator that learns to distinguish fake from real samples.

The CGAN is a conditional variation of the GAN where the generator is instructed to generate a real sample having specific characteristics rather than a generic sample from full distribution. Such condition could be the label associated with an image like in this tutorial or a more detailed tag as shown in the example below:

![Image credit: (Scott Reed)[/~https://github.com/reedscot/icml2016]](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gan/CGAN_mnist_R/dcgan_network.jpg)

## Initial setup

The following packages are needed to run the tutorial:

```
require("imager")
require("dplyr")
require("readr")
require("mxnet")
```

The full demo is comprised of the two following scripts:

```CGAN_mnist_setup.R```: prepare data and define the model structure
```CGAN_train.R```: execute the training

## Data preperation

The MNIST dataset is available [here](https://www.kaggle.com/c/digit-recognizer/data)). Once train.csv is downloaded into the data/ folder, we can import into R.

```train <- read_csv('data/train.csv')
train <- data.matrix(train)
train_data <- train[,-1]
train_data <- t(train_data/255*2-1)
train_label <- as.integer(train[,1])
dim(train_data) <- c(28, 28, 1, ncol(train_data))
```
Custom iterators are defined in ```iterators.R``` and imported by ```CGAN_mnist_setup.R```

## Generator
The generator is a network that creates novel samples (MNIST images) from 2 inputs:

- Noise vector
- Labels defining the object condition (which digit to produce)

The noise vector provides the building blocks to the Generator model, which will learns how to structure that noise into a sample. The mx.symbol.Deconvolution operator is used to upsample the initial input from a 1x1 shape up to a 28x28 image.

The information on the label for which to generate a fake sample is provided by a one-hot encoding of the label indices that is appended to the random noise. For MNIST, the 0-9 indices are therefore converted into a binary vector of length 10. More complex applications would require embeddings rather than simple one-hot to encode the condition.

## Discriminator
The discriminator attempts to distinguish between fake samples produced by the generator and real ones sampled from MNIST training data.

In a conditional GAN, the labels associated with the samples are also provided to the Discriminator. In this demo, this information is again provided as a hot-hot encoding of the label that is broadcast to match the image dimensions (10 -> 28x28x10).

## Training logic
The training process of the discriminator is most obvious: the loss is simple a binary TRUE/FALSE response and that loss is propagated back into the CNN network. It can therefore be understood as a simple binary classification problem.

```### Train loop on fake
mx.exec.update.arg.arrays(exec_D, arg.arrays =
list(data=D_data_fake, digit=D_digit_fake, label=mx.nd.array(rep(0, batch_size))),
match.name=TRUE)
mx.exec.forward(exec_D, is.train=T)
mx.exec.backward(exec_D)
update_args_D<- updater_D(weight = exec_D$ref.arg.arrays, grad = exec_D$ref.grad.arrays)
mx.exec.update.arg.arrays(exec_D, update_args_D, skip.null=TRUE)
### Train loop on real
mx.exec.update.arg.arrays(exec_D, arg.arrays =
list(data=D_data_real, digit=D_digit_real, label=mx.nd.array(rep(1, batch_size))),
match.name=TRUE)
mx.exec.forward(exec_D, is.train=T)
mx.exec.backward(exec_D)
update_args_D<- updater_D(weight = exec_D$ref.arg.arrays, grad = exec_D$ref.grad.arrays)
mx.exec.update.arg.arrays(exec_D, update_args_D, skip.null=TRUE)
```

The generator loss comes from the backpropagation of the the discriminator loss into its generated output. By faking the generator labels to be real samples into the discriminator, the discriminator back-propagated loss provides the generator with the information on how to best adapt its parameters to trick the discriminator into believing the fake samples are real.

This requires to backpropagate the gradients up to the input data of the discriminator (whereas this input gradient is typically ignored in vanilla feedforward network).

```### Update Generator weights - use a seperate executor for writing data gradients
exec_D_back <- mxnet:::mx.symbol.bind(symbol = D_sym,
arg.arrays = exec_D$arg.arrays,
aux.arrays = exec_D$aux.arrays, grad.reqs = rep("write", length(exec_D$arg.arrays)),
ctx = devices)
mx.exec.update.arg.arrays(exec_D_back, arg.arrays =
list(data=D_data_fake, digit=D_digit_fake, label=mx.nd.array(rep(1, batch_size))),
match.name=TRUE)
mx.exec.forward(exec_D_back, is.train=T)
mx.exec.backward(exec_D_back)
D_grads <- exec_D_back$ref.grad.arrays$data
mx.exec.backward(exec_G, out_grads=D_grads)
update_args_G <- updater_G(weight = exec_G$ref.arg.arrays, grad = exec_G$ref.grad.arrays)
mx.exec.update.arg.arrays(exec_G, update_args_G, skip.null=TRUE)
```

The above training steps are executed in the ```CGAN_train.R``` script.

## Monitor the training

During training, the [imager](http://dahtah.github.io/imager/) package facilitates the visual quality assessment of the fake samples.

```if (iteration==1 | iteration %% 100==0){
par(mfrow=c(3,3), mar=c(0.1,0.1,0.1,0.1))
for (i in 1:9) {
img <- as.array(exec_G$ref.outputs$G_sym_output)[,,,i]
plot(as.cimg(img), axes=F)
}
}
```
Below are samples obtained at different stage of the training.

Starting from noise:

![](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gan/CGAN_mnist_R/CGAN_1.png)

Slowly getting it - iteration 200:

![](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gan/CGAN_mnist_R/CGAN_200.png)

Generate specified digit images on demand - iteration 2400:

![](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gan/CGAN_mnist_R/CGAN_2400.png)

## Inference

Once the model is trained, synthetic images of the desired digit can be produced by feeding the generator with fixed labels rather than the randomly generated ones used during the training.

Here we will generate fake ```9```:

```digit <- mx.nd.array(rep(9, times=batch_size))
data <- mx.nd.one.hot(indices = digit, depth = 10)
data <- mx.nd.reshape(data = data, shape = c(1,1,-1, batch_size))
exec_G <- mx.simple.bind(symbol = G_sym, data=data_shape_G, ctx = devices, grad.req = "null")
mx.exec.update.arg.arrays(exec_G, G_arg_params, match.name=TRUE)
mx.exec.update.arg.arrays(exec_G, list(data=data), match.name=TRUE)
mx.exec.update.aux.arrays(exec_G, G_aux_params, match.name=TRUE)
mx.exec.forward(exec_G, is.train=F)
```
![](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gan/CGAN_mnist_R/CGAN_infer_9.png)

Further details of the CGAN methodology can be found in the paper [Generative Adversarial Text to Image Synthesis](https://arxiv.org/abs/1605.05396).


61 changes: 61 additions & 0 deletions example/gluon/actor_critic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# Actor Critic Model

This example shows an actor critic model that consists of a critic that measures how good an action taken is and an actor that controls the agent's behavior.
In our example actor and critic use the same model:

```
class Policy(gluon.Block):
def __init__(self, **kwargs):
super(Policy, self).__init__(**kwargs)
with self.name_scope():
self.dense = nn.Dense(16, in_units=4, activation='relu')
self.action_pred = nn.Dense(2, in_units=16)
self.value_pred = nn.Dense(1, in_units=16)
def forward(self, x):
x = self.dense(x)
probs = self.action_pred(x)
values = self.value_pred(x)
return F.softmax(probs), values
```
The example uses [Gym](https://gym.openai.com/docs/), which is a toolkit for developing and comparing reinforcement learning algorithms. The model is running an instance of [CartPole-v0](https://gym.openai.com/envs/CartPole-v0/) that simulates a pole that is attached by an un-actuated joint to a cart, which moves along a frictionless track. The goal is to prevent it from falling over.


The example provides the following commandline options:
```
MXNet actor-critic example
optional arguments:
-h, --help show this help message and exit
--gamma G discount factor (default: 0.99)
--seed N random seed (default: 1)
--render render the environment
--log-interval N interval between training status logs (default: 10)
```

To run the model execute, type
```
python actor_critic.py --render
```

You will get an output like the following:
![](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/example/gluon/actor_critic/actor_critic.gif)

Loading

0 comments on commit e7640da

Please sign in to comment.