From d6038fbdb4a402893ed5a923907a00ae0af5c873 Mon Sep 17 00:00:00 2001 From: Qing Date: Wed, 24 Oct 2018 15:15:29 -0700 Subject: [PATCH 1/9] add example --- .../mxnet_java_install_and_run_examples.md | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 docs/tutorials/scala/mxnet_java_install_and_run_examples.md diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md new file mode 100644 index 000000000000..9784607670b3 --- /dev/null +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -0,0 +1,137 @@ +# Install and run Java Examples + +## Prerequisites: +Please follow the Step 1 in the [Scala configuration](http://mxnet.incubator.apache.org/install/scala_setup.html#setup-instructions) +These should help you install the correct Java version and all dependecies. + +## Import and run the Java package +For users using a desktop/laptop, we recommend to use IntelliJ IDE as it is tested and supported to provide necessary documentation for the Java API. + +Alternatively, users can follow the second instruction to setup a empty Maven project for Java. + +### IntelliJ instruction +If you are using a computer with Ubuntu16.04 or Mac, you can install IntelliJ to run the Java package. Please follow the instruction below: + +1. Create a new Java project in IntelliJ. Fire up IntelliJ and click `Create New Project`. It should open a popup window like this : + +2. Click `Next`, and in the `Create project from template` window, do not select anything and click `Next` again. + +3. In the next window choose your `Project name` and the `Project location` and click on `Finish`. + +4. Let’s add the Java Inference API jars that we grabbed from Mavel Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. + +6. Create a new Java class under the folder `your-project-name/src`. Let’s call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet we create an NDArray object in Java and print it’s shape. +```java +import org.apache.mxnet.javaapi.Context; +import org.apache.mxnet.javaapi.NDArray; + +public class JavaSample { + public static void main(String[] args) { + System.out.println("Hello"); + NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); + + System.out.println("Shape of NDarray is : " + nd.shape()); + + } +} +``` + +7. If all went well, you should see an output like this : (Ignore the SLF4J warnings). +``` +Hello +SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". +SLF4J: Defaulting to no-operation (NOP) logger implementation +SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. +Shape of NDarray is : (10,20) +Process finished with exit code 0 +``` +This means you have successfully set it up on your machine + +### Run the project manually in Maven +In this example, Maven is being used to create the project. This tutorial referred the [Maven in 5 min](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html) tutorial. + +1. Create a new folder and run the following commands +``` +mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false +``` +You can specify the `groupId` and `artifactId` to your favourite names. You can also create a maven project using empty archetype. + +2. then go to `pom.xml` file in your project folder and add the following content. +- Change the `osx-x86_64` to `linux-x86_64` if your platform is linux. +- Change `cpu` into `gpu` if you are using gpu +- Change the version of your package from `1.3.1` to the matched jar version. +```xml + + org.apache.mxnet + mxnet-full_2.11-osx-x86_64-cpu + 1.3.1 + system + path-to-your-jar/jarName.jar + + + args4j + args4j + 2.0.29 + + + org.slf4j + slf4j-api + 1.7.7 + + + org.slf4j + slf4j-log4j12 + 1.7.7 + +``` +3. Finally you can replace the code in `App.java` +```java +import org.apache.mxnet.javaapi.Context; +import org.apache.mxnet.javaapi.NDArray; + +public class App { + public static void main(String[] args) { + System.out.println("Hello"); + NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); + + System.out.println("Shape of NDarray is : " + nd.shape()); + + } +} +``` +make the package by +``` +mvn package +``` + +and run it by +``` +java -cp target/my-app-1.0-SNAPSHOT.jar:/.jar com.mycompany.app.App +``` +The result looks like this: +``` +Hello +SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". +SLF4J: Defaulting to no-operation (NOP) logger implementation +SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. +Shape of NDarray is : (10,20) +``` + +### (Optional) add log4j configuration +As you may notice, all of the examples contains the SLF4J error logs. +It caused by the missing configuration file. This problem can be solved easily by following steps: + +1. go to `target/classes` folder + +2. create a file namely `log4j.properties` and paste the following content inside: +``` +log4j.rootLogger = info, stdout + +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target = System.out +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n +``` +You can set the `info` to `warn` or `debug` if you would like to see less/more information when you run the code + + From 91e0edb559e266ea185bb998be675a63b5361e3d Mon Sep 17 00:00:00 2001 From: Qing Date: Thu, 25 Oct 2018 14:27:11 -0700 Subject: [PATCH 2/9] clean up nit --- .../mxnet_java_install_and_run_examples.md | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index 9784607670b3..5ba19914650a 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -2,41 +2,40 @@ ## Prerequisites: Please follow the Step 1 in the [Scala configuration](http://mxnet.incubator.apache.org/install/scala_setup.html#setup-instructions) -These should help you install the correct Java version and all dependecies. +These should help you install the correct Java version and all dependencies. ## Import and run the Java package -For users using a desktop/laptop, we recommend to use IntelliJ IDE as it is tested and supported to provide necessary documentation for the Java API. +For users using a desktop/laptop, we recommend using IntelliJ IDE as it is tested and supported to provide the necessary documentation for the Java API. -Alternatively, users can follow the second instruction to setup a empty Maven project for Java. +Alternatively, users can follow the second instruction to set up an empty Maven project for Java. ### IntelliJ instruction If you are using a computer with Ubuntu16.04 or Mac, you can install IntelliJ to run the Java package. Please follow the instruction below: -1. Create a new Java project in IntelliJ. Fire up IntelliJ and click `Create New Project`. It should open a popup window like this : +1. Create a new Java project in IntelliJ. Fire up IntelliJ and click `Create New Project`. 2. Click `Next`, and in the `Create project from template` window, do not select anything and click `Next` again. 3. In the next window choose your `Project name` and the `Project location` and click on `Finish`. -4. Let’s add the Java Inference API jars that we grabbed from Mavel Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. +4. Let’s add the Java Inference API jars that we grabbed from Maven Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. -6. Create a new Java class under the folder `your-project-name/src`. Let’s call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet we create an NDArray object in Java and print it’s shape. +6. Create a new Java class under the folder `your-project-name/src`. Let’s call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet, we create an NDArray object in Java and print its shape. ```java import org.apache.mxnet.javaapi.Context; import org.apache.mxnet.javaapi.NDArray; public class JavaSample { - public static void main(String[] args) { - System.out.println("Hello"); - NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); +public static void main(String[] args) { + System.out.println("Hello"); + NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); - System.out.println("Shape of NDarray is : " + nd.shape()); - - } + System.out.println("Shape of NDarray is : " + nd.shape()); +} } ``` -7. If all went well, you should see an output like this : (Ignore the SLF4J warnings). +7. If all went well, you should see an output like this : (Ignore the SLF4J warnings). ``` Hello SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". @@ -57,31 +56,32 @@ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -Darchety You can specify the `groupId` and `artifactId` to your favourite names. You can also create a maven project using empty archetype. 2. then go to `pom.xml` file in your project folder and add the following content. + - Change the `osx-x86_64` to `linux-x86_64` if your platform is linux. - Change `cpu` into `gpu` if you are using gpu - Change the version of your package from `1.3.1` to the matched jar version. ```xml - org.apache.mxnet - mxnet-full_2.11-osx-x86_64-cpu - 1.3.1 - system - path-to-your-jar/jarName.jar + org.apache.mxnet + mxnet-full_2.11-osx-x86_64-cpu + 1.3.1 + system + path-to-your-jar/jarName.jar - args4j - args4j - 2.0.29 - + args4j + args4j + 2.0.29 + - org.slf4j - slf4j-api - 1.7.7 + org.slf4j + slf4j-api + 1.7.7 - org.slf4j - slf4j-log4j12 - 1.7.7 + org.slf4j + slf4j-log4j12 + 1.7.7 ``` 3. Finally you can replace the code in `App.java` @@ -90,13 +90,13 @@ import org.apache.mxnet.javaapi.Context; import org.apache.mxnet.javaapi.NDArray; public class App { - public static void main(String[] args) { - System.out.println("Hello"); - NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); +public static void main(String[] args) { + System.out.println("Hello"); + NDArray nd = NDArray.ones(Context.cpu(), new int[] {10, 20}); - System.out.println("Shape of NDarray is : " + nd.shape()); + System.out.println("Shape of NDarray is : " + nd.shape()); - } +} } ``` make the package by @@ -118,7 +118,7 @@ Shape of NDarray is : (10,20) ``` ### (Optional) add log4j configuration -As you may notice, all of the examples contains the SLF4J error logs. +As you may notice, all of the examples contain the SLF4J error logs. It caused by the missing configuration file. This problem can be solved easily by following steps: 1. go to `target/classes` folder @@ -134,4 +134,3 @@ log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] ``` You can set the `info` to `warn` or `debug` if you would like to see less/more information when you run the code - From eec117f8f84cec1aa27f00d8d9addfb7878d25ca Mon Sep 17 00:00:00 2001 From: Qing Date: Thu, 25 Oct 2018 15:38:59 -0700 Subject: [PATCH 3/9] find the pain point --- docs/tutorials/scala/mxnet_java_install_and_run_examples.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index 5ba19914650a..d00f2b686229 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -18,9 +18,9 @@ If you are using a computer with Ubuntu16.04 or Mac, you can install IntelliJ to 3. In the next window choose your `Project name` and the `Project location` and click on `Finish`. -4. Let’s add the Java Inference API jars that we grabbed from Maven Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. +4. Let's add the Java Inference API jars that we grabbed from Maven Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. -6. Create a new Java class under the folder `your-project-name/src`. Let’s call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet, we create an NDArray object in Java and print its shape. +6. Create a new Java class under the folder `your-project-name/src`. Let's call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet, we create an NDArray object in Java and print its shape. ```java import org.apache.mxnet.javaapi.Context; import org.apache.mxnet.javaapi.NDArray; From 79e0df8c2b53fa349ce418a03ed74c8fb1b8f947 Mon Sep 17 00:00:00 2001 From: Qing Date: Thu, 25 Oct 2018 15:57:09 -0700 Subject: [PATCH 4/9] add java tut into whitelist --- tests/tutorials/test_sanity_tutorials.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tutorials/test_sanity_tutorials.py b/tests/tutorials/test_sanity_tutorials.py index cd3f6bfcbace..078e96b3b29e 100644 --- a/tests/tutorials/test_sanity_tutorials.py +++ b/tests/tutorials/test_sanity_tutorials.py @@ -49,6 +49,7 @@ 'scala/mnist.md', 'scala/index.md', 'scala/mxnet_scala_on_intellij.md', + 'scala/mxnet_java_install_and_run_examples.md', 'sparse/index.md', 'speech_recognition/index.md', 'unsupervised_learning/index.md', From c44e03fb22c175a16fb649ade15aeea149504a1f Mon Sep 17 00:00:00 2001 From: Qing Date: Thu, 25 Oct 2018 23:13:11 -0700 Subject: [PATCH 5/9] Trigger CI From 505965d6bc2e3f23b7f124fdd21114a67aa86f32 Mon Sep 17 00:00:00 2001 From: Qing Date: Sat, 27 Oct 2018 15:55:40 -0700 Subject: [PATCH 6/9] add java demo and split scala demo --- .../mxnet_java_install_and_run_examples.md | 25 +---- scala-package/mxnet-demo/java-demo/Makefile | 54 ++++++++++ scala-package/mxnet-demo/java-demo/README.md | 76 +++++++++++++ .../mxnet-demo/java-demo/bin/demo.sh | 20 ++++ .../mxnet-demo/java-demo/bin/run_od.sh | 21 ++++ scala-package/mxnet-demo/java-demo/pom.xml | 25 +++++ .../src/main/java/sample/HelloWorld.java | 28 +++++ .../src/main/java/sample/ObjectDetection.java | 101 ++++++++++++++++++ .../mxnet-demo/{ => scala-demo}/Makefile | 2 +- .../mxnet-demo/{ => scala-demo}/README.md | 8 +- .../mxnet-demo/{ => scala-demo}/bin/demo.sh | 0 .../mxnet-demo/{ => scala-demo}/bin/run_im.sh | 0 .../mxnet-demo/{ => scala-demo}/pom.xml | 0 .../src/main/scala/sample/HelloWorld.scala | 0 .../sample/ImageClassificationExample.scala | 0 15 files changed, 336 insertions(+), 24 deletions(-) create mode 100644 scala-package/mxnet-demo/java-demo/Makefile create mode 100644 scala-package/mxnet-demo/java-demo/README.md create mode 100644 scala-package/mxnet-demo/java-demo/bin/demo.sh create mode 100644 scala-package/mxnet-demo/java-demo/bin/run_od.sh create mode 100644 scala-package/mxnet-demo/java-demo/pom.xml create mode 100644 scala-package/mxnet-demo/java-demo/src/main/java/sample/HelloWorld.java create mode 100644 scala-package/mxnet-demo/java-demo/src/main/java/sample/ObjectDetection.java rename scala-package/mxnet-demo/{ => scala-demo}/Makefile (98%) rename scala-package/mxnet-demo/{ => scala-demo}/README.md (91%) rename scala-package/mxnet-demo/{ => scala-demo}/bin/demo.sh (100%) rename scala-package/mxnet-demo/{ => scala-demo}/bin/run_im.sh (100%) rename scala-package/mxnet-demo/{ => scala-demo}/pom.xml (100%) rename scala-package/mxnet-demo/{ => scala-demo}/src/main/scala/sample/HelloWorld.scala (100%) rename scala-package/mxnet-demo/{ => scala-demo}/src/main/scala/sample/ImageClassificationExample.scala (100%) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index d00f2b686229..823cf2c2a532 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -4,6 +4,9 @@ Please follow the Step 1 in the [Scala configuration](http://mxnet.incubator.apache.org/install/scala_setup.html#setup-instructions) These should help you install the correct Java version and all dependencies. +## Run the Java example project +We have provided a general MXNet Java template under `scala-package/mxnet-demo/java-demo` which contains complete instruction on running the Hello world and Object detection examples. Please copy the downloaded MXNet Java package jar file to the `java-demo` folder before build the package there. + ## Import and run the Java package For users using a desktop/laptop, we recommend using IntelliJ IDE as it is tested and supported to provide the necessary documentation for the Java API. @@ -59,7 +62,7 @@ You can specify the `groupId` and `artifactId` to your favourite names. You can - Change the `osx-x86_64` to `linux-x86_64` if your platform is linux. - Change `cpu` into `gpu` if you are using gpu -- Change the version of your package from `1.3.1` to the matched jar version. +- Change the version of your package from `1.3.1-SNAPSHOT` to the matched jar version. ```xml org.apache.mxnet @@ -115,22 +118,4 @@ SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Shape of NDarray is : (10,20) -``` - -### (Optional) add log4j configuration -As you may notice, all of the examples contain the SLF4J error logs. -It caused by the missing configuration file. This problem can be solved easily by following steps: - -1. go to `target/classes` folder - -2. create a file namely `log4j.properties` and paste the following content inside: -``` -log4j.rootLogger = info, stdout - -log4j.appender.stdout = org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target = System.out -log4j.appender.stdout.layout = org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n -``` -You can set the `info` to `warn` or `debug` if you would like to see less/more information when you run the code - +``` \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/Makefile b/scala-package/mxnet-demo/java-demo/Makefile new file mode 100644 index 000000000000..340a50f75965 --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/Makefile @@ -0,0 +1,54 @@ +# 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. + +SCALA_VERSION_PROFILE := 2.11 +SCALA_VERSION := 2.11.8 +MXNET_VERSION := 1.3.1-SNAPSHOT + +ifeq ($(OS),Windows_NT) + UNAME_S := Windows +else + UNAME_S := $(shell uname -s) +endif + +ifeq ($(UNAME_S), Windows) + # TODO: currently scala package does not support windows + SCALA_PKG_PROFILE := windows +else + ifeq ($(UNAME_S), Darwin) + SCALA_PKG_PROFILE := osx-x86_64-cpu + else + SCALA_PKG_PROFILE := linux-x86_64 + ifeq ($(USE_CUDA), 1) + SCALA_PKG_PROFILE := $(SCALA_PKG_PROFILE)-gpu + else + SCALA_PKG_PROFILE := $(SCALA_PKG_PROFILE)-cpu + endif + endif +endif + +javademo: + (mvn package -Dmxnet.profile=$(SCALA_PKG_PROFILE) \ + -Dmxnet.scalaprofile=$(SCALA_VERSION_PROFILE) \ + -Dmxnet.version=$(MXNET_VERSION) \ + -Dscala.version=$(SCALA_VERSION)) + +javaclean: + (mvn clean -Dmxnet.profile=$(SCALA_PKG_PROFILE) \ + -Dmxnet.scalaprofile=$(SCALA_VERSION_PROFILE) \ + -Dmxnet.version=$(MXNET_VERSION) \ + -Dscala.version=$(SCALA_VERSION)) \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/README.md b/scala-package/mxnet-demo/java-demo/README.md new file mode 100644 index 000000000000..6b81a8311d3f --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/README.md @@ -0,0 +1,76 @@ +# MXNet Java Sample Project +This is an project created to use Maven-published Scala/Java package with two Java examples. +## Setup +Please copy the downloaded MXNet Java package jar file under the `java-demo` folder. + +User are required to use `mvn package` to build the package, + which are shown below: +```Bash +export SCALA_VERSION_PROFILE=2.11 SCALA_VERSION=2.11.8 MXNET_VERSION=1.3.1-SNAPSHOT +export SCALA_PKG_PROFILE= +mvn package -Dmxnet.profile=$(SCALA_PKG_PROFILE) \ + -Dmxnet.scalaprofile=$(SCALA_VERSION_PROFILE) \ + -Dmxnet.version=$(MXNET_VERSION) \ + -Dscala.version=$(SCALA_VERSION) +``` +These environment variable (`SCALA_PKG_PROFILE`, `SCALA_VERSION_PROFILE`, `MXNET_VERSION`, `SCALA_VERSION`) +should be set before executing the line above. + +You can also use the `Makefile` as an alternative to do the same thing. Simply do the following: +```Bash +make javademo +``` +This will load the default parameter for all the environment variable. + If you want to run with GPU on Linux, just simply add `USE_CUDA=1` when you run the make file + +## Run +### Hello World +The Scala file is being executed using Java. You can execute the helloWorld example as follows: +```Bash +java -Xmx8G -cp $CLASSPATH sample.HelloWorld +``` +However, you have to define the Classpath before you run the demo code. More information can be found in the `demo.sh` And you can run the bash script as follows: +```Bash +bash bin/demo.sh +``` +It will load the library automatically and run the example +### Object Detection using Inference API +We also provide an example to do object detection, which downloads a ImageNet trained resnet50 model and runs inference on an image to return the classification result as +```Bash +Class: car +Probabilties: 0.99847263 +Coord:312.21335, 72.02908, 456.01443, 150.66176 +Class: bicycle +Probabilties: 0.9047381 +Coord:155.9581, 149.96365, 383.83694, 418.94516 +Class: dog +Probabilties: 0.82268167 +Coord:83.82356, 179.14001, 206.63783, 476.78754 +``` + +you can run using the command shown below: +```Bash +java -Xmx8G -cp $CLASSPATH sample.ObjectDetection +``` +or script as follows: +```Bash +bash bin/run_od.sh +``` + +If you want to test run on GPU, you can set a environment variable as follows: +```Bash +export SCALA_TEST_ON_GPU=1 +``` +## Clean up +Clean up for Maven package is simple, you can run the pre-configed `Makefile` as: +```Bash +make javaclean +``` + +## Q & A +If you are facing opencv issue on Ubuntu, please try as follows to install opencv 3.4 (required by 1.2.0 package and above) +```Bash +sudo add-apt-repository ppa:timsc/opencv-3.4 +sudo apt-get update +sudo apt install libopencv-imgcodecs3.4 +``` \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/bin/demo.sh b/scala-package/mxnet-demo/java-demo/bin/demo.sh new file mode 100644 index 000000000000..50e7fb9eb97d --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/bin/demo.sh @@ -0,0 +1,20 @@ +# 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. +#!/bin/bash +CURR_DIR=$(cd $(dirname $0)/../; pwd) +CLASSPATH=$CLASSPATH:$CURR_DIR/target/*:$CLASSPATH:$CURR_DIR/* +java -Xmx8G -cp $CLASSPATH sample.HelloWorld \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/bin/run_od.sh b/scala-package/mxnet-demo/java-demo/bin/run_od.sh new file mode 100644 index 000000000000..5cbc53fbcefe --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/bin/run_od.sh @@ -0,0 +1,21 @@ +# 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. +#!/bin/bash +CURR_DIR=$(cd $(dirname $0)/../; pwd) + +CLASSPATH=$CLASSPATH:$CURR_DIR/target/*:$CLASSPATH:$CURR_DIR/* +java -Xmx8G -cp $CLASSPATH sample.ObjectDetection \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/pom.xml b/scala-package/mxnet-demo/java-demo/pom.xml new file mode 100644 index 000000000000..5014d2e09f55 --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + Demo + mxnet-java-demo + 1.0-SNAPSHOT + MXNet Java Demo + + + + org.apache.mxnet + mxnet-full_${mxnet.scalaprofile}-${mxnet.profile} + ${mxnet.version} + system + ${project.basedir}/mxnet-full_${mxnet.scalaprofile}-${mxnet.profile}-${mxnet.version}.jar + + + commons-io + commons-io + 2.4 + + + \ No newline at end of file diff --git a/scala-package/mxnet-demo/java-demo/src/main/java/sample/HelloWorld.java b/scala-package/mxnet-demo/java-demo/src/main/java/sample/HelloWorld.java new file mode 100644 index 000000000000..60619dc8a806 --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/src/main/java/sample/HelloWorld.java @@ -0,0 +1,28 @@ +/* + * 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. + */ +package sample; + +import org.apache.mxnet.javaapi.*; +import java.util.Arrays; + +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello World!"); + NDArray nd = new NDArray(new float[]{2.0f, 3.0f}, new Shape(new int[]{1, 2}), Context.cpu()); + System.out.println(nd.shape()); + } +} diff --git a/scala-package/mxnet-demo/java-demo/src/main/java/sample/ObjectDetection.java b/scala-package/mxnet-demo/java-demo/src/main/java/sample/ObjectDetection.java new file mode 100644 index 000000000000..bf9a93ae8217 --- /dev/null +++ b/scala-package/mxnet-demo/java-demo/src/main/java/sample/ObjectDetection.java @@ -0,0 +1,101 @@ +/* + * 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. + */ +package sample; +import org.apache.mxnet.infer.javaapi.ObjectDetectorOutput; +import org.apache.mxnet.javaapi.*; +import org.apache.mxnet.infer.javaapi.ObjectDetector; +import org.apache.commons.io.FileUtils; +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ObjectDetection { + private static String modelPath; + private static String imagePath; + + private static void downloadUrl(String url, String filePath) { + File tmpFile = new File(filePath); + if (!tmpFile.exists()) { + try { + FileUtils.copyURLToFile(new URL(url), tmpFile); + } catch (Exception exception) { + System.err.println(exception); + } + } + } + + public static void downloadModelImage() { + String tempDirPath = System.getProperty("java.io.tmpdir"); + System.out.println("tempDirPath: %s".format(tempDirPath)); + imagePath = tempDirPath + "/inputImages/resnetssd/dog-ssd.jpg"; + String imgURL = "https://s3.amazonaws.com/model-server/inputs/dog-ssd.jpg"; + downloadUrl(imgURL, imagePath); + modelPath = tempDirPath + "resnetssd/resnet50_ssd_model"; + System.out.println("Download model files, this can take a while..."); + String modelURL = "https://s3.amazonaws.com/model-server/models/resnet50_ssd/"; + downloadUrl(modelURL + "resnet50_ssd_model-symbol.json", + tempDirPath + "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/resnetssd/resnet50_ssd_model-symbol.json"); + downloadUrl(modelURL + "resnet50_ssd_model-0000.params", + tempDirPath + "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/resnetssd/resnet50_ssd_model-0000.params"); + downloadUrl(modelURL + "synset.txt", + tempDirPath + "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/resnetssd/synset.txt"); + } + + static List> + runObjectDetectionSingle(String modelPathPrefix, String inputImagePath, List context) { + Shape inputShape = new Shape(new int[] {1, 3, 512, 512}); + List inputDescriptors = new ArrayList(); + inputDescriptors.add(new DataDesc("data", inputShape, DType.Float32(), "NCHW")); + ObjectDetector objDet = new ObjectDetector(modelPathPrefix, inputDescriptors, context, 0); + return objDet.imageObjectDetect(ObjectDetector.loadImageFromFile(inputImagePath), 3); + } + + public static void main(String[] args) { + List context = new ArrayList(); + if (System.getenv().containsKey("SCALA_TEST_ON_GPU") && + Integer.valueOf(System.getenv("SCALA_TEST_ON_GPU")) == 1) { + context.add(Context.gpu()); + } else { + context.add(Context.cpu()); + } + downloadModelImage(); + Shape inputShape = new Shape(new int[] {1, 3, 512, 512}); + Shape outputShape = new Shape(new int[] {1, 6132, 6}); + int width = inputShape.get(2); + int height = inputShape.get(3); + List> output + = runObjectDetectionSingle(modelPath, imagePath, context); + String outputStr = "\n"; + for (List ele : output) { + for (ObjectDetectorOutput i : ele) { + outputStr += "Class: " + i.getClassName() + "\n"; + outputStr += "Probabilties: " + i.getProbability() + "\n"; + + List coord = Arrays.asList(i.getXMin() * width, + i.getXMax() * height, i.getYMin() * width, i.getYMax() * height); + StringBuilder sb = new StringBuilder(); + for (float c: coord) { + sb.append(", ").append(c); + } + outputStr += "Coord:" + sb.substring(2)+ "\n"; + } + } + System.out.println(outputStr); + } +} \ No newline at end of file diff --git a/scala-package/mxnet-demo/Makefile b/scala-package/mxnet-demo/scala-demo/Makefile similarity index 98% rename from scala-package/mxnet-demo/Makefile rename to scala-package/mxnet-demo/scala-demo/Makefile index 227697ba2e8a..458077d13904 100644 --- a/scala-package/mxnet-demo/Makefile +++ b/scala-package/mxnet-demo/scala-demo/Makefile @@ -17,7 +17,7 @@ SCALA_VERSION_PROFILE := 2.11 SCALA_VERSION := 2.11.8 -MXNET_VERSION := 1.2.0 +MXNET_VERSION := 1.3.0 ifeq ($(OS),Windows_NT) UNAME_S := Windows diff --git a/scala-package/mxnet-demo/README.md b/scala-package/mxnet-demo/scala-demo/README.md similarity index 91% rename from scala-package/mxnet-demo/README.md rename to scala-package/mxnet-demo/scala-demo/README.md index e30a61a2fc13..d8695a37cbc0 100644 --- a/scala-package/mxnet-demo/README.md +++ b/scala-package/mxnet-demo/scala-demo/README.md @@ -4,7 +4,7 @@ This is an project created to use Maven-published Scala package with two Scala e User are required to use `mvn package` to build the package, which are shown below: ```Bash -export SCALA_VERSION_PROFILE=2.11 SCALA_VERSION=2.11.8 MXNET_VERSION=1.2.0 +export SCALA_VERSION_PROFILE=2.11 SCALA_VERSION=2.11.8 MXNET_VERSION=1.3.0 export SCALA_PKG_PROFILE= mvn package -Dmxnet.profile=$(SCALA_PKG_PROFILE) \ -Dmxnet.scalaprofile=$(SCALA_VERSION_PROFILE) \ @@ -12,7 +12,9 @@ mvn package -Dmxnet.profile=$(SCALA_PKG_PROFILE) \ -Dscala.version=$(SCALA_VERSION) ``` These environment variable (`SCALA_PKG_PROFILE`, `SCALA_VERSION_PROFILE`, `MXNET_VERSION`, `SCALA_VERSION`) -should be set before executing the line above. +should be set before executing the line above. + +To obtain the most recent MXNet version, please click [here](https://mvnrepository.com/search?q=org.apache.mxnet) You can also use the `Makefile` as an alternative to do the same thing. Simply do the following: ```Bash @@ -59,7 +61,7 @@ make scalaclean ``` ## Q & A -If you are facing opencv issue on Ubuntu, please try as follows to install opencv 3.4 (required by 1.2.0 package) +If you are facing opencv issue on Ubuntu, please try as follows to install opencv 3.4 (required by 1.2.0 package and above) ```Bash sudo add-apt-repository ppa:timsc/opencv-3.4 sudo apt-get update diff --git a/scala-package/mxnet-demo/bin/demo.sh b/scala-package/mxnet-demo/scala-demo/bin/demo.sh similarity index 100% rename from scala-package/mxnet-demo/bin/demo.sh rename to scala-package/mxnet-demo/scala-demo/bin/demo.sh diff --git a/scala-package/mxnet-demo/bin/run_im.sh b/scala-package/mxnet-demo/scala-demo/bin/run_im.sh similarity index 100% rename from scala-package/mxnet-demo/bin/run_im.sh rename to scala-package/mxnet-demo/scala-demo/bin/run_im.sh diff --git a/scala-package/mxnet-demo/pom.xml b/scala-package/mxnet-demo/scala-demo/pom.xml similarity index 100% rename from scala-package/mxnet-demo/pom.xml rename to scala-package/mxnet-demo/scala-demo/pom.xml diff --git a/scala-package/mxnet-demo/src/main/scala/sample/HelloWorld.scala b/scala-package/mxnet-demo/scala-demo/src/main/scala/sample/HelloWorld.scala similarity index 100% rename from scala-package/mxnet-demo/src/main/scala/sample/HelloWorld.scala rename to scala-package/mxnet-demo/scala-demo/src/main/scala/sample/HelloWorld.scala diff --git a/scala-package/mxnet-demo/src/main/scala/sample/ImageClassificationExample.scala b/scala-package/mxnet-demo/scala-demo/src/main/scala/sample/ImageClassificationExample.scala similarity index 100% rename from scala-package/mxnet-demo/src/main/scala/sample/ImageClassificationExample.scala rename to scala-package/mxnet-demo/scala-demo/src/main/scala/sample/ImageClassificationExample.scala From 569c8f23a34970f7813a530b72672fd1a4e5bfd2 Mon Sep 17 00:00:00 2001 From: Lanking Date: Sun, 28 Oct 2018 20:05:13 -0700 Subject: [PATCH 7/9] address the comments --- docs/tutorials/scala/mxnet_java_install_and_run_examples.md | 2 +- scala-package/mxnet-demo/java-demo/README.md | 6 +++--- .../mxnet-demo/java-demo/bin/{demo.sh => java_sample.sh} | 0 scala-package/mxnet-demo/scala-demo/README.md | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename scala-package/mxnet-demo/java-demo/bin/{demo.sh => java_sample.sh} (100%) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index 823cf2c2a532..930a3c5602c6 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -67,7 +67,7 @@ You can specify the `groupId` and `artifactId` to your favourite names. You can org.apache.mxnet mxnet-full_2.11-osx-x86_64-cpu - 1.3.1 + 1.3.1-SNAPSHOT system path-to-your-jar/jarName.jar diff --git a/scala-package/mxnet-demo/java-demo/README.md b/scala-package/mxnet-demo/java-demo/README.md index 6b81a8311d3f..ffe614a29287 100644 --- a/scala-package/mxnet-demo/java-demo/README.md +++ b/scala-package/mxnet-demo/java-demo/README.md @@ -27,11 +27,11 @@ This will load the default parameter for all the environment variable. ### Hello World The Scala file is being executed using Java. You can execute the helloWorld example as follows: ```Bash -java -Xmx8G -cp $CLASSPATH sample.HelloWorld +java -cp $CLASSPATH sample.HelloWorld ``` However, you have to define the Classpath before you run the demo code. More information can be found in the `demo.sh` And you can run the bash script as follows: ```Bash -bash bin/demo.sh +bash bin/java_sample.sh ``` It will load the library automatically and run the example ### Object Detection using Inference API @@ -50,7 +50,7 @@ Coord:83.82356, 179.14001, 206.63783, 476.78754 you can run using the command shown below: ```Bash -java -Xmx8G -cp $CLASSPATH sample.ObjectDetection +java -cp $CLASSPATH sample.ObjectDetection ``` or script as follows: ```Bash diff --git a/scala-package/mxnet-demo/java-demo/bin/demo.sh b/scala-package/mxnet-demo/java-demo/bin/java_sample.sh similarity index 100% rename from scala-package/mxnet-demo/java-demo/bin/demo.sh rename to scala-package/mxnet-demo/java-demo/bin/java_sample.sh diff --git a/scala-package/mxnet-demo/scala-demo/README.md b/scala-package/mxnet-demo/scala-demo/README.md index d8695a37cbc0..300fc7b2e108 100644 --- a/scala-package/mxnet-demo/scala-demo/README.md +++ b/scala-package/mxnet-demo/scala-demo/README.md @@ -27,7 +27,7 @@ This will load the default parameter for all the environment variable. ### Hello World The Scala file is being executed using Java. You can execute the helloWorld example as follows: ```Bash -java -Xmx8G -cp $CLASSPATH sample.HelloWorld +java -cp $CLASSPATH sample.HelloWorld ``` However, you have to define the Classpath before you run the demo code. More information can be found in the `demo.sh` And you can run the bash script as follows: ```Bash @@ -43,7 +43,7 @@ You can review the complete example [here](https://github.com/apache/incubator-m you can run using the command shown below: ```Bash -java -Xmx8G -cp $CLASSPATH sample.ImageClassificationExample +java -cp $CLASSPATH sample.ImageClassificationExample ``` or script as follows: ```Bash From 551d0d19bd545deea7619e73479048f9e296559d Mon Sep 17 00:00:00 2001 From: Lanking Date: Sun, 28 Oct 2018 20:07:57 -0700 Subject: [PATCH 8/9] change the examples --- docs/tutorials/scala/mxnet_java_install_and_run_examples.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index 930a3c5602c6..717e1afba84f 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -5,7 +5,9 @@ Please follow the Step 1 in the [Scala configuration](http://mxnet.incubator.apa These should help you install the correct Java version and all dependencies. ## Run the Java example project -We have provided a general MXNet Java template under `scala-package/mxnet-demo/java-demo` which contains complete instruction on running the Hello world and Object detection examples. Please copy the downloaded MXNet Java package jar file to the `java-demo` folder before build the package there. +We have provided a general MXNet Java template under `scala-package/mxnet-demo/java-demo` which contains the necessary project files for you to get started. It contains a simple Hello world! equivalent program `JavaSample.java` and a full fledged `ObjectDetection.java `that shows how to run Object Detection on images using MXNet and pre-trained SSD model. + +Alternatively you could build project from scratch following the below instructions. ## Import and run the Java package For users using a desktop/laptop, we recommend using IntelliJ IDE as it is tested and supported to provide the necessary documentation for the Java API. @@ -38,7 +40,7 @@ public static void main(String[] args) { } ``` -7. If all went well, you should see an output like this : (Ignore the SLF4J warnings). +7. If all went well, you should see an output like this : ``` Hello SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". From 077fc51b0d6f358f18fbf233fd683c9518e69071 Mon Sep 17 00:00:00 2001 From: Qing Date: Mon, 29 Oct 2018 11:47:53 -0700 Subject: [PATCH 9/9] fix the wrong configuration --- docs/tutorials/scala/mxnet_java_install_and_run_examples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md index 717e1afba84f..83e1ec5b2daa 100644 --- a/docs/tutorials/scala/mxnet_java_install_and_run_examples.md +++ b/docs/tutorials/scala/mxnet_java_install_and_run_examples.md @@ -23,7 +23,7 @@ If you are using a computer with Ubuntu16.04 or Mac, you can install IntelliJ to 3. In the next window choose your `Project name` and the `Project location` and click on `Finish`. -4. Let's add the Java Inference API jars that we grabbed from Maven Central. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. +4. Let's add the Java Inference API jars that we build from source. At the top of the window, Go to the `File -> Project Structure`. In the popup window that opens up, click on `Libraries -> +` and select the path to the jar files downloaded. Click `Apply` and then click `OK`. 6. Create a new Java class under the folder `your-project-name/src`. Let's call this class `JavaSample.java`. Type in the following code snippet and run it. In this code snippet, we create an NDArray object in Java and print its shape. ```java