This is an automated email from the ASF dual-hosted git repository.
nswamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new 9e681f2 [MXNET-531] Maven demo project (#11451)
9e681f2 is described below
commit 9e681f24aec043e9e80270872048cec2c93d1286
Author: Lanking <[email protected]>
AuthorDate: Tue Jul 3 23:33:00 2018 -0700
[MXNET-531] Maven demo project (#11451)
* Maven demo project
---
scala-package/macros/pom.xml | 5 +
scala-package/mxnet-demo/Makefile | 54 +++++++++++
scala-package/mxnet-demo/README.md | 67 ++++++++++++++
scala-package/mxnet-demo/bin/demo.sh | 20 ++++
scala-package/mxnet-demo/bin/run_im.sh | 21 +++++
scala-package/mxnet-demo/pom.xml | 103 +++++++++++++++++++++
.../src/main/scala/sample/HelloWorld.scala | 26 ++++++
.../scala/sample/ImageClassificationExample.scala | 97 +++++++++++++++++++
8 files changed, 393 insertions(+)
diff --git a/scala-package/macros/pom.xml b/scala-package/macros/pom.xml
index d80f725..9436af6 100644
--- a/scala-package/macros/pom.xml
+++ b/scala-package/macros/pom.xml
@@ -51,6 +51,11 @@
<scope>provided</scope>
<type>${libtype}</type>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.1</version>
+ </dependency>
</dependencies>
diff --git a/scala-package/mxnet-demo/Makefile
b/scala-package/mxnet-demo/Makefile
new file mode 100644
index 0000000..227697b
--- /dev/null
+++ b/scala-package/mxnet-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.2.0
+
+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
+
+scalademo:
+ (mvn package -Dmxnet.profile=$(SCALA_PKG_PROFILE) \
+ -Dmxnet.scalaprofile=$(SCALA_VERSION_PROFILE) \
+ -Dmxnet.version=$(MXNET_VERSION) \
+ -Dscala.version=$(SCALA_VERSION))
+
+scalaclean:
+ (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/README.md
b/scala-package/mxnet-demo/README.md
new file mode 100644
index 0000000..e30a61a
--- /dev/null
+++ b/scala-package/mxnet-demo/README.md
@@ -0,0 +1,67 @@
+# MXNet Scala Sample Project
+This is an project created to use Maven-published Scala package with two Scala
examples.
+## Setup
+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_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 scalademo
+```
+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
+### Image Classification using Inference API
+We also provide an example to do image classification, which downloads a
ImageNet trained resnet18 model and runs inference on a cute puppy to return
the classification result as
+```Bash
+Classes with top 5 probability = Vector((n02110958 pug, pug-dog,0.49161583),
(n02108422 bull mastiff,0.40025946), (n02108089 boxer,0.04657662), (n04409515
tennis ball,0.028773671), (n02109047 Great Dane,0.009004086))
+```
+You can review the complete example
[here](https://github.com/apache/incubator-mxnet/tree/master/scala-package/examples/src/main/scala/org/apache/mxnetexamples/infer/imageclassifier)
+
+you can run using the command shown below:
+```Bash
+java -Xmx8G -cp $CLASSPATH sample.ImageClassificationExample
+```
+or script as follows:
+```Bash
+bash bin/run_im.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 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)
+```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/bin/demo.sh
b/scala-package/mxnet-demo/bin/demo.sh
new file mode 100644
index 0000000..d5e567b
--- /dev/null
+++ b/scala-package/mxnet-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/target/classes/lib/*
+java -Xmx8G -cp $CLASSPATH sample.HelloWorld
diff --git a/scala-package/mxnet-demo/bin/run_im.sh
b/scala-package/mxnet-demo/bin/run_im.sh
new file mode 100644
index 0000000..68beb89
--- /dev/null
+++ b/scala-package/mxnet-demo/bin/run_im.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/target/classes/lib/*
+java -Xmx8G -cp $CLASSPATH sample.ImageClassificationExample
\ No newline at end of file
diff --git a/scala-package/mxnet-demo/pom.xml b/scala-package/mxnet-demo/pom.xml
new file mode 100644
index 0000000..8fc30e7
--- /dev/null
+++ b/scala-package/mxnet-demo/pom.xml
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>Demo</groupId>
+ <artifactId>mxnet-scala-demo_2.11</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>MXNet Scala Demo</name>
+ <packaging>pom</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.mxnet</groupId>
+
<artifactId>mxnet-full_${mxnet.scalaprofile}-${mxnet.profile}</artifactId>
+ <version>${mxnet.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>${scala.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.3</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>2.7</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>2.9</version>
+ <executions>
+ <execution>
+ <id>copy-dependencies</id>
+ <phase>package</phase>
+ <goals>
+ <goal>copy-dependencies</goal>
+ </goals>
+ <configuration>
+
<outputDirectory>${project.build.outputDirectory}/lib</outputDirectory>
+ <includeScope>runtime</includeScope>
+ <excludeScope>test,provided</excludeScope>
+ <overWriteReleases>false</overWriteReleases>
+ <overWriteSnapshots>false</overWriteSnapshots>
+ <overWriteIfNewer>true</overWriteIfNewer>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.5</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ <configuration>
+ <includes>
+ <include>**/*</include>
+ </includes>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>net.alchim31.maven</groupId>
+ <artifactId>scala-maven-plugin</artifactId>
+ <version>3.2.2</version>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <phase>compile</phase>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/scala-package/mxnet-demo/src/main/scala/sample/HelloWorld.scala
b/scala-package/mxnet-demo/src/main/scala/sample/HelloWorld.scala
new file mode 100644
index 0000000..c625b6d
--- /dev/null
+++ b/scala-package/mxnet-demo/src/main/scala/sample/HelloWorld.scala
@@ -0,0 +1,26 @@
+/*
+ * 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._
+
+object HelloWorld {
+ def main(args: Array[String]): Unit = {
+ println("hello World")
+ val arr = NDArray.ones(2, 3)
+ println(arr.shape)
+ }
+}
\ No newline at end of file
diff --git
a/scala-package/mxnet-demo/src/main/scala/sample/ImageClassificationExample.scala
b/scala-package/mxnet-demo/src/main/scala/sample/ImageClassificationExample.scala
new file mode 100644
index 0000000..b5af654
--- /dev/null
+++
b/scala-package/mxnet-demo/src/main/scala/sample/ImageClassificationExample.scala
@@ -0,0 +1,97 @@
+/*
+ * 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.{Context, DType, DataDesc, Shape}
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import org.apache.mxnet.infer.{ImageClassifier, _}
+
+import scala.collection.JavaConverters._
+import java.io.File
+import java.net.URL
+import org.apache.commons.io._
+
+import scala.collection.mutable.ListBuffer
+
+/**
+ * Example showing usage of Infer package to do inference on resnet-18 model
+ * Follow instructions in README.md to run this example.
+ */
+object ImageClassificationExample {
+
+ def downloadUrl(url: String, filePath: String) : Unit = {
+ var tmpFile = new File(filePath)
+ if (!tmpFile.exists()) {
+ FileUtils.copyURLToFile(new URL(url), tmpFile)
+ }
+ }
+
+ def downloadModelImage() : (String, String) = {
+ val tempDirPath = System.getProperty("java.io.tmpdir")
+ printf("tempDirPath: %s".format(tempDirPath))
+ val imgPath = tempDirPath + "/inputImages/resnet18/Pug-Cookie.jpg"
+ val imgURL = "https://s3.amazonaws.com/model-server/inputs/Pug-Cookie.jpg"
+ downloadUrl(imgURL, imgPath)
+
+ val baseUrl = "https://s3.us-east-2.amazonaws.com/scala-infer-models"
+ var tmpPath = tempDirPath + "/resnet18/resnet-18-symbol.json"
+ var tmpUrl = baseUrl + "/resnet-18/resnet-18-symbol.json"
+ downloadUrl(tmpUrl, tmpPath)
+
+ tmpPath = tempDirPath + "/resnet18/resnet-18-0000.params"
+ tmpUrl = baseUrl + "/resnet-18/resnet-18-0000.params"
+ downloadUrl(tmpUrl, tmpPath)
+
+ tmpPath = tempDirPath + "/resnet18/synset.txt"
+ tmpUrl = baseUrl + "/resnet-18/synset.txt"
+ downloadUrl(tmpUrl, tmpPath)
+
+ (imgPath, tempDirPath + "/resnet18/resnet-18")
+ }
+
+ def main(args: Array[String]): Unit = {
+
+ var context = Context.cpu()
+ if (System.getenv().containsKey("SCALA_TEST_ON_GPU") &&
+ System.getenv("SCALA_TEST_ON_GPU").toInt == 1) {
+ context = Context.gpu()
+ }
+ val (inputImagePath, modelPathPrefix) = downloadModelImage()
+
+ val dType = DType.Float32
+ val inputShape = Shape(1, 3, 224, 224)
+ val inputDescriptor = IndexedSeq(DataDesc("data", inputShape, dType,
"NCHW"))
+
+ // Create object of ImageClassifier class
+ val imgClassifier: ImageClassifier = new
+ ImageClassifier(modelPathPrefix, inputDescriptor, context)
+
+ // Loading single image from file and getting BufferedImage
+ val img = ImageClassifier.loadImageFromFile(inputImagePath)
+
+ // Running inference on single image
+ val output = imgClassifier.classifyImage(img, Some(5))
+
+ // Printing top 5 class probabilities
+ for (i <- output) {
+ printf("Classes with top 5 probability = %s \n", i)
+ }
+
+ }
+}
\ No newline at end of file