Java Code Examples for org.nd4j.linalg.factory.Nd4j#toNpyByteArray()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#toNpyByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseMemMapTest.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
@Override
public io.vertx.core.json.JsonObject getConfigObject() throws Exception {
    INDArray arr = Nd4j.linspace(1, 4, 4);
    File tmpFile = new File(temporary.getRoot(), "tmpfile.npy");
    byte[] save = Nd4j.toNpyByteArray(arr);
    org.apache.commons.io.FileUtils.writeByteArrayToFile(tmpFile, save);
    InferenceConfiguration inferenceConfiguration =
            InferenceConfiguration.builder()
                    .servingConfig(ServingConfig.builder()
                            .httpPort(port)
                            .build())
                    .memMapConfig(MemMapConfig.builder()
                            .arrayPath(tmpFile.getAbsolutePath())
                            .build())
                    .build();

    return new JsonObject(inferenceConfiguration.toJson());
}
 
Example 2
Source File: MemMapSpecificVerticleTest.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
@Override
public JsonObject getConfigObject() throws Exception {
    File unkVectorPath = temporary.newFile();
    Nd4j.writeAsNumpy(unknownVector,unkVectorPath);
    INDArray arr = Nd4j.linspace(1,8,8).reshape(2,4);
    File tmpFile = new File(temporary.getRoot(),"tmpfile.npy");
    byte[] save = Nd4j.toNpyByteArray(arr);
    FileUtils.writeByteArrayToFile(tmpFile, save);
    InferenceConfiguration inferenceConfiguration =
            InferenceConfiguration.builder()
                    .servingConfig(ServingConfig.builder()
                            .httpPort(port)
                            .build())
                    .memMapConfig(MemMapConfig.builder()
                            .unkVectorPath(unkVectorPath.getAbsolutePath())
                            .arrayPath(tmpFile.getAbsolutePath())
                            .build())
                    .build();

    return new JsonObject(inferenceConfiguration.toJson());
}
 
Example 3
Source File: BatchNumpyInputParserTest.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void runAdd(TestContext testContext) throws Exception {
    BatchNumpyInputParserVerticle verticleRef = (BatchNumpyInputParserVerticle) verticle;
    NativeImageLoader nativeImageLoader = new NativeImageLoader();
    Image image = nativeImageLoader.asImageMatrix(new ClassPathResource("data/5.png").getFile());
    File tmpFile = temporary.newFile();
    byte[] npyContents = Nd4j.toNpyByteArray(image.getImage());
    try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
        IOUtils.write(npyContents, fileOutputStream);
        fileOutputStream.flush();
    }


    given().port(port)
            .multiPart("input1", tmpFile)
            .when().post("/")
            .then().statusCode(200);
    assertNotNull("Inputs were null. This means parsing failed.", verticleRef.getBatch());
    assertTrue(verticleRef.getBatch().length >= 1);
    assertNotNull(verticleRef.getBatch());
}
 
Example 4
Source File: SameDiffVerticleClassificationMetricsTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runAdd(TestContext testContext) throws Exception {
    INDArray x = Nd4j.create(new float[]{1.0f, 2.0f});
    INDArray y = Nd4j.create(new float[]{2.0f, 3.0f});
    byte[] xNpy = Nd4j.toNpyByteArray(x);
    byte[] yNpy = Nd4j.toNpyByteArray(y);


    File xFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(xFile, xNpy);

    File yFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(yFile, yNpy);


    Response response = given().port(port)
            .multiPart("x", xFile)
            .multiPart("y", yFile)
            .post("/nd4j/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    INDArray bodyResult = BinarySerde.toArray(ByteBuffer.wrap(response.getBody().asByteArray()));
    assertArrayEquals(new long[]{2}, bodyResult.shape());
    assertEquals(Nd4j.create(new float[]{3.0f, 5.0f}), bodyResult);

    given().port(port).get("/metrics")
            .then()
            .statusCode(200).and()
            .contentType("text/plain");

    Response response1 = given().port(port).get("/metrics")
            .andReturn();

    System.out.println(response1.asString());

}
 
Example 5
Source File: SameDiffVerticleNumpyTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runAdd(TestContext testContext) throws Exception {
    INDArray x = Nd4j.create(new float[]{1.0f, 2.0f});
    INDArray y = Nd4j.create(new float[]{2.0f, 3.0f});
    byte[] xNpy = Nd4j.toNpyByteArray(x);
    byte[] yNpy = Nd4j.toNpyByteArray(y);


    File xFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(xFile, xNpy);

    File yFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(yFile, yNpy);


    Response response = given().port(port)
            .multiPart("x", xFile)
            .multiPart("y", yFile)
            .post("/numpy/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    INDArray bodyResult = Nd4j.createNpyFromByteArray(response.getBody().asByteArray());
    assertArrayEquals(new long[]{2}, bodyResult.shape());
    assertEquals(Nd4j.create(new float[]{3.0f, 5.0f}), bodyResult);
}
 
Example 6
Source File: SameDiffVerticleNd4jTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runAdd(TestContext testContext) throws Exception {
    INDArray x = Nd4j.create(new float[]{1.0f, 2.0f});
    INDArray y = Nd4j.create(new float[]{2.0f, 3.0f});
    byte[] xNpy = Nd4j.toNpyByteArray(x);
    byte[] yNpy = Nd4j.toNpyByteArray(y);


    File xFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(xFile, xNpy);

    File yFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(yFile, yNpy);


    Response response = given().port(port)
            .multiPart("x", xFile)
            .multiPart("y", yFile)
            .post("/nd4j/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    INDArray bodyResult = BinarySerde.toArray(ByteBuffer.wrap(response.getBody().asByteArray()));
    assertArrayEquals(new long[]{2}, bodyResult.shape());
    assertEquals(Nd4j.create(new float[]{3.0f, 5.0f}), bodyResult);


}
 
Example 7
Source File: OnnxTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runSqueezenet(TestContext testContext) throws Exception {

    long inputTensorSize = 224 * 224 * 3;

    FloatPointer inputTensorValues = new FloatPointer(inputTensorSize);
    FloatIndexer idx = FloatIndexer.create(inputTensorValues);
    for (long i = 0; i < inputTensorSize; i++)
        idx.put(i, (float) i / (inputTensorSize + 1));

    DataBuffer buffer = Nd4j.createBuffer(inputTensorValues, DataType.FLOAT, inputTensorSize, idx);

    INDArray contents = Nd4j.create(buffer);

    byte[] npyContents = Nd4j.toNpyByteArray(contents);

    File inputFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(inputFile, npyContents);

    for (int i = 0; i < 5; i++) {
        Response response = given().port(port)
                .multiPart("data_0", inputFile)
                .post("nd4j/numpy")
                .andReturn();
        //TODO: report memory leak in DNNL execution provider to ORT
        assertEquals("Response failed", 200, response.getStatusCode());

        INDArray bodyResult = Nd4j.createNpyFromByteArray(response.getBody().asByteArray());

        assertEquals(1.99018, bodyResult.getFloat(0), 1e-4);

        assertArrayEquals(new long[]{1, 1000}, bodyResult.shape());
    }
}
 
Example 8
Source File: OnnxMultipleOutputsTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runFaceDetector(TestContext testContext) throws Exception {
    File imageFile = Paths.get(new ClassPathResource(".").getFile().getAbsolutePath(), "inference/onnx/data/1.jpg").toFile();

    if (!imageFile.exists()) {
        FileUtils.copyURLToFile(new URL("https://github.com/KonduitAI/konduit-serving-examples/raw/master/data/facedetector/1.jpg"), imageFile);
    }
    NativeImageLoader nativeImageLoader = new NativeImageLoader(240, 320);
    Image image = nativeImageLoader.asImageMatrix(imageFile);

    INDArray contents = image.getImage();

    byte[] npyContents = Nd4j.toNpyByteArray(contents);

    File inputFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(inputFile, npyContents);

    Response response = given().port(port)
            .multiPart("input", inputFile)
            .body(npyContents)
            .post("nd4j/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    JsonObject output = new JsonObject(response.asString());

    assertTrue(output.containsKey("scores"));
    assertTrue(output.containsKey("boxes"));

    INDArray scores = ObjectMappers.fromJson(output.getJsonObject("scores").encode(), NDArrayOutput.class).getNdArray();
    assertEquals(0.9539676, scores.getFloat(0), 1e-6);
    assertArrayEquals(new long[]{1, 8840}, scores.shape());

    INDArray boxes = ObjectMappers.fromJson(output.getJsonObject("boxes").encode(), NDArrayOutput.class).getNdArray();
    assertEquals(0.002913665, boxes.getFloat(0), 1e-6);
    assertArrayEquals(new long[]{1, 17680}, boxes.shape());
}
 
Example 9
Source File: JsonSerdeUtilsTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeSerializeBase64Numpy() {
    INDArray numpyBinary = Nd4j.scalar(1.0);
    byte[] npy = Nd4j.toNpyByteArray(numpyBinary);
    JsonObject value = new JsonObject();
    value.put("npy", npy);
    JsonObject valuesWrapper = new JsonObject();
    valuesWrapper.put("values", value);
    // Run the test
    final INDArray result = JsonSerdeUtils.deSerializeBase64Numpy(valuesWrapper, "npy");
    assertEquals(numpyBinary, result);
}
 
Example 10
Source File: NumpyFormatTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testToNpyFormat() throws Exception {

        val dir = testDir.newFolder();
        new ClassPathResource("numpy_arrays/").copyDirectory(dir);

        File[] files = dir.listFiles();
        int cnt = 0;

        for(File f : files){
            if(!f.getPath().endsWith(".npy")){
                log.warn("Skipping: {}", f);
                continue;
            }

            String path = f.getAbsolutePath();
            int lastDot = path.lastIndexOf('.');
            int lastUnderscore = path.lastIndexOf('_');
            String dtype = path.substring(lastUnderscore+1, lastDot);
//            System.out.println(path + " : " + dtype);

            DataType dt = DataType.fromNumpy(dtype);
            //System.out.println(dt);

            INDArray arr = Nd4j.arange(12).castTo(dt).reshape(3,4);
            byte[] bytes = Nd4j.toNpyByteArray(arr);
            byte[] expected = FileUtils.readFileToByteArray(f);
/*
            log.info("E: {}", Arrays.toString(expected));
            for( int i=0; i<expected.length; i++ ){
                System.out.print((char)expected[i]);
            }

            System.out.println();System.out.println();

            log.info("A: {}", Arrays.toString(bytes));
            for( int i=0; i<bytes.length; i++ ){
                System.out.print((char)bytes[i]);
            }
            System.out.println();
*/

            assertArrayEquals("Failed with file [" + f.getName() + "]", expected, bytes);
            cnt++;
        }

        assertTrue(cnt > 0);
    }
 
Example 11
Source File: NumpyFormatTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testToNpyFormatScalars() throws Exception {
//        File dir = new File("C:\\DL4J\\Git\\dl4j-test-resources\\src\\main\\resources\\numpy_arrays\\scalar");

        val dir = testDir.newFolder();
        new ClassPathResource("numpy_arrays/scalar/").copyDirectory(dir);

        File[] files = dir.listFiles();
        int cnt = 0;

        for(File f : files){
            if(!f.getPath().endsWith(".npy")){
                log.warn("Skipping: {}", f);
                continue;
            }

            String path = f.getAbsolutePath();
            int lastDot = path.lastIndexOf('.');
            int lastUnderscore = path.lastIndexOf('_');
            String dtype = path.substring(lastUnderscore+1, lastDot);
//            System.out.println(path + " : " + dtype);

            DataType dt = DataType.fromNumpy(dtype);
            //System.out.println(dt);

            INDArray arr = Nd4j.scalar(dt, 1);
            byte[] bytes = Nd4j.toNpyByteArray(arr);
            byte[] expected = FileUtils.readFileToByteArray(f);

            /*
            log.info("E: {}", Arrays.toString(expected));
            for( int i=0; i<expected.length; i++ ){
                System.out.print((char)expected[i]);
            }

            System.out.println();System.out.println();

            log.info("A: {}", Arrays.toString(bytes));
            for( int i=0; i<bytes.length; i++ ){
                System.out.print((char)bytes[i]);
            }
            System.out.println();
            */

            assertArrayEquals("Failed with file [" + f.getName() + "]", expected, bytes);
            cnt++;

            System.out.println();
        }

        assertTrue(cnt > 0);
    }
 
Example 12
Source File: OnnxMultipleInputsTest.java    From konduit-serving with Apache License 2.0 3 votes vote down vote up
@Test
public void runAdd(TestContext testContext) throws Exception {

    long inputTensorSize = 3 * 4 * 5;

    FloatPointer inputTensorValues = new FloatPointer(inputTensorSize);
    FloatIndexer idx = FloatIndexer.create(inputTensorValues);
    for (long i = 0; i < inputTensorSize; i++)
        idx.put(i, (float) i / (inputTensorSize + 1));

    DataBuffer buffer = Nd4j.createBuffer(inputTensorValues, DataType.FLOAT, inputTensorSize, idx);

    INDArray contents = Nd4j.create(buffer);

    byte[] npyContents = Nd4j.toNpyByteArray(contents);

    File inputFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(inputFile, npyContents);

    Response response = given().port(port)
            .multiPart("x", inputFile)
            .multiPart("y", inputFile)
            .post("nd4j/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    INDArray bodyResult = Nd4j.createNpyFromByteArray(response.getBody().asByteArray());

    assertEquals(0.032786883, bodyResult.getFloat(1), 1e-6);

    assertArrayEquals(new long[]{1, 60}, bodyResult.shape());
}