forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MXNet Java bug fixes and experience improvement (apache#14213)
* improve Java user experience * add the new examples * fixed based on the comments
- Loading branch information
1 parent
915a4ff
commit d463059
Showing
9 changed files
with
231 additions
and
25 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
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
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
131 changes: 131 additions & 0 deletions
131
scala-package/mxnet-demo/java-demo/src/main/java/mxnet/ImageClassification.java
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,131 @@ | ||
/* | ||
* 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 mxnet; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.mxnet.infer.javaapi.Predictor; | ||
import org.apache.mxnet.javaapi.*; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ImageClassification { | ||
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"); | ||
String baseUrl = "https://s3.us-east-2.amazonaws.com/scala-infer-models"; | ||
downloadUrl(baseUrl + "/resnet-18/resnet-18-symbol.json", | ||
tempDirPath + "/resnet18/resnet-18-symbol.json"); | ||
downloadUrl(baseUrl + "/resnet-18/resnet-18-0000.params", | ||
tempDirPath + "/resnet18/resnet-18-0000.params"); | ||
downloadUrl(baseUrl + "/resnet-18/synset.txt", | ||
tempDirPath + "/resnet18/synset.txt"); | ||
downloadUrl("https://s3.amazonaws.com/model-server/inputs/Pug-Cookie.jpg", | ||
tempDirPath + "/inputImages/resnet18/Pug-Cookie.jpg"); | ||
modelPath = tempDirPath + File.separator + "resnet18/resnet-18"; | ||
imagePath = tempDirPath + File.separator + | ||
"inputImages/resnet18/Pug-Cookie.jpg"; | ||
} | ||
|
||
/** | ||
* Helper class to print the maximum prediction result | ||
* @param probabilities The float array of probability | ||
* @param modelPathPrefix model Path needs to load the synset.txt | ||
*/ | ||
private static String printMaximumClass(float[] probabilities, | ||
String modelPathPrefix) throws IOException { | ||
String synsetFilePath = modelPathPrefix.substring(0, | ||
1 + modelPathPrefix.lastIndexOf(File.separator)) + "/synset.txt"; | ||
BufferedReader reader = new BufferedReader(new FileReader(synsetFilePath)); | ||
ArrayList<String> list = new ArrayList<>(); | ||
String line = reader.readLine(); | ||
|
||
while (line != null){ | ||
list.add(line); | ||
line = reader.readLine(); | ||
} | ||
reader.close(); | ||
|
||
int maxIdx = 0; | ||
for (int i = 1;i<probabilities.length;i++) { | ||
if (probabilities[i] > probabilities[maxIdx]) { | ||
maxIdx = i; | ||
} | ||
} | ||
|
||
return "Probability : " + probabilities[maxIdx] + " Class : " + list.get(maxIdx) ; | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Download the model and Image | ||
downloadModelImage(); | ||
|
||
// Prepare the model | ||
List<Context> context = new ArrayList<Context>(); | ||
context.add(Context.cpu()); | ||
List<DataDesc> inputDesc = new ArrayList<>(); | ||
Shape inputShape = new Shape(new int[]{1, 3, 224, 224}); | ||
inputDesc.add(new DataDesc("data", inputShape, DType.Float32(), "NCHW")); | ||
Predictor predictor = new Predictor(modelPath, inputDesc, context,0); | ||
|
||
// Prepare data | ||
NDArray nd = Image.imRead(imagePath, 1, true); | ||
nd = Image.imResize(nd, 224, 224, null); | ||
nd = NDArray.transpose(nd, new Shape(new int[]{2, 0, 1}), null)[0]; // HWC to CHW | ||
nd = NDArray.expand_dims(nd, 0, null)[0]; // Add N -> NCHW | ||
nd = nd.asType(DType.Float32()); // Inference with Float32 | ||
|
||
// Predict directly | ||
float[][] result = predictor.predict(new float[][]{nd.toArray()}); | ||
try { | ||
System.out.println("Predict with Float input"); | ||
System.out.println(printMaximumClass(result[0], modelPath)); | ||
} catch (IOException e) { | ||
System.err.println(e); | ||
} | ||
|
||
// predict with NDArray | ||
List<NDArray> ndList = new ArrayList<>(); | ||
ndList.add(nd); | ||
List<NDArray> ndResult = predictor.predictWithNDArray(ndList); | ||
try { | ||
System.out.println("Predict with NDArray"); | ||
System.out.println(printMaximumClass(ndResult.get(0).toArray(), modelPath)); | ||
} catch (IOException e) { | ||
System.err.println(e); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
scala-package/mxnet-demo/java-demo/src/main/java/mxnet/NDArrayCreation.java
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,47 @@ | ||
/* | ||
* 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 mxnet; | ||
|
||
import org.apache.mxnet.javaapi.*; | ||
|
||
public class NDArrayCreation { | ||
static NDArray$ NDArray = NDArray$.MODULE$; | ||
public static void main(String[] args) { | ||
|
||
// Create new NDArray | ||
NDArray nd = new NDArray(new float[]{2.0f, 3.0f}, new Shape(new int[]{1, 2}), Context.cpu()); | ||
System.out.println(nd); | ||
|
||
// create new Double NDArray | ||
NDArray ndDouble = new NDArray(new double[]{2.0d, 3.0d}, new Shape(new int[]{2, 1}), Context.cpu()); | ||
System.out.println(ndDouble); | ||
|
||
// create ones | ||
NDArray ones = NDArray.ones(Context.cpu(), new int[] {1, 2, 3}); | ||
System.out.println(ones); | ||
|
||
// random | ||
NDArray random = NDArray.random_uniform( | ||
NDArray.new random_uniformParam() | ||
.setLow(0.0f) | ||
.setHigh(2.0f) | ||
.setShape(new Shape(new int[]{10, 10})) | ||
)[0]; | ||
System.out.println(random); | ||
} | ||
} |
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