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

[MXNET-1187] Added Tutorial for Java under mxnet.io/docs/tutorials #13183

Merged
merged 5 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ Select API: 
* [MXNet-Scala Examples](https://github.com/apache/incubator-mxnet/tree/master/scala-package/examples/src/main/scala/org/apache/mxnetexamples)
<hr>

## Java Tutorials
* Getting Started
* [Developer Environment Setup on IntelliJ IDE](/tutorials/java/mxnet_java_on_intellij.html)
* [MXNet-Java Examples](https://github.com/apache/incubator-mxnet/tree/master/scala-package/examples/src/main/java/org/apache/mxnetexamples)
<hr>

## C++ Tutorials

* Models
Expand Down
210 changes: 210 additions & 0 deletions docs/tutorials/java/mxnet_java_on_intellij.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# Run MXNet Java Examples Using the IntelliJ IDE (macOS)

This tutorial guides you through setting up a simple Java project in IntelliJ IDE on macOS and demonstrates usage of the MXNet Java APIs.

## Prerequisites:
To use this tutorial you need the following pre-requisites:

- [Java 8 JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
- [Maven](https://maven.apache.org/install.html)
- [OpenCV](https://opencv.org/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is OpenCV a requirement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If OpenCV is not installed, one will run into this exception :

Exception in thread "main" java.lang.UnsatisfiedLinkError: /private/var/folders/gf/frgm66d17v76pgmx1t44qjfxxsv1yh/T/mxnet2167090749058732516/mxnet-scala: dlopen(/private/var/folders/gf/frgm66d17v76pgmx1t44qjfxxsv1yh/T/mxnet2167090749058732516/mxnet-scala, 1): Library not loaded: /usr/local/opt/opencv/lib/libopencv_stitching.3.4.dylib
  Referenced from: /private/var/folders/gf/frgm66d17v76pgmx1t44qjfxxsv1yh/T/mxnet2167090749058732516/mxnet-scala
  Reason: image not found
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
	at java.lang.Runtime.load0(Runtime.java:809)
	at java.lang.System.load(System.java:1086)
	at org.apache.mxnet.util.NativeLibraryLoader$.loadLibraryFromStream(NativeLibraryLoader.scala:140)
	at org.apache.mxnet.util.NativeLibraryLoader$.loadLibrary(NativeLibraryLoader.scala:93)
	at org.apache.mxnet.Base$.<init>(Base.scala:70)
	at org.apache.mxnet.Base$.<clinit>(Base.scala)
	at org.apache.mxnet.NDArray$.initNDArrayModule(NDArray.scala:159)
	at org.apache.mxnet.NDArray$.<init>(NDArray.scala:39)
	at org.apache.mxnet.NDArray$.<clinit>(NDArray.scala)
	at org.apache.mxnet.javaapi.NDArray$.ones(NDArray.scala:50)
	at org.apache.mxnet.javaapi.NDArray.ones(NDArray.scala)
	at mxnet.App.main(App.java:10)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I believe, it's a dependency for the MXNet package itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I think we should fix this in the future.

- [IntelliJ IDE](https://www.jetbrains.com/idea/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also point to the community edition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


### MacOS Prerequisites

**Step 1.** Install brew:
```
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

Or, if you already have brew, update it:
```
brew update
```

**Step 2.** Install Java 8:
```
brew tap caskroom/versions
brew cask install java8
```

**Step 3.** Install maven:
```
brew install maven
```

**Step 4.** Install OpenCV:
```
brew install opencv
```

You can also run this tutorial on an Ubuntu machine after installing the following prerequisites.
### Ubuntu Prerequisites

**Step 1.** Download the MXNet source.

```bash
git clone --recursive https://github.com/apache/incubator-mxnet.git mxnet
cd mxnet
```

**Step 2.** Run the dependency installation scripts.

```bash
sudo ./ci/docker/install/ubuntu_core.sh
sudo ./ci/docker/install/ubuntu_scala.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to install Scala SBT for running Java if there is JAR provided. Maybe replace by just install JDK and Maven?

```

The `ubuntu_scala.sh` installs the common dependencies required for both MXNet Scala and MXNet Java packages.

## Set Up Your Project

**Step 1.** Install and setup [IntelliJ IDEA](https://www.jetbrains.com/idea/)

**Step 2.** Create a new Project:

![intellij welcome](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/scala/intellij-welcome.png)

From the IntelliJ welcome screen, select "Create New Project".

Choose the Maven project type.

Select the checkbox for `Create from archetype`, then choose `org.apache.maven.archetypes:maven-archetype-quickstart` from the list below. More on this can be found on a Maven tutorial : [Maven in 5 Minutes](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html).

![maven project type - archetype](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/project-archetype.png)

click `Next`.

![project metadata](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/intellij-project-metadata.png)

Set the project's metadata. For this tutorial, use the following:

**GroupId**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you creating a new GroupId and Artifact Id? you should use the existing project identifiers org.apache and mxnet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a sample project to showcase how to integrate MXNet into an existing/new java project. It's independent of the existing scala/java package in the codebase.
Hence I kept the groupId and artifact Id as placeholder or random example names.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok got it!, thanks for the clarification

```
mxnet
```
**ArtifactId**
```
ArtifactId: javaMXNet
```
**Version**
```
1.0-SNAPSHOT
```

TODO
![project properties](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/intellij-project-properties.png)

Review the project's properties. The settings can be left as their default.

TODO
![project location](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/intellij-project-location.png)

Set the project's location. The rest of the settings can be left as their default.

TODO
![project 1](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/intellij-project-pom.png)

After clicking Finish, you will be presented with the project's first view.
The project's `pom.xml` will be open for editing.

**Step 3.** Add the following Maven dependency to your `pom.xml` file under the `dependencies` tag:

```html
<dependency>
<groupId>org.apache.mxnet</groupId>
<artifactId>mxnet-full_2.11-osx-x86_64-cpu</artifactId>
<version>1.4.0</version>
</dependency>
```

To view the latest MXNet Maven packages, you can check [MXNet Maven package repository](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.apache.mxnet%22)


**Step 4.** Import dependencies with Maven:

- Note the prompt in the lower right corner that states "Maven projects need to be imported". If this is not visible, click on the little greed balloon that appears in the lower right corner.

![import_dependencies](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/project-import-changes.png)

Click "Import Changes" in this prompt.

**Step 5.** Build the project:
- To build the project, from the menu choose Build, and then choose Build Project.

**Step 6.** Navigate to the App.java class in the project and paste the following code, overwriting the original hello world code.
```java
package mxnet;

import org.apache.mxnet.javaapi.Context;
import org.apache.mxnet.javaapi.NDArray;

public class App
{
public static void main( String[] args )
{
NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20});
System.out.println( "Testing MXNet by generating a 10x20 NDArray" );
System.out.println("Shape of NDArray is : " + nd.shape());
}
}
```

**Step 7.** Now run the App.java by clicking the green arrow as highlighted in the image below.

![run hello mxnet](https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/java/intellij-run-projects.png)


The result should be this output:

```
Testing MXNet by generating a 10x20 NDArray
Shape of NDArray is : (10,20)

Process finished with exit code 0
```


### Troubleshooting

If you get an error, check the dependencies at the beginning of this tutorial. For example, you might see the following in the middle of the error messages, where `x.x` would the version it's looking for.

```
...
Library not loaded: /usr/local/opt/opencv/lib/libopencv_calib3d.x.x.dylib
...
```

This can be resolved be installing OpenCV.


### Command Line Build Option

- You can also compile the project by using the following command at the command line. Change directories to this project's root folder then run the following:

```bash
mvn clean install dependency:copy-dependencies
```
If the command succeeds, you should see a lot of info and some warning messages, followed by:

```bash
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.475 s
[INFO] Finished at: 2018-11-08T05:06:31-08:00
[INFO] ------------------------------------------------------------------------
```
The build generates a new jar file in the `target` folder called `javaMXNet-1.0-SNAPSHOT.jar`.

To run the App.java use the following command from the project's root folder and you should see the same output as we got when the project was run from IntelliJ.
```bash
java -cp target/javaMXNet-1.0-SNAPSHOT.jar:target/dependency/* mxnet.App
```

## Next Steps
For more information about MXNet Java resources, see the following:

* [Java Inference API](https://mxnet.incubator.apache.org/api/java/infer.html)
* [Java Inference Examples](https://github.com/apache/incubator-mxnet/tree/java-api/scala-package/examples/src/main/java/org/apache/mxnetexamples/infer/)
* [MXNet Tutorials Index](http://mxnet.io/tutorials/index.html)
3 changes: 2 additions & 1 deletion tests/tutorials/test_sanity_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
'unsupervised_learning/index.md',
'vision/index.md',
'tensorrt/index.md',
'tensorrt/inference_with_trt.md']
'tensorrt/inference_with_trt.md',
nswamy marked this conversation as resolved.
Show resolved Hide resolved
'java/mxnet_java_on_intellij.md']
whitelist_set = set(whitelist)

def test_tutorial_downloadable():
Expand Down