org.nd4j.serde.base64.Nd4jBase64 Java Examples

The following examples show how to use org.nd4j.serde.base64.Nd4jBase64. 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: CSVSparkTransformTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testTransformer() throws Exception {
        List<Writable> input = new ArrayList<>();
        input.add(new DoubleWritable(1.0));
        input.add(new DoubleWritable(2.0));

        Schema schema = new Schema.Builder().addColumnDouble("1.0").addColumnDouble("2.0").build();
        List<Writable> output = new ArrayList<>();
        output.add(new Text("1.0"));
        output.add(new Text("2.0"));

        TransformProcess transformProcess =
                new TransformProcess.Builder(schema).convertToString("1.0").convertToString("2.0").build();
        CSVSparkTransform csvSparkTransform = new CSVSparkTransform(transformProcess);
        String[] values = new String[] {"1.0", "2.0"};
        SingleCSVRecord record = csvSparkTransform.transform(new SingleCSVRecord(values));
        Base64NDArrayBody body = csvSparkTransform.toArray(new SingleCSVRecord(values));
        INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
        assertTrue(fromBase64.isVector());
//        System.out.println("Base 64ed array " + fromBase64);
    }
 
Example #2
Source File: ImageSparkTransformTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testBatchImageSparkTransform() throws Exception {
        int seed = 12345;

        File f0 = new ClassPathResource("datavec-spark-inference/testimages/class1/A.jpg").getFile();
        File f1 = new ClassPathResource("datavec-spark-inference/testimages/class1/B.png").getFile();
        File f2 = new ClassPathResource("datavec-spark-inference/testimages/class1/C.jpg").getFile();

        BatchImageRecord batch = new BatchImageRecord();
        batch.add(f0.toURI());
        batch.add(f1.toURI());
        batch.add(f2.toURI());

        ImageTransformProcess imgTransformProcess = new ImageTransformProcess.Builder().seed(seed)
                        .scaleImageTransform(10).cropImageTransform(5).build();

        ImageSparkTransform imgSparkTransform = new ImageSparkTransform(imgTransformProcess);
        Base64NDArrayBody body = imgSparkTransform.toArray(batch);

        INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
//        System.out.println("Base 64ed array " + fromBase64);
        assertEquals(3, fromBase64.size(0));
    }
 
Example #3
Source File: ImageSparkTransformTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testSingleImageSparkTransform() throws Exception {
        int seed = 12345;

        File f1 = new ClassPathResource("datavec-spark-inference/testimages/class1/A.jpg").getFile();

        SingleImageRecord imgRecord = new SingleImageRecord(f1.toURI());

        ImageTransformProcess imgTransformProcess = new ImageTransformProcess.Builder().seed(seed)
                        .scaleImageTransform(10).cropImageTransform(5).build();

        ImageSparkTransform imgSparkTransform = new ImageSparkTransform(imgTransformProcess);
        Base64NDArrayBody body = imgSparkTransform.toArray(imgRecord);

        INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
//        System.out.println("Base 64ed array " + fromBase64);
        assertEquals(1, fromBase64.size(0));
    }
 
Example #4
Source File: CSVSparkTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param singleCsvRecord
 * @return
 */
public Base64NDArrayBody transformSequenceArrayIncremental(BatchCSVRecord singleCsvRecord) {
    List<List<List<Writable>>> converted =  executeToSequence(toArrowWritables(toArrowColumnsString(
            bufferAllocator,transformProcess.getInitialSchema(),
            singleCsvRecord.getRecordsAsString()),
            transformProcess.getInitialSchema()),transformProcess);
    ArrowWritableRecordTimeSeriesBatch arrowWritableRecordBatch = (ArrowWritableRecordTimeSeriesBatch) converted;
    INDArray arr = RecordConverter.toTensor(arrowWritableRecordBatch);
    try {
        return new Base64NDArrayBody(Nd4jBase64.base64String(arr));
    } catch (IOException e) {
        log.error("",e);
    }

    return null;
}
 
Example #5
Source File: CSVSparkTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param singleCsvRecord
 * @return
 */
public Base64NDArrayBody transformSequenceArrayIncremental(BatchCSVRecord singleCsvRecord) {
    List<List<List<Writable>>> converted =  executeToSequence(toArrowWritables(toArrowColumnsString(
            bufferAllocator,transformProcess.getInitialSchema(),
            singleCsvRecord.getRecordsAsString()),
            transformProcess.getInitialSchema()),transformProcess);
    ArrowWritableRecordTimeSeriesBatch arrowWritableRecordBatch = (ArrowWritableRecordTimeSeriesBatch) converted;
    INDArray arr = RecordConverter.toTensor(arrowWritableRecordBatch);
    try {
        return new Base64NDArrayBody(Nd4jBase64.base64String(arr));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
 
Example #6
Source File: ImageSparkTransformTest.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleImageSparkTransform() throws Exception {
    int seed = 12345;

    File f1 = new ClassPathResource("/testimages/class1/A.jpg").getFile();

    SingleImageRecord imgRecord = new SingleImageRecord(f1.toURI());

    ImageTransformProcess imgTransformProcess = new ImageTransformProcess.Builder().seed(seed)
                    .scaleImageTransform(10).cropImageTransform(5).build();

    ImageSparkTransform imgSparkTransform = new ImageSparkTransform(imgTransformProcess);
    Base64NDArrayBody body = imgSparkTransform.toArray(imgRecord);

    INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
    System.out.println("Base 64ed array " + fromBase64);
    assertEquals(1, fromBase64.size(0));
}
 
Example #7
Source File: ImageSparkTransformTest.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatchImageSparkTransform() throws Exception {
    int seed = 12345;

    File f0 = new ClassPathResource("/testimages/class1/A.jpg").getFile();
    File f1 = new ClassPathResource("/testimages/class1/B.png").getFile();
    File f2 = new ClassPathResource("/testimages/class1/C.jpg").getFile();

    BatchImageRecord batch = new BatchImageRecord();
    batch.add(f0.toURI());
    batch.add(f1.toURI());
    batch.add(f2.toURI());

    ImageTransformProcess imgTransformProcess = new ImageTransformProcess.Builder().seed(seed)
                    .scaleImageTransform(10).cropImageTransform(5).build();

    ImageSparkTransform imgSparkTransform = new ImageSparkTransform(imgTransformProcess);
    Base64NDArrayBody body = imgSparkTransform.toArray(batch);

    INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
    System.out.println("Base 64ed array " + fromBase64);
    assertEquals(3, fromBase64.size(0));
}
 
Example #8
Source File: CSVSparkTransformTest.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformer() throws Exception {
    List<Writable> input = new ArrayList<>();
    input.add(new DoubleWritable(1.0));
    input.add(new DoubleWritable(2.0));

    Schema schema = new Schema.Builder().addColumnDouble("1.0").addColumnDouble("2.0").build();
    List<Writable> output = new ArrayList<>();
    output.add(new Text("1.0"));
    output.add(new Text("2.0"));

    TransformProcess transformProcess =
            new TransformProcess.Builder(schema).convertToString("1.0").convertToString("2.0").build();
    CSVSparkTransform csvSparkTransform = new CSVSparkTransform(transformProcess);
    String[] values = new String[] {"1.0", "2.0"};
    SingleCSVRecord record = csvSparkTransform.transform(new SingleCSVRecord(values));
    Base64NDArrayBody body = csvSparkTransform.toArray(new SingleCSVRecord(values));
    INDArray fromBase64 = Nd4jBase64.fromBase64(body.getNdarray());
    assertTrue(fromBase64.isVector());
    System.out.println("Base 64ed array " + fromBase64);
}
 
Example #9
Source File: CSVSparkTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a raw record via
 * the {@link TransformProcess}
 * to a base 64ed ndarray
 * @param record the record to convert
 * @return the base 64ed ndarray
 * @throws IOException
 */
public Base64NDArrayBody toArray(SingleCSVRecord record) throws IOException {
    List<Writable> record2 = toArrowWritablesSingle(
            toArrowColumnsStringSingle(bufferAllocator,
                    transformProcess.getInitialSchema(),record.getValues()),
            transformProcess.getInitialSchema());
    List<Writable> finalRecord = execute(Arrays.asList(record2),transformProcess).get(0);
    INDArray convert = RecordConverter.toArray(DataType.DOUBLE, finalRecord);
    return new Base64NDArrayBody(Nd4jBase64.base64String(convert));
}
 
Example #10
Source File: DataVecTransformClientTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecord() throws Exception {
    SingleCSVRecord singleCsvRecord = new SingleCSVRecord(new String[] {"0", "0"});
    SingleCSVRecord transformed = client.transformIncremental(singleCsvRecord);
    assertEquals(singleCsvRecord.getValues().size(), transformed.getValues().size());
    Base64NDArrayBody body = client.transformArrayIncremental(singleCsvRecord);
    INDArray arr = Nd4jBase64.fromBase64(body.getNdarray());
    assumeNotNull(arr);
}
 
Example #11
Source File: DataVecTransformClientTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchRecord() throws Exception {
    SingleCSVRecord singleCsvRecord = new SingleCSVRecord(new String[] {"0", "0"});

    BatchCSVRecord batchCSVRecord = new BatchCSVRecord(Arrays.asList(singleCsvRecord, singleCsvRecord));
    BatchCSVRecord batchCSVRecord1 = client.transform(batchCSVRecord);
    assertEquals(batchCSVRecord.getRecords().size(), batchCSVRecord1.getRecords().size());

    Base64NDArrayBody body = client.transformArray(batchCSVRecord);
    INDArray arr = Nd4jBase64.fromBase64(body.getNdarray());
    assumeNotNull(arr);
}
 
Example #12
Source File: ImageSparkTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public Base64NDArrayBody toArray(BatchImageRecord batch) throws IOException {
    List<INDArray> records = new ArrayList<>();

    for (SingleImageRecord imgRecord : batch.getRecords()) {
        ImageWritable record2 = imageTransformProcess.transformFileUriToInput(imgRecord.getUri());
        INDArray finalRecord = imageTransformProcess.executeArray(record2);
        records.add(finalRecord);
    }

    INDArray array = Nd4j.concat(0, records.toArray(new INDArray[records.size()]));

    return new Base64NDArrayBody(Nd4jBase64.base64String(array));
}
 
Example #13
Source File: CSVSparkTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a raw record via
 * the {@link TransformProcess}
 * to a base 64ed ndarray
 * @param batch the record to convert
 * @return teh base 64ed ndarray
 * @throws IOException
 */
public Base64NDArrayBody toArray(BatchCSVRecord batch) throws IOException {
    List<List<Writable>> converted =  execute(toArrowWritables(toArrowColumnsString(
            bufferAllocator,transformProcess.getInitialSchema(),
            batch.getRecordsAsString()),
            transformProcess.getInitialSchema()),transformProcess);

    ArrowWritableRecordBatch arrowRecordBatch = (ArrowWritableRecordBatch) converted;
    INDArray convert = ArrowConverter.toArray(arrowRecordBatch);
    return new Base64NDArrayBody(Nd4jBase64.base64String(convert));
}
 
Example #14
Source File: NDArrayDeSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String field = node.get("array").asText();
    INDArray ret = Nd4jBase64.fromBase64(field);
    return ret;
}
 
Example #15
Source File: CSVSparkTransformTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testSingleBatchSequence() throws Exception {
        List<Writable> input = new ArrayList<>();
        input.add(new DoubleWritable(1.0));
        input.add(new DoubleWritable(2.0));

        Schema schema = new Schema.Builder().addColumnDouble("1.0").addColumnDouble("2.0").build();
        List<Writable> output = new ArrayList<>();
        output.add(new Text("1.0"));
        output.add(new Text("2.0"));

        TransformProcess transformProcess =
                new TransformProcess.Builder(schema).convertToString("1.0").convertToString("2.0").build();
        CSVSparkTransform csvSparkTransform = new CSVSparkTransform(transformProcess);
        String[] values = new String[] {"1.0", "2.0"};
        SingleCSVRecord record = csvSparkTransform.transform(new SingleCSVRecord(values));
        BatchCSVRecord batchCSVRecord = new BatchCSVRecord();
        for (int i = 0; i < 3; i++)
            batchCSVRecord.add(record);
        BatchCSVRecord batchCSVRecord1 = csvSparkTransform.transform(batchCSVRecord);
        SequenceBatchCSVRecord sequenceBatchCSVRecord = new SequenceBatchCSVRecord();
        sequenceBatchCSVRecord.add(Arrays.asList(batchCSVRecord));
        Base64NDArrayBody sequenceArray = csvSparkTransform.transformSequenceArray(sequenceBatchCSVRecord);
        INDArray outputBody = Nd4jBase64.fromBase64(sequenceArray.getNdarray());


         //ensure accumulation
        sequenceBatchCSVRecord.add(Arrays.asList(batchCSVRecord));
        sequenceArray = csvSparkTransform.transformSequenceArray(sequenceBatchCSVRecord);
        assertArrayEquals(new long[]{2,2,3},Nd4jBase64.fromBase64(sequenceArray.getNdarray()).shape());

        SequenceBatchCSVRecord transformed = csvSparkTransform.transformSequence(sequenceBatchCSVRecord);
        assertNotNull(transformed.getRecords());
//        System.out.println(transformed);


    }
 
Example #16
Source File: NDArraySerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example #17
Source File: NearestNeighborsClient.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Run a k nearest neighbors search
 * on a NEW data point
 * @param k the number of results
 *          to retrieve
 * @param arr the array to run the search on.
 *            Note that this must be a row vector
 * @return
 * @throws Exception
 */
public NearestNeighborsResults knnNew(int k, INDArray arr) throws Exception {
    Base64NDArrayBody base64NDArrayBody =
                    Base64NDArrayBody.builder().k(k).ndarray(Nd4jBase64.base64String(arr)).build();

    val req = Unirest.post(url + "/knnnew");
    req.header("accept", "application/json")
            .header("Content-Type", "application/json").body(base64NDArrayBody);
    addAuthHeader(req);

    NearestNeighborsResults ret = req.asObject(NearestNeighborsResults.class).getBody();

    return ret;
}
 
Example #18
Source File: CSVSparkTransform.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a raw record via
 * the {@link TransformProcess}
 * to a base 64ed ndarray
 * @param record the record to convert
 * @return the base 64ed ndarray
 * @throws IOException
 */
public Base64NDArrayBody toArray(SingleCSVRecord record) throws IOException {
    List<Writable> record2 = toArrowWritablesSingle(
            toArrowColumnsStringSingle(bufferAllocator,
                    transformProcess.getInitialSchema(),record.getValues()),
            transformProcess.getInitialSchema());
    List<Writable> finalRecord = execute(Arrays.asList(record2),transformProcess).get(0);
    INDArray convert = RecordConverter.toArray(finalRecord);
    return new Base64NDArrayBody(Nd4jBase64.base64String(convert));
}
 
Example #19
Source File: DataVecTransformClientTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecord() throws Exception {
    SingleCSVRecord singleCsvRecord = new SingleCSVRecord(new String[] {"0", "0"});
    SingleCSVRecord transformed = client.transformIncremental(singleCsvRecord);
    assertEquals(singleCsvRecord.getValues().size(), transformed.getValues().size());
    Base64NDArrayBody body = client.transformArrayIncremental(singleCsvRecord);
    INDArray arr = Nd4jBase64.fromBase64(body.getNdarray());
    assumeNotNull(arr);
}
 
Example #20
Source File: DataVecTransformClientTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchRecord() throws Exception {
    SingleCSVRecord singleCsvRecord = new SingleCSVRecord(new String[] {"0", "0"});

    BatchCSVRecord batchCSVRecord = new BatchCSVRecord(Arrays.asList(singleCsvRecord, singleCsvRecord));
    BatchCSVRecord batchCSVRecord1 = client.transform(batchCSVRecord);
    assertEquals(batchCSVRecord.getRecords().size(), batchCSVRecord1.getRecords().size());

    Base64NDArrayBody body = client.transformArray(batchCSVRecord);
    INDArray arr = Nd4jBase64.fromBase64(body.getNdarray());
    assumeNotNull(arr);
}
 
Example #21
Source File: ImageSparkTransform.java    From DataVec with Apache License 2.0 5 votes vote down vote up
public Base64NDArrayBody toArray(BatchImageRecord batch) throws IOException {
    List<INDArray> records = new ArrayList<>();

    for (SingleImageRecord imgRecord : batch.getRecords()) {
        ImageWritable record2 = imageTransformProcess.transformFileUriToInput(imgRecord.getUri());
        INDArray finalRecord = imageTransformProcess.executeArray(record2);
        records.add(finalRecord);
    }

    long shape[] = records.get(0).shape();
    INDArray array = Nd4j.create(records, new long[] {records.size(), shape[1], shape[2], shape[3]});

    return new Base64NDArrayBody(Nd4jBase64.base64String(array));
}
 
Example #22
Source File: CSVSparkTransform.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a raw record via
 * the {@link TransformProcess}
 * to a base 64ed ndarray
 * @param batch the record to convert
 * @return teh base 64ed ndarray
 * @throws IOException
 */
public Base64NDArrayBody toArray(BatchCSVRecord batch) throws IOException {
    List<List<Writable>> converted =  execute(toArrowWritables(toArrowColumnsString(
            bufferAllocator,transformProcess.getInitialSchema(),
            batch.getRecordsAsString()),
            transformProcess.getInitialSchema()),transformProcess);

    ArrowWritableRecordBatch arrowRecordBatch = (ArrowWritableRecordBatch) converted;
    INDArray convert = ArrowConverter.toArray(arrowRecordBatch);
    return new Base64NDArrayBody(Nd4jBase64.base64String(convert));
}
 
Example #23
Source File: CSVSparkTransformTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleBatchSequence() throws Exception {
    List<Writable> input = new ArrayList<>();
    input.add(new DoubleWritable(1.0));
    input.add(new DoubleWritable(2.0));

    Schema schema = new Schema.Builder().addColumnDouble("1.0").addColumnDouble("2.0").build();
    List<Writable> output = new ArrayList<>();
    output.add(new Text("1.0"));
    output.add(new Text("2.0"));

    TransformProcess transformProcess =
            new TransformProcess.Builder(schema).convertToString("1.0").convertToString("2.0").build();
    CSVSparkTransform csvSparkTransform = new CSVSparkTransform(transformProcess);
    String[] values = new String[] {"1.0", "2.0"};
    SingleCSVRecord record = csvSparkTransform.transform(new SingleCSVRecord(values));
    BatchCSVRecord batchCSVRecord = new BatchCSVRecord();
    for (int i = 0; i < 3; i++)
        batchCSVRecord.add(record);
    BatchCSVRecord batchCSVRecord1 = csvSparkTransform.transform(batchCSVRecord);
    SequenceBatchCSVRecord sequenceBatchCSVRecord = new SequenceBatchCSVRecord();
    sequenceBatchCSVRecord.add(Arrays.asList(batchCSVRecord));
    Base64NDArrayBody sequenceArray = csvSparkTransform.transformSequenceArray(sequenceBatchCSVRecord);
    INDArray outputBody = Nd4jBase64.fromBase64(sequenceArray.getNdarray());


     //ensure accumulation
    sequenceBatchCSVRecord.add(Arrays.asList(batchCSVRecord));
    sequenceArray = csvSparkTransform.transformSequenceArray(sequenceBatchCSVRecord);
    assertArrayEquals(new long[]{2,2,3},Nd4jBase64.fromBase64(sequenceArray.getNdarray()).shape());

    SequenceBatchCSVRecord transformed = csvSparkTransform.transformSequence(sequenceBatchCSVRecord);
    assertNotNull(transformed.getRecords());
    System.out.println(transformed);


}
 
Example #24
Source File: NDArraySerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example #25
Source File: NDArrayDeSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String field = node.get("array").asText();
    INDArray ret = Nd4jBase64.fromBase64(field.toString());
    return ret;

}
 
Example #26
Source File: NDArraySerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();

}
 
Example #27
Source File: ImageSparkTransform.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Base64NDArrayBody toArray(SingleImageRecord record) throws IOException {
    ImageWritable record2 = imageTransformProcess.transformFileUriToInput(record.getUri());
    INDArray finalRecord = imageTransformProcess.executeArray(record2);

    return new Base64NDArrayBody(Nd4jBase64.base64String(finalRecord));
}
 
Example #28
Source File: ModelServerDirectInferenceExample.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
public void run() throws Exception {
    final File file = new File(inputFile);

    if (!file.exists() || !file.isFile()) {
        System.err.format("unable to access file %s\n", inputFile);
        System.exit(2);
    }

    // Open file
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

    SkilClient skilClient = new SkilClient(textAsJson);

    // Read each line
    String line = null;
    while ((line = br.readLine()) != null) {
        // Check if label indicator is up front
        String label = null;
        if (line.matches("^\\d:\\s.*")) {
            label = line.substring(0, 1);
        }

        // Just in case
        line = StringUtils.removePattern(line, "^\\d:\\s");
        String[] fields = line.split(",");

        // Maybe strip quotes
        for (int i = 0; i < fields.length; i++) {
            final String field = fields[i];
            if (field.matches("^\".*\"$")) {
                fields[i] = field.substring(1, field.length() - 1);
            }
        }

        int[] shape = (isSequential) ?
                new int[] { 1, 1, fields.length} :
                new int[] { 1, fields.length};

        INDArray array = Nd4j.create(shape);

        for (int i=0; i<fields.length; i++) {
            // TODO: catch NumberFormatException
            Double d = Double.parseDouble(fields[i]);
            int[] idx = (isSequential) ?
                    new int[]{0, 0, i} :
                    new int[]{0, i};

            array.putScalar(idx, d);
        }

        Inference.Request request = new Inference.Request(Nd4jBase64.base64String(array));
        Inference.Response.Classify response = skilClient.classify(inferenceEndpoint, request);

        System.out.format("Inference response: %s\n", response.toString());
        if (label != null) {
            System.out.format("  Label expected: %s\n", label);
        }
    }

    br.close();
}
 
Example #29
Source File: SparkTransformServerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public INDArray getNDArray(JsonNode node) throws IOException {
    return Nd4jBase64.fromBase64(node.getObject().getString("ndarray"));
}
 
Example #30
Source File: ImageSparkTransformServerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public INDArray getNDArray(JsonNode node) throws IOException {
    return Nd4jBase64.fromBase64(node.getObject().getString("ndarray"));
}