org.datavec.image.loader.NativeImageLoader Java Examples

The following examples show how to use org.datavec.image.loader.NativeImageLoader. 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: PreProcessLocal.java    From Java-Deep-Learning-Cookbook with MIT License 6 votes vote down vote up
protected void entryPoint(String[] args) throws Exception {

        if(localSaveDir.equals("{PATH-TO-SAVE-PREPROCESSED-DATASET}")){
            System.out.println("Please specify the directory in which pre-processed dataset needs to be stored");
            System.exit(0);
        }
        
        TinyImageNetFetcher f = new TinyImageNetFetcher();
        f.downloadAndExtract();

        //Preprocess the training set
        File baseDirTrain = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/train");
        File saveDirTrain = new File(localSaveDir, "train");
        if(!saveDirTrain.exists())
            saveDirTrain.mkdirs();
        SparkDataUtils.createFileBatchesLocal(baseDirTrain, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTrain, batchSize);

        //Preprocess the test set
        File baseDirTest = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/test");
        File saveDirTest = new File(localSaveDir, "test");
        if(!saveDirTest.exists())
            saveDirTest.mkdirs();
        SparkDataUtils.createFileBatchesLocal(baseDirTest, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTest, batchSize);

        System.out.println("----- Data Preprocessing Complete -----");
    }
 
Example #2
Source File: MnistPrediction.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    if (!modelPath.exists()) {
        logger.info("The model not found. Have you trained it?");
        return;
    }
    MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelPath);
    String path = fileChose();
    File file = new File(path);

    INDArray image = new NativeImageLoader(height, width, channels).asMatrix(file);
    new ImagePreProcessingScaler(0, 1).transform(image);

    // Pass through to neural Net
    INDArray output = model.output(image);

    logger.info("File: {}", path);
    logger.info("Probabilities: {}", output);
}
 
Example #3
Source File: TestImageNet.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testImageNetLabels() throws IOException {
    // set up model
    ZooModel model = VGG19.builder().numClasses(0).build(); //num labels doesn't matter since we're getting pretrained imagenet
    ComputationGraph initializedModel = (ComputationGraph) model.initPretrained();

    // set up input and feedforward
    NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    INDArray image = loader.asMatrix(classloader.getResourceAsStream("deeplearning4j-zoo/goldenretriever.jpg"));
    DataNormalization scaler = new VGG16ImagePreProcessor();
    scaler.transform(image);
    INDArray[] output = initializedModel.output(false, image);

    // check output labels of result
    String decodedLabels = new ImageNetLabels().decodePredictions(output[0]);
    log.info(decodedLabels);
    assertTrue(decodedLabels.contains("golden_retriever"));

    // clean up for current model
    Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();
    System.gc();
}
 
Example #4
Source File: BaseImageRecordReader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException {
    this.appendLabel = conf.getBoolean(APPEND_LABEL, appendLabel);
    this.labels = new ArrayList<>(conf.getStringCollection(LABELS));
    this.height = conf.getLong(HEIGHT, height);
    this.width = conf.getLong(WIDTH, width);
    this.channels = conf.getLong(CHANNELS, channels);
    this.cropImage = conf.getBoolean(CROP_IMAGE, cropImage);
    if ("imageio".equals(conf.get(IMAGE_LOADER))) {
        this.imageLoader = new ImageLoader(height, width, channels, cropImage);
    } else {
        this.imageLoader = new NativeImageLoader(height, width, channels, imageTransform);
    }
    this.conf = conf;
    initialize(split);
}
 
Example #5
Source File: ModelUtils.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void trainModel(MultiLayerNetwork model, boolean invertColors, InputStream customImage, int customLabel) throws Exception {
    List<INDArray> extraFeatures = new LinkedList<>();
    List<Integer> extraLabels = new LinkedList<>();
    final INDArray[] customData = {null, null};
    if (customImage != null) {
        NativeImageLoader loader = new NativeImageLoader(width, height, channels);
        DataNormalization scaler = invertColors ? new ImagePreProcessingScaler(1, 0) : new ImagePreProcessingScaler(0, 1);
        customData[0] = loader.asMatrix(customImage);
        scaler.transform(customData[0]);
        customData[1] = Nd4j.create(1, 10);
        customData[1].putScalar(customLabel, 1.0);
        extraFeatures.add(customData[0]);
        extraLabels.add(customLabel);
    }
    trainModel(model, extraFeatures, extraLabels);
}
 
Example #6
Source File: BaseImageRecordReader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException {
    this.appendLabel = conf.getBoolean(APPEND_LABEL, appendLabel);
    this.labels = new ArrayList<>(conf.getStringCollection(LABELS));
    this.height = conf.getInt(HEIGHT, height);
    this.width = conf.getInt(WIDTH, width);
    this.channels = conf.getInt(CHANNELS, channels);
    this.cropImage = conf.getBoolean(CROP_IMAGE, cropImage);
    if ("imageio".equals(conf.get(IMAGE_LOADER))) {
        this.imageLoader = new ImageLoader(height, width, channels, cropImage);
    } else {
        this.imageLoader = new NativeImageLoader(height, width, channels, imageTransform);
    }
    this.conf = conf;
    initialize(split);
}
 
Example #7
Source File: YOLOModel.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
public void init() {
	try {
		if (Objects.isNull(modelPath)) {

			yoloModel = (ComputationGraph) YOLO2.builder().build().initPretrained();
			setModelClasses(COCO_CLASSES);
		} else {
			yoloModel = ModelSerializer.restoreComputationGraph(modelPath);
			if (!(yoloModel.getOutputLayer(0) instanceof Yolo2OutputLayer)) {
				throw new Error("The model is not an YOLO model (output layer is not Yolo2OutputLayer)");
			}
			setModelClasses(classes.split("\\,"));
		}
		imageLoader = new NativeImageLoader(getInputWidth(), getInputHeight(), getInputChannels(),
				new ColorConversionTransform(COLOR_BGR2RGB));
		loadInputParameters();
	} catch (IOException e) {
		throw new Error("Not able to init the model", e);
	}
}
 
Example #8
Source File: ImageClassifier.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
private INDArray imageToArray(InputStream imageIS, int height, int width, int channels) {
	NativeImageLoader loader = new NativeImageLoader(height, width, channels, true);
	INDArray imageArray = null;
	try {
		if (channels == 1) {
			imageArray = loader.asRowVector(imageIS);
		} else {
			imageArray = loader.asMatrix(imageIS);
		}
	} catch (Exception e) {
		throw new Error("Not able to convert image input stream to array.", e);
	}
	if (normalization != null) {
		normalization.transform(imageArray);
	}
	return imageArray;
}
 
Example #9
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 6 votes vote down vote up
public void initialize(String windowName,
                       boolean outputFrames,
                       double threshold,
                       String model, Strategy strategy) throws IOException {
    this.trackingThreshold = threshold;
    this.outputFrames = outputFrames;
    this.preTrainedCifarModel = model;
    this.strategy = strategy;
    stackMap.put(windowName, new Stack<>());
    ComputationGraph yolo = (ComputationGraph) YOLO2.builder().build().initPretrained();
    prepareYOLOLabels();

    trainCifar10Model.loadTrainedModel(preTrainedCifarModel);
    modelsMap.put(windowName, yolo);
    loader = new NativeImageLoader(selectedSpeed.height, selectedSpeed.width, 3);
    warmUp(yolo);
}
 
Example #10
Source File: PreprocessSpark.java    From Java-Deep-Learning-Cookbook with MIT License 6 votes vote down vote up
protected void entryPoint(String[] args) throws Exception {
    JCommander jcmdr = new JCommander(this);
    jcmdr.parse(args);
    //JCommanderUtils.parseArgs(this, args);
    SparkConf conf = new SparkConf();
    conf.setMaster("local[*]");
    conf.setAppName("DL4JTinyImageNetSparkPreproc");
    JavaSparkContext sc = new JavaSparkContext(conf);

    //Create training set
    JavaRDD<String> filePathsTrain = SparkUtils.listPaths(sc, sourceDir + "/train", true, NativeImageLoader.ALLOWED_FORMATS);
    SparkDataUtils.createFileBatchesSpark(filePathsTrain, saveDir, batchSize, sc);

    //Create test set
    JavaRDD<String> filePathsTest = SparkUtils.listPaths(sc, sourceDir + "/test", true, NativeImageLoader.ALLOWED_FORMATS);
    SparkDataUtils.createFileBatchesSpark(filePathsTest, saveDir, batchSize, sc);


    System.out.println("----- Data Preprocessing Complete -----");
}
 
Example #11
Source File: PreprocessSpark.java    From Java-Deep-Learning-Cookbook with MIT License 6 votes vote down vote up
protected void entryPoint(String[] args) throws Exception {
    JCommander jcmdr = new JCommander(this);
    jcmdr.parse(args);
    //JCommanderUtils.parseArgs(this, args);
    SparkConf conf = new SparkConf();
    conf.setMaster("local[*]");
    conf.setAppName("DL4JTinyImageNetSparkPreproc");
    JavaSparkContext sc = new JavaSparkContext(conf);

    //Create training set
    JavaRDD<String> filePathsTrain = SparkUtils.listPaths(sc, sourceDir + "/train", true, NativeImageLoader.ALLOWED_FORMATS);
    SparkDataUtils.createFileBatchesSpark(filePathsTrain, saveDir, batchSize, sc);

    //Create test set
    JavaRDD<String> filePathsTest = SparkUtils.listPaths(sc, sourceDir + "/test", true, NativeImageLoader.ALLOWED_FORMATS);
    SparkDataUtils.createFileBatchesSpark(filePathsTest, saveDir, batchSize, sc);


    System.out.println("----- Data Preprocessing Complete -----");
}
 
Example #12
Source File: PreProcessLocal.java    From Java-Deep-Learning-Cookbook with MIT License 6 votes vote down vote up
protected void entryPoint(String[] args) throws Exception {

        if(localSaveDir.equals("{PATH-TO-SAVE-PREPROCESSED-DATASET}")){
            System.out.println("Please specify the directory in which pre-processed dataset needs to be stored");
            System.exit(0);
        }

        TinyImageNetFetcher f = new TinyImageNetFetcher();
        f.downloadAndExtract();

        //Preprocess the training set
        File baseDirTrain = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/train");
        File saveDirTrain = new File(localSaveDir, "train");
        if(!saveDirTrain.exists())
            saveDirTrain.mkdirs();
        SparkDataUtils.createFileBatchesLocal(baseDirTrain, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTrain, batchSize);

        //Preprocess the test set
        File baseDirTest = DL4JResources.getDirectory(ResourceType.DATASET, f.localCacheName() + "/test");
        File saveDirTest = new File(localSaveDir, "test");
        if(!saveDirTest.exists())
            saveDirTest.mkdirs();
        SparkDataUtils.createFileBatchesLocal(baseDirTest, NativeImageLoader.ALLOWED_FORMATS, true, saveDirTest, batchSize);

        System.out.println("----- Data Preprocessing Complete -----");
    }
 
Example #13
Source File: ImageTransformProcessStepRunner.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
public ImageTransformProcessStepRunner(PipelineStep pipelineStep) {
    super(pipelineStep);

    this.imageLoadingStepConfig = (ImageLoadingStep) pipelineStep;

    imageLoaders = imageLoadingStepConfig.getInputNames().stream()
            .map(inputName -> {
                Long[] values = imageLoadingStepConfig.getDimensionsConfigs().getOrDefault(inputName, null);
                if (values != null) {
                    return new Pair<>(inputName, new NativeImageLoader(values[0], values[1], values[2]));
                } else {
                    return new Pair<>(inputName, new NativeImageLoader());
                }
            })
            .collect(Collectors.toMap(Pair::getKey, Pair::getValue));

    Preconditions.checkState(!imageLoaders.isEmpty(), "No image loaders specified.");
}
 
Example #14
Source File: KonduitServingLauncherWithProcessesTest.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
@Test
public void testServeForegroundWorkflow() throws IOException, InterruptedException {
    // Running in foreground
    List<String> logs = runAndTailOutput(this::serverStartLogInLine, "serve", "-id", TEST_SERVER_ID, "-c", testAndGetImageConfiguration());
    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID, "started")));
    assertThat(runAndGetOutput("logs", TEST_SERVER_ID).split(System.lineSeparator()).length, Matchers.lessThan(logs.size()));

    String inspectOutput = runAndGetOutput("inspect", TEST_SERVER_ID);
    InferenceConfiguration inferenceConfiguration = InferenceConfiguration.fromJson(
            inspectOutput.substring(inspectOutput.indexOf("{"))); // Finding the json string

    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID,
            String.format("%s:%s",
                    inferenceConfiguration.getServingConfig().getListenHost(),
                    inferenceConfiguration.getServingConfig().getHttpPort()),
            "started")));

    String imagePath = new ClassPathResource("/data/5_32x32.png").getFile().getAbsolutePath();

    JsonObject outputJson = new JsonObject(runAndGetOutput("predict", "-it", "IMAGE", TEST_SERVER_ID, imagePath));
    NDArrayOutput ndArrayOutput = ObjectMappers.fromJson(outputJson.getJsonObject("default").encode(), NDArrayOutput.class);

    assertEquals(new NativeImageLoader().asMatrix(imagePath), ndArrayOutput.getNdArray());
}
 
Example #15
Source File: KonduitServingLauncherWithoutProcessesTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void testServeBackgroundWorkflow() throws IOException, InterruptedException {
    String testCommand = System.getProperty("sun.java.command");
    System.setProperty("sun.java.command", KonduitServingLauncher.class.getCanonicalName());

    // Running in foreground
    String configuration = testAndGetImageConfiguration();
    assertThat(runAndGetOutput("serve", "-id", TEST_SERVER_ID, "-c", configuration, "-b"),
            Matchers.stringContainsInOrder(Arrays.asList("For server status, execute:", "konduit", "list",
                    "For logs, execute:", "konduit", "logs", TEST_SERVER_ID)));

    Thread.sleep(10000);

    String logs = runAndTailOutput(this::serverStartLogInLine, "logs", TEST_SERVER_ID, "-f");
    assertThat(runAndGetOutput("logs", TEST_SERVER_ID).split(System.lineSeparator()).length,
            Matchers.lessThanOrEqualTo(logs.split(System.lineSeparator()).length));

    String inspectOutput = runAndGetOutput("inspect", TEST_SERVER_ID);
    InferenceConfiguration inferenceConfiguration = InferenceConfiguration.fromJson(
            inspectOutput.substring(inspectOutput.indexOf("{"))); // Finding the json string

    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID,
            String.format("%s:%s",
                    inferenceConfiguration.getServingConfig().getListenHost(),
                    inferenceConfiguration.getServingConfig().getHttpPort()),
            "started")));

    String imagePath = new ClassPathResource("/data/5_32x32.png").getFile().getAbsolutePath();

    String predictionOutput = runAndGetOutput("predict", "-it", "IMAGE", TEST_SERVER_ID, imagePath);
    JsonObject outputJson = new JsonObject(predictionOutput.substring(predictionOutput.indexOf("{"), predictionOutput.lastIndexOf("}") + 1));

    NDArrayOutput ndArrayOutput = ObjectMappers.fromJson(outputJson.getJsonObject("default").encode(), NDArrayOutput.class);

    assertEquals(new NativeImageLoader().asMatrix(imagePath), ndArrayOutput.getNdArray());

    System.setProperty("sun.java.command", testCommand);
}
 
Example #16
Source File: ImageConversionUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static INDArray convert(Mat image) {
    INDArray retVal = null;
    try {
        new NativeImageLoader().asRowVector(image);
    }
    catch (IOException e) {
        throw new RuntimeException(e);
    }
    return retVal;
}
 
Example #17
Source File: HistoryProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private INDArray transform(INDArray raw) {
    long[] shape = raw.shape();

    // before accessing the raw pointer, we need to make sure that array is actual on the host side
    Nd4j.getAffinityManager().ensureLocation(raw, AffinityManager.Location.HOST);

    Mat ocvmat = new Mat((int)shape[0], (int)shape[1], CV_32FC(3), raw.data().pointer());
    Mat cvmat = new Mat(shape[0], shape[1], CV_8UC(3));
    ocvmat.convertTo(cvmat, CV_8UC(3), 255.0, 0.0);
    cvtColor(cvmat, cvmat, COLOR_RGB2GRAY);
    Mat resized = new Mat(conf.getRescaledHeight(), conf.getRescaledWidth(), CV_8UC(1));
    resize(cvmat, resized, new Size(conf.getRescaledWidth(), conf.getRescaledHeight()));
    //   show(resized);
    //   waitKP();
    //Crop by croppingHeight, croppingHeight
    Mat cropped = resized.apply(new Rect(conf.getOffsetX(), conf.getOffsetY(), conf.getCroppingWidth(),
                    conf.getCroppingHeight()));
    //System.out.println(conf.getCroppingWidth() + " " + cropped.data().asBuffer().array().length);

    INDArray out = null;
    try {
        out = new NativeImageLoader(conf.getCroppingHeight(), conf.getCroppingWidth()).asMatrix(cropped);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //System.out.println(out.shapeInfoToString());
    out = out.reshape(1, conf.getCroppingHeight(), conf.getCroppingWidth());
    INDArray compressed = out.castTo(DataType.UBYTE);
    return compressed;
}
 
Example #18
Source File: ModelUtils.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void trainModel(MultiLayerNetwork model, boolean invertColors, List<InputStream> customImage,
        List<Integer> customLabel) throws Exception {

    List<INDArray> extraFeatures = new LinkedList<>();
    List<Integer> extraLabels = new LinkedList<>();
    for (int i = 0; i < customImage.size(); i++) {
        NativeImageLoader loader = new NativeImageLoader(width, height, channels);
        DataNormalization scaler = invertColors ? new ImagePreProcessingScaler(1, 0) : new ImagePreProcessingScaler(0, 1);
        INDArray feature = loader.asMatrix(customImage.get(i));
        scaler.transform(feature);
        extraFeatures.add(feature);
        extraLabels.add(customLabel.get(i));
    }
    trainModel(model, extraFeatures, extraLabels);
}
 
Example #19
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 #20
Source File: VideoRecordReader.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException {
    this.conf = conf;
    this.appendLabel = conf.getBoolean(APPEND_LABEL, false);
    this.height = conf.getInt(HEIGHT, height);
    this.width = conf.getInt(WIDTH, width);
    if ("imageio".equals(conf.get(IMAGE_LOADER))) {
        this.imageLoader = new ImageLoader(height, width);
    } else {
        this.imageLoader = new NativeImageLoader(height, width);
    }

    initialize(split);
}
 
Example #21
Source File: Predict.java    From dl4j-tutorials with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testPath = "data/test";
    File testDir = new File(testPath);
    File[] files = testDir.listFiles();

    Pair<MultiLayerNetwork, Normalizer> modelAndNormalizer = ModelSerializer
            .restoreMultiLayerNetworkAndNormalizer(new File("model/AlexNet.zip"), false);

    NativeImageLoader imageLoader = new NativeImageLoader(256, 256, 3);

    MultiLayerNetwork network = modelAndNormalizer.getFirst();
    DataNormalization normalizer = (DataNormalization) modelAndNormalizer.getSecond();

    Map<Integer, String> map = new HashMap<>();
    map.put(0, "CITY");
    map.put(1, "DESERT");
    map.put(2, "FARMLAND");
    map.put(3, "LAKE");
    map.put(4, "MOUNTAIN");
    map.put(5, "OCEAN");

    for (File file : files) {
        INDArray indArray = imageLoader.asMatrix(file);
        normalizer.transform(indArray);

        int[] values = network.predict(indArray);
        String label = map.get(values[0]);

        System.out.println(file.getName() + "," + label);
    }
}
 
Example #22
Source File: Classifier.java    From java-ml-projects with Apache License 2.0 5 votes vote down vote up
public static void init() throws IOException {
	String modelPath = Properties.classifierModelPath(); 
	labels = Properties.classifierLabels();
	int[] format = Properties.classifierInputFormat();
	loader = new NativeImageLoader(format[0], format[1], format[2]);
	model = ModelSerializer.restoreComputationGraph(modelPath);
	model.init();
}
 
Example #23
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 #24
Source File: KonduitServingLauncherWithoutProcessesTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void testServeForegroundWorkflow() throws IOException, InterruptedException {
    String testCommand = System.getProperty("sun.java.command");
    System.setProperty("sun.java.command", KonduitServingLauncher.class.getCanonicalName());

    // Running in foreground
    String logs = runAndTailOutput(this::serverStartLogInLine, "serve", "-id", TEST_SERVER_ID, "-c", testAndGetImageConfiguration());
    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID, "started")));
    assertThat(runAndGetOutput("logs", TEST_SERVER_ID).split(System.lineSeparator()).length,
            Matchers.lessThan(logs.split(System.lineSeparator()).length));

    String inspectOutput = runAndGetOutput("inspect", TEST_SERVER_ID);
    InferenceConfiguration inferenceConfiguration = InferenceConfiguration.fromJson(
            inspectOutput.substring(inspectOutput.indexOf("{"))); // Finding the json string

    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID,
            String.format("%s:%s",
                    inferenceConfiguration.getServingConfig().getListenHost(),
                    inferenceConfiguration.getServingConfig().getHttpPort()),
            "started")));

    String imagePath = new ClassPathResource("/data/5_32x32.png").getFile().getAbsolutePath();

    String predictionOutput = runAndGetOutput("predict", "-it", "IMAGE", TEST_SERVER_ID, imagePath);
    JsonObject outputJson = new JsonObject(predictionOutput.substring(predictionOutput.indexOf("{"), predictionOutput.lastIndexOf("}") + 1));

    NDArrayOutput ndArrayOutput = ObjectMappers.fromJson(outputJson.getJsonObject("default").encode(), NDArrayOutput.class);

    assertEquals(new NativeImageLoader().asMatrix(imagePath), ndArrayOutput.getNdArray());

    System.setProperty("sun.java.command", testCommand);
}
 
Example #25
Source File: TransformRotatingImages.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private static void cropAllWithYOLOBoundingBox() throws Exception {
    ComputationGraph yolo = (ComputationGraph) YOLO2.builder().build().initPretrained();
    File file = new File(BASE_PATH);
    File[] list = file.listFiles();
    sortFiles(list);
    Speed selectedSpeed = Speed.MEDIUM;
    LOADER = new NativeImageLoader(selectedSpeed.height, selectedSpeed.width, 3);
    Stream.of(list).parallel().forEach(e -> {
        try {
            cropImageWithYOLOBoundingBox(yolo, selectedSpeed, e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    });
}
 
Example #26
Source File: KonduitServingLauncherWithProcessesTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void testServeBackgroundWorkflow() throws IOException, InterruptedException {
    Nd4j.create(10, 10);

    // Running in foreground
    String configuration = testAndGetImageConfiguration();
    assertThat(runAndGetOutput("serve", "-id", TEST_SERVER_ID, "-c", configuration, "-b"),
            Matchers.stringContainsInOrder(Arrays.asList("For server status, execute: 'konduit list'",
                    String.format("For logs, execute: 'konduit logs %s'", TEST_SERVER_ID))));

    Thread.sleep(10000);

    List<String> logs = runAndTailOutput(this::serverStartLogInLine, "logs", TEST_SERVER_ID, "-f");
    assertThat(runAndGetOutput("logs", TEST_SERVER_ID).split(System.lineSeparator()).length,
            Matchers.lessThanOrEqualTo(logs.size()));

    String inspectOutput = runAndGetOutput("inspect", TEST_SERVER_ID);
    InferenceConfiguration inferenceConfiguration = InferenceConfiguration.fromJson(
            inspectOutput.substring(inspectOutput.indexOf("{"))); // Finding the json string

    assertThat(runAndGetOutput("list"), Matchers.stringContainsInOrder(Arrays.asList(TEST_SERVER_ID,
            String.format("%s:%s",
                    inferenceConfiguration.getServingConfig().getListenHost(),
                    inferenceConfiguration.getServingConfig().getHttpPort()),
            "started")));

    String imagePath = new ClassPathResource("/data/5_32x32.png").getFile().getAbsolutePath();

    JsonObject outputJson = new JsonObject(runAndGetOutput("predict", "-it", "IMAGE", TEST_SERVER_ID, imagePath));
    NDArrayOutput ndArrayOutput = ObjectMappers.fromJson(outputJson.getJsonObject("default").encode(), NDArrayOutput.class);

    assertEquals(new NativeImageLoader().asMatrix(imagePath), ndArrayOutput.getNdArray());
}
 
Example #27
Source File: CatVsDogRecognition.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private INDArray imageFileToMatrix(File file) throws IOException {
    NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
    INDArray image = loader.asMatrix(new FileInputStream(file));
    DataNormalization dataNormalization = new VGG16ImagePreProcessor();
    dataNormalization.transform(image);
    return image;
}
 
Example #28
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private INDArray prepareImage(BufferedImage convert, int width, int height) throws IOException {
    NativeImageLoader loader = new NativeImageLoader(height, width, 3);
    ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);

    INDArray indArray = loader.asMatrix(convert);
    if (indArray == null) {
        return null;
    }
    imagePreProcessingScaler.transform(indArray);
    return indArray;
}
 
Example #29
Source File: SolverDL4j.java    From twse-captcha-solver-dl4j with MIT License 5 votes vote down vote up
/**
 * Describe <code>loadImage</code> method here.
 *
 * @param path a <code>File</code> value
 * @return an <code>INDArray</code> value
 * @exception IOException if an error occurs
 */
private INDArray loadImage(File path) throws IOException {
  int height = 60;
  int width = 200;
  int channels = 1;

  // height, width, channels
  NativeImageLoader loader = new NativeImageLoader(height, width, channels);
  INDArray image = loader.asMatrix(path);

  DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
  scaler.transform(image);

  return image;
}
 
Example #30
Source File: Vgg16DeepLearning4jClassifier.java    From vision4j-collection with MIT License 5 votes vote down vote up
private void init(File computationGraph) throws IOException {
    this.vgg16 = ModelSerializer.restoreComputationGraph(computationGraph);
    this.scaler = new VGG16ImagePreProcessor();
    this.imageSize = new ImageSize(224, 224, 3);
    this.imageLoader = new NativeImageLoader(imageSize.getHeight(), imageSize.getWidth(), imageSize.channels());

    ArrayList<String> labels = ImageNetLabels.getLabels();
    String[] categoriesArray = Constants.IMAGENET_CATEGORIES;
    this.categories = new Categories(IntStream.range(0, categoriesArray.length)
            .mapToObj(i -> new Category(categoriesArray[i], i))
            .collect(Collectors.toList()));
}