org.datavec.image.transform.ImageTransformProcess Java Examples

The following examples show how to use org.datavec.image.transform.ImageTransformProcess. 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: 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 #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: 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 #5
Source File: ImageTransformProcessStepRunner.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private INDArray applyTransform(ImageTransformProcess imageTransformProcess, NativeImageLoader nativeImageLoader, INDArray input) throws IOException {
    if (imageTransformProcess != null) {
        return imageTransformProcess.executeArray(new ImageWritable(nativeImageLoader.asFrame(input)));
    } else {
        return input;
    }
}
 
Example #6
Source File: ImageSparkTransformServer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void runMain(String[] args) throws Exception {
    JCommander jcmdr = new JCommander(this);

    try {
        jcmdr.parse(args);
    } catch (ParameterException e) {
        //User provides invalid input -> print the usage info
        jcmdr.usage();
        if (jsonPath == null)
            System.err.println("Json path parameter is missing.");
        try {
            Thread.sleep(500);
        } catch (Exception e2) {
        }
        System.exit(1);
    }

    if (jsonPath != null) {
        String json = FileUtils.readFileToString(new File(jsonPath));
        ImageTransformProcess transformProcess = ImageTransformProcess.fromJson(json);
        transform = new ImageSparkTransform(transformProcess);
    } else {
        log.warn("Server started with no json for transform process. Please ensure you specify a transform process via sending a post request with raw json"
                + "to /transformprocess");
    }

    server = Server.forRouter(Mode.PROD, port, this::createRouter);
}
 
Example #7
Source File: ImageTransformProcessStepRunner.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public void processValidWritable(Writable writable, List<Writable> record, int inputIndex, Object... extraArgs) {
    String inputName = imageLoadingStepConfig.getInputNames().get(inputIndex);

    NativeImageLoader nativeImageLoader = imageLoaders.get(inputName);

    ImageTransformProcess imageTransformProcess = null;
    if (imageLoadingStepConfig.getImageTransformProcesses() != null) {
        imageTransformProcess = imageLoadingStepConfig.getImageTransformProcesses().get(inputName);
    }

    INDArray input;

    try {
        if (writable instanceof ImageWritable) {
            input = nativeImageLoader.asMatrix(((ImageWritable) writable).getFrame());
        } else if (writable instanceof BytesWritable) {
            input = nativeImageLoader.asMatrix(((BytesWritable) writable).getContent());
        } else if (writable instanceof Text) {
            input = nativeImageLoader.asMatrix(writable.toString());
        } else if (writable instanceof NDArrayWritable) {
            input = ((NDArrayWritable) writable).get();
        } else {
            throw new IllegalArgumentException("Illegal type to load from " + writable.getClass());
        }

        INDArray output;

        if (imageLoadingStepConfig.isUpdateOrderingBeforeTransform()) {
            output = applyTransform(imageTransformProcess, nativeImageLoader, permuteImageOrder(input));
        } else {
            output = permuteImageOrder(applyTransform(imageTransformProcess, nativeImageLoader, input));
        }

        record.add(new NDArrayWritable(output));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: DataVecTransformClient.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #9
Source File: DataVecTransformClient.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #10
Source File: ImageSparkTransformServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    return transform.getImageTransformProcess();
}
 
Example #11
Source File: ImageSparkTransformServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    this.transform = new ImageSparkTransform(imageTransformProcess);
}
 
Example #12
Source File: CSVSparkTransformServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    log.error("Unsupported operation: getImageTransformProcess not supported for class", getClass());
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #13
Source File: CSVSparkTransformServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    log.error("Unsupported operation: setImageTransformProcess not supported for class", getClass());
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #14
Source File: CSVSparkTransformServer.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    log.error("Unsupported operation: setImageTransformProcess not supported for class", getClass());
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #15
Source File: DataVecTransformClient.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #16
Source File: DataVecTransformClient.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #17
Source File: ImageSparkTransformServer.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    return transform.getImageTransformProcess();
}
 
Example #18
Source File: ImageSparkTransformServer.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageTransformProcess(ImageTransformProcess imageTransformProcess) {
    this.transform = new ImageSparkTransform(imageTransformProcess);
}
 
Example #19
Source File: CSVSparkTransformServer.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public ImageTransformProcess getImageTransformProcess() {
    log.error("Unsupported operation: getImageTransformProcess not supported for class", getClass());
    throw new UnsupportedOperationException("Invalid operation for " + this.getClass());
}
 
Example #20
Source File: DataVecTransformService.java    From DataVec with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @return
 */
ImageTransformProcess getImageTransformProcess();
 
Example #21
Source File: DataVecTransformService.java    From DataVec with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param imageTransformProcess
 */
void setImageTransformProcess(ImageTransformProcess imageTransformProcess);
 
Example #22
Source File: DataVecTransformService.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param imageTransformProcess
 */
void setImageTransformProcess(ImageTransformProcess imageTransformProcess);
 
Example #23
Source File: DataVecTransformService.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @return
 */
ImageTransformProcess getImageTransformProcess();