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

Commit a024a90

Browse files
piyushghailanking520
authored andcommitted
[MXNET-1195] Cleanup Scala README file (#13582)
* Updated the Scala-Readme with upto-date information * Updated the header * Removed redundant build status * Minor formatting changes * Addressed the PR feedback * Added section on Scala training APIs * Removed mention of deprecated Model API
1 parent 1128f8e commit a024a90

File tree

1 file changed

+119
-135
lines changed

1 file changed

+119
-135
lines changed

scala-package/README.md

Lines changed: 119 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,136 @@
1-
<img src=https://raw.githubusercontent.com/dmlc/dmlc.github.io/master/img/logo-m/mxnet2.png width=135/> Deep Learning for Scala/Java
1+
MXNet Package for Scala/Java
22
=====
33

4-
[![Build Status](http://jenkins.mxnet-ci.amazon-ml.com/job/incubator-mxnet/job/master/badge/icon)](http://jenkins.mxnet-ci.amazon-ml.com/job/incubator-mxnet/job/master/)
5-
[![GitHub license](http://dmlc.github.io/img/apache2.svg)](./LICENSE)
6-
7-
Here you find the MXNet Scala Package!
8-
It brings flexible and efficient GPU/CPU computing and state-of-art deep learning to JVM.
4+
The MXNet Scala/Java Package brings flexible and efficient GPU/CPU computing and state-of-art deep learning to JVM.
95

106
- It enables you to write seamless tensor/matrix computation with multiple GPUs
117
in Scala, Java and other languages built on JVM.
128
- It also enables you to construct and customize the state-of-art deep learning models in JVM languages,
139
and apply them to tasks such as image classification and data science challenges.
10+
- The Scala/Java Inferece APIs provides an easy out of the box solution for loading pre-trained MXNet models and running inference on them.
1411

15-
Install
12+
Pre-Built Maven Packages
1613
------------
17-
18-
Technically, all you need is the `mxnet-full_2.11-{arch}-{xpu}-{version}.jar` in your classpath.
19-
It will automatically extract the native library to a tempfile and load it.
20-
You can find the pre-built jar file in [here](https://search.maven.org/search?q=g:org.apache.mxnet)
21-
and also our nightly build package [here](https://repository.apache.org/#nexus-search;gav~org.apache.mxnet~)
2214

23-
Currently we provide `linux-x86_64-gpu`, `linux-x86_64-cpu` and `osx-x86_64-cpu`. Support for Windows will come soon.
24-
Use the following dependency in maven, change the artifactId according to your own architecture, e.g., `mxnet-full_2.11-osx-x86_64-cpu` for OSX (and cpu-only).
15+
### Stable ###
16+
17+
The MXNet Scala/Java packages can be easily included in your Maven managed project.
18+
The stable jar files for the packages are available on the [MXNet Maven Package Repository](https://search.maven.org/search?q=g:org.apache.mxnet)
19+
Currently we provide packages for Linux (Ubuntu 16.04) (CPU and GPU) and macOS (CPU only). Stable packages for Windows and CentOS will come soon. For now, if you have a CentOS machine, follow the ```Build From Source``` section below.
20+
21+
To add MXNet Scala/Java package to your project, add the dependency as shown below corresponding to your platform, under the ```dependencies``` tag in your project's ```pom.xml``` :
22+
23+
**Linux GPU**
24+
25+
<a href="https://mvnrepository.com/artifact/org.apache.mxnet/mxnet-full_2.11-linux-x86_64-gpu"><img src="https://img.shields.io/badge/org.apache.mxnet-linux gpu-green.svg" alt="maven badge"/></a>
26+
27+
```HTML
28+
<dependency>
29+
<groupId>org.apache.mxnet</groupId>
30+
<artifactId>mxnet-full_2.11-linux-x86_64-gpu</artifactId>
31+
<version>[1.3.1,)</version>
32+
</dependency>
33+
```
34+
35+
**Linux CPU**
36+
37+
<a href="https://mvnrepository.com/artifact/org.apache.mxnet/mxnet-full_2.11-linux-x86_64-cpu"><img src="https://img.shields.io/badge/org.apache.mxnet-linux cpu-green.svg" alt="maven badge"/></a>
38+
39+
```HTML
40+
<dependency>
41+
<groupId>org.apache.mxnet</groupId>
42+
<artifactId>mxnet-full_2.11-linux-x86_64-cpu</artifactId>
43+
<version>[1.3.1,)</version>
44+
</dependency>
45+
```
46+
47+
**macOS CPU**
48+
49+
<a href="https://mvnrepository.com/artifact/org.apache.mxnet/mxnet-full_2.11-osx-x86_64-cpu"><img src="https://img.shields.io/badge/org.apache.mxnet-macOS cpu-green.svg" alt="maven badge"/></a>
50+
51+
```HTML
52+
<dependency>
53+
<groupId>org.apache.mxnet</groupId>
54+
<artifactId>mxnet-full_2.11-osx-x86_64-cpu</artifactId>
55+
<version>[1.3.1,)</version>
56+
</dependency>
57+
```
58+
59+
**Note:** ```<version>[1.3.1,)<\version>``` indicates that we will fetch packages with version 1.3.1 or higher. This will always ensure that the pom.xml is able to fetch the latest and greatest jar files from Maven.
60+
61+
### Nightly ###
62+
63+
Apart from these, the nightly builds representing the bleeding edge development on Scala/Java packages are also available on the [MXNet Maven Nexus Package Repository](https://repository.apache.org/#nexus-search;gav~org.apache.mxnet~~~~).
64+
Currently we provide nightly packages for Linux (CPU and GPU) and MacOS (CPU only). The Linux nightly jar files also work on CentOS. Nightly packages for Windows will come soon.
65+
66+
Add the following ```repository``` to your project's ```pom.xml``` file :
67+
68+
````html
69+
<repositories>
70+
<repository>
71+
<id>Apache Snapshot</id>
72+
<url>https://repository.apache.org/content/groups/snapshots</url>
73+
</repository>
74+
</repositories>
75+
````
76+
77+
Also, add the dependency which corresponds to your platform to the ```dependencies``` tag :
78+
79+
**Linux GPU**
80+
81+
<a href="https://repository.apache.org/#nexus-search;gav~org.apache.mxnet~mxnet-full_2.11-linux-x86_64-gpu~~~"><img src="https://img.shields.io/badge/org.apache.mxnet-linux gpu-green.svg" alt="maven badge"/></a>
82+
83+
```HTML
84+
<dependency>
85+
<groupId>org.apache.mxnet</groupId>
86+
<artifactId>mxnet-full_2.11-linux-x86_64-gpu</artifactId>
87+
<version>[1.5.0,)</version>
88+
</dependency>
89+
```
90+
91+
**Linux CPU**
92+
93+
<a href="https://repository.apache.org/#nexus-search;gav~org.apache.mxnet~mxnet-full_2.11-osx-x86_64-cpu~~~"><img src="https://img.shields.io/badge/org.apache.mxnet-linux cpu-green.svg" alt="maven badge"/></a>
2594

2695
```HTML
2796
<dependency>
2897
<groupId>org.apache.mxnet</groupId>
29-
<artifactId>mxnet-full_2.10-linux-x86_64-gpu</artifactId>
30-
<version>0.1.1</version>
98+
<artifactId>mxnet-full_2.11-linux-x86_64-cpu</artifactId>
99+
<version>[1.5.0,)</version>
31100
</dependency>
32101
```
33102

34-
You can also use `mxnet-core_2.10-0.1.1.jar` and put the compiled native library somewhere in your load path.
103+
**macOS CPU**
35104

105+
<a href="https://mvnrepository.com/artifact/org.apache.mxnet/mxnet-full_2.11-osx-x86_64-cpu"><img src="https://img.shields.io/badge/org.apache.mxnet-macOS cpu-green.svg" alt="maven badge"/></a>
36106
```HTML
37107
<dependency>
38108
<groupId>org.apache.mxnet</groupId>
39-
<artifactId>mxnet-core_2.10</artifactId>
40-
<version>0.1.1</version>
109+
<artifactId>mxnet-full_2.11-osx-x86_64-cpu</artifactId>
110+
<version>[1.5.0,)</version>
41111
</dependency>
42112
```
43113

44-
If you have some native libraries conflict with the ones in the provided 'full' jar (e.g., you use openblas instead of atlas), this is a recommended way.
45-
Refer to the next section for how to build it from the very source.
114+
**Note:** ```<version>[1.5.0,)<\version>``` indicates that we will fetch packages with version 1.5.0 or higher. This will always ensure that the pom.xml is able to fetch the latest and greatest jar files from Maven Snapshot repository.
46115

47-
Build
116+
Build From Source
48117
------------
49118

50-
Checkout the [Installation Guide](http://mxnet.incubator.apache.org/install/index.html) contains instructions to install mxnet.
51-
Then you can compile the Scala Package by
119+
Checkout the [Installation Guide](http://mxnet.incubator.apache.org/install/index.html) contains instructions to install mxnet package and build it from source.
120+
If you have built MXNet from source and are looking to setup Scala from that point, you may simply run the following from the MXNet source root:
52121

53122
```bash
54123
make scalapkg
55124
```
56125

57-
(Optional) run unit/integration tests by
126+
You can also run the unit tests and integration tests on the Scala Package by :
58127

59128
```bash
60129
make scalaunittest
61130
make scalaintegrationtest
62131
```
63132

64-
Or run a subset of unit tests by, e.g.,
133+
Or run a subset of unit tests, for e.g.,
65134

66135
```bash
67136
make SCALA_TEST_ARGS=-Dsuites=org.apache.mxnet.NDArraySuite scalaunittest
@@ -70,123 +139,38 @@ make SCALA_TEST_ARGS=-Dsuites=org.apache.mxnet.NDArraySuite scalaunittest
70139
If everything goes well, you will find jars for `assembly`, `core` and `example` modules.
71140
Also it produces the native library in `native/{your-architecture}/target`, which you can use to cooperate with the `core` module.
72141

73-
Once you've downloaded and unpacked MNIST dataset to `./data/`, run the training example by
74-
75-
```bash
76-
java -Xmx4G -cp \
77-
scala-package/assembly/{your-architecture}/target/*:scala-package/examples/target/*:scala-package/examples/target/classes/lib/* \
78-
org.apache.mxnet.examples.imclassification.TrainMnist \
79-
--data-dir=./data/ \
80-
--num-epochs=10 \
81-
--network=mlp \
82-
--cpus=0,1,2,3
83-
```
142+
Examples & Usage
143+
-------
144+
- To set up the Scala Project using IntelliJ IDE on macOS follow the instructions [here](https://mxnet.incubator.apache.org/tutorials/scala/mxnet_scala_on_intellij.html).
145+
- Several examples on using the Scala APIs are provided in the [Scala Examples Folder](https://github.com/apache/incubator-mxnet/tree/master/scala-package/examples/)
84146

85-
If you've compiled with `USE_DIST_KVSTORE` enabled, the python tools in `mxnet/tracker` can be used to launch distributed training.
86-
The following command runs the above example using 2 worker nodes (and 2 server nodes) in local. Refer to [Distributed Training](http://mxnet.incubator.apache.org/how_to/multi_devices.html) for more details.
147+
Scala Training APIs
148+
-------
149+
- Module API :
150+
[The Module API](https://mxnet.incubator.apache.org/api/scala/module.html) provides an intermediate and high-level interface for performing computation with neural networks in MXNet. Modules provide high-level APIs for training, predicting, and evaluating.
87151

88-
```bash
89-
tracker/dmlc_local.py -n 2 -s 2 \
90-
java -Xmx4G -cp \
91-
scala-package/assembly/{your-architecture}/target/*:scala-package/examples/target/*:scala-package/examples/target/classes/lib/* \
92-
org.apache.mxnet.examples.imclassification.TrainMnist \
93-
--data-dir=./data/ \
94-
--num-epochs=10 \
95-
--network=mlp \
96-
--cpus=0 \
97-
--kv-store=dist_sync
98-
```
152+
- KVStore API :
153+
To run training over multiple GPUs and multiple hosts, one can use the [KVStore API](https://mxnet.incubator.apache.org/api/scala/kvstore.html).
99154

100-
Change the arguments and have fun!
155+
- IO/Data Loading :
156+
MXNet Scala provides APIs for preparing data to feed as an input to models. Check out [Data Loading API](https://mxnet.incubator.apache.org/api/scala/io.html) for more info.
157+
158+
Other available Scala APIs for training can be found [here](https://mxnet.incubator.apache.org/api/scala/index.html).
159+
101160

102-
Usage
161+
Scala Inference APIs
103162
-------
104-
Here is a Scala example of what training a simple 3-layer multilayer perceptron on MNIST looks like. You can download the MNIST dataset using [get_mnist_data script](https://github.com/apache/incubator-mxnet/blob/master/scala-package/core/scripts/get_mnist_data.sh).
105-
106-
```scala
107-
import org.apache.mxnet._
108-
import org.apache.mxnet.optimizer.SGD
109-
110-
// model definition
111-
val data = Symbol.Variable("data")
112-
val fc1 = Symbol.FullyConnected(name = "fc1")()(Map("data" -> data, "num_hidden" -> 128))
113-
val act1 = Symbol.Activation(name = "relu1")()(Map("data" -> fc1, "act_type" -> "relu"))
114-
val fc2 = Symbol.FullyConnected(name = "fc2")()(Map("data" -> act1, "num_hidden" -> 64))
115-
val act2 = Symbol.Activation(name = "relu2")()(Map("data" -> fc2, "act_type" -> "relu"))
116-
val fc3 = Symbol.FullyConnected(name = "fc3")()(Map("data" -> act2, "num_hidden" -> 10))
117-
val mlp = Symbol.SoftmaxOutput(name = "sm")()(Map("data" -> fc3))
118-
119-
// load MNIST dataset
120-
val trainDataIter = IO.MNISTIter(Map(
121-
"image" -> "data/train-images-idx3-ubyte",
122-
"label" -> "data/train-labels-idx1-ubyte",
123-
"data_shape" -> "(1, 28, 28)",
124-
"label_name" -> "sm_label",
125-
"batch_size" -> "50",
126-
"shuffle" -> "1",
127-
"flat" -> "0",
128-
"silent" -> "0",
129-
"seed" -> "10"))
130-
131-
val valDataIter = IO.MNISTIter(Map(
132-
"image" -> "data/t10k-images-idx3-ubyte",
133-
"label" -> "data/t10k-labels-idx1-ubyte",
134-
"data_shape" -> "(1, 28, 28)",
135-
"label_name" -> "sm_label",
136-
"batch_size" -> "50",
137-
"shuffle" -> "1",
138-
"flat" -> "0", "silent" -> "0"))
139-
140-
// setup model and fit the training data
141-
val model = FeedForward.newBuilder(mlp)
142-
.setContext(Context.cpu())
143-
.setNumEpoch(10)
144-
.setOptimizer(new SGD(learningRate = 0.1f, momentum = 0.9f, wd = 0.0001f))
145-
.setTrainData(trainDataIter)
146-
.setEvalData(valDataIter)
147-
.build()
148-
```
163+
The [Scala Inference APIs](https://mxnet.incubator.apache.org/api/scala/infer.html) provide an easy, out of the box solution to load a pre-trained MXNet model and run inference on it. The Inference APIs are present in the [Infer Package](https://github.com/apache/incubator-mxnet/tree/master/scala-package/infer) under the MXNet Scala Package repository, while the documentation for the Infer API is available [here](https://mxnet.incubator.apache.org/api/scala/docs/index.html#org.apache.mxnet.infer.package).
149164

150-
Predict using the model in the following way:
151-
152-
```scala
153-
val probArrays = model.predict(valDataIter)
154-
// in this case, we do not have multiple outputs
155-
require(probArrays.length == 1)
156-
val prob = probArrays(0)
157-
158-
// get real labels
159-
import scala.collection.mutable.ListBuffer
160-
valDataIter.reset()
161-
val labels = ListBuffer.empty[NDArray]
162-
while (valDataIter.hasNext) {
163-
val evalData = valDataIter.next()
164-
labels += evalData.label(0).copy()
165-
}
166-
val y = NDArray.concatenate(labels)
167-
168-
// get predicted labels
169-
val py = NDArray.argmax_channel(prob)
170-
require(y.shape == py.shape)
171-
172-
// calculate accuracy
173-
var numCorrect = 0
174-
var numInst = 0
175-
for ((labelElem, predElem) <- y.toArray zip py.toArray) {
176-
if (labelElem == predElem) {
177-
numCorrect += 1
178-
}
179-
numInst += 1
180-
}
181-
val acc = numCorrect.toFloat / numInst
182-
println(s"Final accuracy = $acc")
183-
```
165+
Java Inference APIs
166+
-------
167+
The [Java Inference APIs](http://mxnet.incubator.apache.org/api/java/index.html) also provide an easy, out of the box solution to load a pre-trained MXNet model and run inference on it. The Inference APIs are present in the [Infer Package](https://github.com/apache/incubator-mxnet/tree/master/scala-package/infer/src/main/scala/org/apache/mxnet/infer/javaapi) under the MXNet Scala Package repository, while the documentation for the Infer API is available [here](https://mxnet.incubator.apache.org/api/java/docs/index.html#org.apache.mxnet.infer.package).
168+
More APIs will be added to the Java Inference APIs soon.
184169

185-
Release
170+
JVM Memory Management
186171
-------
187-
- Version 0.1.1, March 24, 2016.
188-
- Bug fix for MAE & MSE metrics.
189-
- Version 0.1.0, March 22, 2016.
172+
The Scala/Java APIs also provide an automated resource management system, thus making it easy to manage the native memory footprint without any degradation in performance.
173+
More details about JVM Memory Management are available [here](https://github.com/apache/incubator-mxnet/blob/master/scala-package/memory-management.md).
190174

191175
License
192176
-------

0 commit comments

Comments
 (0)