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 aa3f065  [MXNET-116] Optimized functions with batch input (#10343)
aa3f065 is described below

commit aa3f06511e96cbffc7a9a2d9e0a96cf0e446d646
Author: Lanking <lanking...@live.com>
AuthorDate: Wed Apr 4 11:13:58 2018 -0700

    [MXNET-116] Optimized functions with batch input (#10343)
    
    * Add batch inputs and GPU Tests
---
 .../imageclassifier/ImageClassifierExample.scala   | 56 +++++++++++++++++-----
 .../objectdetector/SSDClassifierExample.scala      | 54 +++++++++++++++++----
 .../ImageClassifierExampleSuite.scala              | 14 ++++--
 .../ObjectDetectorExampleSuite.scala               | 11 ++++-
 .../ml/dmlc/mxnet/infer/ImageClassifier.scala      | 19 ++------
 5 files changed, 115 insertions(+), 39 deletions(-)

diff --git 
a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExample.scala
 
b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExample.scala
index 22c49e9..17b11de 100644
--- 
a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExample.scala
+++ 
b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExample.scala
@@ -20,21 +20,25 @@ package ml.dmlc.mxnetexamples.inferexample.imageclassifier
 import ml.dmlc.mxnet.Shape
 import org.kohsuke.args4j.{CmdLineParser, Option}
 import org.slf4j.LoggerFactory
-
-import ml.dmlc.mxnet.{DType, DataDesc}
+import ml.dmlc.mxnet.{DType, DataDesc, Context}
 import ml.dmlc.mxnet.infer.ImageClassifier
 
 import scala.collection.JavaConverters._
 import java.io.File
 
+import scala.collection.mutable.ListBuffer
+
 /**
   * Example showing usage of Infer package to do inference on resnet-152 model
   * Follow instructions in README.md to run this example.
   */
 object ImageClassifierExample {
+
   private val logger = LoggerFactory.getLogger(classOf[ImageClassifierExample])
 
-  def runInferenceOnSingleImage(modelPathPrefix: String, inputImagePath: 
String):
+
+  def runInferenceOnSingleImage(modelPathPrefix: String, inputImagePath: 
String,
+                                context: Array[Context]):
   IndexedSeq[IndexedSeq[(String, Float)]] = {
     val dType = DType.Float32
     val inputShape = Shape(1, 3, 224, 224)
@@ -43,7 +47,7 @@ object ImageClassifierExample {
 
     // Create object of ImageClassifier class
     val imgClassifier: ImageClassifier = new
-        ImageClassifier(modelPathPrefix, inputDescriptor)
+        ImageClassifier(modelPathPrefix, inputDescriptor, context)
 
     // Loading single image from file and getting BufferedImage
     val img = ImageClassifier.loadImageFromFile(inputImagePath)
@@ -54,7 +58,8 @@ object ImageClassifierExample {
     output
   }
 
-  def runInferenceOnBatchOfImage(modelPathPrefix: String, inputImageDir: 
String):
+  def runInferenceOnBatchOfImage(modelPathPrefix: String, inputImageDir: 
String,
+                                 context: Array[Context]):
   IndexedSeq[IndexedSeq[(String, Float)]] = {
     val dType = DType.Float32
     val inputShape = Shape(1, 3, 224, 224)
@@ -63,23 +68,52 @@ object ImageClassifierExample {
 
     // Create object of ImageClassifier class
     val imgClassifier: ImageClassifier = new
-        ImageClassifier(modelPathPrefix, inputDescriptor)
+        ImageClassifier(modelPathPrefix, inputDescriptor, context)
 
     // Loading batch of images from the directory path
-    val imgList = ImageClassifier.loadInputBatch(inputImageDir)
+    val batchFiles = generateBatches(inputImageDir, 20)
+    var outputList = IndexedSeq[IndexedSeq[(String, Float)]]()
 
-    // Running inference on batch of images loaded in previous step
-    val outputList = imgClassifier.classifyImageBatch(imgList, Some(5))
+    for (batchFile <- batchFiles) {
+      val imgList = ImageClassifier.loadInputBatch(batchFile)
+      // Running inference on batch of images loaded in previous step
+      outputList ++= imgClassifier.classifyImageBatch(imgList, Some(5))
+    }
 
     outputList
   }
 
+  def generateBatches(inputImageDirPath: String, batchSize: Int = 100): 
List[List[String]] = {
+    val dir = new File(inputImageDirPath)
+    require(dir.exists && dir.isDirectory,
+      "input image directory: %s not found".format(inputImageDirPath))
+    val output = ListBuffer[List[String]]()
+    var batch = ListBuffer[String]()
+    for (imgFile: File <- dir.listFiles()){
+      batch += imgFile.getPath
+      if (batch.length == batchSize) {
+        output += batch.toList
+        batch = ListBuffer[String]()
+      }
+    }
+    output += batch.toList
+    output.toList
+  }
+
   def main(args: Array[String]): Unit = {
     val inst = new ImageClassifierExample
     val parser: CmdLineParser = new CmdLineParser(inst)
+
+    var context = Context.cpu()
+    if (System.getenv().containsKey("SCALA_TEST_ON_GPU") &&
+      System.getenv("SCALA_TEST_ON_GPU").toInt == 1) {
+      context = Context.gpu()
+    }
+
     try {
       parser.parseArgument(args.toList.asJava)
 
+
       val modelPathPrefix = if (inst.modelPathPrefix == null) 
System.getenv("MXNET_DATA_DIR")
       else inst.modelPathPrefix
 
@@ -89,14 +123,14 @@ object ImageClassifierExample {
       val inputImageDir = if (inst.inputImageDir == null) 
System.getenv("MXNET_DATA_DIR")
       else inst.inputImageDir
 
-      val singleOutput = runInferenceOnSingleImage(modelPathPrefix, 
inputImagePath)
+      val singleOutput = runInferenceOnSingleImage(modelPathPrefix, 
inputImagePath, context)
 
       // Printing top 5 class probabilities
       for (i <- singleOutput) {
         printf("Classes with top 5 probability = %s \n", i)
       }
 
-      val batchOutput = runInferenceOnBatchOfImage(modelPathPrefix, 
inputImageDir)
+      val batchOutput = runInferenceOnBatchOfImage(modelPathPrefix, 
inputImageDir, context)
 
       val d = new File(inputImageDir)
       val filenames = d.listFiles.filter(_.isFile).toList
diff --git 
a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/SSDClassifierExample.scala
 
b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/SSDClassifierExample.scala
index 9b8a26c..930ef13 100644
--- 
a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/SSDClassifierExample.scala
+++ 
b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/SSDClassifierExample.scala
@@ -17,7 +17,9 @@
 
 package ml.dmlc.mxnetexamples.inferexample.objectdetector
 
-import ml.dmlc.mxnet.{DType, Shape, DataDesc}
+import java.io.File
+
+import ml.dmlc.mxnet.{Context, DType, DataDesc, Shape}
 import ml.dmlc.mxnet.infer._
 import org.kohsuke.args4j.{CmdLineParser, Option}
 import org.slf4j.LoggerFactory
@@ -25,6 +27,8 @@ import org.slf4j.LoggerFactory
 import scala.collection.JavaConverters._
 import java.nio.file.{Files, Paths}
 
+import scala.collection.mutable.ListBuffer
+
 class SSDClassifierExample {
   @Option(name = "--model-path-prefix", usage = "the input model directory and 
prefix of the model")
   private val modelPathPrefix: String = "/model/ssd_resnet50_512"
@@ -39,7 +43,8 @@ object SSDClassifierExample {
   private val logger = LoggerFactory.getLogger(classOf[SSDClassifierExample])
   private type SSDOut = (String, Array[Float])
 
-  def runObjectDetectionSingle(modelPathPrefix: String, inputImagePath: 
String):
+  def runObjectDetectionSingle(modelPathPrefix: String, inputImagePath: String,
+                               context: Array[Context]):
   IndexedSeq[IndexedSeq[(String, Array[Float])]] = {
     val dType = DType.Float32
     val inputShape = Shape(1, 3, 512, 512)
@@ -47,25 +52,50 @@ object SSDClassifierExample {
     val outputShape = Shape(1, 6132, 6)
     val inputDescriptors = IndexedSeq(DataDesc("data", inputShape, dType, 
"NCHW"))
     val img = ImageClassifier.loadImageFromFile(inputImagePath)
-    val objDetector = new ObjectDetector(modelPathPrefix, inputDescriptors)
+    val objDetector = new ObjectDetector(modelPathPrefix, inputDescriptors, 
context)
     val output = objDetector.imageObjectDetect(img, Some(3))
 
     output
   }
 
-  def runObjectDetectionBatch(modelPathPrefix: String, inputImageDir: String):
+  def runObjectDetectionBatch(modelPathPrefix: String, inputImageDir: String,
+                              context: Array[Context]):
   IndexedSeq[IndexedSeq[(String, Array[Float])]] = {
     val dType = DType.Float32
     val inputShape = Shape(1, 3, 512, 512)
     // ssd detections, numpy.array([[id, score, x1, y1, x2, y2]...])
     val outputShape = Shape(1, 6132, 6)
     val inputDescriptors = IndexedSeq(DataDesc("data", inputShape, dType, 
"NCHW"))
-    val imgList = ImageClassifier.loadInputBatch(inputImageDir)
-    val objDetector = new ObjectDetector(modelPathPrefix, inputDescriptors)
-    val outputList = objDetector.imageBatchObjectDetect(imgList, Some(1))
+    val objDetector = new ObjectDetector(modelPathPrefix, inputDescriptors, 
context)
+    // Loading batch of images from the directory path
+    val batchFiles = generateBatches(inputImageDir, 20)
+    var outputList = IndexedSeq[IndexedSeq[(String, Array[Float])]]()
+
+    for (batchFile <- batchFiles) {
+      val imgList = ImageClassifier.loadInputBatch(batchFile)
+      // Running inference on batch of images loaded in previous step
+      outputList ++= objDetector.imageBatchObjectDetect(imgList, Some(5))
+    }
     outputList
   }
 
+  def generateBatches(inputImageDirPath: String, batchSize: Int = 100): 
List[List[String]] = {
+    val dir = new File(inputImageDirPath)
+    require(dir.exists && dir.isDirectory,
+      "input image directory: %s not found".format(inputImageDirPath))
+    val output = ListBuffer[List[String]]()
+    var batch = ListBuffer[String]()
+    for (imgFile: File <- dir.listFiles()){
+      batch += imgFile.getPath
+      if (batch.length == batchSize) {
+        output += batch.toList
+        batch = ListBuffer[String]()
+      }
+    }
+    output += batch.toList
+    output.toList
+  }
+
   def main(args: Array[String]): Unit = {
     val inst = new SSDClassifierExample
     val parser : CmdLineParser = new CmdLineParser(inst)
@@ -79,6 +109,12 @@ object SSDClassifierExample {
       sys.exit(1)
     }
 
+    var context = Context.cpu()
+    if (System.getenv().containsKey("SCALA_TEST_ON_GPU") &&
+      System.getenv("SCALA_TEST_ON_GPU").toInt == 1) {
+      context = Context.gpu()
+    }
+
     try {
       val inputShape = Shape(1, 3, 512, 512)
       val outputShape = Shape(1, 6132, 6)
@@ -87,7 +123,7 @@ object SSDClassifierExample {
       val height = inputShape(3)
       var outputStr : String = "\n"
 
-      val output = runObjectDetectionSingle(mdprefixDir, imgPath)
+      val output = runObjectDetectionSingle(mdprefixDir, imgPath, context)
 
 
       for (ele <- output) {
@@ -104,7 +140,7 @@ object SSDClassifierExample {
       }
       logger.info(outputStr)
 
-      val outputList = runObjectDetectionBatch(mdprefixDir, imgDir)
+      val outputList = runObjectDetectionBatch(mdprefixDir, imgDir, context)
 
       outputStr = "\n"
       for (idx <- outputList.indices) {
diff --git 
a/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExampleSuite.scala
 
b/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExampleSuite.scala
index 18608b0..da0cb2e 100644
--- 
a/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExampleSuite.scala
+++ 
b/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/imageclassifier/ImageClassifierExampleSuite.scala
@@ -19,8 +19,10 @@ package ml.dmlc.mxnetexamples.inferexample.imageclassifier
 
 import org.scalatest.{BeforeAndAfterAll, FunSuite}
 import org.slf4j.LoggerFactory
-
 import java.io.File
+
+import ml.dmlc.mxnet.Context
+
 import sys.process.Process
 
 /**
@@ -54,13 +56,19 @@ class ImageClassifierExampleSuite extends FunSuite with 
BeforeAndAfterAll {
       "inputImages/Pug-Cookie.jpg"
     val inputImageDir = tempDirPath + File.separator + "inputImages/"
 
+    var context = Context.cpu()
+    if (System.getenv().containsKey("SCALA_TEST_ON_GPU") &&
+      System.getenv("SCALA_TEST_ON_GPU").toInt == 1) {
+      context = Context.gpu()
+    }
+
     val output = ImageClassifierExample.runInferenceOnSingleImage(modelDirPath 
+ "resnet-18",
-      inputImagePath)
+      inputImagePath, context)
 
     assert(output(0).toList.head._1 === "n02110958 pug, pug-dog")
 
     val outputList = 
ImageClassifierExample.runInferenceOnBatchOfImage(modelDirPath + "resnet-18",
-      inputImageDir)
+      inputImageDir, context)
 
     assert(outputList(0).toList.head._1 === "n02110958 pug, pug-dog")
 
diff --git 
a/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/ObjectDetectorExampleSuite.scala
 
b/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/ObjectDetectorExampleSuite.scala
index 77f540e..fc295a7 100644
--- 
a/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/ObjectDetectorExampleSuite.scala
+++ 
b/scala-package/examples/src/test/scala/ml/dmlc/mxnetexamples/inferexample/objectdetector/ObjectDetectorExampleSuite.scala
@@ -19,6 +19,7 @@ package ml.dmlc.mxnetexamples.inferexample.objectdetector
 
 import java.io.File
 
+import ml.dmlc.mxnet.Context
 import org.scalatest.{BeforeAndAfterAll, FunSuite}
 import org.slf4j.LoggerFactory
 
@@ -58,14 +59,20 @@ class ObjectDetectorExampleSuite extends FunSuite with 
BeforeAndAfterAll {
       "inputImages/dog-ssd.jpg"
     val inputImageDir = tempDirPath + File.separator + "inputImages/"
 
+    var context = Context.cpu()
+    if (System.getenv().containsKey("SCALA_TEST_ON_GPU") &&
+      System.getenv("SCALA_TEST_ON_GPU").toInt == 1) {
+      context = Context.gpu()
+    }
+
     val output = SSDClassifierExample.runObjectDetectionSingle(modelDirPath + 
"resnet50_ssd_model",
-      inputImagePath)
+      inputImagePath, context)
 
     assert(output(0)(0)._1 === "car")
 
     val outputList = SSDClassifierExample.runObjectDetectionBatch(
       modelDirPath + "resnet50_ssd_model",
-      inputImageDir)
+      inputImageDir, context)
 
     assert(output(0)(0)._1 === "car")
 
diff --git 
a/scala-package/infer/src/main/scala/ml/dmlc/mxnet/infer/ImageClassifier.scala 
b/scala-package/infer/src/main/scala/ml/dmlc/mxnet/infer/ImageClassifier.scala
index f05b2e2..d545ffc 100644
--- 
a/scala-package/infer/src/main/scala/ml/dmlc/mxnet/infer/ImageClassifier.scala
+++ 
b/scala-package/infer/src/main/scala/ml/dmlc/mxnet/infer/ImageClassifier.scala
@@ -195,19 +195,10 @@ object ImageClassifier {
 
   /**
     * Loading input batch of images
-    * @param inputImageDirPath
-    * @return List of buffered images
+    * @param inputImagePaths
+    * @return List of buffered images-
     */
-  def loadInputBatch(inputImageDirPath: String): List[BufferedImage] = {
-    val dir = new File(inputImageDirPath)
-    require(dir.exists && dir.isDirectory,
-      "input image directory: %s not found".format(inputImageDirPath))
-
-    val inputBatch = ListBuffer[BufferedImage]()
-    for (imgFile: File <- dir.listFiles()){
-      val img = ImageIO.read(imgFile)
-      inputBatch += img
-    }
-    inputBatch.toList
+  def loadInputBatch(inputImagePaths: List[String]): 
Traversable[BufferedImage] = {
+    inputImagePaths.map(path => ImageIO.read(new File(path)))
   }
-}
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
nsw...@apache.org.

Reply via email to