This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...e/examples/src/main/scala/org/apache/mxnetexamples/infer/predictor/PredictorExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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 org.apache.mxnetexamples.infer.predictor | ||
|
||
import java.io.File | ||
|
||
import scala.io | ||
import org.apache.mxnet._ | ||
import org.apache.mxnet.infer.Predictor | ||
import org.apache.mxnetexamples.benchmark.CLIParserBase | ||
import org.kohsuke.args4j.{CmdLineParser, Option} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
object PredictorExample { | ||
|
||
def loadModel(modelPathPrefix : String, inputDesc : IndexedSeq[DataDesc], | ||
context : Context, epoch : Int): Predictor = { | ||
new Predictor(modelPathPrefix, inputDesc, context, Some(epoch)) | ||
} | ||
|
||
def doInference(predictor : Predictor, imageND : NDArray): IndexedSeq[NDArray] = { | ||
predictor.predictWithNDArray(IndexedSeq(imageND)) | ||
} | ||
|
||
def preProcess(imagePath: String, h: Int, w: Int) : NDArray = { | ||
var img = Image.imRead(imagePath) | ||
img = Image.imResize(img, h, w) | ||
// HWC -> CHW | ||
img = NDArray.api.transpose(img, Some(Shape(2, 0, 1))) | ||
img = NDArray.api.expand_dims(img, 0) | ||
img.asType(DType.Float32) | ||
} | ||
|
||
def postProcess(modelPathPrefix : String, result : Array[Float]) : String = { | ||
val dirPath = modelPathPrefix.substring(0, 1 + modelPathPrefix.lastIndexOf(File.separator)) | ||
val d = new File(dirPath) | ||
require(d.exists && d.isDirectory, s"directory: $dirPath not found") | ||
val f = io.Source.fromFile(dirPath + "synset.txt") | ||
val s = f.getLines().toIndexedSeq | ||
val maxIdx = result.zipWithIndex.maxBy(_._1)._2 | ||
printf(s"Predict Result ${s(maxIdx)} with prob ${result(maxIdx)}\n") | ||
s(maxIdx) | ||
} | ||
|
||
def main(args : Array[String]): Unit = { | ||
val inst = new CLIParser | ||
val parser: CmdLineParser = new CmdLineParser(inst) | ||
|
||
parser.parseArgument(args.toList.asJava) | ||
|
||
var context = Context.cpu() | ||
if (System.getenv().containsKey("SCALA_TEST_ON_GPU") && | ||
System.getenv("SCALA_TEST_ON_GPU").toInt == 1) { | ||
context = Context.gpu() | ||
} | ||
|
||
val imgWidth = 224 | ||
val imgHeight = 224 | ||
|
||
val inputDesc = IndexedSeq(new DataDesc("data", Shape(1, 3, imgHeight, imgWidth), | ||
DType.Float32, Layout.NCHW)) | ||
|
||
val predictor = loadModel(inst.modelPathPrefix, inputDesc, context, 0) | ||
val img = preProcess(inst.inputImagePath, imgHeight, imgWidth) | ||
val result = doInference(predictor, img)(0).toArray | ||
postProcess(inst.modelPathPrefix, result) | ||
} | ||
|
||
} | ||
|
||
class CLIParser extends CLIParserBase{ | ||
@Option(name = "--model-path-prefix", usage = "the input model directory") | ||
val modelPathPrefix: String = "/resnet-152/resnet-152" | ||
@Option(name = "--input-image", usage = "the input image") | ||
val inputImagePath: String = "/images/kitten.jpg" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...mples/src/test/scala/org/apache/mxnetexamples/infer/predictor/PredictorExampleSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 org.apache.mxnetexamples.infer.predictor | ||
|
||
import java.io.File | ||
|
||
import org.apache.mxnet._ | ||
import org.apache.mxnetexamples.Util | ||
import org.scalatest.{BeforeAndAfterAll, FunSuite} | ||
import org.slf4j.LoggerFactory | ||
|
||
class PredictorExampleSuite extends FunSuite with BeforeAndAfterAll { | ||
private val logger = LoggerFactory.getLogger(classOf[PredictorExampleSuite]) | ||
private var modelDirPrefix = "" | ||
private var inputImagePath = "" | ||
private var context = Context.cpu() | ||
|
||
override def beforeAll(): Unit = { | ||
logger.info("Downloading resnet-18 model") | ||
|
||
val tempDirPath = System.getProperty("java.io.tmpdir") | ||
logger.info("tempDirPath: %s".format(tempDirPath)) | ||
|
||
val baseUrl = "https://s3.us-east-2.amazonaws.com/scala-infer-models" | ||
|
||
Util.downloadUrl(baseUrl + "/resnet-18/resnet-18-symbol.json", | ||
tempDirPath + "/resnet18/resnet-18-symbol.json") | ||
Util.downloadUrl(baseUrl + "/resnet-18/resnet-18-0000.params", | ||
tempDirPath + "/resnet18/resnet-18-0000.params") | ||
Util.downloadUrl(baseUrl + "/resnet-18/synset.txt", | ||
tempDirPath + "/resnet18/synset.txt") | ||
Util.downloadUrl("https://s3.amazonaws.com/model-server/inputs/Pug-Cookie.jpg", | ||
tempDirPath + "/inputImages/resnet18/Pug-Cookie.jpg") | ||
|
||
modelDirPrefix = tempDirPath + File.separator + "resnet18/resnet-18" | ||
inputImagePath = tempDirPath + File.separator + | ||
"inputImages/resnet18/Pug-Cookie.jpg" | ||
|
||
if (System.getenv().containsKey("SCALA_TEST_ON_GPU") && | ||
System.getenv("SCALA_TEST_ON_GPU").toInt == 1) { | ||
context = Context.gpu() | ||
} | ||
val props = System.getProperties | ||
props.setProperty("mxnet.disableShapeCheck", "true") | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
val props = System.getProperties | ||
props.setProperty("mxnet.disableShapeCheck", "false") | ||
} | ||
|
||
test("test Predictor With Fixed Shape and random shape") { | ||
val inputDesc = IndexedSeq(new DataDesc("data", Shape(1, 3, 224, 224), | ||
DType.Float32, Layout.NCHW)) | ||
val predictor = PredictorExample.loadModel(modelDirPrefix, inputDesc, context, 0) | ||
// fix size | ||
var img = PredictorExample.preProcess(inputImagePath, 224, 224) | ||
var result = PredictorExample.doInference(predictor, img)(0) | ||
var top1 = PredictorExample.postProcess(modelDirPrefix, result.toArray) | ||
assert(top1 === "n02110958 pug, pug-dog") | ||
// random size 512 | ||
img = PredictorExample.preProcess(inputImagePath, 512, 512) | ||
result = PredictorExample.doInference(predictor, img)(0) | ||
top1 = PredictorExample.postProcess(modelDirPrefix, result.toArray) | ||
assert(top1 === "n02110958 pug, pug-dog") | ||
// original size | ||
img = PredictorExample.preProcess(inputImagePath, 1024, 576) | ||
result = PredictorExample.doInference(predictor, img)(0) | ||
top1 = PredictorExample.postProcess(modelDirPrefix, result.toArray) | ||
assert(top1 === "n02110958 pug, pug-dog") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters