I use java runtime API to run the ssd gluoncv code(corresponding to deploy_ssd_gluoncv.py), but i seem to get wrong result, the following code is my main func:
public class Demo {
public static void main(String[] args) throws IOException {
String jsonPath = "python/data/graph_ssd.json";
String paramsPath = "python/data/params_ssd.params";
String libPath = "python/data/lib_ssd.so";
String imagePath = "python/data/street_small_resizeOut.jpg";
Module libSSD = Module.load(libPath);
String jsonSSD = Demo.readJsonFile(jsonPath);
byte[] paramsSSD = Demo.toByteArray(paramsPath);
TVMContext ctx = TVMContext.cpu(0);
long[] shape = new long[]{1,3,512,512};
NDArray intputImg = NDArray.empty(shape, ctx);
float[] rgbArray = Demo.getPixels(imagePath);
intputImg.copyFrom(rgbArray);
GraphModule module = GraphRuntime.create(jsonSSD, libSSD, ctx);
module.loadParams(paramsSSD);
module.setInput("data", intputImg);
module.run();
long[] classIDShape = new long[]{1,100,1,};
long[] scoreShape = new long[]{1,100,1};
long[] boxShape = new long[]{1,100,4};
NDArray outputScore = NDArray.empty(scoreShape, ctx);
float[] score = module.getOutput(1, outputScore).asFloatArray();
for (int i=0; i < score.length; i++)
{
System.out.println(score[i]);
}
module.release();
}
another code:
private static String readJsonFile(String fileName) {
String jsonStr;
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static float[] getPixels(String filePath)
{
try {
BufferedImage img = ImageIO.read(new File(filePath));
byte[] pixels = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();
float[] ret = new float[pixels.length];
for(int i=0; i< pixels.length; i++)
{
ret[i] = (float)pixels[i];
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static byte[] toByteArray(String filename) throws IOException {
File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}
finally, i got the score:
can someone solve this?