Run MXNet Scala Examples Using the IntelliJ IDE

This tutorial guides you through setting up a Scala project in the IntelliJ IDE and shows how to use an MXNet package from your application.

Prerequisites:

To use this tutorial you need the following items, however after this list, installation info for macOS is provided for your benefit:

Mac Prerequisites Setup

For other operating systems, visit each Prerequisite’s website and follow their installations instructions. For macOS, you’re in luck:

Step 1. Install brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2. Install Java 8 JDK:

brew tap caskroom/versions
brew cask install java8

Step 3. Install maven:

brew update
brew install maven

Build the MXNet Shared Library and Scala Package

This depends on your operating system. Instructions for macOS, Ubuntu, and Windows are provided:

OS Step 1 Step 2
macOS Shared Library for macOS Scala Package for macOS
Ubuntu Shared Library for Ubuntu Scala Package for Ubuntu
Windows Shared Library for Windows Scala Package for Windows

Build Scala from an Existing MXNet Installation

If you have already built MXNet from source and are looking to setup Scala from that point, you may simply run the following from the MXNet source root:

make scalapkg
make scalainstall

Set Up Your Project

Now that you’ve installed your prerequisites, you are ready to setup IntelliJ and your first MXNet-Scala project!

Step 1. Install and setup IntelliJ: - When prompted for what to features to enable during IntelliJ’s first startup, make sure you select Scala.

- Install the plugin for IntelliJ IDE by following these steps:

On Menu, choose Preferences, choose Plugins, type Scala, and then choose Install. For further plugin help and instructions, refer to Scala plugin setup for IDE.

Step 2. Create a new project:

intellij welcome

From the IntelliJ welcome screen, select “Create New Project”.

maven project type

Choose the Maven project type.

maven project type - archetype

Select the checkbox for Create from archetype.

maven project type - archetype

Click the Add Archetype button, and add the following information to each field.

GroupId

net.alchim31.maven

ArtifactId

scala-archetype-simple

Version

1.6

Repository

https://mvnrepository.com/artifact/net.alchim31.maven/scala-archetype-simple

maven project type - archetype

Click Ok to add the archetype, make sure it is selected from the list, and then click Next.

project metadata

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

GroupId

your-name

ArtifactId

ArtifactId: scalaMXNet

Version

1.0-SNAPSHOT

project properties

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

project location

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

project 1

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. Setup project properties:

  • Specify project properties in pom.xml by pasting the following content in the properties tag. You will be overwriting the scala.version tag in the process, upgrading from 2.11.5 to 2.11.8.

  2.11.8
  2.11

Step 4. Setup project profiles and platforms:

  • Specify project profiles and platforms in pom.xml by pasting the following content below the closing properties tag:

    
        osx-x86_64-cpu
        
            osx-x86_64-cpu
        
    
    
        linux-x86_64-cpu
        
            linux-x86_64-cpu
        
    
    
        linux-x86_64-gpu
        
            linux-x86_64-gpu
        
    

Step 5. Setup project dependencies:

  • Specify project dependencies in pom.xml adding the dependencies listed below. Place them inside the dependencies tag:

  
  
    ml.dmlc.mxnet
    mxnet-full_${scala.binary.version}-${platform}
    1.2.0
    system
    /Development/incubator-mxnet/scala-package/assembly/osx-x86_64-cpu/target/mxnet-full_2.11-osx-x86_64-cpu-1.2.0-SNAPSHOT.jar
  
  
    args4j
    args4j
    2.0.29
  
  
    org.slf4j
    slf4j-api
    ${slf4jVersion}
  
  
    org.slf4j
    slf4j-log4j12
    ${slf4jVersion}
  
  
  
    org.scala-lang
    scala-library
    ${scala.version}
  
  
  
    junit
    junit
    4.11
    test
  
  
    org.specs2
    specs2-core_${scala.compat.version}
    2.4.16
    test
  
  
    org.specs2
    specs2-junit_${scala.compat.version}
    2.4.16
    test
  
  
    org.scalatest
    scalatest_${scala.compat.version}
    2.2.4
    test
  

project 2

Note the systemPath tag and update it to match the file path to the jar file that was created when you built the MXNet-Scala package. It can be found in the mxnet-incubator/scala-package/assembly/{platform}/target directory, and is named with the pattern mxnet-full_${scala.binary.version}-${platform}-{version-SNAPSHOT}.jar.

Step 6. Import dependencies with Maven:

  • Note the prompt in the lower right corner that states “Maven projects need to be imported”.

project 3

Click “Import Changes” in this prompt.

Step 7. Build the project:

  • To build the project, from the menu choose Build, and then choose Build Project.

Note: During the build you may experience [ERROR] scalac error: bad option: '-make:transitive'. You can fix this by deleting or commenting this out in your pom.xml. This line in question is the arg tag, and it should contain: -make:transitive.

Step 8. Run the Hello World App:

hello world app

Navigate to the App included with the project.

run hello world

Run the App by clicking the green arrow, and verify the Hello World output

Step 9. Run Sample MXNet Code in the App:

run hello mxnet

Paste the following code in the App, overwriting the original hello world code. Then click the green arrow to run it.

object App extends App {
  import ml.dmlc.mxnet._
  import org.apache.log4j.BasicConfigurator
  BasicConfigurator.configure()

  private val a = NDArray.ones(2, 3)
  println("Testing MXNet by generating an 2x3 NDArray...")
  println("Shape is: ")
  println(a.shape)
}

run hello world

Your result should be similar to this output.

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 folder then run the following:
    mvn clean package -e -P osx-x86_64-cpu

The -P parameter tells the build which platform to target. The -e will give you more details if the build fails. If it succeeds, you should see a lot of info and some warning messages, followed by:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.186 s
[INFO] Finished at: 2018-03-06T15:17:36-08:00
[INFO] Final Memory: 11M/155M
[INFO] ------------------------------------------------------------------------

The build generates a new jar file in the target folder called scalaInference-1.0-SNAPSHOT.jar.

Next Steps

For more information about MXNet Scala resources, see the following: