Example usage for org.deeplearning4j.nn.conf.inputs InputType convolutionalFlat

List of usage examples for org.deeplearning4j.nn.conf.inputs InputType convolutionalFlat

Introduction

In this page you can find the example usage for org.deeplearning4j.nn.conf.inputs InputType convolutionalFlat.

Prototype

public static InputType convolutionalFlat(long height, long width, long depth) 

Source Link

Document

Input type for convolutional (CNN) data, where the data is in flattened (row vector) format.

Usage

From source file:org.audiveris.omr.classifier.DeepClassifier.java

License:Open Source License

private MultiLayerNetwork createNetwork() {
    logger.info("Creating a brand new {}", getName());

    final long seed = 6;
    final double learningRate = constants.learningRate.getValue();
    final int iterations = constants.iterations.getValue();

    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder() //
            .seed(seed) //
            .iterations(iterations) //
            .regularization(true) //
            .l2(0.0005) //
            .learningRate(learningRate) // was .01 in original MNIST example
            //.biasLearningRate(0.02)
            //.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75)
            .weightInit(WeightInit.XAVIER) //
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) //
            .updater(Updater.NESTEROVS).momentum(0.9) //
            .list() //
            .layer(0, new ConvolutionLayer.Builder(5, 5) //
                    .nIn(1) //
                    .stride(1, 1) //
                    .nOut(20) //
                    .activation(Activation.IDENTITY) //
                    .build()) //
            .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) //
                    .kernelSize(2, 2) //
                    .stride(2, 2) //
                    .build()) //
            .layer(2, new ConvolutionLayer.Builder(5, 5) //
                    .stride(1, 1) //
                    .nOut(50) //
                    .activation(Activation.IDENTITY) //
                    .build()) //
            .layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) //
                    .kernelSize(2, 2) //
                    .stride(2, 2) //
                    .build()) //
            .layer(4, new DenseLayer.Builder() //
                    .nOut(500) //
                    .activation(Activation.RELU) //
                    .build()) //
            .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) //
                    .nOut(SHAPE_COUNT) //
                    .activation(Activation.SOFTMAX) //
                    .build()) //
            .setInputType(InputType.convolutionalFlat(ScaledBuffer.HEIGHT, ScaledBuffer.WIDTH, 1));

    MultiLayerConfiguration conf = builder.build();
    model = new MultiLayerNetwork(conf);
    model.init();//from w w  w.ja  va 2  s  . c o m

    return model;
}

From source file:org.audiveris.omrdataset.train.Training.java

License:Open Source License

/**
 * Perform the training of the neural network.
 * <p>//from  w  w  w . j  ava  2s .  c  om
 * Before training is launched, if the network model exists on disk it is reloaded, otherwise a
 * brand new one is created.
 *
 * @throws Exception in case of IO problem or interruption
 */
public void process() throws Exception {
    Files.createDirectories(MISTAKES_PATH);

    int nChannels = 1; // Number of input channels
    int batchSize = 64; // Batch size
    int nEpochs = 1; //3; //10; //2; // Number of training epochs
    int iterations = 1; // 2; //10; // Number of training iterations
    int seed = 123; //

    // Pixel norms
    NormalizerStandardize normalizer = NormalizerSerializer.getDefault().restore(PIXELS_PATH.toFile());

    // Get the dataset using the record reader. CSVRecordReader handles loading/parsing
    int labelIndex = CONTEXT_WIDTH * CONTEXT_HEIGHT; // format: all cells then label
    int numLinesToSkip = 1; // Because of header comment line
    String delimiter = ",";

    RecordReader trainRecordReader = new CSVRecordReader(numLinesToSkip, delimiter);
    trainRecordReader.initialize(new FileSplit(FEATURES_PATH.toFile()));
    logger.info("Getting dataset from {} ...", FEATURES_PATH);

    RecordReaderDataSetIterator trainIter = new RecordReaderDataSetIterator(trainRecordReader, batchSize,
            labelIndex, numClasses, -1);
    trainIter.setCollectMetaData(true); //Instruct the iterator to collect metadata, and store it in the DataSet objects

    RecordReader testRecordReader = new CSVRecordReader(numLinesToSkip, delimiter);
    testRecordReader.initialize(new FileSplit(FEATURES_PATH.toFile()));

    RecordReaderDataSetIterator testIter = new RecordReaderDataSetIterator(testRecordReader, batchSize,
            labelIndex, numClasses, -1);
    testIter.setCollectMetaData(true); //Instruct the iterator to collect metadata, and store it in the DataSet objects

    // Normalization
    DataSetPreProcessor preProcessor = new MyPreProcessor(normalizer);
    trainIter.setPreProcessor(preProcessor);
    testIter.setPreProcessor(preProcessor);

    if (false) {
        System.out.println("\n  +++++ Test Set Examples MetaData +++++");

        while (testIter.hasNext()) {
            DataSet ds = testIter.next();
            List<RecordMetaData> testMetaData = ds.getExampleMetaData(RecordMetaData.class);

            for (RecordMetaData recordMetaData : testMetaData) {
                System.out.println(recordMetaData.getLocation());
            }
        }

        testIter.reset();
    }

    final MultiLayerNetwork model;

    if (Files.exists(MODEL_PATH)) {
        model = ModelSerializer.restoreMultiLayerNetwork(MODEL_PATH.toFile(), false);
        logger.info("Model restored from {}", MODEL_PATH.toAbsolutePath());
    } else {
        logger.info("Building model from scratch");

        MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder() //
                .seed(seed) //
                .iterations(iterations) //
                .regularization(true) //
                .l2(0.0005) //
                .learningRate(.002) // HB: was .01 initially
                //.biasLearningRate(0.02)
                //.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75)
                .weightInit(WeightInit.XAVIER) //
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) //
                .updater(Updater.NESTEROVS).momentum(0.9) //
                .list() //
                .layer(0, new ConvolutionLayer.Builder(5, 5) //
                        .name("C0") //
                        .nIn(nChannels) //
                        .stride(1, 1) //
                        .nOut(20) //
                        .activation(Activation.IDENTITY) //
                        .build()) //
                .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) //
                        .name("S1") //
                        .kernelSize(2, 2) //
                        .stride(2, 2) //
                        .build()) //
                .layer(2, new ConvolutionLayer.Builder(5, 5) //
                        .name("C2") //
                        .stride(1, 1) //
                        .nOut(50) //
                        .activation(Activation.IDENTITY) //
                        .build()) //
                .layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) //
                        .name("S3") //
                        .kernelSize(2, 2) //
                        .stride(2, 2) //
                        .build()) //
                .layer(4, new DenseLayer.Builder() //
                        .name("D4") //
                        .nOut(500) //
                        .activation(Activation.RELU) //
                        .build()) //
                .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) //
                        .name("O5") //
                        .nOut(numClasses) //
                        .activation(Activation.SOFTMAX) //
                        .build()) //
                .setInputType(InputType.convolutionalFlat(CONTEXT_HEIGHT, CONTEXT_WIDTH, 1));

        MultiLayerConfiguration conf = builder.build();
        model = new MultiLayerNetwork(conf);
        model.init();
    }

    // Prepare monitoring
    UIServer uiServer = null;

    try {
        if (true) {
            //Initialize the user interface backend
            uiServer = UIServer.getInstance();

            //Configure where the network information (gradients, score vs. time etc) is to be stored. Here: store in memory.
            StatsStorage statsStorage = new InMemoryStatsStorage(); //Alternative: new FileStatsStorage(File), for saving and loading later

            //Attach the StatsStorage instance to the UI: this allows the contents of the StatsStorage to be visualized
            uiServer.attach(statsStorage);

            //Then add the StatsListener to collect this information from the network, as it trains
            model.setListeners(new StatsListener(statsStorage), new ScoreIterationListener(10));
        } else {
            model.setListeners(new ScoreIterationListener(10));
        }

        logger.info("Training model...");

        for (int epoch = 1; epoch <= nEpochs; epoch++) {
            Path epochFolder = Main.cli.mistakes ? MISTAKES_PATH.resolve("epoch#" + epoch) : null;
            long start = System.currentTimeMillis();
            model.fit(trainIter);

            long stop = System.currentTimeMillis();
            double dur = stop - start;
            logger.info(String.format("*** End epoch#%d, time: %.0f sec", epoch, dur / 1000));

            // Save model
            ModelSerializer.writeModel(model, MODEL_PATH.toFile(), false);
            ModelSerializer.addNormalizerToModel(MODEL_PATH.toFile(), normalizer);
            logger.info("Model+normalizer stored as {}", MODEL_PATH.toAbsolutePath());
            //
            //                logger.info("Evaluating model...");
            //
            //                Evaluation eval = new Evaluation(OmrShapes.NAMES);
            //
            //                while (testIter.hasNext()) {
            //                    DataSet ds = testIter.next();
            //                    List<RecordMetaData> testMetaData = ds.getExampleMetaData(RecordMetaData.class);
            //                    INDArray output = model.output(ds.getFeatureMatrix(), false);
            //                    eval.eval(ds.getLabels(), output, testMetaData);
            //                }
            //
            //                System.out.println(eval.stats());
            //                testIter.reset();
            //
            //                //Get a list of prediction errors, from the Evaluation object
            //                //Prediction errors like this are only available after calling iterator.setCollectMetaData(true)
            //                List<Prediction> mistakes = eval.getPredictionErrors();
            //                logger.info("Epoch#{} Prediction Errors: {}", epoch, mistakes.size());
            //
            //                //We can also load a subset of the data, to a DataSet object:
            //                //Here we load the raw data:
            //                List<RecordMetaData> predictionErrorMetaData = new ArrayList<RecordMetaData>();
            //
            //                for (Prediction p : mistakes) {
            //                    predictionErrorMetaData.add(p.getRecordMetaData(RecordMetaData.class));
            //                }
            //
            //                List<Record> predictionErrorRawData = testRecordReader.loadFromMetaData(
            //                        predictionErrorMetaData);
            //
            //                for (int ie = 0; ie < mistakes.size(); ie++) {
            //                    Prediction p = mistakes.get(ie);
            //                    List<Writable> rawData = predictionErrorRawData.get(ie).getRecord();
            //                    saveMistake(p, rawData, epochFolder);
            //                }
            //
            //
            //                // To avoid long useless sessions...
            //                if (mistakes.isEmpty()) {
            //                    logger.info("No mistakes left, training stopped.");
            //
            //                    break;
            //                }
        }
    } finally {
        // Stop monitoring
        if (uiServer != null) {
            uiServer.stop();
        }
    }

    logger.info("****************Example finished********************");
}

From source file:org.knime.ext.dl4j.base.mln.ConvMultiLayerNetFactory.java

License:Open Source License

@Override
protected MultiLayerNetwork createMlnWithLearnerParameters(final List<Layer> layers) {
    final NeuralNetConfiguration.ListBuilder listBuilder = createListBuilderWithLearnerParameters(layers);
    listBuilder.setInputType(InputType.convolutionalFlat(m_height, m_width, m_channels));

    final MultiLayerConfiguration layerConf = listBuilder.build();
    final MultiLayerNetwork mln = new MultiLayerNetwork(layerConf);
    mln.init();/*from w ww  .  j  av a  2  s .  com*/
    return mln;
}

From source file:org.knime.ext.dl4j.base.mln.ConvMultiLayerNetFactory.java

License:Open Source License

@Override
protected MultiLayerNetwork createMlnWithoutLearnerParameters(final List<Layer> layers) {
    final NeuralNetConfiguration.ListBuilder listBuilder = new NeuralNetConfiguration.Builder().list();
    int currentLayerIndex = 0;
    for (final Layer layer : layers) {
        listBuilder.layer(currentLayerIndex, layer);
        currentLayerIndex++;/*w ww  . j av  a 2  s  .  com*/
    }

    listBuilder.setInputType(InputType.convolutionalFlat(m_height, m_width, m_channels));

    final MultiLayerConfiguration layerConf = listBuilder.build();
    final MultiLayerNetwork mln = new MultiLayerNetwork(layerConf);
    mln.init();
    return mln;
}

From source file:org.knime.ext.dl4j.base.mln.ConvMultiLayerNetFactory2.java

License:Open Source License

@Override
protected MultiLayerNetwork createMlnWithLearnerParameters(final List<Layer> layers,
        final LearnerParameterSettingsModels2 learnerParameters) {
    final NeuralNetConfiguration.ListBuilder listBuilder = createListBuilderWithLearnerParameters(layers,
            learnerParameters);//from  w  w  w  . j a  va  2  s  . co  m
    listBuilder.setInputType(InputType.convolutionalFlat(m_height, m_width, m_channels));

    final MultiLayerConfiguration layerConf = listBuilder.build();
    final MultiLayerNetwork mln = new MultiLayerNetwork(layerConf);
    mln.init();
    return mln;
}

From source file:org.knime.ext.dl4j.base.mln.ConvMultiLayerNetFactory2.java

License:Open Source License

@Override
protected MultiLayerNetwork createMlnWithoutLearnerParameters(final List<Layer> layers) {
    final NeuralNetConfiguration.ListBuilder listBuilder = new NeuralNetConfiguration.Builder().list();
    listBuilder.setInputType(InputType.convolutionalFlat(m_height, m_width, m_channels));

    int currentLayerIndex = 0;
    for (final Layer layer : layers) {
        listBuilder.layer(currentLayerIndex, layer);
        currentLayerIndex++;/*from w  ww .j  a v a2  s.co  m*/
    }

    final MultiLayerConfiguration layerConf = listBuilder.build();
    final MultiLayerNetwork mln = new MultiLayerNetwork(layerConf);
    mln.init();
    return mln;
}

From source file:weka.classifiers.functions.Dl4jMlpClassifier.java

License:Open Source License

/**
 * The method used to train the classifier.
 *
 * @param data set of instances serving as training data
 * @throws Exception if something goes wrong in the training process
 *///from  w  w  w .j  av a  2  s .  com
@Override
public void buildClassifier(Instances data) throws Exception {
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        // Can classifier handle the data?
        getCapabilities().testWithFail(data);

        // Check basic network structure
        if (m_layers.length == 0) {
            throw new Exception("No layers have been added!");
        }
        if (!(m_layers[m_layers.length - 1] instanceof OutputLayer)) {
            throw new Exception("Last layer in network must be an output layer!");
        }

        // Remove instances with missing class and check that instances and
        // predictor attributes remain.
        data = new Instances(data);
        data.deleteWithMissingClass();
        m_zeroR = null;
        if (data.numInstances() == 0 || data.numAttributes() < 2) {
            m_zeroR = new ZeroR();
            m_zeroR.buildClassifier(data);
            return;
        }

        // Replace missing values
        m_replaceMissing = new ReplaceMissingValues();
        m_replaceMissing.setInputFormat(data);
        data = Filter.useFilter(data, m_replaceMissing);

        // Retrieve two different class values used to determine filter
        // transformation
        double y0 = data.instance(0).classValue();
        int index = 1;
        while (index < data.numInstances() && data.instance(index).classValue() == y0) {
            index++;
        }
        if (index == data.numInstances()) {
            // degenerate case, all class values are equal
            // we don't want to deal with this, too much hassle
            throw new Exception("All class values are the same. At least two class values should be different");
        }
        double y1 = data.instance(index).classValue();

        // Replace nominal attributes by binary numeric attributes.
        m_nominalToBinary = new NominalToBinary();
        m_nominalToBinary.setInputFormat(data);
        data = Filter.useFilter(data, m_nominalToBinary);

        // Standardize or normalize (as requested), including the class
        if (m_standardizeInsteadOfNormalize) {
            m_normalize = new Standardize();
            m_normalize.setOptions(new String[] { "-unset-class-temporarily" });
        } else {
            m_normalize = new Normalize();
        }
        m_normalize.setInputFormat(data);
        data = Filter.useFilter(data, m_normalize);

        double z0 = data.instance(0).classValue();
        double z1 = data.instance(index).classValue();
        m_x1 = (y0 - y1) / (z0 - z1); // no division by zero, since y0 != y1
                                      // guaranteed => z0 != z1 ???
        m_x0 = (y0 - m_x1 * z0); // = y1 - m_x1 * z1

        // Randomize the data, just in case
        Random rand = new Random(getSeed());
        data.randomize(rand);

        // Initialize random number generator for construction of network
        NeuralNetConfiguration.Builder builder = new NeuralNetConfiguration.Builder();
        if (getOptimizationAlgorithm() == null) {
            builder.setOptimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);
        } else {
            builder.setOptimizationAlgo(getOptimizationAlgorithm());
        }
        builder.setSeed(rand.nextInt());

        // Construct the mlp configuration
        ListBuilder ip = builder.list(getLayers());
        int numInputAttributes = getDataSetIterator().getNumAttributes(data);

        // Connect up the layers appropriately
        for (int x = 0; x < m_layers.length; x++) {

            // Is this the first hidden layer?
            if (x == 0) {
                setNumIncoming(m_layers[x], numInputAttributes);
            } else {
                setNumIncoming(m_layers[x], getNumUnits(m_layers[x - 1]));
            }

            // Is this the output layer?
            if (x == m_layers.length - 1) {
                ((OutputLayer) m_layers[x]).setNOut(data.numClasses());
            }
            ip = ip.layer(x, m_layers[x]);
        }

        // If we have a convolutional network
        if (getDataSetIterator() instanceof ImageDataSetIterator) {
            ImageDataSetIterator idsi = (ImageDataSetIterator) getDataSetIterator();
            ip.setInputType(
                    InputType.convolutionalFlat(idsi.getWidth(), idsi.getHeight(), idsi.getNumChannels()));
        } else if (getDataSetIterator() instanceof ConvolutionalInstancesIterator) {
            ConvolutionalInstancesIterator cii = (ConvolutionalInstancesIterator) getDataSetIterator();
            ip.setInputType(InputType.convolutionalFlat(cii.getWidth(), cii.getHeight(), cii.getNumChannels()));
        }

        ip = ip.pretrain(false).backprop(true);

        MultiLayerConfiguration conf = ip.build();

        if (getDebug()) {
            System.err.println(conf.toJson());
        }

        // build the network
        m_model = new MultiLayerNetwork(conf);
        m_model.init();

        if (getDebug()) {
            System.err.println(m_model.conf().toYaml());
        }

        ArrayList<IterationListener> listeners = new ArrayList<IterationListener>();
        listeners.add(
                new ScoreIterationListener(data.numInstances() / getDataSetIterator().getTrainBatchSize()));

        // if the log file doesn't point to a directory, set up the listener
        if (getLogFile() != null && !getLogFile().isDirectory()) {
            int numMiniBatches = (int) Math
                    .ceil(((double) data.numInstances()) / ((double) getDataSetIterator().getTrainBatchSize()));
            listeners.add(new FileIterationListener(getLogFile().getAbsolutePath(), numMiniBatches));
        }

        m_model.setListeners(listeners);

        // Abusing the MultipleEpochsIterator because it splits the data into
        // batches
        DataSetIterator iter = getDataSetIterator().getIterator(data, getSeed());
        for (int i = 0; i < getNumEpochs(); i++) {
            m_model.fit(iter); // Note that this calls the reset() method of the
                               // iterator
            if (getDebug()) {
                m_log.info("*** Completed epoch {} ***", i + 1);
            }
            iter.reset();
        }
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}