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

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

Introduction

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

Prototype

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

Source Link

Document

Input type for convolutional (CNN) data, that is 4d with shape [miniBatchSize, channels, height, width].

Usage

From source file:cnn.image.classification.CNNImageClassification.java

public static void main(String[] args) {
    int nChannels = 3;
    int outputNum = 10;
    //        int numExamples = 80;
    int batchSize = 10;
    int nEpochs = 20;
    int iterations = 1;
    int seed = 123;
    int height = 32;
    int width = 32;
    Random randNumGen = new Random(seed);
    System.out.println("Load data....");

    File parentDir = new File("train1/");

    FileSplit filesInDir = new FileSplit(parentDir, allowedExtensions, randNumGen);

    ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();

    BalancedPathFilter pathFilter = new BalancedPathFilter(randNumGen, allowedExtensions, labelMaker);

    //Split the image files into train and test. Specify the train test split as 80%,20%
    InputSplit[] filesInDirSplit = filesInDir.sample(pathFilter, 100, 0);
    InputSplit[] filesInDirSplitTest = filesInDir.sample(pathFilter, 0, 100);

    InputSplit trainData = filesInDirSplit[0];
    InputSplit testData = filesInDirSplitTest[1];

    System.out.println("train = " + trainData.length());
    System.out.println("test = " + testData.length());
    //Specifying a new record reader with the height and width you want the images to be resized to.
    //Note that the images in this example are all of different size
    //They will all be resized to the height and width specified below
    ImageRecordReader recordReader = new ImageRecordReader(height, width, nChannels, labelMaker);

    //Often there is a need to transforming images to artificially increase the size of the dataset

    recordReader.initialize(trainData);//from   w  ww  .  j  ava2  s .c  o m

    DataSetIterator dataIterTrain = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);
    //        recordReader.reset();
    recordReader.initialize(testData);
    DataSetIterator dataIterTest = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);

    DataNormalization scaler = new ImagePreProcessingScaler(0, 1);

    dataIterTrain.setPreProcessor(scaler);
    dataIterTest.setPreProcessor(scaler);

    System.out.println("Build model....");
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(seed)
            .iterations(iterations).regularization(true).l2(0.0005)
            //                .dropOut(0.5)
            .learningRate(0.001)//.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(nChannels).stride(1, 1).nOut(20)
                            .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("identity").build())
            .layer(3,
                    new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
                            .kernelSize(2, 2).stride(2, 2).build())
            .layer(4, new DenseLayer.Builder().activation("relu").nOut(500).build())
            .layer(5,
                    new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputNum)
                            .activation("softmax").build())
            .setInputType(InputType.convolutional(height, width, nChannels)) //See note below
            .backprop(true).pretrain(false);

    MultiLayerConfiguration b = new NeuralNetConfiguration.Builder().seed(seed).iterations(iterations)
            .regularization(false).l2(0.005) // tried 0.0001, 0.0005
            .learningRate(0.0001) // tried 0.00001, 0.00005, 0.000001
            .weightInit(WeightInit.XAVIER).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .updater(Updater.NESTEROVS).momentum(0.9).list().layer(0, new ConvolutionLayer.Builder(5, 5)
                    //nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied
                    .nIn(nChannels).stride(1, 1).nOut(50) // tried 10, 20, 40, 50
                    .activation("relu").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(100) // tried 25, 50, 100
                    .activation("relu").build())
            .layer(3,
                    new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
                            .kernelSize(2, 2).stride(2, 2).build())
            .layer(4, new DenseLayer.Builder().activation("relu").nOut(500).build())
            .layer(5,
                    new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputNum)
                            .activation("softmax").build())
            .backprop(true).pretrain(false).cnnInputSize(height, width, nChannels).build();

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

    System.out.println("Train model....");
    model.setListeners(new ScoreIterationListener(1));
    //        for( int i=0; i<nEpochs; i++ ) {
    //            model.setListeners(new HistogramIterationListener(1));

    MultipleEpochsIterator trainIter = new MultipleEpochsIterator(nEpochs, dataIterTrain, 2);
    model.fit(trainIter);

    //            System.out.println("*** Completed epoch - " + i + "  ***");

    System.out.println("Evaluate model....");
    //            Evaluation eval = new Evaluation(outputNum);
    //            while(dataIterTest.hasNext()){
    //                DataSet ds = dataIterTest.next();
    //                INDArray output = model.output(ds.getFeatureMatrix(), false);
    //                eval.eval(ds.getLabels(), output);
    //            }
    //            System.out.println(eval.stats());
    //            dataIterTest.reset();
    //        }

    Evaluation eval1 = model.evaluate(dataIterTest);
    System.out.println(eval1.stats());

    System.out.println("****************Example finished********************");
}