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

The following examples show how to use org.nd4j.linalg.factory.Nd4j#createNpyFromByteArray() . 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: VertxArrayConversion.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a {@link Buffer}
 * to an {@link INDArray}
 * using one of three types:
 * numpy: (converts using {@link Nd4j#createNpyFromByteArray(byte[])}
 * nd4j: (converts using {@link BinarySerde#toArray(ByteBuffer)}
 * with a direct byte buffer copy (nd4j requires direct allocation
 * for byte buffers
 * json:  (converts with a straight for loop, note that this only supports matrices only)
 *
 * @param buffer the buffer to convert
 * @param type   the type of buffer
 * @return the created ndarray
 */
public static INDArray toArray(Buffer buffer, String type) {
    INDArray trueFeedback = null;
    switch (type) {
        case "numpy":
            trueFeedback = Nd4j.createNpyFromByteArray(buffer.getBytes());
            break;
        case "nd4j":
            ByteBuffer direct = ByteBuffer.allocateDirect(buffer.length());
            direct.put(buffer.getBytes());
            direct.rewind();
            trueFeedback = BinarySerde.toArray(direct);
            break;
        case "json":
            JsonArray jsonArray = new JsonArray(buffer.toString());
            INDArray arr = Nd4j.create(jsonArray.size(), jsonArray.getJsonArray(0).size());
            for (int i = 0; i < arr.rows(); i++) {
                for (int j = 0; j < arr.columns(); j++) {
                    arr.putScalar(i, j, jsonArray.getJsonArray(i).getDouble(j));
                }
            }

            trueFeedback = arr;
            break;
        default:
            throw new IllegalArgumentException("Illegal type " + type);

    }

    return trueFeedback;
}
 
Example 2
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 3
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 4
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());
}
 
Example 5
Source File: JsonSerdeUtils.java    From konduit-serving with Apache License 2.0 2 votes vote down vote up
/**
 * De serialize a base 64 numpy array.
 * @param schemaWithValues a json object in the form of:
 *                         {"values : {"fieldName": base64 string}}
 * @param fieldName the field name of the numpy array to de serialize
 * @return the de serialized numpy array using {@link Nd4j#createNpyFromByteArray(byte[])}
 */
public static INDArray deSerializeBase64Numpy(JsonObject schemaWithValues,String fieldName) {
    byte[] numpyValue  = schemaWithValues.getJsonObject("values").getBinary(fieldName);
    return Nd4j.createNpyFromByteArray(numpyValue);
}