org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler Java Examples

The following examples show how to use org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler. 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: ImageInstanceIterator.java    From wekaDeeplearning4j with GNU General Public License v3.0 7 votes vote down vote up
/**
 * This method returns the iterator. Scales all intensity values: it divides them by 255.
 *
 * @param data the dataset to use
 * @param seed the seed for the random number generator
 * @param batchSize the batch size to use
 * @return the iterator
 */
@Override
public DataSetIterator getDataSetIterator(Instances data, int seed, int batchSize)
    throws Exception {

  batchSize = Math.min(data.numInstances(), batchSize);
  validate(data);
  ImageRecordReader reader = getImageRecordReader(data);

  // Required for supporting channels-last models (currently only EfficientNet)
  if (getChannelsLast())
    reader.setNchw_channels_first(false);

  final int labelIndex = 1; // Use explicit label index position
  final int numPossibleLabels = data.numClasses();
  DataSetIterator tmpIter =
      new RecordReaderDataSetIterator(reader, batchSize, labelIndex, numPossibleLabels);
  DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
  scaler.fit(tmpIter);
  tmpIter.setPreProcessor(scaler);
  return tmpIter;
}
 
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: ImageInstanceIterator.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method returns the iterator. Scales all intensity values: it divides them by 255.
 *
 * @param data the dataset to use
 * @param seed the seed for the random number generator
 * @param batchSize the batch size to use
 * @return the iterator
 */
@Override
public DataSetIterator getDataSetIterator(Instances data, int seed, int batchSize)
    throws Exception {

  batchSize = Math.min(data.numInstances(), batchSize);
  validate(data);
  ImageRecordReader reader = getImageRecordReader(data);

  // Required for supporting channels-last models (currently only EfficientNet)
  if (getChannelsLast())
    reader.setNchw_channels_first(false);

  final int labelIndex = 1; // Use explicit label index position
  final int numPossibleLabels = data.numClasses();
  DataSetIterator tmpIter =
      new RecordReaderDataSetIterator(reader, batchSize, labelIndex, numPossibleLabels);
  DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
  scaler.fit(tmpIter);
  tmpIter.setPreProcessor(scaler);
  return tmpIter;
}
 
Example #4
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 #5
Source File: ImageClassifierAPI.java    From Java-Deep-Learning-Cookbook with MIT License 5 votes vote down vote up
public static INDArray generateOutput(File inputFile, String modelFileLocation) throws IOException, InterruptedException {
    //retrieve the saved model
    final File modelFile = new File(modelFileLocation);
    final MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelFile);
    final RecordReader imageRecordReader = generateReader(inputFile);
    final ImagePreProcessingScaler normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile);
    final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(imageRecordReader,1).build();
    normalizerStandardize.fit(dataSetIterator);
    dataSetIterator.setPreProcessor(normalizerStandardize);
    return model.output(dataSetIterator);
}
 
Example #6
Source File: ImagePreProcessingSerializerStrategy.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public ImagePreProcessingScaler restore(InputStream stream) throws IOException {
    DataInputStream dataOutputStream = new DataInputStream(stream);
    double minRange = dataOutputStream.readDouble();
    double maxRange = dataOutputStream.readDouble();
    double maxPixelVal = dataOutputStream.readDouble();
    ImagePreProcessingScaler ret =  new ImagePreProcessingScaler(minRange,maxRange);
    ret.setMaxPixelVal(maxPixelVal);
    return ret;
}
 
Example #7
Source File: ImagePreProcessingSerializerStrategy.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ImagePreProcessingScaler normalizer, OutputStream stream) throws IOException {
    try(DataOutputStream dataOutputStream = new DataOutputStream(stream)) {
        dataOutputStream.writeDouble(normalizer.getMinRange());
        dataOutputStream.writeDouble(normalizer.getMaxRange());
        dataOutputStream.writeDouble(normalizer.getMaxPixelVal());
        dataOutputStream.flush();
    }
}
 
Example #8
Source File: ImagePreProcessortTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSegmentation(){

    INDArray f = Nd4j.math().floor(Nd4j.rand(DataType.FLOAT, 3, 3, 16, 16).muli(255));
    INDArray l = Nd4j.math().floor(Nd4j.rand(DataType.FLOAT, 3, 10, 8, 8).muli(255));

    ImagePreProcessingScaler s = new ImagePreProcessingScaler();
    s.fitLabel(true);

    s.fit(new DataSet(f,l));

    INDArray expF = f.div(255);
    INDArray expL = l.div(255);

    DataSet d = new DataSet(f.dup(), l.dup());
    s.transform(d);
    assertEquals(expF, d.getFeatures());
    assertEquals(expL, d.getLabels());


    s.fit(new SingletonDataSetIterator(new DataSet(f, l)));

    INDArray f2 = f.dup();
    INDArray l2 = l.dup();
    s.transform(f2);
    s.transformLabel(l2);
    assertEquals(expF, f2);
    assertEquals(expL, l2);

    s.revertFeatures(f2);
    s.revertLabels(l2);
    assertEquals(f, f2);
    assertEquals(l, l2);
}
 
Example #9
Source File: ModelUtils.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String predict(MultiLayerNetwork model, INDArray nd, boolean invertColors) {
        // invert black-white 
        DataNormalization scaler = new ImagePreProcessingScaler(invertColors? 1 : 0, 
        invertColors ? 0 :1);
        scaler.transform(nd);
        preprocess(nd);
//      System.out.println("I have to predict "+nd);
        int p = model.predict(nd)[0];
        System.out.println("prediction = "+model.predict(nd)[0]);
        return String.valueOf(p);
    }
 
Example #10
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 #11
Source File: MnistTestFXApp.java    From java-ml-projects with Apache License 2.0 5 votes vote down vote up
private void predictImage(BufferedImage img ) throws IOException {
	ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);
	INDArray image = loader.asRowVector(img);
	imagePreProcessingScaler.transform(image);
	INDArray output = model.output(image);
	String putStr = output.toString();
	lblResult.setText("Prediction: " + model.predict(image)[0] + "\n " + putStr);
}
 
Example #12
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 #13
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 #14
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private INDArray prepareImage(INDArray indArray) throws IOException {

        if (indArray == null) {
            return null;
        }
        ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);
        imagePreProcessingScaler.transform(indArray);
        return indArray;
    }
 
Example #15
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private INDArray prepareImage(Mat frame) throws IOException {
    if (frame == null) {
        return null;
    }
    ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);

    INDArray indArray = loader.asMatrix(frame);
    if (indArray == null) {
        return null;
    }
    imagePreProcessingScaler.transform(indArray);
    return indArray;
}
 
Example #16
Source File: ImageClassifierAPI.java    From Java-Deep-Learning-Cookbook with MIT License 5 votes vote down vote up
public static INDArray generateOutput(File inputFile, String modelFileLocation) throws IOException, InterruptedException {
    //retrieve the saved model
    final File modelFile = new File(modelFileLocation);
    final MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelFile);
    final RecordReader imageRecordReader = generateReader(inputFile);
    final ImagePreProcessingScaler normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile);
    final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(imageRecordReader,1).build();
    normalizerStandardize.fit(dataSetIterator);
    dataSetIterator.setPreProcessor(normalizerStandardize);
    return model.output(dataSetIterator);
}
 
Example #17
Source File: ModelUtils.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Thread train() {
    Thread t = new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    List<TrainRequest> toProcess = new LinkedList<>();
                    synchronized (trainRequests) {
                        if (trainRequests.isEmpty()) {
                            System.out.println("Waiting for train requests...");
                            trainRequests.wait();
                            System.out.println("Got train requests...");

                        }
                        toProcess.addAll(trainRequests);
                        trainRequests.clear();
                    }
                    List<INDArray> features = new ArrayList<>(toProcess.size());
                    List<Integer> labels = new ArrayList<>(toProcess.size());
                    for (TrainRequest request : toProcess) {
                        NativeImageLoader loader = new NativeImageLoader(width, height, channels);
                        DataNormalization scaler = request.invert ? new ImagePreProcessingScaler(1, 0) : new ImagePreProcessingScaler(0, 1);
                        INDArray f = loader.asMatrix(new ByteArrayInputStream(request.b));
                        scaler.transform(f);
                        features.add(f);
                        labels.add(request.label);
                    }
                    MultiLayerNetwork result = trainModel(latestModel, features, labels);
                    if (callback != null) {
                        // this is synchronous!
                        System.out.println("invoking callback, Synchronous call!");
                        callback.accept(result);
                        System.out.println("invoked callback,");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
    return t;
}
 
Example #18
Source File: YOLOModel.java    From java-ml-projects with Apache License 2.0 4 votes vote down vote up
private INDArray loadImage(File imgFile) throws IOException {
	INDArray image = imageLoader.asMatrix(imgFile);
	ImagePreProcessingScaler scaler = new ImagePreProcessingScaler(0, 1);
	scaler.transform(image);
	return image;
}
 
Example #19
Source File: ModelUtils.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void evaluateModel(MultiLayerNetwork model, boolean invertColors) throws IOException {
        LOGGER.info("******EVALUATE MODEL******");

        ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();
        ImageRecordReader recordReader = new ImageRecordReader(height,width,channels,labelMaker);
//        recordReader.setListeners(new LogRecordListener());

        // Initialize the record reader
        // add a listener, to extract the name

        File testData = new File(DATA_PATH + "/mnist_png/testing");
        FileSplit test = new FileSplit(testData,NativeImageLoader.ALLOWED_FORMATS,randNumGen);

        // The model trained on the training dataset split
        // now that it has trained we evaluate against the
        // test data of images the network has not seen

        recordReader.initialize(test);
        DataNormalization scaler = new ImagePreProcessingScaler(invertColors ? 1 : 0, invertColors ? 0 : 1);
        DataSetIterator testIter = new RecordReaderDataSetIterator(recordReader,batchSize,1,outputNum);
        scaler.fit(testIter);
        testIter.setPreProcessor(scaler);

        /*
        log the order of the labels for later use
        In previous versions the label order was consistent, but random
        In current verions label order is lexicographic
        preserving the RecordReader Labels order is no
        longer needed left in for demonstration
        purposes
        */
        LOGGER.info(recordReader.getLabels().toString());

        // Create Eval object with 10 possible classes
        Evaluation eval = new Evaluation(outputNum);


        // Evaluate the network
        while (testIter.hasNext()) {
            DataSet next = testIter.next();
            INDArray output = model.output(next.getFeatureMatrix());
            // Compare the Feature Matrix from the model
            // with the labels from the RecordReader
            eval.eval(next.getLabels(), output);

        }

        LOGGER.info(eval.stats());
    }